Local State by Example: The Basics
A stream processor reads events one at a time. To do anything useful, it must remember things between events: how much this card spent in the last hour, which zone this driver was in, where this viewer paused the show. That memory is called state. This page walks through six real-world systems, slowly, and shows exactly what each one remembers, where it keeps it, how it uses it, and what goes wrong if you build it the obvious way.
The Basics, in 7 Ideas
Idea 1 — State is what you remember between events
Some jobs need no memory. "Drop every event where the amount is zero" looks at one event and decides. Done.
Most real jobs do need memory. "Flag a card that spent more than ₹50,000 in the last hour" cannot be answered from one event. You need to know what the card spent before this event. That remembered information is the state.
Idea 2 — The key decides which machine gets the event
Every Kafka event has a key, like card_id or driver_id. Kafka hashes the key to pick a partition. So every event for card 4417 lands in the same partition, in the order it was written.
Kafka Streams then gives each partition to exactly one task, and a task runs on one thread on one machine. Put together:
All events for one key are handled by one thread on one machine, one at a time, in order. This single sentence is why everything on this page works.
It means the machine handling card 4417 sees all of that card's history. It never needs to ask anyone else. And because only one thread touches that card, there are no races and no locks.
Idea 3 — The memory lives on the same machine (a "local store")
Each task gets its own small key-value database called a state store. By default it is RocksDB, an embedded database that lives inside your app's process and keeps its data on the local disk, with hot data cached in memory.
Why on the same machine? Speed. Look at what one read costs:
| Where the data lives | Cost of one read | What that means at 50,000 events/sec |
|---|---|---|
| Local store (RocksDB, cached) | a few microseconds | Easy. One thread can keep up. |
| Remote cache (Redis in the same data centre) | about 0.5–1 ms, often much worse at the slowest 1% | Each event waits on the network. You need many threads, and Redis becomes a shared bottleneck that everyone depends on. |
| Remote database | a few ms | Usually too slow to call on every single event. |
Idea 4 — Every change is also backed up to Kafka (the "changelog")
A local disk can die. So every time the task writes to its store, Kafka Streams also writes the same key and value to a special topic called the changelog topic (it is named <app-id>-<store-name>-changelog).
The changelog is compacted: Kafka keeps only the latest value for each key and throws older ones away over time. So the changelog does not grow forever. It stays about the same size as the store itself.
Idea 5 — After a crash, the memory is rebuilt from the changelog
If machine A dies, Kafka Streams moves A's tasks to machine B. Machine B starts with an empty store. It reads the changelog from the beginning and writes every key back into a fresh RocksDB. When it reaches the end, the store is exactly as it was, and B starts processing new events from where A stopped.
Rebuilding a big store can take minutes. To make it fast you can turn on standby replicas (num.standby.replicas=1): another machine quietly keeps a warm copy of the store by following the changelog all the time, so it can take over in seconds.
Idea 6 — If events arrive with the wrong key, re-key them (a "repartition")
Sometimes the events you get are keyed by one thing, but you need to group them by another. Example: driver pings arrive keyed by driver_id, but you want counts per zone_id. The fix is to change the key and write the events to a new internal topic, called a repartition topic. Kafka then sends each event to the partition for its new key. It costs one extra trip through Kafka, but after it, each zone again lives on exactly one machine.
Idea 7 — Other services can read the store directly ("interactive queries")
Usually the store is private to the stream processor. But you can also let an API read it. Kafka Streams can tell you which machine holds a given key. Your API then calls that machine, and that machine reads its local store and answers. The store becomes a small, always-fresh database for that one job.
Four Shapes of State
| Shape | What it stores | Plain example | Used in |
|---|---|---|---|
| Latest value | Only the newest value for each key. Each event replaces the old one. | "Where did this viewer pause?" | Netflix, DoorDash |
| Running total | A number (or a few numbers) that each event adds to or subtracts from. | "How many drivers are in this zone right now?" | Uber, LinkedIn |
| Time window | A total that only counts events inside a time range, like "today" or "last hour". Old windows are deleted automatically. | "How many times did this user see this ad today?" | Fraud, Uber, Ads |
| Small state machine | The current step of a process, plus rules about which step can come next. | "This order is at picked up; next can only be delivered." | DoorDash |
Kafka Streams gives you two ways to write the logic:
- The DSL — ready-made building blocks like
count(),reduce(),aggregate(),join(). Use it when your logic fits one of them. It creates the store and changelog for you. - The Processor API — you write a
process(record)method and callstore.get()/store.put()yourself. Use it when your logic is custom rules, like fraud checks or a state machine.
Both use the same stores, the same changelogs and the same crash recovery underneath.