Elasticsearch Circuit Breaker Trips: Why Fielddata on Text Fields OOMs the JVM Old Generation
Executing an aggregation on an un-indexed text field causes Elasticsearch to load entire inverted index terms into in-memory fielddata, triggering CircuitBreakingException and freezing queries. How doc_values, keyword sub-fields, and parent circuit breakers safeguard node stability.
During an incident in a real-time analytics cluster, an Elasticsearch 8 cluster serving customer support query dashboards became unresponsive. Query latencies spiked from 25 milliseconds to over 45 seconds, worker nodes dropped out of the cluster state, and search requests failed with CircuitBreakingException: [parent] Data too large, data for [<transport_request>] would be [31845102592/29.6gb], which is larger than the limit of [30601641984/28.5gb]. Node logs showed massive garbage collection pauses in the JVM G1GC old generation, exceeding 12 seconds per cycle.
The root cause was traced to an analytics dashboard that executed a terms aggregation on an unmapped log_message field of type text with dynamic mapping enabled, triggering Elasticsearch to load in-memory fielddata across millions of unique tokens.
1. Inverted Index vs Doc Values
To understand why this query crashed the JVM, we must look at how Lucene indexes text. Lucene is optimized for full-text search using an inverted index. An inverted index maps individual terms (words) to the list of document IDs that contain them:
Term -> Document IDs
"timeout" -> [Doc 1, Doc 4, Doc 9]
"connection"-> [Doc 1, Doc 2, Doc 5]
"failed" -> [Doc 2, Doc 3, Doc 9]
While an inverted index makes searching for "connection AND timeout" extremely fast, it cannot efficiently answer aggregations or sorting. For an aggregation like "find the top 10 most frequent terms across 50 million documents", the engine must know which terms belong to Doc 1, Doc 2, and so on. Inverting the index at query time across millions of documents requires an un-inverted column-oriented data structure.
For structured fields (such as numbers, dates, booleans, and keyword strings), Elasticsearch builds doc_values on disk at index time. Doc values are columnar, sequential, and memory-mapped via the Linux page cache. The operating system handles caching automatically, leaving the JVM heap free.
2. The Fielddata Disaster on Text Fields
However, Lucene does not support doc_values on analyzed text fields because token analysis splits text into arbitrary token streams with position offsets. Instead, if a user forces an aggregation or sorting on a text field, Elasticsearch falls back to fielddata:
- Elasticsearch reads the entire inverted index dictionary for that field from disk into JVM heap.
- It builds an in-memory un-inverted index, mapping every document ID to every analyzed token it contains.
- Because text fields often have high cardinality (e.g. stack traces, URLs, error messages), this in-memory hash map consumes gigabytes of heap space instantly.
- Once loaded into JVM memory, fielddata remains resident in the JVM Old Generation for the lifetime of the Lucene segment.
When multiple search threads execute aggregations on text fields simultaneously, fielddata allocates tens of gigabytes of heap within milliseconds, triggering the JVM circuit breaker or throwing fatal OutOfMemoryError exceptions.
3. Circuit Breaker Mechanics
Elasticsearch implements internal circuit breakers to protect nodes from heap exhaustion. The key breakers include:
indices.breaker.fielddata.limit: Defaults to 40% of JVM heap. Caps memory allocated to fielddata structures.indices.breaker.request.limit: Defaults to 60% of JVM heap. Caps memory for request-level aggregations and bucket calculations.indices.breaker.total.use_real_memory: Defaults to true. Uses real JVM memory usage reported by the garbage collector rather than estimated byte tracking.indices.breaker.total.limit: Defaults to 95% of JVM heap. The parent breaker that aborts any request pushing total memory usage beyond safe margins.
When the parent circuit breaker trips, it rejects incoming transport requests with HTTP 429 status codes, preserving the node process from fatal JVM crashes but disrupting client applications.
4. Prevention and Remediation
A. Multi-Fields with Keyword Sub-Fields
Never aggregate directly on a text field. Instead, configure an index mapping that indexes the field as both analyzed text (for full-text search) and exact keyword (with doc_values enabled for aggregations and sorting):
PUT /production-logs
{
"mappings": {
"properties": {
"log_message": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
Queries then aggregate on log_message.keyword, which uses off-heap doc_values instead of JVM fielddata.
B. Disallow Fielddata on Text Fields
Ensure that fielddata loading is disabled explicitly on text mappings. In Elasticsearch 5.x and newer, fielddata on text is disabled by default, but legacy templates or dynamic scripts may enable it. Confirm that fielddata: false is enforced:
PUT /production-logs/_mapping
{
"properties": {
"log_message": {
"type": "text",
"fielddata": false
}
}
}
C. Clearing Stale Fielddata from JVM Heap
If fielddata has already accumulated in heap on an active node, clear the cache across all indices immediately using the Nodes API:
POST /_cache/clear?fielddata=true
Verify fielddata memory release via the Index Stats API:
GET /_nodes/stats/indices/fielddata?fields=*
Summary
Fielddata turns an analyzed text inverted index inside out inside JVM heap, creating massive memory bloat and tripping circuit breakers. By keeping text fields strictly for search, leveraging off-heap doc_values via keyword sub-fields for aggregations, and setting strict fielddata limits, distributed search clusters stay resilient and responsive under heavy analytical workloads.