Coordinate writers

Coordinate writers

Keep concurrent writers from undoing each other's work, with a match sequence number or a fencing token.

Two tools keep writers from stepping on each other. A match sequence number makes an append conditional on where the stream ends; use it when a writer must see the latest records before it adds its own. A fencing token names the one writer allowed to write; use it to hand a stream from one writer to another safely. You can use both at once. Writer coordination explains the ideas. The API reference has append and fence, and the CLI has nova fence.

Append only at the expected position

AppendMatch / appendMatch appends only if the stream's next sequence number is the one you give. Here the writer expects the stream to end at 2; the same call again finds it at 3:

// next is where this writer expects the stream to end.
res, err := p.AppendMatch(ctx, next, client.Bodies([]byte("third"))...)
if seq, ok := errors.AsType[*client.SequenceError](err); ok {
	// Another writer appended first: read what it wrote before you decide.
	fmt.Println("the stream moved on; its tail is", seq.Tail.SeqNum)
	return nil
}

if err != nil {
	return err
}

fmt.Println("appended at", res.Start.SeqNum)
Output, run twice with next = 2
appended at 2
the stream moved on; its tail is 3

CheckTail returns the tail as a uint64, so pass it as client.SeqNum(tail.SeqNum).

A miss appends nothing and fails with SequenceError (HTTP 412), which holds the stream's tail. Read the records from your expected position up to that tail, then decide. The producer stays open and goes on with the next append. A producer sends a conditional batch apart from other records, so the condition applies to its first record.

Sending the same conditional append again is safe: if the first copy was stored, the second one misses. This is how you store a batch at most once. A miss with Resent / resent set came after a reconnect, so an earlier copy may be stored; read to find out. Scalable streams refuse a match, because each range has its own sequence.

Take over with a fencing token

Fence / fence installs a token, or replaces the one that stands. A writer then presents it when it opens a producer or makes a one-shot append. To take a stream over, fence a new token, then open a producer with it:

// Install a new token: writers that present another one are cut off.
boundary, err := c.Fence(ctx, stream, "writer-2")
if err != nil {
	return err
}

fmt.Println("records before", boundary.SeqNum, "were written under the old token")

p, err := c.OpenStream(ctx, stream, client.WithFencingToken("writer-2"))
if err != nil {
	return err
}
defer p.Close(ctx)

res, err := p.Append(ctx, client.Bodies([]byte("from writer 2"))...)
if err != nil {
	return err
}

fmt.Println("appended at", res.Start.SeqNum)
Output
records before 2 were written under the old token
appended at 2

c.Presenting(token).Append presents a token on one-shot appends.

The fence returns a boundary: the records before it were written under the old token. By the time it returns, writers that present another token can append nothing more. A token holds at most 36 bytes of UTF-8.

The stream's producer policy decides who must present a token. On a fenced stream, every writer must present the installed token while one is installed. On an any stream, only writers that present a token are checked, and writers without one are admitted.

When a writer is fenced out

A writer whose token is no longer the installed one is refused with FenceError (HTTP 412), which names the installed token:

_, err := c.Presenting("writer-1").Append(ctx, stream, client.Bodies([]byte("late"))...)
if fe, ok := errors.AsType[*client.FenceError](err); ok {
	// Another writer took over: stop writing.
	fmt.Println("fenced out; the installed token is", fe.Installed)
	return nil
}

if err != nil {
	return err
}
Output
fenced out; the installed token is writer-2

*FenceError also matches client.ErrFencedOut with errors.Is.

A producer that was open under the old token ends, and its next append fails the same way. Trying again with the same token cannot succeed: stop writing, or fence a new token if you mean to take over.

Read or clear the token

FencingToken / fencingToken reads the installed token, empty when there is none, without risking a refusal. An empty token clears it:

installed, err := c.FencingToken(ctx, stream)
if err != nil {
	return err
}

fmt.Printf("the installed token is %q\n", installed)

// An empty token clears it.
if _, err := c.Fence(ctx, stream, ""); err != nil {
	return err
}
Output
the installed token is "writer-2"

Clearing the token ends the producers that present the old one. On an any stream, writers without a token go on appending. On a scalable stream, one token covers every range.

See also

On this page