Video in progress

Strong Junior 🏗️ System design

Explain the CAP theorem in plain words. What does it mean in practice?

In a distributed system, when the network splits (a partition), you have to pick one of two: either you always give correct data and reject some requests (consistency), or you answer everyone but some answers may be stale (availability).

📖 Answer

C — consistency: every read sees the latest write. A — availability: every working node answers requests. P — partition tolerance: the system keeps running even when communication between nodes is cut.

People often say "pick two out of three", but that is misleading. A network failure will happen in a distributed system anyway, so you can't give up P. The real choice only exists during a partition: C or A.

An example. Two data centers, and the link between them is cut. A user changed their balance in the Tashkent DC.

  • Choosing CP: the Samarkand DC returns an error, saying "the data may be stale". That's right for a bank account.
  • Choosing AP: Samarkand shows the old balance, and the data is reconciled once the link is back. That's perfectly fine for like counts or a feed.

In practice the choice is made per feature, not for the whole system. In one application it's normal for payments to be CP and recommendations to be AP.

In an interview, if you want to go one step further, mention PACELC: even without a partition there is a trade-off between latency and consistency.