Tutorial: Distributed Sequencer
A sequence works like a database identity column that several processes can share. It is one source of increasing numbers, such as invoice or order numbers. The numbers don't have duplicates, even when the processes are on different machines or retry after a timeout. For the concepts, see Distributed Sequencer.
In this tutorial, you create a sequence and allocate values. You also reserve ranges and use idempotency keys for safe retries.
Create a Sequence
Start kahuna-cli and create an orders sequence.
kahuna-cli> create-sequence orders 0 1
r0 created orders current 0 increment 1 max -
The command has three arguments.
orders: sequence name.0: initial value.1: increment.
With these defaults, the first allocated value is 1.
By default, Kahuna reserves values in blocks of 1000 and hands them out from memory. To commit each value individually, create the sequence with a per-sequence block size of 1.
kahuna-cli --create-sequence invoices --initial-value 0 --increment 1 --block-size 1
Allocate One Value
kahuna-cli> next-sequence orders create-order-1001
r1 next orders 1
The optional second argument is an idempotency key. If the client retries the same operation after a timeout, the same key create-order-1001 returns the same allocation. It doesn't consume another value.
Reserve a Range
Reserve five IDs for a worker.
kahuna-cli> reserve-sequence orders 5 worker-a-batch-1
r2 reserved orders 2..6 count 5
The worker can now use the values 2 to 6 locally. It doesn't need a network call for each ID.
Inspect the Sequence
kahuna-cli> get-sequence orders
r1 get orders current 1000 increment 1 max - block default incarnation 0
Workers ask the sequence owner for values. The owner serves them from an in-memory block that it reserved in one Raft commit.
current 1000 means that the highest reserved value is 1000. It isn't the last value handed out. The node that owns the sequence is the leader of its partition. It reserves a block of values in one Raft commit and serves next calls from memory. This means that current can be ahead of the last value that your application consumed. For block sizes and gaps, see Sequence Model and Allocation.
Update a Sequence
Use update-sequence to change the reserved high-water mark or the per-sequence block size. You don't need to delete the sequence.
kahuna-cli --update-sequence orders --current-value 5000 --block-size 100
The next allocation returns 5001. The update starts a new incarnation (a new generation of the sequence's values). The update takes about one server SequencerBlockLease to return, five seconds by default. During that time, allocations return MustRetry. For more information, see Updating a Sequence.
Use Single-Command Mode
You can also run the same workflow in single-command mode.
kahuna-cli --create-sequence invoices --initial-value 1000 --increment 1
kahuna-cli --update-sequence invoices --current-value 5000 --block-size 100
kahuna-cli --next-sequence invoices --idempotency-key invoice-req-1
kahuna-cli --reserve-sequence invoices --count 10 --idempotency-key invoice-batch-1
kahuna-cli --get-sequence invoices
Use a Client
This example creates a new sequence, web-orders. An attempt to create orders again would fail with AlreadyExists.
- .NET
- TypeScript
using Kahuna.Client;
var client = new KahunaClient("https://localhost:8082");
await client.CreateSequence("web-orders");
long orderId = await client.NextSequenceValue(
"web-orders",
idempotencyKey: "create-order-1001"
);
KahunaSequenceRange batch = await client.ReserveSequenceRange(
"web-orders",
count: 5,
idempotencyKey: "worker-a-batch-1"
);
Console.WriteLine($"Next order: {orderId}");
Console.WriteLine($"Reserved: {batch.Start}..{batch.End}");
import { KahunaClient } from "kahuna-client";
const client = new KahunaClient({
endpoints: ["https://localhost:8082"]
});
await client.createSequence("web-orders");
const orderId = await client.nextSequenceValue("web-orders", {
idempotencyKey: "create-order-1001"
});
const batch = await client.reserveSequenceRange("web-orders", 5, {
idempotencyKey: "worker-a-batch-1"
});
console.log(`Next order: ${orderId}`);
console.log(`Reserved: ${batch.start}..${batch.end}`);
Clean Up
kahuna-cli> delete-sequence orders
deleted
kahuna-cli> delete-sequence web-orders
deleted
Deleting a sequence removes the named counter. Creating the same sequence again starts it from the new creation parameters.