Ordered IDs
A database identity column gives each new row the next number, but only in one database. A retried insert can also take a second number for the same record. Use Kahuna sequences when several services need to allocate unique, ordered values from one shared counter. An idempotency key makes a retried request return the value it already received.
These uses are good fits.
- invoice numbers
- ticket numbers
- customer-visible order numbers
- reservation ranges for batch imports
Create a Sequence
- .NET
- TypeScript
KahunaSequence sequence = await client.CreateSequence(
name: "invoices",
initialValue: 1000,
increment: 1
);
await client.createSequence("invoices", {
initialValue: 1000,
increment: 1
});
With initialValue: 1000 and increment: 1, the first invoice number is 1001.
Allocate One Value
- .NET
- TypeScript
long invoiceNumber = await client.NextSequenceValue(
"invoices",
idempotencyKey: "invoice-request-9f8c"
);
const invoiceNumber = await client.nextSequenceValue("invoices", {
idempotencyKey: "invoice-request-9f8c"
});
Use an idempotency key when the caller may retry after a timeout. The same request can be retried, and it doesn't consume another value.
Reserve a Range
For batch work, reserve several values in one call.
- .NET
- TypeScript
KahunaSequenceRange range = await client.ReserveSequenceRange(
"invoices",
count: 100,
idempotencyKey: "invoice-import-2026-06-15"
);
for (long value = range.Start; value <= range.End; value++)
Console.WriteLine(value);
const range = await client.reserveSequenceRange("invoices", 100, {
idempotencyKey: "invoice-import-2026-06-15"
});
for (let value = range.start; value <= range.end; value += 1) {
console.log(value);
}
Range reservation reduces coordination overhead when a worker needs many IDs.
Gaps in the Numbering
Values are unique and increasing, but they can have gaps. By default, a node reserves a block of values 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. A value that a caller allocates and doesn't use is also a gap.
If invoice numbers or similar must not skip values, use these practices to reduce the gaps.
- Create the sequence with a
blockSizeof1. No reserved block is then abandoned, but each value costs one replicated commit. - Send an idempotency key with each allocation. A retry then doesn't take a second number.
- Store each number with the record that used it. You can then account for a skipped number, for example after a caller crashed between allocating and saving.
See Gaps for the full list of causes.
Operational Notes
- Use one sequence per independent number line.
- Use idempotency keys for all externally retried allocation requests.
- Reserve ranges for batch imports or high-throughput producers.
- Do not assume unused values from a reserved range will be recycled.