Highlights

Contents

BACKEND ENGINEERING / UNIT VIII / FIELD GUIDEQueues, Kafkaand eventsWork that happens after the response, reliablyorder124order125order126order127order12848104811481248134814481548164817481848194820482148224823kitchenanalyticssay it once, read it many timesQueuesacks, retries, DLQsKafkapartitions, offsets, ISREventsschemas, outbox, sagasSchedulesone job, twenty instances
Before we start

How to read this chapter

A request should do what the user is waiting for and nothing else. Everything else, the email, the receipt, the kitchen ticket, the analytics, is work that can happen a moment later, and this unit is about making sure it does happen, exactly as often as it should.

The first four parts cover queues and background jobs. They explain why work leaves the request, how consumers take and acknowledge messages, what “at least once” and “exactly once” really promise, and what to do with messages that fail. Parts V to VII cover Kafka, which looks like a queue and is really a replicated log, from partitions and offsets to replication, transactions and the operational problems of lag and ordering. Parts VIII and IX lift the view to events as a way of designing systems, with schemas, eventual consistency, event sourcing, the transactional outbox and sagas. Part X handles scheduled work and follows one order’s events end to end.

Each section starts from a Wren situation, explains the mechanism, works the numbers and then shows a figure. Picture this boxes give analogies, notes add detail, Watch out names real mistakes, and Interview lens gives an answer to say aloud. In one breath closes each part.

By the end you should be able to explain why exactly-once delivery is hard and what to build instead, size a worker pool and a backlog, choose Kafka partition keys, design an outbox, and answer “you have 20 instances, how does a daily job run once?”

The running system

Meet Wren

Wren is the illustrative food-ordering service from earlier units. These are the numbers this unit uses. All are assumptions chosen for readable arithmetic, not measurements.

Setting Value What it controls
Orders at peak 50 per second Job and event volume
Events per order about 5 (placed, paid, accepted, assigned, delivered) 250 events per second at peak
Average event size 1 KB Kafka storage
Kafka topic order-events 12 partitions, replication factor 3, min.insync.replicas 2 Parallelism and durability
Retention 7 days Replay window
Receipt email 300 ms per send Worker sizing
Receipt PDF 800 ms per render Worker sizing
Queue visibility timeout 30 s Redelivery
Retry policy 8 retries with waits from 1 s doubling to 128 s, then a dead-letter queue Failure handling
App instances 20 at peak Scheduled job duplication

The main flow in this unit is what happens after POST /orders commits order 124. Part X follows its events to the kitchen, the receipt, analytics and search.

The map

What is inside

Move slow and fragile work out of the request, and make sure it still happens, once, in the right order.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Why work leaves therequest2Consuming safely3Delivery semantics4Retries, poison messagesand DLQs5Kafka fundamentals6Kafka reliability7Running Kafka8Event-driven design9Outbox, inbox and sagas10Scheduled work and theorder end to end
  1. 01Why work leaves the requestSections 1 to 3. We take slow and fragile chores out of the request path and hand them to a queue.
  2. 02Consuming safelySections 4 to 6. We look at the consumer's side of the contract, how long it may hold a message, how many it may take at once, and what happens when it cannot keep up.
  3. 03Delivery semanticsSections 7 to 9. We pin down what a messaging system promises about how many times each message is handled.
  4. 04Retries, poison messages and DLQsSections 10 to 12. We decide what a consumer does when processing fails.
  5. 05Kafka fundamentalsSections 13 to 15. We open Kafka and find a distributed, replicated log rather than a queue.
  6. 06Kafka reliabilitySections 16 to 19. We make Kafka keep records through broker failures and producer retries.
  7. 07Running KafkaSections 20 to 22. We deal with the problems that show up once Kafka carries real traffic.
  8. 08Event-driven designSections 23 to 25. We step back from brokers to the way events shape a system's design.
  9. 09Outbox, inbox and sagasSections 26 to 29. We solve the problem every event-driven system meets on its first day, changing the database and publishing an event as one reliable step.
  10. 10Scheduled work and the order end to endSections 30 to 33. We handle work triggered by the clock rather than by a request, then follow order 124's events through every consumer.
  11. +Interview prepInterview question bank, graded exercises and worked solutions.