|
| 1 | + |
| 2 | +=== Don't touch these settings! |
| 3 | + |
| 4 | +There are a few hotspots in Elasticsearch that people just can't seem to avoid |
| 5 | +tweaking. We understand: knobs just beg to be turned. |
| 6 | + |
| 7 | +But of all the knobs to turn, these you should _really_ leave alone. They are |
| 8 | +often abused and will contribute to terrible stability or terrible performance. |
| 9 | +Or both. |
| 10 | + |
| 11 | +==== Garbage Collector |
| 12 | + |
| 13 | +As briefly introduces in <<garbage_collector_primer>>, the JVM uses a garbage |
| 14 | +collector to free unused memory. This tip is really an extension of the last tip, |
| 15 | +but deserves it's own section for emphasis: |
| 16 | + |
| 17 | +Do not change the default garbage collector! |
| 18 | + |
| 19 | +The default GC for Elasticsearch is Concurrent-Mark and Sweep (CMS). This GC |
| 20 | +runs concurrently with the execution of the application so that it can minimize |
| 21 | +pauses. It does, however, have two stop-the-world phases. It also has trouble |
| 22 | +collecting large heaps. |
| 23 | + |
| 24 | +Despite these downsides, it is currently the best GC for low-latency server software |
| 25 | +like Elasticsearch. The official recommendation is to use CMS. |
| 26 | + |
| 27 | +There is a newer GC called the Garbage First GC (G1GC). This newer GC is designed |
| 28 | +to minimize pausing even more than CMS, and operate on large heaps. It works |
| 29 | +by dividing the heap into regions and predicting which regions contain the most |
| 30 | +reclaimable space. By collecting those regions first ("garbage first"), it can |
| 31 | +minimize pauses and operate on very large heaps. |
| 32 | + |
| 33 | +Sounds great! Unfortunately, G1GC is still new and fresh bugs are found routinely. |
| 34 | +These bugs are usually of the segfault variety, and will cause hard crashes. |
| 35 | +The Lucene test suite is brutal on GC algorithms, and it seems that G1GC hasn't |
| 36 | +had the kinks worked out yet. |
| 37 | + |
| 38 | +We would like to recommend G1GC someday, but for now, it is simply not stable |
| 39 | +enough to meet the demands of Elasticsearch and Lucene. |
| 40 | + |
| 41 | +==== Threadpools |
| 42 | + |
| 43 | +Everyone _loves_ to tweak threadpools. For whatever reason, it seems people |
| 44 | +cannot resist increasing thread counts. Indexing a lot? More threads! Searching |
| 45 | +a lot? More threads! Node idling 95% of the time? More threads! |
| 46 | + |
| 47 | +The default threadpool settings in Elasticsearch are very sensible. For all |
| 48 | +threadpools (except `search`) the threadcount is set to the number of CPU cores. |
| 49 | +If you have 8 cores, you can only be running 8 threads simultaneously. It makes |
| 50 | +sense to only assign 8 threads to any particular threadpool. |
| 51 | + |
| 52 | +Search gets a larger threadpool, and is configured to `# cores * 3`. |
| 53 | + |
| 54 | +You might argue that some threads can block (such as on a disk I/O operation), |
| 55 | +which is why you need more threads. This is not actually not a problem in Elasticsearch: |
| 56 | +much of the disk IO is handled by threads managed by Lucene, not Elasticsearch. |
| 57 | + |
| 58 | +Furthermore, threadpools cooperate by passing work between each other. You don't |
| 59 | +need to worry about a networking thread blocking because it is waiting on a disk |
| 60 | +write. The networking thread will have long since handed off that work unit to |
| 61 | +another threadpool and gotten back to networking. |
| 62 | + |
| 63 | +Finally, the compute capacity of your process is finite. More threads just forces |
| 64 | +the processor to switch thread contexts. A processor can only run one thread |
| 65 | +at a time, so when it needs to switch to a different thread, it stores the current |
| 66 | +state (registers, etc) and loads another thread. If you are lucky, the switch |
| 67 | +will happen on the same core. If you are unlucky, the switch may migrate to a |
| 68 | +different core and require transport on inter-core communication bus. |
| 69 | + |
| 70 | +This context switching eats up cycles simply doing administrative housekeeping |
| 71 | + -- estimates can peg it as high as 30us on modern CPUs. So unless the thread |
| 72 | + will be blocked for longer than 30us, it is highly likely that that time would |
| 73 | + have been better spent just processing and finishing early. |
| 74 | + |
| 75 | + People routinely set threadpools to silly values. On 8 core machines, we have |
| 76 | + run across configs with 60, 100 or even 1000 threads. These settings will simply |
| 77 | + thrash the CPU more than getting real work done. |
| 78 | + |
| 79 | + So. Next time you want to tweak a threadpool...please don't. And if you |
| 80 | + _absolutely cannot resist_, please keep your core count in mind and perhaps set |
| 81 | + the count to double. More than that is just a waste. |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
| 87 | + |
0 commit comments