Skip to main content

Distributed Sequencer

A database identity column hands out unique, increasing numbers, but only in one database. It cannot number orders created by several services that write to different stores, and a client that retries an insert after a timeout can end up with a second row and a second number for the same order.

Kahuna's distributed sequencer provides named sequences of increasing integers that any service can call. An idempotency key makes a retried request return the original value. The request doesn't get a new value. Sequences are replicated with Raft, like persistent locks and keys. Use them when several processes need one authoritative source of numbers, such as IDs or version numbers. Sequences can also reserve contiguous batches of numbers.

Distributed sequencer

What It Provides​

  • Per-sequence monotonicity: values increase for a single sequence name.
  • Uniqueness: committed allocations for the same sequence aren't duplicated in one incarnation. An update starts a new incarnation and can reissue numbers if it lowers the current value; see Sequence model and allocation.
  • Gaps but no duplicates: values are unique and increasing, but they can have gaps. A node reserves a block of values (1000 by default) and serves it from memory. If the node restarts or evicts the sequence, the unused rest of the block is skipped. This also happens when the node loses ownership of the sequence. If your numbers must not skip, as with invoice numbers, see Gaps.
  • Range reservations: callers can reserve contiguous batches such as 101..200.
  • Idempotent retries: allocation requests can include an idempotency key so retried requests return the original allocation.
  • Safe updates: callers can change the current value and the increment. They can also change the max value or the per-sequence block size (how many values a node reserves in one durable commit and then serves from memory). Before Kahuna returns success for the update, it waits until no node can hand out values from a block reserved under the old settings.
  • Persistent durability: sequence state is replicated through Raft and survives leader changes.

Kahuna does not provide a single global order across all sequence names. Ordering is scoped to each sequence.

Common Use Cases​

  • Invoice and order numbers, or customer IDs.
  • Build numbers and artifact versions.
  • Monotonic event offsets for a tenant or stream.
  • Version tokens that an external system uses to reject writes from a stale writer. These are values you allocate yourself; they are separate from the fencing token that Kahuna returns with each lock.
  • Batch allocation for workers that need a local block of unique IDs.

Best Practices​

  • Use one sequence for each ordering domain, such as orders or tenant-a/invoices.
  • Use idempotency keys for retried allocation requests.
  • Reserve ranges for high-throughput workers that can consume a local block of IDs.
  • Use a per-sequence blockSize of 1 only for numbering domains that need one durable commit per value.
  • Set MaxValue only if the domain has a real upper bound.
  • Don't depend on gapless behavior. It requires a blockSize of 1 and the consumption of all allocated values. All retried requests must also use the same idempotency key.

Next Steps​