DEV Community

Sergey Shinder
Sergey Shinder

Posted on

Our incident script reported exactly ten thousand failed labels because Elasticsearch had stopped counting there

In October one of our carriers had a label outage for about three hours. Every attempt we made to create a label is logged to Elasticsearch with the shipment id and the result, and after an incident a small script tells us how many failed. Customer service uses that number to plan their emails, and finance uses it in the claim to the carrier.

The script asked Elasticsearch for documents matching the carrier, the time window and a failed result, with size zero, and printed hits.total.value. It printed 10000. Nobody found a round number suspicious at the end of a long evening, and the claim went in for ten thousand labels.

Since version 7, Elasticsearch counts total hits accurately only up to ten thousand by default. Past that it stops counting, returns ten thousand, and sets hits.total.relation to gte rather than eq. Counting every match costs work the search does not need, and most searches only show the first page. The field telling us the number was a lower bound was in every response the script had ever received. The script did not read it.

A week later our own reconciliation of shipments that went out late found more than double. With track_total_hits set to true, the real count of failed attempts was twenty three thousand four hundred.

That was also not the number anyone wanted. Our client retries a failed label twice, so attempts are not shipments. Counting distinct shipment ids gave fourteen thousand nine hundred. We did not use the cardinality aggregation for that, because Elasticsearch documents it as approximate. The script uses a composite aggregation on shipment id and pages through every bucket, which is exact.

The script now sets track_total_hits to true on any query whose total it prints, and refuses to print a total whose relation is not eq. Its output gives attempts and shipments on separate lines, with the query used for each, so the person reading it can see what was counted. We sent the carrier a corrected claim, and customer service sent a second round of emails.

Ten thousand was not our number. It was where Elasticsearch stopped looking, and it had told us so in a field we never read.

– Sergey Shinder

Top comments (1)

Collapse
 
prascli profile image
Indra Prasetya •

We saw a parallel silent-failure mode in CI test-health checks: a shard split across 4 parallel jobs made the old check compare 160 s wall time against a 400 s serial prediction, so it stayed quiet until a 6× slowdown. The fix was dividing predictedMs by the job count — now 160 s vs 100 s flags at 1.6×. A quiet dashboard is indistinguishable from a healthy one, so nothing looked broken until the pipeline timed out months later.