Once analyzed strings have been loaded into fielddata, they will sit there until evicted (or your node crashes). For that reason it is important to keep an eye on this memory usage, understand how and when it loads, and how you can limit the impact on your cluster.
Fielddata is loaded lazily. If you never aggregate on an analyzed string, you’ll never load fielddata into memory. Furthermore, fielddata is loaded on a per-field basis, meaning only actively used fields will incur the "fielddata tax".
However, there is a subtle surprise lurking here. Suppose your query is highly selective and only returns 100 hits. Most people assume fielddata is only loaded for those 100 documents.
In reality, fielddata will be loaded for all documents in that index (for that particular field), regardless of the query’s specificity. The logic is: if you need access to documents X, Y, and Z for this query, you will probably need access to other documents in the next query.
Unlike doc values, the fielddata structure is not created at index time. Instead, it is populated on-the-fly when the query is run. This is a potentially non-trivial operation and can take some time. It is cheaper to load all the values once, and keep them in memory, than load only a portion of the total fielddata repeatedly.
The JVM heap is a limited resource that should be used wisely. A number of mechanisms exist to limit the impact of fielddata on heap usage. These limits are important because abuse of the heap will cause node instability (thanks to slow garbage collections) or even node death (with an OutOfMemory exception).
indices.fielddata.cache.size controls how much heap space is allocated
As you are issuing queries, aggregations on analyzed strings will load into fielddata
if the field wasn’t previously loaded. If the resulting fielddata size would
exceed the specified
size, other values will be evicted in order to make space.
By default, this setting is unbounded—Elasticsearch will never evict data from fielddata.
This default was chosen deliberately: fielddata is not a transient cache. It is an in-memory data structure that must be accessible for fast execution, and it is expensive to build. If you have to reload data for every request, performance is going to be awful.
A bounded size forces the data structure to evict data. We will look at when to set this value, but first a warning:
This setting is a safeguard, not a solution for insufficient memory.
If you don’t have enough memory to keep your fielddata resident in memory, Elasticsearch will constantly have to reload data from disk, and evict other data to make space. Evictions cause heavy disk I/O and generate a large amount of garbage in memory, which must be garbage collected later on.
Imagine that you are indexing logs, using a new index every day. Normally you are interested in data from only the last day or two. Although you keep older indices around, you seldom need to query them. However, with the default settings, the fielddata from the old indices is never evicted! fielddata will just keep on growing until you trip the fielddata circuit breaker (see Circuit Breaker), which will prevent you from loading any more fielddata.
At that point, you’re stuck. While you can still run queries that access fielddata from the old indices, you can’t load any new values. Instead, we should evict old values to make space for the new values.
To prevent this scenario, place an upper limit on the fielddata by adding this
setting to the
There is another setting that you may see online:
We beg that you never use this setting! It will likely be deprecated in the future.
This setting tells Elasticsearch to evict values from fielddata if they are older
expire, whether the values are being used or not.
This is terrible for performance. Evictions are costly, and this effectively schedules evictions on purpose, for no real gain.
There isn’t a good reason to use this setting; we literally cannot theory-craft a hypothetically useful situation. It exists only for backward compatibility at the moment. We mention the setting in this book only since, sadly, it has been recommended in various articles on the Internet as a good performance tip.
It is not. Never use it!
It is important to keep a close watch on how much memory is being used by fielddata, and whether any data is being evicted. High eviction counts can indicate a serious resource issue and a reason for poor performance.
Fielddata usage can be monitored:
?fields=*, the memory usage is broken down for each field.
An astute reader might have noticed a problem with the fielddata size settings. fielddata size is checked after the data is loaded. What happens if a query arrives that tries to load more into fielddata than available memory? The answer is ugly: you would get an OutOfMemoryException.
Elasticsearch includes a fielddata circuit breaker that is designed to deal with this situation. The circuit breaker estimates the memory requirements of a query by introspecting the fields involved (their type, cardinality, size, and so forth). It then checks to see whether loading the required fielddata would push the total fielddata size over the configured percentage of the heap.
If the estimated query size is larger than the limit, the circuit breaker is tripped and the query will be aborted and return an exception. This happens before data is loaded, which means that you won’t hit an OutOfMemoryException.
The circuit breaker limits can be specified in the
file, or can be updated dynamically on a live cluster:
It is best to configure the circuit breaker with a relatively conservative
value. Remember that fielddata needs to share the heap with the
circuit breaker, the indexing memory buffer, the filter cache, Lucene data
structures for open indices, and various other transient data structures. For
this reason, it defaults to a fairly conservative 60%. Overly optimistic
settings can cause potential OOM exceptions, which will take down an entire
On the other hand, an overly conservative value will simply return a query exception that can be handled by your application. An exception is better than a crash. These exceptions should also encourage you to reassess your query: why does a single query need more than 60% of the heap?
In Fielddata Size, we spoke about adding a limit to the size of fielddata,
to ensure that old unused fielddata can be evicted. The relationship between
indices.breaker.fielddata.limit is an
important one. If the circuit-breaker limit is lower than the cache size, no data will ever be evicted. In order for it to work properly, the
circuit breaker limit must be higher than the cache size.
It is important to note that the circuit breaker compares estimated query size against the total heap size, not against the actual amount of heap memory used. This is done for a variety of technical reasons (for example, the heap may look full but is actually just garbage waiting to be collected, which is hard to estimate properly). But as the end user, this means the setting needs to be conservative, since it is comparing against total heap, not free heap.