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 offsets

A 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:

OffsetEventStateBookmark
1000last commit100, flushed1000
1001–103030 impressions130, in cache onlystill 1000
—💥 crash130 loststill 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:

The store chain. CachingKeyValueStore, the in-memory buffer, passes an entry down on eviction under size pressure or on flush at commit. ChangeLoggingKeyValueBytesStore then performs two independent writes: wrapped put of k, v into the RocksDB memtable, local and synchronous; and log of k, v through producer send, async, buffered, and not acked. There is no transaction across the two and no rollback.
The middle layer makes two independent writes, not one atomic one A crash between the two writes leaves RocksDB holding an update the changelog never received — and that is fine, because RocksDB is never trusted across a failure.

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 four steps of a Streams commit, strictly in order, drawn as a table. Step 1, flush cache: write-through to RocksDB and the changelog producer; the sends are buffered, not yet acked, so state is written but not durable. Step 2, producer.flush: block until every changelog and output record is acked by the brokers; nothing below runs until it returns; now state is durable. Step 3, consumer.commitSync: only now does the input offset advance, always the last of the three to move. Step 4, write the .checkpoint file: note the changelog offset so a local restart restores from there. The invariant: state may be ahead of the committed offset, which only means a few events are reprocessed; the committed offset may never be ahead of state, which would mean updates silently lost.
Every commit runs four steps in a fixed order, and the offset always moves last The order is the whole design: by making the offset move last, Streams chooses duplicate work over data loss.

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:

PairIf they disagree after a crashNeeds a transaction?
RocksDB ↔ changelogThrow RocksDB away and rebuild it from the changelogNo. RocksDB is a disposable copy
Changelog ↔ input offsetWrong counts: events lost or counted twiceYes, 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:

Two pairs of writes, and what happens if each pair diverges. The pair RocksDB write and changelog write may diverge, and the fix is simply to discard RocksDB and rebuild it from the changelog: harmless. The pair changelog write and input offset commit may diverge, and that produces wrong answers; this is the pair the commit cycle manages, by ordering under at-least-once and by a Kafka transaction under exactly-once.
The pair that must not drift apart is the changelog and the offset — not RocksDB RocksDB is a disposable cache of the changelog, so disagreement there costs a rebuild. Both halves of the dangerous pair are writes to Kafka, which is why one Kafka transaction can cover them.

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:

WindowWhat happens
Changelog send fails, RocksDB already writtenThe 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 writesNo 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 failsThe error comes before the changelog write, so nothing reaches the changelog. It means disk trouble; the task dies.

↑ Back to Does Every Event Write to the Changelog?