Single logo
  • Courses
  • Blog
  • About
Join free on SkoolJoin free on Skool

Learning path · 15 steps

AI System Design: Reliable, Fast and Affordable LLM Apps
  1. 1
    Fallback Models for an LLM App: A Chain That Checks Region and Fit, Flags Degraded Answers and Gets Rehearsed
  2. 2
    Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
  3. 3
    Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
  4. 4
    A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue
  5. 5
    Caching for AI Apps: Four Layers, What Goes in Each Key, and How a Delete Travels
  6. 6
    Semantic Caching: Why a Similarity Threshold Can't Stop Confident Wrong Answers
  7. 7
    LLM Model Routing: Rules, a Classifier or a Cascade, Judged on Cost per Win and Every Topic
  8. 8
    Observability and Tracing for LLM Apps: A Span per Stage, Redaction First, Bad Traces Become Tests
  9. 9
    Cutting p95 and Cost in Half for an LLM App: Read the Bill, Match the Lever, Guard Success
  10. 10
    Guardrails for an LLM Agent: Check What Comes In, What It Wants to Do, and What Goes Out
  11. 11
    Embedding Migration With Zero Downtime: Two Indexes, a Dual-Write Backfill, a Shadow Check and a Flag
  12. 12
    Design a Multi-Tenant RAG Assistant, Part 1: Requirements, Rough Numbers and a Contract
  13. 13
    Design a Multi-Tenant RAG Assistant, Part 2: Tenant Isolation, a Scoped Cache and Walking an Outage
  14. 14
    A 500 MB PDF Takes 50 s to Answer, Get It to 2 s: Measure First, Then Promise Per Path
  15. 15
    A 500 MB PDF From 50 s to 2 s, the Fast Path: Queue, Status, Streaming and an Acceptance Test That Fails
  1. Blog
  2. /
  3. AI Engineering
  4. /
  5. AI System Design

Learning path: AI System Design: Reliable, Fast and Affordable LLM Apps · Step 4 of 15

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.

Chaand Sheikh

·27 November 2026AI System Design

What you will learn

  • Explain at-most-once and at-least-once delivery, and why ingestion picks at least once
  • Make a repeat delivery harmless with an idempotent consumer keyed on document id and text fingerprint
  • Choose a lease length and renew it for long jobs
  • Stop a poison document with a retry limit, a dead letter queue and a visible status

Before you start

  • The RAG Pipeline Explained
  • Retries and Backoff for Reliable Tasks

A user uploads a file, and it has to end up searchable, even when workers crash. A message queue sits in the middle of that path, and it makes you a promise. It's not the one most people assume. We'll see it with a few pictures and a small simulation, and you'll leave with an answer you can say in an interview.

The path of one document

Upload, queue, worker, index, status Upload Queue Worker read, chunk Index Status This lesson spends its time on the queue
Save the file, say thanks, put a message on the queue. A worker does the rest.

The upload comes in, we save the file and say thanks. A message goes on the queue. A worker picks it up, reads it and cuts it into chunks. The chunks go into the index (see Chunking Strategies for RAG). And a status tells the user where their file has got to.

Tickets on the kitchen rail

Picture a restaurant kitchen with order tickets on a rail. A cook takes a ticket, and a timer starts on it. That timer is called a lease. When the dish goes out, the cook spikes the ticket: that's the acknowledgment. Now say the cook walks off and says nothing. The timer runs out, and the ticket slides back on the rail. A second cook makes the dish again, so now there are two plates for one order.

So the rail doesn't lose an order, but it can hand one out twice. That's the promise we're working with.

One crash, three extra chunks

Design ingestion for two million PDFs a day, and tell me what happens when a worker crashes mid-batch.

That volume is just for illustration. In a small simulation, we put four documents on the queue. A worker writes the chunks of document two into the index, and crashes before it can say done. The lease runs out, document two comes back, and the next worker writes it all over again. Document two goes from 3 chunks to 6. The index ends with 11 chunks, when the documents only hold 8. Nobody saw an error, and search can now return the same passage twice.

When do you say done?

It comes down to when the worker acknowledges.

At most once against at least once Ack, then work crash in between: the document is gone at most once a silent miss Work, then ack crash in between: the message comes back at least once a duplicate we can fix This design never claims exactly once
For ingestion, a missing document is silent and a duplicate can be fixed, so take at least once.

Acknowledge first, then do the work, and a crash in between loses the document. That's at most once. Do the work first and acknowledge after, and a crash in between brings the message back. That's at least once. For ingestion we take at least once. And don't promise exactly once in your interview: we can't control delivery that tightly across a crash. What we can do is make a repeat harmless.

Make the repeat harmless

