Lesson 6 of 9 · 24m
Aggregations, Cardinality, and Slow Search Triage
Use aggregations with cost awareness, understand high-cardinality memory pressure, and triage slow searches through tasks, hot threads, and shard-level evidence.
A dashboard can be 'just a search' the way a freight train is 'just a vehicle.' Every panel may run several queries and aggregations across weeks of data. The cluster does not care that the UI was convenient. Aggregations are powerful because they compute summaries over matching documents, and dangerous because they do exactly that.
Aggregations operate over the query result set
A terms aggregation over hostnames is not inherently expensive. It becomes expensive when the query matches hundreds of millions of documents or the field has very high cardinality:
{
"size": 0,
"query": {"range": {"@timestamp": {"gte": "now-1h"}}},
"aggs": {
"services": {
"terms": {"field": "service.raw", "size": 20},
"aggs": {
"p95_latency": {
"percentiles": {"field": "latency_ms", "percents": [95]}
}
}
}
}
}The time range is not decoration. It is the cost boundary. Dashboards that default to seven days can make a healthy cluster look undersized.
High cardinality is memory pressure wearing a query costume
Fields such as request IDs, user IDs, container IDs, and full URLs can have millions of distinct values. Terms aggregations, sorting, and exact field access require data structures that support fast lookup. Even with doc values and off-heap designs, cardinality still costs memory, disk, and compute.
A common incident pattern is:
new dashboard panel -> terms agg on user.id -> cluster GC pressure -> slow nodes -> shard relocation -> worse IOThe immediate fix is to stop or constrain the query. The durable fix is to redesign the dashboard around bounded time ranges, lower-cardinality dimensions, sampled data, pre-aggregated rollups, or explicit user-selected filters.
Use the task management API during incidents
When searches pile up, inspect the live work:
curl -s 'http://localhost:9200/_tasks?actions=*search*&detailed&pretty'Cancel a runaway task when appropriate:
curl -X POST 'http://localhost:9200/_tasks/<node>:<task_id>/_cancel'This is an operational pressure valve, not a substitute for fixing the query. Capture the request body first if you can; otherwise the evidence disappears with the incident.
Hot threads and slow logs tell different stories
Hot threads show where node CPU time is going:
curl -s 'http://localhost:9200/_nodes/hot_threads'Slow logs capture queries and indexing operations that cross configured thresholds. Enable them deliberately and preserve enough context to reconstruct the request shape. Also inspect queue rejection metrics: search rejections mean the node cannot keep up with incoming search concurrency, while write rejections point at indexing pressure.
Reduce fan-out and needless work
Search only the indices required. A wildcard like logs-* can hit hundreds of shards if lifecycle boundaries are too granular. Use smaller time windows, fewer shard targets, runtime fields sparingly, and transform/rollup pipelines for repeated aggregation patterns. Cheapest query is the one you do not run every ten seconds for every viewer.
Field notes
size: 0removes hit fetching but does not make aggregation computation free.- High
terms.sizeis a warning sign; returning thousands of buckets is rarely a good UI or cluster decision. - Approximate cardinality aggregations trade exactness for tractable memory behavior.
- Dashboard refreshes multiply load. Twenty viewers at a ten-second refresh are a distributed denial-of-service accident.
Was this lesson helpful?