RAG Chatbot: Scaling It
From minute twenty, four complaints arrive about the assistant you just shipped. An answer shows someone text they had no right to open. A policy was rewritten yesterday and the old wording keeps coming back. Someone in one country receives another country's rules, and no component has a bug. And at nine the whole company turns up at once, for a call that is slow and priced.
The Document the Asker May Not Open
"An engineer asks about pay bands and the reply quotes an HR file they have no right to see. Where do you stop that?" This is the part you parked in Scope and promised to take first. Stop it before the prompt exists, because the model is not a control. A rule in its prompt is a request, and whatever it has read, a differently worded question can draw out.
Each chunk now carries a copy of its document's permission groups, written at ingestion. The server takes the asker's groups from the sign-in token, the reason the API took the asker from there, and filters the search by them, so a chunk outside those groups cannot come back. Assume 100 bytes of groups on each of the 8 million chunks. That is 800 MB, and the index stays inside its 50 GB.
A filter on a copy can be out of date, so the final five are checked against the source system, live, before the prompt is built. A chunk that fails gives way to the next in rank, which is checked the same way. The token's groups are a copy too, so this check also catches a person moved out of a group this morning, and a document deleted at its source fails it as well.
What it costs starts with a copy that lags by up to 15 minutes. Until a change lands, the live check is what stands between the asker and the old permission. In the other direction, someone just granted access misses the document for those minutes, a miss and not a leak. The check itself is a live call per answer to each source. A source that does not reply in time has its chunks dropped and the status names it, which the asker can tell apart from the documents having no answer. And a narrow filter slows the search or makes it miss, which you park for the deep dive.
The Policy That Changed Yesterday
"HR rewrote the leave policy yesterday and the assistant is still quoting the old one. Why, and what changes?" It quotes the old one because the index only changes on Sunday, and Scope agreed 15 minutes. A nightly rebuild would embed everything every night and still miss that. So a stream replaces the weekly job.
Change events from the sources enter a log keyed by document ID, so two edits of one document stay in order, as Chapter 6's Queues, Logs and Pub-Sub lays out. A log suits this better than a work queue because order per document matters, and at under a chunk a second, one reader per partition is never the bottleneck.
A worker takes an event and reads the document as its source has it now. It splits the document and hashes each chunk, a hash being a short fingerprint of its text. Only new hashes go to the embedding model, and an unchanged chunk reuses the vector it already has. The chunks go into the index and the store under the new version number, where no question can use them yet.
One write then flips the document's live version, a single column on its row in the chunk store. The text fetch returns a chunk only if its version is the live one, so a question sees one version or the other, never both. Only after the flip does the worker remove the old chunks. A delete writes the next version number with no chunks behind it, which closes the document to every question at once, and a slow worker's flip cannot reopen it. The same cleanup follows. A change of permissions takes the same log and rewrites the groups on the document's chunks, with nothing to embed.
What it costs is a pipeline to run, and you watch the age of its oldest unread event against the 15 minutes. Delivery is at least once, the default in Chapter 6's Delivery Semantics at Scale, and that makes a repeat ordinary traffic, which has to be harmless. A write keyed by document, version and position lands on the same row twice, and a repeated event finds every hash known and embeds nothing. A flip only moves a version upward, so a slow worker cannot undo a newer one.
Events can also be lost, with nothing later to expose them, so the old job stays as a nightly comparison of each source's listing, with versions and groups, against the index. A difference goes on the log as an ordinary event, and since an event only tells a worker to go and look, a false alarm costs one read. A lost delete or revocation still cannot reach an answer, because the live check refuses it. A lost edit can quote the old text until that night, and a lost grant misses the document until then: the two cases outside the 15 minutes.
The Wrong Country's Policy
"A manager in Germany asks about parental leave and gets France's policy, and nothing in your system malfunctioned. What do you change?" Look at the chunk. From the middle of the German policy it says "employees receive sixteen weeks" and never says Germany, so the two countries' chunks really are neighbours.
Ingestion now writes the title path into each chunk before embedding it, so the text itself says which policy and which country it belongs to. It also stores the country as a field beside the groups. The server filters on that field: the country the question names, otherwise the one on the asker's profile, and a document that names no country passes for everyone.
Exact words need a search of their own. A keyword index sits beside the vectors, because policy names and codes must match exactly and nearness in meaning does not promise that. It runs on machines of its own, since the vector node has no room. Each list gives 25, and they merge by rank. A re-ranking model then reads the question with each of the 50 and keeps five, assume in 200 milliseconds. It scores question and passage together, which a distance between two vectors cannot do.
None of that counts yet, because nothing has measured it. So add an evaluation set: assume 300 real questions, each with the document that should answer it. Say it before you claim anything: "Before I call this better I want a number: 300 real questions, scored before and after."
What it costs is a second index for ingestion and the filter to cover, and the people who write the set. It also costs time. Assume the steps before the model, the filter and the live check among them, take about half a second, and the model's first words a second more. The re-ranker brings that to 1.7 seconds against the agreed 2, and the next probe spends the 300 milliseconds left. If the set shows keyword search adds nothing on this corpus, it goes. If the re-ranker or one search fails, the rest still answer, with worse ranking and no outage.
Nine O'Clock
"Every answer ties up five seconds and a cent, and at nine the whole company arrives together. What gives out first?" The rented ceiling does, well before your servers, which are mostly waiting. That leaves three problems, so three changes: the wait, the ceiling and the bill.
The wait is about what the asker sees. Stream the words, the first of them about a second after the prompt goes out, which is 1.7 seconds after the question by the sum you just made. The five seconds still happen, and nobody stares at a blank screen through them.
For the ceiling, assume the provider allows 100 calls at once. Assume too that the first minutes after nine run at twice the hour's rate. That is 20 a second, and at 5 seconds each it is 100 open, with nothing to spare. So a model gateway, a small service every answer call passes through, caps the calls open and holds the excess in a short queue. The re-ranker is rented as well, and its quota is counted in tokens. Fifty passages of 2 KB are about 25,000 tokens a question, so 20 a second is about 30 million a minute, five times a vendor's published third tier. So the gateway caps the re-rank calls too, and you ask the vendor for that quota by name, or re-rank 20 passages, about 12 million.
The budget sets how short. Those 300 milliseconds, at 20 calls starting each second, are about 6 waiting. Past that bound the gateway refuses early and the asker gets the retrieved sources without an answer, the status sources only. That is the fast refusal of Chapter 6's Backpressure and Load Shedding, and it arrives while the sources are still useful.
The bill gets an answer cache, keyed by the normalised question plus the IDs and versions of the five chunks. So retrieval and the live check run first, and the cache sits behind them. A permission change leaves the key alone, so that check is what refuses a revoked asker, and the cache does not reduce its load: at most 5 calls an answer, up to 100 a second at 20 questions. Two askers share an answer only if they were allowed the same evidence. An edit changes the key by itself, so the invalidation Chapter 2's Caching at Scale warns about has nothing to do. Assume entries expire after a day, which bounds how long a deleted document's text sits there unreachable.
Assume one question in five repeats. With a warm cache the first minutes fall to 16 a second and 80 open, and up to about $360 comes off the $1,800 a day, since a hit still pays for the embedding and the re-rank. A Monday opens cold, at 100 open, which is why the gateway comes first. Say plainly that at most a fifth of the bill is the smaller win here, and the 20 calls of room at nine are the larger. You can give it in one breath: "The ceilings go first: 100 open on the model, and a token quota on the re-ranker. A gateway holds both lines, and the cache buys 20 model calls of room and up to a fifth of the bill."
What it costs is a gateway on the path, so run two, each allowed half the ceiling, and losing one means early refusals until it is replaced. The queue can hide a provider outage if depth is all anyone watches, so watch answer times as well. Reworded repeats miss, and a lost cache takes you back to 100 open and no worse. If repeats fall short, buy quota, or route simple questions to a cheaper model once the evaluation set shows it holds up.
The Design at Minute Thirty
A question now enters through the load balancer and the app servers. It passes vector and keyword search under the permission filter, then merge and re-rank, the live check, the answer cache, the model gateway and the model, and returns with citations. Below that, source changes enter the log, workers chunk, hash and embed, and both indexes and the chunk store are written before the flip. The weekly job survives as the nightly comparison, and the evaluation set sits off the path.
- Answering the leak with a stronger system prompt, which trusts the model to keep what it has already read.
- Adding new chunks without retiring the old, so both versions of a policy can land in one prompt.
- Reaching for a bigger embedding model with no way to show it helped.
- Keying an answer cache on the question alone, which reopens the leak through the cache.
- Each of the four fixes traced to the leak, the stale policy, the wrong country or nine o'clock.
- The cache placed behind the live check and keyed by chunk versions, unprompted.
- The evaluation set brought in before any claim that something improved.
- The provider's ceiling treated as a number to design against.
Knowledge Check
Each chunk carries a copy of its document's groups, and the final five also go to the source. What does that live check add?
- It replaces the filter, so the index need not hold a copy of any groups
- It catches a permission or a delete that the copied groups have not reached
- It stops the model repeating restricted text it has read in the prompt
- It proves the asker's token is genuine, which the filter cannot establish
Why does one write to a document's live version keep two versions of a policy out of one prompt?
- The write deletes the old chunks from both indexes in one transaction
- The worker embeds the new version first, so its vectors outrank the old
- The text fetch returns only chunks at the live version, whatever matched
- The log holds two edits in order, so an old chunk cannot be searched again
You add a re-ranker at 200 milliseconds. What lets you keep it, and say it helped?
- It fits, since 200 milliseconds is small beside the 5 seconds an answer takes
- It fits at 1.7 of the 2 seconds, and the 300 questions score better with it
- It fits, because streaming hides any delay before the first words arrive
- It fits, and a second model reading every passage can only improve answers
Why does the answer cache's key hold the IDs and versions of the five chunks?
- So reworded questions find each other, since they retrieve the same chunks
- So the cache can answer before retrieval, saving the search and the model
- So askers share only on the same evidence, and an edit changes the key
- So the key stays shorter than the question, and more answers fit on a node
You got correct