The fix sits in the consumer, the part that reads from the queue. Before it writes, it builds a key from the document id and a fingerprint of the text. If it has already seen that key, it writes nothing. That's an idempotent consumer: doing the job twice leaves the same result as doing it once.

Same simulated crash. Document two comes back, the key matches, and it stays at 3 chunks. The total goes from 11 to 8. Now look at the deliveries: document two is delivered 2 times in both runs. We didn't change delivery at all. We changed what a repeat does. From the simulator's output (simulated):

"crash_naive": { "chunks": 11, "d2_chunks": 6, "deliveries": { "d1": 1, "d2": 2, "bad": 3, "d3": 1 } }
"crash_idem":  { "chunks": 8,  "d2_chunks": 3, "deliveries": { "d1": 1, "d2": 2, "bad": 3, "d3": 1 } }

This is the same idea as an idempotency key on a retried API call; see Retries and Backoff for Reliable Tasks.

A lease longer than the job

How long should that timer be? Too short, and a slow worker loses its ticket while it's still cooking, because a lease can't tell slow from dead. In a small simulation, that's two deliveries of one document; the slow worker's late write gets skipped, so it stays at 3 chunks. The key saved us again.

Too long, and a crashed job just sits there while the user waits for nothing. So set it longer than a normal job, using job times you've measured, not a guess. For a long job, the worker can renew its lease while it works, like resetting the timer on the ticket. (This lease advice is reasoning, not a simulated result.)

Poison files and a clear status

One file has text our parser can't read, so it's never going to work. That's a poison document. With no limit on tries, we stopped the simulation at 30 worker calls, and 27 of those went to this one file. To be fair, the other three documents still got indexed, because the broken one goes to the back of the line. The cost is wasted work, and it only stopped because we stopped it.

Retry limit and dead letter queue No limit 27 of 30 calls to one broken file status: retry Limit 3 moves to dead letters status: dead index stays 8 chunks Owner fixes it new text, new key status: indexed 2 chunks All simulated, four documents
A user gets a clear answer, not a spinner that never ends.

So set a limit of three tries. After that, the document moves to a dead letter queue, and the index keeps its 8 chunks. On the status board, three documents say indexed and one says dead. Somebody has to own that dead letter queue: they fix the file and put it back. It's new text, so it gets a new key, and it's indexed with 2 chunks.

Follow-ups and a simple rubric

These answers are reasoning, not results:

  • What's your partition key? The document id, so repeats of one document stay together, and one big customer doesn't pile up on one partition.
  • How do you test it? Kill a worker between the write and the done, then compare chunks in the index with chunks in the documents. And watch the age of the oldest message and the dead letter count.
  • What if the user edits the file? New text means a new key, but the old chunks are still there, so you replace them by document id.
LevelSounds like
Weak"The queue handles it, and we retry."
GoodAt least once, names the lease, has an idempotency key
StrongAdds a dead letter queue with an owner, a status the user can see, and one measured failure, labelled as simulated

The rubric is a teaching guide, not a guaranteed result. When the uploads arrive faster than workers can clear them, see Back Pressure for LLM Traffic.

Recap

IdeaWhat to remember
DeliveryAt least once: plan for repeats, acknowledge after the work
Idempotent consumerSimulated: 11 chunks back to 8, with the same two deliveries
LeaseLonger than a measured normal job; renew it for long jobs
Poison fileLimit of three, dead letter queue with an owner, a status the user sees

Try it yourself

Say one file crashes the worker on every delivery, so it can't even report a failure. Talk it through, and say what the user sees at the end.

Tried the exercise? Share your answer or ask a question.

The StudyEasy AI Community is free to join, with the lesson files, notebooks and code.

Join free on Skool

Keep going in the learning path AI System Design: Reliable, Fast and Affordable LLM Apps · step 4 of 15

Previous
Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
Next post
Caching for AI Apps: Four Layers, What Goes in Each Key, and How a Delete Travels

Related course on Udemy

Machine Learning Engineer: From First Model to ML Engineer

Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML 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.

Caching for AI Apps: Four Layers, What Goes in Each Key, and How a Delete Travels

A support bot's cache can hit 97% of requests and still answer only 52% correctly. Four cache layers, the key each one needs, how a document delete has to travel through all of them, and a 200-request simulation that puts hit rate next to correct answers.

On this page

  • The path of one document
  • Tickets on the kitchen rail
  • One crash, three extra chunks
  • When do you say done?
  • Make the repeat harmless
  • A lease longer than the job
  • Poison files and a clear status
  • Follow-ups and a simple rubric
  • Recap
  • Try it yourself
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

Related course on Udemy

Machine Learning Engineer: From First Model to ML Engineer

Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (paid course, opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.