CAP and reliability at a basic level
The CAP theorem says that during a network partition a distributed system must choose between consistency and availability. The choice is made per feature: for a balance an error is better than a stale number, while a catalogue may serve slightly stale data. QA tests the chosen behaviour through fault injection, together with timeouts, a circuit breaker with recovery and degradation of non-critical features.
System design · Scaling
Ledger is unavailable. Show the last known balance or show an error?
Ledger is down for a minute. The app has two options: show a balance from a cache that is 10 minutes old, or honestly say "unavailable right now". For a news feed the first is fine. For a balance by which the user decides whether funds are enough, it is not. That is exactly the choice CAP describes. The lesson covers this choice at the level needed in an interview and the patterns that keep one service's failure from taking down the rest.
How it works and why
The CAP theorem: during a network partition (P) a distributed system must choose between consistency (C: everyone reads the latest data) and availability (A: every request gets a response). Partitions do happen in real networks, so the choice is effectively between C and A at the moment of failure. When the network is healthy you can have both, but latency and the cost of agreement force trade-offs again.
What is chosen during a failure
Better an error
Balance, debits. Refuse rather than act on stale data and go negative.
Better stale
Catalogue, likes, recommendations. Answer even if the data is a little old.
The practical conclusion for QA: you need to know which behaviour was chosen for each feature and test exactly that. If the balance is meant to be CP, then when Ledger is unavailable the API returns an error, not an old balance from a cache.
Example: a dependency failure
test('balance: an error, not a stale number', async ({ page }) => {
await page.goto('/account');
await expect(page.getByTestId('balance')).toHaveText('1 500,00');
await page.route('**/api/ledger/balance', (r) => r.abort());
await page.reload();
await expect(page.getByRole('alert')).toContainText('Balance is temporarily unavailable');
await expect(page.getByTestId('balance')).toBeHidden();
});
The test reproduces the failure at the network level and checks not only that a message is present but that the stale number is gone. If the number remains, the system chose AP where CP was expected. The fix belongs to the product (do not cache the balance or do not show the cache without a label), not to the test.
Reliability patterns
- Timeout: on every call to another service. Without it one slow service blocks all threads.
- Circuit breaker: after a series of errors it opens: the dependency is not called for a while and a fallback or error is returned at once; then a trial request (half-open) decides whether to close again.
- Graceful degradation: if recommendations are unavailable, the payment page still works.
- Bulkhead: resource isolation so one feature's failure does not take all connections.
This is verified with chaos testing and fault injection: delay a dependency's responses, kill an instance, cut the database connection and see whether the system behaves as designed. Start on staging with controlled experiments and a clear criterion of what counts as normal.
Limits and mistakes
- CAP is not about slowness: "the system is slow" is not the same as a network partition. It is a simplified model; do not stretch it further in an interview.
- One choice for the whole system. In reality the choice is per feature: balance CP, catalogue AP.
- Retries without a circuit breaker during a failure increase load on an already sick dependency.
Check yourself
A test disabled Ledger and the UI showed an old balance without a label. A bug or normal?
Answer
It depends on the choice for the feature. If the balance is meant to be CP, it is a bug: the client may act on an outdated number. If AP is acceptable, the cache is shown with a staleness label.
What does a circuit breaker do and what to check in a test?
Answer
After a series of errors it stops calling the dependency and returns a fallback or error at once. Check that requests really stop, that the response is fast and that calls resume after the dependency recovers.