Deep Read · DDIA · Chapter 11

Stream Processing

Designing Data-Intensive Applications · Ch 11 · Martin Kleppmann · 2017

中文 →

What's this chapter about?

The "purchase alert" that pops the instant you tap your card, the little car sliding across your ride-hailing map, the viewer count ticking up in a live stream—none of these can "wait until tonight to compute." Last chapter's batch processing is like washing one big tub of laundry: you collect a full load before running it, so what you see is always "yesterday's world." This chapter is about a different kind of work: process each piece of data the moment it arrives, so the result always hugs "right now." That's stream processing.

An analogy first

Picture every thing that happens—a click, a payment, a sensor reading—as a little sticky note you write once and never change: "at this time, this person did this." Stream processing is standing next to a conveyor belt of these notes as they keep flying in, reading each one as it comes, updating the books and firing whatever alert is due on the spot—rather than waiting for a whole sack to pile up before opening it.

Why the old world was hard

Batch processing has one incurable flaw: it's always waiting—waiting for a batch to fill, waiting for the whole batch to finish—so what you get is always the stale books from hours ago. But plenty of things in real life can't wait: fraud must be blocked on the spot, flash-sale inventory watched live, an outage flagged this instant. You can't recompute an entire day's data from scratch just to glance at the present moment.

The core mechanism, intuitively

Two plain ideas hold up stream processing. First, a never-deleted ledger. Don't toss each sticky note after reading it—pin them one after another onto a long conveyor belt and never tear them off; whoever wants to read brings their own bookmark, notes where they've reached, and can rewind the bookmark to re-read anytime (this is exactly what Kafka does). Second, the ledger and the balance are the same thing. Your bank balance is nothing but the sum of every transaction replayed from the start; conversely, as long as you keep every transaction, the balance at any moment can be recomputed. "Store every change, not just the latest state"—that small pivot is the soul of the chapter.

What it buys you / how to choose

With this replayable ledger, a change in one place can fan out to everyone who needs it within seconds: the moment the database changes, the search index, cache, and reports update live; fraud detection finishes deciding before your finger leaves the screen. Use streaming when you need it "on demand right now"; leave "run tonight, out by morning" work to batch. One honest cost: the moment you chase real-time, "time" itself gets slippery—a message sent from the subway may arrive minutes late; out-of-order and late events mean "what exactly happened this minute" never has a hard deadline, only a rough line you draw while admitting a few will slip through.

Remember this one line

Stream processing = process each item as it arrives, keeping results glued to the present, for the sake of "on demand right now." Two pillars: a never-deleted, replayable ledger (Kafka), and "store every change, not just the latest state"—the balance is just the sum of the ledger. The cost: real-time makes "time" slippery; out-of-order and late events have no hard deadline.

Want CDC, stream-table duality, windows & watermarks, exactly-once, and diagrams? → Switch to Deep mode