QA Interview Hub · Knowledge base · Quizzes · Українська

System Design › Microservices and test strategy

Eventual consistency and sagas in tests

Eventual consistency means that data in different parts of the system becomes consistent not at once but after some time. A delay between an event and its processing is normal; the bug is in a test that assumed instantness. The test polls the state with a timeout from the SLA, not a fixed sleep. A saga is tested by breaking every step: compensation, redelivery and a final state with no half-finished operations.

System design · Microservices

Ledger already shows the new balance, but the history does not. A bug or normal?

After a transfer the balance in Ledger has changed, but the history service has not yet received the event. The test opened the history at once and found no record: red. A second later the record appeared. This is not a system bug but eventual consistency: data in different parts of the system becomes consistent not at once but after some time. The bug is in the test, which assumed instantness.

How it works and why

Services talk by events through a queue and there is a delay between "event published" and "event processed". It is not always the same: it depends on load, consumer lag, retries. So a test that waits a fixed time is either too short (flaky) or too long (slow). The right form: poll the state until it becomes the expected one, but not longer than the limit the business promises.

How to wait for an async result

Bad

sleep / immediately

A fixed pause: either flaky or needlessly slow. An immediate check catches the "not arrived yet" state.

Good

poll + SLA

Polling until the expected state with a timeout from the SLA: fast when normal, fails on degradation.

Figure 1. A fixed pause versus polling.

The timeout must match the SLA. If the business promises an update within 5 seconds, a test with a 60-second timeout will miss a degradation: the system became ten times slower and the test is green.

Example: a saga with compensation

A saga performs a business operation across several services without a distributed transaction. Every step has a compensating action. An order: reserve stock, charge payment, create shipment. If the shipment cannot be created, the money must be refunded and the reservation released. The test breaks the last step and waits for the system to reach a consistent end by itself.

// Delivery is stubbed: always answers 503
await deliveryStub.failWith(503);
const { id } = await api.createOrder(cart);

await expect(async () => {
  const order = await api.getOrder(id);
  expect(order.status).toBe('CANCELLED');
  expect(order.refundStatus).toBe('REFUNDED');
  expect(await stock.reserved(cart.sku)).toBe(0);
}).toPass({ timeout: 10_000, intervals: [250, 500, 1000] });

toPass repeats the whole block until all three checks pass together. The reserve check matters: without it you can miss a "half-finished" state where the money is refunded but the stock stays reserved.

What else to test in a saga

  • A failure at each step and the matching compensation.
  • A failure of the compensation itself: is there a retry and an alert.
  • Redelivery of an event at each step: the handler must be idempotent.
  • A final state with no "half-finished" operations.

Limits and alternatives

Polling does not guarantee that the state ever converges: if an event is lost the test simply fails on the timeout, which is the right behaviour. When a service exposes events or webhooks a test can subscribe to them instead of polling, but polling is simpler and independent of internals. The "action, then wait" pair does not replace negative checks: a saga must also be tested against the failures you model.

Common mistakes

  • sleep(3000) instead of polling: unstable and slow.
  • A blanket 60-second timeout "so it does not fail": degradation stays invisible.
  • Checking only the order status and not the money and reserve: a "half-finished" state goes unnoticed.

Check yourself

A test with a 60 s poll timeout is green while the business promises 5 s. What is wrong?

Answer

The timeout is too generous: a degradation from 2 s to 40 s will not show. Take the timeout from the SLA with a margin for normal variance.

Why does a saga test check both the money and the reserve, not only the order status?

Answer

The CANCELLED status can appear while one of the compensations has not run yet. The final state is consistent only when all side effects are reverted.

In an interview. Junior: In an asynchronous system I do not check at once but wait for the final state by polling with a timeout, not with sleep. Middle/Senior: I take the timeout from the SLA so degradation is visible. For sagas I break each step and check the compensation, its redelivery and a final state with no half-finished operations: money, reserve and status are consistent.

More articles in this chapter