DEFINE SEQUENCE statement
A sequence is used to generate reliable, monotonically increasing numeric sequences in both single-node and clustered SurrealDB deployments (multiple compute nodes backed by TiKV). It uses a batch-allocation strategy to minimise coordination while guaranteeing global uniqueness.
The key features of a sequence are as follows:
Batch allocation: Nodes request ranges of sequence values at once, reducing network chatter and coordination overhead.
Node ownership tagging: Every batch is tagged with the requesting node's UUID to prevent overlap between nodes.
Durable Persistence: Sequence metadata is stored in the underlying key-value store to survive restarts and network partitions.
Concurrent, thread-safe access: A DashMap caches active sequences, allowing lock-free reads on the hot path.
Exponential back-off with full jitter: When a batch-allocation attempt fails, the node retries with an exponential delay that includes full jitter to avoid thundering-herd effects across the cluster.
Automatic cleanup: Listens for namespace and database-removal events and purges the corresponding sequence state.
The sequence implementation avoids contention by having each node reserve a range of sequence values, allowing it to serve multiple requests locally without requiring distributed coordination for every request. When a node exhausts its allocated range, it acquires a new batch from the distributed store.
Statement syntax
Examples
A sequence can be created with nothing more than a name.
The BATCH, START, and TIMEOUT clauses can be included to configure the sequence.
Sequences are never rolled back, even in a failed transaction. This differs from an approach like a single record with a manually incrementing value.