Conditional appends coordinate writers against a durable stream. They let a writer append only when the stream is at an expected position or when the writer holds the expected fencing token. This is useful for single-writer systems that need safe failover. A replacement writer can inspect the stream, determine the next expected position, and use a conditional append to avoid writing against stale state.Documentation Index
Fetch the complete documentation index at: https://s2.dev/docs/llms.txt
Use this file to discover all available pages before exploring further.
Mechanisms
Theappend API supports two concurrency-control mechanisms:
Match sequence number
Specifying expected current state allows optimistic concurrency control. You can provide the sequence number that you expect S2 to assign to the first record in a batch as thematch_seq_num.
If it does not match, this will result in a 412 Precondition Failed status or corresponding SDK error type.
Fencing tokens
Fencing is a form of pessimistic concurrency control. It is a cooperative mechanism, so an append that does not specify a fencing token will still be allowed. When an append does include afencing_token and it does not match, this results in a 412 Precondition Failed status or corresponding SDK error type.
Setting the fencing token requires appending a
fence command record.Fencing example
Set a fencing token via the CLI:Failure handling
Append condition failures return structured412 error bodies that identify the current fencing token or assignable sequence number.
In an append session, such a failure poisons the session. Establish a new session before continuing to write.
Ownership patterns
Conditional appends enforce write-side preconditions at S2. On the read side, any reader can ensure its knowledge of the stream is current by making acheck-tail call.
This round-trip can be avoided by a single logical owner of a stream using a lease. The lease itself may be modelled on top of the S2 stream by periodically appending application-level heartbeats identifying the current physical owner.
See also
Appends
Understand append acknowledgements, batching, and durability.
Command records
See how S2 applies fence and trim directives from records.
Append condition failures
Inspect structured
412 responses for failed append preconditions.
