Inside the Streams Commit Cycle
Linked from Does Every Event Write to the Changelog?
The Write-Back Cache, Step by Step
Repeated updates to the same key are conflated in memory. Streams keeps an in-memory write-back cache in front of RocksDB (statestore.cache.max.bytes, formerly cache.max.bytes.buffering; 10 MB per instance by default). Each event updates the cache only; nothing touches disk or Kafka until a flush:
user_1 = 100, then 50 impressions arrive
event 1: read 100 (from RocksDB) → cache: user_1 = 101 no I/O
event 2: read 101 (from cache) → cache: user_1 = 102 no I/O
...
event 50: cache: user_1 = 150 no I/O
FLUSH: RocksDB ← user_1 = 150 (one write)
changelog ← user_1 = 150 (one record)
downstream ← user_1 = 150 (one update)
then commit input offsetsA flush happens on every commit (commit.interval.ms: 30 s by default, 100 ms with exactly-once) or earlier when the cache fills up, evicting least-recently-used keys. So the write rate tracks distinct keys touched per flush, not event count. Hot keys cost almost nothing; a perfectly uniform key space approaches 1:1. The writes that do happen are batched, compressed, sequential appends, not a round trip per record. The trade-off: a bigger cache or longer interval means fewer writes but later updates downstream; a cache size of 0 writes every increment.
Why losing the cache on a crash loses no events. Committing an offset saves a bookmark in __consumer_offsets: "everything up to here is done; restart me from here." Streams always flushes state first, then saves the bookmark, so the bookmark never runs ahead of saved state:
| Offset | Event | State | Bookmark |
|---|---|---|---|
| 1000 | last commit | 100, flushed | 1000 |
| 1001–1030 | 30 impressions | 130, in cache only | still 1000 |
| — | 💥 crash | 130 lost | still 1000 |
On restart the changelog restores 100, the bookmark says start at 1000, and re-reading 1001–1030 gets back to 130. Saving the bookmark first would be worse: a crash before flushing would restore 100 and skip those 30 events forever.
The Store Chain and the Commit Order
Why this is safe. A flushed entry travels down one store chain — but read the middle layer carefully, because it performs two independent writes, not one atomic one:
There is no transaction across those two lines, and no rollback. A crash between them leaves RocksDB holding an update the changelog never received. That window is real — see Why There Is No Transaction Here below for why it costs nothing.
Then at each commit, Streams strictly orders what follows:
The invariant that falls out: state can be ahead of the committed offset; the committed offset can never be ahead of state. That asymmetry is deliberate — the forbidden direction would mean silently lost updates, while the allowed one only means a few events get reprocessed. Streams chooses duplicate work over data loss.
One consequence worth internalizing: because the cache also evicts under memory pressure mid-interval, the changelog being ahead of the committed offset is not a rare crash artifact — it is the normal steady state. Which is exactly what makes the commit-cycle failure scenarios interesting.
Why There Is No Transaction Here
A flush makes three writes: RocksDB, the changelog, and then the input offset. None of them share a transaction. There are two pairs to check, and they get opposite answers:
| Pair | If they disagree after a crash | Needs a transaction? |
|---|---|---|
| RocksDB ↔ changelog | Throw RocksDB away and rebuild it from the changelog | No. RocksDB is a disposable copy |
| Changelog ↔ input offset | Wrong counts: events lost or counted twice | Yes, for exact results |
Why RocksDB needs none. RocksDB and the changelog are not two equal copies that must agree. The changelog is the source of truth; RocksDB is a local copy built from it. Streams never trusts RocksDB after a crash: it either proves the copy is current (the .checkpoint file) or deletes it and rebuilds. Disagreement costs a rebuild, never a wrong answer.
So the pair that needs atomicity is a different pair entirely:
Why the changelog and offset do need one. Both are writes to Kafka, so they can share a Kafka transaction:
at_least_once(default): no transaction, only ordering. Changelog first, offset last. You never lose an event, but a crash between the two counts events twice (the trap above).exactly_once_v2: changelog, output and offset commit in one Kafka transaction. All three land or none do.
RocksDB can't join that transaction. Coordinating a local embedded store with a Kafka cluster on every commit would cost far more than an occasional rebuild.
The crash windows, and why each is survivable:
| Window | What happens |
|---|---|
| Changelog send fails, RocksDB already written | The task fails and is closed dirty, which wipes RocksDB. Streams rebuilds it from the changelog and reprocesses input from the last committed offset. |
Hard kill (kill -9) between the two writes | No wipe runs, so RocksDB keeps some writes the changelog never got. But the offset is back at the last commit, so those keys are reprocessed anyway. Result: ordinary at-least-once double-counting, nothing new. Under exactly-once, any unclean restart wipes RocksDB, so this window doesn't exist. |
| RocksDB write fails | The error comes before the changelog write, so nothing reaches the changelog. It means disk trouble; the task dies. |