Replication and Replica Lag: Why a Revoked User Still Sees the Folder, and Which Reads Go to the Primary
An admin revokes a user's access to the Payroll folder, but the permission check reads from a replica that is 30 seconds behind (synthetic), so the assistant keeps quoting it. Route the reads that must be fresh to the primary, fail closed past a lag bound, and remember that replication copies mistakes too.
What you will learn
- Explain primary, replica and replica lag, and why a stale read can be a security bug
- Compare synchronous and asynchronous replication and the cost of each
- Route permission checks and read-your-writes to the primary, or wait for the replica to catch up
- Explain why replication is not a backup and how to test that a revoke propagates
Before you start
- SQL Essentials: SELECT, WHERE, JOIN, GROUP BY
- Database Index for an AI App
Replication means you keep copies of your database on other machines, called replicas, so reads can spread out. Every copy runs a little behind the original, and that delay is replica lag. In most apps a few seconds of lag is harmless. In an AI assistant that decides who may see which documents, it can turn into a security problem.
The revoked folder that still answers
Our support assistant answers tickets for many companies, and it only reads documents a user is allowed to see. An admin removes a user's access to the Payroll folder. That change is written to the primary, the main copy that takes every write.
But the permission check reads from a replica, and the replica hasn't received the change yet. The user asks a question and the assistant happily quotes the Payroll folder. The lag here is 30 seconds (synthetic numbers). For 30 seconds the revoked user still sees the data.
A user's access is revoked, but for 30 seconds the assistant still cites the folder. Explain it, and fix it.
- Name the cause. The permission check read from an asynchronous replica, which applies changes a little after the primary.
- Route the reads that must be fresh. Permission checks, and a user's reads right after their own write, go to the primary or wait until the replica has caught up.
- Prove it. Measure the lag, and test that a revoke is enforced within a bound you choose.
In short: fresh-or-safe reads go to the primary, and everything else can live with a little delay.
Photocopies in the branch offices
Think of a company with a head office and a few branch offices. Head office keeps the master rule book, and every change is made there first. That's the primary. Each branch gets a photocopy by courier, and the courier arrives a day late. That's a replica, and the day is the lag.
A customer walks into a branch and asks about a rule that changed this morning. The branch answers from yesterday's copy. That's a stale read. If the answer must be current, you phone head office.
One write, two copies
A write arrives (revoke access for this user) and the primary commits it, which means the change is saved for good. The primary then sends the change to the replica through its replication log, the ordered list of every change it made. The replica applies changes in order, so it's always some distance behind.
Most of the time that distance is milliseconds. Under a heavy batch job or a slow network it grows, and here it reaches 30 seconds. Any read that hits the replica inside that window gets the old answer. The replica isn't broken. It's just behind.
Synchronous or asynchronous
So why not keep every replica up to date at once? With synchronous replication the primary waits until a replica confirms the change before it tells the user the write is done. That replica has no lag, but every write gets slower. And if that replica goes down, writes can stall, because the primary is waiting for a confirmation that never comes.
With asynchronous replication the primary answers right away and ships the change afterwards. Writes stay fast, but reads from the replica can be stale. Most systems run asynchronous replicas for scale and handle the few reads that must be fresh on purpose. That trade-off is what the interviewer wants to hear.
| Synchronous | Asynchronous | |
|---|---|---|
| Lag on that replica | None | Milliseconds, sometimes much more |
| Write latency | Higher on every write | Unchanged |
| If the replica fails | Writes can stall | Writes carry on |
| Use it for | Writes you can never lose once confirmed | Read scale, most of the time |
Route the reads that must be fresh
- Read-your-writes. If a user just changed something, their next reads go to the primary for a short window, so they always see their own change.
- Permission checks. They're small and cheap, and they decide who sees what, so they read from the primary every time.
- Wait for the position. A smarter option is to remember the log position of the revoke and only use a replica once it has caught up to that position.
- Everything else, like ticket history or search results, can read from replicas.
And if the lag goes past your bound, fail closed: deny access until the replica catches up. A refused answer is annoying. A leaked payroll file is an incident.
Where weak answers go wrong
"Just add a cache in front." A cache is another copy, so it can hold the old permission even longer. Every copy needs the revoke to reach it. The same tenant and user scoping problem shows up in Caching for AI Apps.
"Make everything synchronous." That fixes lag for one replica but slows every write and can stall writes when that replica fails. Route only the reads that matter.
"We have replicas, so we have a backup." Replication copies mistakes too. If someone deletes a table, the delete reaches every replica within seconds. A backup is a separate copy from a point in time, and you prove it by restoring it. Replication helps availability and scale. It doesn't replace backups.
How you prove it works
Watch replica lag in seconds for each replica, and the time from a revoke to the first denied request. Then run the failure test in a test environment: pause the replica on purpose so the lag grows, revoke a user, and ask a question straight away. It passes only when the assistant refuses the folder within your bound, even while the replica is behind. Without that test you're only hoping the revoke arrives in time.
The follow-ups usually come back to one idea, knowing which reads must be fresh:
- Sync or async? Async for scale. Sync only where losing a confirmed write is unacceptable, and you accept slower writes there.
- How do you do read-your-writes? Send the user's reads to the primary for a short window, or wait until the replica reaches the write's log position.
- How do you test that a revoke propagates? Add lag on purpose, revoke, query straight away, and assert the answer is denied.
Recap
| Copy | What it does |
|---|---|
| Primary | Takes every write, plus the reads that must be fresh: permission checks and read-your-writes |
| Replica | Takes everything else; always assume it's a little behind, fail closed past the bound |
| Backup | A separate point-in-time copy, proven by a restore. Replication copies your mistakes as fast as your data |
Replicas spread reads. When the data itself no longer fits one primary, the next step is sharding.
Try it yourself
A user edits their own support ticket and then reloads the page, which reads from a replica. Describe two ways to make sure they see their edit, and say what each one costs.
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.
