Back to blog

Why Your Tests Are Flaky in Distributed Systems: Network Partitions, Clock Skew, and How to Fix Them

Prasandeep

Published 6 min readFlaky Tests
Why Your Tests Are Flaky in Distributed Systems: Network Partitions, Clock Skew, and How to Fix Them

Your test passes on your laptop. It fails in CI. It passes again on the next run. The product team says “it works in staging.” You spend an afternoon chasing a race condition that disappears the moment you add more logging.

In a single-process application this is usually a timing bug or a shared resource. In a distributed system it is often something more fundamental: the test (or the system under test) is assuming that machines can always talk to each other and that their clocks agree. Neither is true.

This article covers the two most common physical causes of this class of flake—network partitions and clock skew—and the practical ways SDETs can stop tests from depending on them. For the broader nondeterminism playbook, see Fix Flaky Tests: 2026 Masterclass. For where resilience checks sit in a balanced portfolio, see Modern Test Pyramid 2026: Complete Strategy.

Why “it works on my machine” fails here

On your laptop everything runs on one clock and one network namespace. In CI, staging, or production you have:

  • Multiple processes or services that can lose connectivity with each other.
  • Clocks that drift, even when NTP is running.
  • Timeouts, retries, and “last write wins” logic that behave differently under those conditions.

A test that passes only when the network is perfect and every clock is identical is not testing the system you actually ship. It is testing a lucky environment.

Network partitions and what they do to tests

A network partition means some group of machines can no longer exchange messages, while both sides are otherwise healthy. In test environments this shows up as:

  • A container or pod that is temporarily unreachable.
  • A cloud region or availability zone that loses east-west traffic.
  • A firewall or network policy change that drops packets long enough for timeouts to fire.
  • Toxiproxy, Chaos Mesh, or tc rules you (or the platform) introduced.

What the test usually does wrong

Many tests do this:

Typescript
await api.createOrder(payload); const order = await api.getOrder(orderId); expect(order.status).toBe("CONFIRMED");

If the write lands on one replica and the read hits another replica that has not yet seen the write (or cannot reach the first), the assertion fails—even though the system behaved correctly according to its consistency model. The flake is not “random.” The test assumed instant, cross-replica visibility.

Practical fixes

1. Make the consistency expectation explicit.

  • If the service is CP (it prefers consistency), the test should expect either the new value or a clear error/timeout—never a silent stale value.
  • If the service is AP (it prefers availability), the test should allow a short window of eventual consistency and poll with a condition, not a fixed sleep.

2. Use condition-based waiting instead of fixed delays:

Typescript
await expect .poll(async () => { const order = await api.getOrder(orderId); return order.status; }) .toBe("CONFIRMED");

3. In integration tests, control the network deliberately. Tools such as Toxiproxy let you cut the link between specific services, run the test, then restore it and assert recovery. That turns an intermittent flake into a deterministic check.

4. Prefer contract or API-level checks over UI checks for these scenarios. UI tests amplify the problem because they add browser timing on top of network timing. Pair partition-aware API tests with contract testing so interface compatibility stays fast and isolated.

Clock skew and time-based flakiness

Every machine has its own clock. Even with NTP, differences of tens or hundreds of milliseconds are normal. Larger skew appears when VMs are paused, when NTP is broken, or when tests run in containers with drifted host clocks.

Common symptoms

  • “Last write wins” logic keeps the wrong update because one server’s timestamp is ahead.
  • Session or token expiry fires too early on one instance and too late on another.
  • Tests that assert “createdAt is within the last 2 seconds” fail when the database clock and the test runner clock disagree.
  • Ordering-dependent assertions (“the second event must have a later timestamp”) fail under skew.

Practical fixes

1. Never assert on wall-clock time from the system under test unless the test controls that clock.

2. Inject a controllable clock in tests:

Typescript
// In the application under test (example pattern) const now = clock.now(); // instead of Date.now() or Instant.now() // In the test const fixedClock = new FakeClock(new Date("2026-11-26T10:00:00Z")); // or advance it explicitly fixedClock.advance(5_000);

3. For systems that must use timestamps, prefer hybrid logical clocks—or at least a logical counter alongside the timestamp—so causality is preserved even when wall time is wrong.

4. In CI, pin or synchronize clocks where possible, or isolate time-sensitive tests so they do not share a drifted environment with other jobs. Pinning TZ=UTC is necessary but not sufficient when multiple hosts disagree on wall time.

5. Replace “sleep then check” with explicit waits for the business condition (order confirmed, message processed, cache expired) rather than elapsed wall time.

A minimal checklist for distributed-system tests

  1. The test does not assume instant consistency across all replicas.
  2. Reads after writes use polling or a known strong-consistency path, not a single immediate get.
  3. Time-based assertions use a controllable clock or a business event, not raw Date.now().
  4. Network failures are either explicitly injected and asserted, or the test is isolated from real network partitions.
  5. Timeouts in the test are longer than the system’s own retry/timeout budget (or the test asserts the retry behavior itself).
  6. The test documents which consistency model it expects (strong, eventual, or “error on partition”).

What to put in your quality gate

Add a small set of tests that deliberately exercise the failure modes:

  1. One test that cuts the network between writer and reader and verifies the system either blocks or reconciles correctly.
  2. One test that advances or skews the clock and verifies expiry, ordering, or “last write wins” behavior.
  3. Ordinary happy-path tests that use condition waits and avoid wall-clock assertions.

These tests are slower and more careful to write, so keep the number small. Their job is to protect the class of bugs that ordinary unit and API tests miss—the same class that quietly burns CI trust. For triage when flakes still slip through, use the No-BS flaky-test playbook.

Bottom line

Partition and clock flakes look random but usually mean the test assumed full connectivity and matching wall clocks.

Poll for the business outcome, document the consistency model you expect, and keep a few tests that inject network loss or skewed time on purpose.

Match the test to what the system actually guarantees—not to the lucky laptop environment where everything is instant and synchronized.

ShareLinkedInPost on X

Get the next guide in your inbox

Weekly AI testing insights, templates, and tool notes — unsubscribe anytime.