Skip to main content

Sequences Internals

A sequence works like a database identity column that all nodes share. Each allocation returns values that no other caller receives, even across retries and leader changes. This page shows how Kahuna builds sequences on the key/value subsystem. It also explains why unused values become gaps. They don't become duplicates.

Reserved Keyspace​

Sequence state is stored under a reserved internal prefix.

__kahuna:sequences:{sequence-name}

This means that sequences reuse the routing and the persistence behavior of key/value entries, including Raft replication and compare-and-set.

State Format​

SequencerManager serializes sequence state in Kahuna's binary sequence format. Older JSON and earlier binary records are also readable, and they migrate forward on the next write. The state has these fields.

  • Sequence name
  • Current value
  • Initial value
  • Increment
  • Optional max value
  • Optional per-sequence block size
  • Incarnation
  • Created and updated timestamps
  • Idempotency records

Public clients see this state as KahunaSequence or ReadOnlySequenceEntry.

Allocation Flow​

next and reserve follow these steps.

  1. Validate the sequence name and durability.
  2. Load the sequence state from the reserved key.
  3. Check the idempotency map if an idempotency key is provided.
  4. Serve the allocation from the actor's resident block when one is available.
  5. Reserve a new block with a compare-and-set (CAS) on the durable high-water mark when the resident block is empty.
  6. Validate the allocation against MaxValue.
  7. Retry if a concurrent update changed the source revision.

The compare-revision write is what prevents overlapping allocations when multiple clients target the same sequence.

CurrentValue is the durable high-water mark reserved by the owner. With the default block size of 1000, a sequence created at 0 can report CurrentValue = 1000 after the first allocation because the actor reserved a full block and then handed out one value.

Block Lease and Updates​

SequencerBlockLease limits how long an actor can serve a reserved block before it must revalidate the durable record. This limits the stale-owner window after a leadership change.

UpdateSequence rewrites sequence parameters and increments Incarnation. It also clears idempotency records and waits one block lease before it returns success. During that window, allocations for the updated sequence return MustRetry. This means that old and new incarnations can't issue values at the same time after the update is reported complete.

A node configured with SequencerBlockLease = 0 doesn't allow sequence updates. With a zero lease, stale blocks wouldn't have to revalidate.

One Request at a Time​

Each sequence is owned by one SequenceActor on the leader of its partition. The actor processes requests one at a time. This means that it can hand out values from its resident block, and it doesn't need a lock. However, this isn't the distributed correctness mechanism. A block reservation lands only if the durable record has the same revision that the actor read. This means that a former leader can't reserve a range that overlaps the range of the new leader. This is true even if the former leader doesn't know yet that it lost the partition.

An abandoned block can have unused values, for example after a restart or an ownership change. These values become gaps. They don't become duplicates. This guarantee applies within one incarnation of the sequence. The block lease above bounds the window across incarnations.

Idempotency Storage​

When an idempotency key is provided, the resulting allocation is stored in the sequence state. A retry with the same idempotency key returns the stored allocation. It doesn't consume another range.

The stored idempotency key is scoped as a reservation entry. This means that next is treated as a range reservation with count = 1.