Idempotent Retries
An idempotency key lets you retry a sequence allocation safely when you don't know whether the first attempt succeeded. It works like an idempotency key on a payment API, which prevents a retry from charging twice. next and reserve both accept an optional idempotency key.
Lost Responses and Retries
A client can time out after Kahuna commits an allocation but before the response reaches the caller. If you don't use an idempotency key, the retry allocates a new value. One business operation then gets two IDs, for example two order numbers for one order. The first value isn't used, and this also leaves a gap.
With an idempotency key, retrying the same allocation request returns the original committed allocation.
Example Flow
The client sends next-sequence orders with the idempotency key request-123. It loses the response and retries with the same key.
Choosing a Key
Derive the key from the business operation, for example order-{orderId}. Don't derive it from the request. All retries of that operation then send the same key, even when a different process or a redelivered message makes the retry. A key generated fresh for each attempt does not protect anything.
- .NET
- TypeScript
long orderNumber = await client.NextSequenceValue(
"orders",
idempotencyKey: $"order-{orderId}"
);
const orderNumber = await client.nextSequenceValue("orders", {
idempotencyKey: `order-${orderId}`
});
Calling this twice for the same orderId returns the same number both times, within the retention limits below. A key describes one request. If you retry it with a different count in reserve, the call returns InvalidInput. It doesn't return a different allocation.
How Long a Retry Is Recognized
Kahuna stores idempotency records in the replicated record of the sequence. This means that they survive restarts and leader changes. They aren't stored forever. Two limits apply to each sequence.
| Limit | Default | Server option |
|---|---|---|
| Records kept per sequence | 256; the oldest is dropped first | --sequencer-idempotency-retention-max (0 removes the cap) |
| Age of a record | 10 minutes | --sequencer-idempotency-retention-ttl, in seconds (0 disables age pruning) |
A retry within both limits returns the original allocation. After its record is dropped, a retry with the same key allocates new values. The operation then gets a second number, and the first number becomes a gap. On a sequence that receives more than 256 keyed requests within 10 minutes, the count limit is reached first and the effective window is shorter.
If retries can arrive later than that, for example when a queue redelivers a message hours later, store the allocated value with the business record and check it before allocating again. See Server Configuration for the options.
When to Use It
Use idempotency keys in these cases.
- HTTP clients with automatic retry policies.
- Job workers that may restart while processing.
- Message consumers that may receive duplicate messages.
- Any allocation request where duplicate consumption would be expensive or unsafe.
Gaps
Kahuna guarantees uniqueness and monotonicity for committed allocations. It doesn't guarantee that there are no gaps.
Gaps can occur in these cases.
- A request commits, and the caller retries but doesn't send the same idempotency key.
- A retry arrives after its idempotency record was dropped (see How Long a Retry Is Recognized).
- A caller reserves a range and doesn't consume all values.
- A sequence owner abandons the unused part of an in-memory block, for example after a restart or an ownership change.
- A sequence is deleted and recreated with new parameters.
- A sequence is updated, which starts a new incarnation and clears old idempotency records.
If a domain requires strict auditability, store the allocated value together with the business operation that consumed it.
Updates and Incarnations
Idempotency records belong to the sequence incarnation that created them. When update changes a sequence, Kahuna increments Incarnation and clears the idempotency map. Retrying an old idempotency key after the update allocates from the new incarnation. It doesn't replay an allocation from the old one.