Consistency and CAP: What You Give Up When the Network Breaks, Strong vs Eventual, Chosen Row by Row
Emma and Noah press "Assign to me" on ticket 142 while the link between two database copies is cut. Choose consistency and Noah gets "try again"; choose availability and both see "assigned to me" until one gets an apology. CAP in plain words, PACELC's good-day cost, and a two-second question that picks strong or eventual per piece of data.
What you will learn
- Explain strong and eventual consistency and what each one costs
- State CAP in plain words and why the real choice is C or A during a partition
- Walk through a partition with two agents assigning the same ticket
- Pick strong or eventual for each piece of data with the two-second question
Before you start
- Replication and Replica Lag
Once your app keeps copies of its data on more than one machine, you get speed and safety. You also get a question nobody asks until it hurts: what happens when the copies stop talking to each other?
Four words carry this article: strong, eventual, partition and CAP. By the end you will be able to choose, for each piece of data, which promise it needs.
The problem: two agents, one ticket, a cut cable
TicketHub, a support-ticket app, has two copies of its database, copy A and copy B. Emma, an agent, opens ticket 142 and presses "Assign to me". Her click lands on copy A. At the same moment Noah, another agent, presses the very same button, and his click lands on copy B.
Right now the line between the two copies is cut. A broken cable, a jammed network, it doesn't matter which. They cannot talk. That is a network partition.
So who gets the ticket? Both machines are healthy, both received a click, and neither knows about the other.
Strong consistency
Emma's write lands on copy A, but copy A does not answer yet. It waits until copy B confirms it has the same change. Only then does Emma see "OK, it's yours".
The promise: every read sees the latest write, from any copy. You never see old data. The price is waiting.
Eventual consistency
Same click, same copy A. This time copy A says OK straight away, and copy B finds out a moment later: a few milliseconds, maybe a few seconds if the network is slow. That delay is the replication lag from replication and replica lag.
The promise is softer. If nobody writes again, all the copies will agree, eventually. In between, somebody can read an old value.
Two bank branches
Picture a bank with two branches that talk by phone all day, so both always know your balance. Then the phone line is cut.
- Choice one is to close the counters until the line is back. Nobody can withdraw, customers are annoyed, but the books stay correct. That is the consistent choice.
- Choice two is to keep serving everyone. Customers are happy. But you have 100 dollars, take it out at branch one, walk into branch two and take it out again. Two withdrawals, 100 dollars. The branches sort it out when the line returns, and now somebody owes money.
Stop and stay correct, or keep going and clean up later. Hold on to that picture.
CAP in plain words
- C, consistency: everyone sees the same, latest data.
- A, availability: every request gets an answer, not an error.
- P, partition tolerance: the system keeps working even when the line between copies is cut.
People often skip this part. On a real network the line will break sometimes, so you cannot switch P off. The real choice is only this: during a partition, do you give up C or give up A? When everything works, you can have both.
Back to Emma and Noah
Consistency first: copy B cannot check with copy A, so it refuses Noah's click with "try again in a moment". Only Emma owns the ticket. Correct, but Noah sees an error.
Availability first: both copies accept. Emma sees "assigned to me", and so does Noah. When the line returns, the system finds two owners and picks one; the other agent gets an apology. Everyone was served, and one person was misled for a while.
Which data needs which
Real apps don't pick one answer for everything. They pick per piece of data.
| Data | Choice | Why |
|---|---|---|
| Ticket assignment | strong | two owners is a mess |
| Payment / refund | strong | money must never move twice |
| Ticket view counter | eventual | 41 instead of 42 for a second is fine |
| "Agent is typing" status | eventual | gone in seconds anyway |
| Search index | eventual | a few seconds behind is fine |
One app, both choices, decided row by row.
The price on a good day
Even when nothing is broken, the choice costs something. With eventual, one copy confirms and you move on. With strong, the write waits for every copy to confirm, so the slowest copy sets the pace, and a copy in another city makes it worse. There is always a trade between speed and agreement. Researchers later extended the CAP idea to cover this case, under the name PACELC. You don't need to remember the name, only the trade.
Strong vs eventual, side by side
| Strong | Eventual | |
|---|---|---|
| User sees | always the latest value | maybe an old value for a moment |
| Speed | writes wait for the copies: slower | answers at once: faster |
| When copies disagree | refuses or waits until they agree | accepts, fixes it afterwards |
| Good for | money, ownership | counters, status, search |
Neither is better. Each is right for different data.
The one question to ask
What is the harm if two people see different values for two seconds?
- Nothing much (a counter, a status): choose eventual and enjoy the speed.
- Money lost, or a double booking: choose strong and accept the wait.
Ask it for every piece of data and the design mostly writes itself. In an AI assistant, a user's document permissions fail this test badly: the replica lag article shows a revoked user still reading a folder from a copy that is behind.
Common mistakes
- Choosing one consistency level for the whole app, so either everything is slow or money can move twice.
- Forgetting the good-day cost: strong writes are slower even when the network is fine.
- Reading permission checks from an eventually consistent copy.
Recap
| Term | Plain meaning |
|---|---|
| Network partition | the copies cannot talk for a while |
| Strong consistency | the write waits for the copies; every read sees the latest write |
| Eventual consistency | answered at once; others catch up after the replication lag |
| CAP | during a partition, give up consistency or availability |
| PACELC | even with no partition, you trade latency against consistency |
Try it yourself: strong or eventual for these four? Likes on a post, seats left on a flight, a user's display name, stock in a warehouse. Decide with the two-second question first, then paste this into any AI assistant and see if it agrees: "I think likes on a post are eventual, seats left on a flight are strong, a user's display name is eventual, and stock in a warehouse is strong. Challenge each answer and tell me what could go wrong if I am wrong."
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
Fallback Models for an LLM App: A Chain That Checks Region and Fit, Flags Degraded Answers and Gets Rehearsed
When the main model goes down, the backup has to be allowed to take the data, able to read the whole request and tested before the bad day. A simulated four-model chain for an EU customer: a 6,000-token request lands on mid-eu, flagged degraded; a 10,000-token request is served by nobody, on purpose.
Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
A bulk upload buries the chat. In a small simulation, one shared queue grows to 199 waiting and the middle chat answer takes 80.2 s. A bound plus a chat lane brings it to 0.35 s while 178 bulk jobs are told to retry in 10 s, and cancelling abandoned calls saves 21,000 wasted tokens.
Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
Six GPU replicas, two at 95% and four at 30%: round robin counts requests, and requests aren't equal. In a small simulation, least outstanding requests cuts p95 from 6.52 s to 3.05 s, prefix affinity lifts cache hits to 95% but pushes p95 to 11.45 s, a real health check takes errors from 48 to 2, and draining cuts 0 answers instead of 4.
A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue
A queue doesn't lose a document, but it can hand one out twice. In a small simulation, a worker crash makes document two go from 3 to 6 chunks and the index holds 11 when the documents hold 8. An idempotent consumer brings it back to 8 with the same two deliveries, and a limit of three tries moves a poison file to a dead letter queue.
