Flaky-test engineering

Taxonomy: async races, shared state, time/timezone, network, infra. Detect via re-run-on-same-commit, quarantine with an SLA, fix by root cause — retries are triage, not treatment.

About 27 min read3-question knowledge check

The mental model

A smoke detector that false-alarms twice a week does not get fixed — it gets its battery removed. That is the exact failure mode of a flaky test suite: every false red teaches the team to distrust red, until the day a real red is dismissed with "probably just flaky, re-run it" and a genuine regression ships. Flakiness does not merely waste time; it destroys the meaning of the signal, which is the only thing a test suite produces.

The definition to hold precisely: a flaky test passes and fails on the same code. Same commit, different outcomes. That non-determinism is never magic — it is always a hidden variable the test depends on but does not control: time, ordering, shared state, network, or infrastructure load. "Flaky" is therefore not a property a test has; it is a bug a test contains, and frequently the bug is in the product — a race condition in the app that the test trips over honestly. Retrying past those is shipping the race to users.

Why this is an engineering discipline and not a chore: the math is brutal at suite scale. A suite of 500 tests where each is 99.9% reliable — one failure in a thousand runs, which sounds excellent — goes fully green only 0.999^500 ≈ 61% of the time. Two runs in five are red for no reason. At 99% per-test reliability, the same 500-test suite is green about 0.7% of the time: effectively never. Per-test flakiness that rounds to zero compounds into a gate that never opens. This is why serious teams treat flake rate as an SLO, quarantine with expiry dates, and fix by root cause — and why "add a retry and move on" is the compounding debt that eventually forces a suite rewrite.

The full lesson continues with 15 more sections
  • Deep dive
  • Internal architecture
  • Real-world use cases
  • Best practices
  • Common mistakes
  • Performance
  • Security
  • Production troubleshooting
  • Advantages & disadvantages
  • Alternatives
  • Interview expectations
  • Interview questions
  • Facts to remember
  • Cheat sheet
  • Quick revision