A distributed monotonically increasing sequence generator
- Monotonically increasing sequence numbers without large gaps or any duplicates
- Durable and highly available
- Low-latency in a geographically distributed environment
Each node acquires a lease on a key before incrementing the sequence number. The lease is used to ensure that only one node has the exclusive access to the key at any given time, allowing the nodes to generate sequence values without coordination. When clients connect to their nearby node, this allows sequence generation without the latency of cross-region coordination.
The lease is automatically renewed by the node that holds it. If the node goes down, the lease will expire and another node will take over the sequence.
zseqd uses etcd as the backend. Start the servers with the following commands:
etcd
cargo run -- --bind 127.0.0.1:12345 # Launch as many instances as you wantzseqd uses Redis protocol to communicate with clients and implements a subset of the Redis commands and some custom commands.
You can use redis-cli to interact with the server. For redirection to different nodes, cluster mode (-c) must be enabled:
redis-cli -c -p 12345
127.0.0.1:12345> INCR myseq
(integer) 1
127.0.0.1:12345> INCR myseq
(integer) 2
127.0.0.1:12345> INCRBY myseq 10
(integer) 12When another node is holding the lease, the client will be redirected to the lease holder node:
redis-cli -c -p 54321
127.0.0.1:54321> INCR myseq
-> Redirected to slot [0] located at 127.0.0.1:12345
(integer) 13You can explicitly revoke a lease on a key and allow another node to take over with LEASE.REVOKE:
127.0.0.1:12345> LEASE.REVOKE myseq
1