Writer coordination
A stream's producer policy, match sequence numbers, and fencing tokens control how several writers share it.
When several writers append to one stream, their batches interleave in the order Nova receives them. That is fine for a log that many services write to. It is wrong when a write depends on what is already in the stream, or when only one writer should be active. Nova gives you three tools for those cases. A producer policy says how many writers may hold the stream at once. A match sequence number makes an append conditional on the tail. A fencing token shuts out writers that no longer own the stream.
This append expected the stream to end at sequence number 1, but other records had arrived first. Nova stored nothing and returned the real tail:
{
"error": "writer: append at 1, expected 3",
"tail": {
"seq_num": 3,
"timestamp_ms": 1795370800000
}
}Producer policy
| Policy | Writers at once | Use when |
|---|---|---|
any (the default) | Any number of producers and one-shot appends, with their batches interleaved | Many devices, request handlers, or services write to one log |
fenced | One at a time, a producer or a one-shot append | One owner writes the stream, such as an order's command handler |
After the last writer of an any stream leaves, its leader keeps serving the stream for 30 seconds, so a writer that comes back within that time starts at once. A fenced stream is released as soon as its producer closes, so the next owner does not wait. A one-shot append that arrives while another writer holds a fenced stream is refused with a retryable error, and the SDK sends it again.
Match sequence number
A match sequence number is the sequence number the batch's first record must get. If the stream's tail is there, Nova appends the batch. If not, it refuses that batch with 412 and returns the tail, as above. Two appends that expect the same number cannot both succeed. This is optimistic concurrency: you read, decide, and append only if nobody wrote in between. Scalable streams refuse match sequence numbers, because each range has its own sequence.
On a producer session, a refused match ends nothing: the batches after it are not affected. Read the records between your expected position and the returned tail, decide again, and retry with the new tail.
A match sequence number also makes a retry safe. If the first attempt was stored, the retry finds the tail has moved and is refused, so the batch is stored once. A plain append that is sent twice can be stored twice.
This is also why a match can be refused when no other writer appended. After the stream's leader changes, the SDK sends unacknowledged batches again, and a batch that was stored before the change is refused. Check the tail and read from your expected position to see whether your records are there. The failure model explains why.
Fencing tokens
A fencing token is a string of at most 36 UTF-8 bytes. The fence operation installs it on the stream, replaces it, or clears it with an empty token. Writers then present a token when they open a producer session or make a one-shot append. Nova compares the two exactly:
| Writer presents | any stream | fenced stream |
|---|---|---|
| No token | Admitted | Refused while a token is installed |
| The installed token | Admitted | Admitted |
| Any other token | Refused with 412 | Refused with 412 |
The stream's leader applies a fence at once. When the fence returns, sessions that present an old token can no longer append, and their next append fails. A refused writer gets the installed token in the error. Retrying with its old token cannot succeed. Installing the same token again changes nothing.
A fence returns the tail at the moment it took effect. Records below that tail were written before the change, so a new owner can read up to it to see everything the previous owner stored. On a scalable stream, one token covers every range and the fence returns a bookmark instead.
Taking over a stream
A common pattern gives each owner a generation number, such as gen-41 and then gen-42:
- The new owner fences the stream with its own token. It does not need to know the old one.
- It reads up to the tail the fence returned, to learn the stream's state.
- It opens a producer presenting its token, and appends.
A previous owner that is still running fails on its next append and stops. Combine this with match sequence numbers when each append also depends on the stream's contents.
Fencing tokens do not control access
A fencing token keeps cooperating writers in order. It does not stop anyone: an any stream admits writers without a token. Use authentication to control who can write. Nova separately stops a former leader node from writing late; see guarantees.