Read and follow

Read and follow

Read pages of records, follow new records as they arrive, and check where a stream ends.

There are three ways to read a stream. Read returns one page of records and the place to continue from; use it for history and paging. Subscribe returns the records from a start onward and then waits for new ones; use it to process every record as it arrives. Check the tail tells you where the stream ends now.

None of them needs a producer. You can send them to any node. When a node answers with the address of the stream's leader instead, the SDK follows it. Reads explains the ideas. The API reference has read, subscribe, and check tail, and the CLI has nova read, nova tail, and nova check-tail.

Read a page

Read / read returns up to a limit of records from a start, and a bookmark: the place right after the last record. Read from the bookmark to get the next page:

stream := client.Address{Bucket: "demo", Key: "events"}

page, err := c.Read(ctx, stream, client.FromSeq(0), 100) // up to 100 records
if err != nil {
	return err
}

for _, r := range page.Records {
	fmt.Println(r.SeqNum, string(r.Body))
}

// The next page starts at the bookmark after this one.
next, err := c.Read(ctx, stream, client.FromBookmark(page.Bookmark), 100)
if err != nil {
	return err
}

fmt.Println(len(next.Records), "more records; the tail is", next.Tail.SeqNum)
Output
0 first
1 second
0 more records; the tail is 2

A page ends at your limit, at your byte or time bound, or at the tail. An empty page means you have read everything there is. A node returns at most 8,192 records and 8 MiB a page, whatever you ask for, but always at least one record when one is there.

Choose where to start

FromSeq(n)Sequence number
The record with this sequence number
FromBookmark(b)Bookmark
Where an earlier read or subscription stopped
FromBeginning()Beginning
The first record that still exists
FromTime(ms)Time
The first record stamped at or after this time, in milliseconds
FromTailOffset(n)Tail offset
This many records before the tail, for the latest records
FromTail()Tail
The end of the stream, so only records appended later
limitLimitDefaultThe node's page, 8,192
The most records the page holds
WithMaxBytes(n)Byte boundDefaultThe node's page, 8 MiB
The most bytes of bodies and headers the page holds
WithUntil(ms)UntilDefaultNone
Leaves out records stamped at or after this time
WithClamp()ClampDefaultfalse
Moves a start below the first record to the first record, and a start past the tail to the tail, instead of failing

Read the latest records

A tail offset starts a number of records before the end, as tail -n does:

// Start 1 record before the tail.
page, err := c.Read(ctx, stream, client.FromTailOffset(1), 10)
if err != nil {
	return err
}

for _, r := range page.Records {
	fmt.Println(r.SeqNum, string(r.Body))
}
Output
1 second

Follow new records

Subscribe / subscribe delivers batches of records in order, from a start onward, and then waits at the tail for new ones. It takes the same starts as a read. After a quiet spell, a new record can take up to 250 ms to arrive.

for batch, err := range c.Subscribe(ctx, stream, client.FromSeq(0)) {
	if err != nil {
		return err // trimmed, missing, or out of retry budget
	}

	for _, r := range batch.Records {
		fmt.Println(r.SeqNum, string(r.Body))
	}
}
Output, with a third record appended while it runs
0 first
1 second
2 third

Cancel ctx to stop following. The loop then ends without an error.

If the connection breaks, the SDK reconnects and goes on after the last record it delivered, so you see no record twice and miss none. It keeps trying for the retry budget, 30 seconds by default, which starts again with each delivery. When a token expires, it reopens the subscription with a fresh one, which does not count against the budget. The loop ends with an error when the records at its start were trimmed, the stream is missing, or the budget runs out.

Check the tail

CheckTail / checkTail returns the sequence number the next record will get. After an append is acknowledged, every tail check includes it, on any node.

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

fmt.Println("the next record gets", tail.SeqNum)
Output
the next record gets 2

The tail also carries the time of the last record, and a new stream's tail is 0. On a scalable stream, the answer is a bookmark only, because each range has its own tail.

When the records are trimmed

A read that starts below the stream's trim point fails with ErrTrimmed / TrimmedError (HTTP 410). Read again with the clamp option to start at the first record left:

page, err := c.Read(ctx, stream, client.FromSeq(0), 100)
if errors.Is(err, client.ErrTrimmed) {
	// Records before the trim point are gone: start at the first one left.
	page, err = c.Read(ctx, stream, client.FromSeq(0), 100, client.WithClamp())
}

if err != nil {
	return err
}

if len(page.Records) > 0 {
	fmt.Println("the first record left is", page.Records[0].SeqNum)
}
Output, after a trim to 1
the first record left is 1

A read of a missing stream fails with ErrStreamNotFound / StreamNotFoundError (HTTP 404), unless its bucket creates streams on read. On a scalable stream, a sequence number or a tail offset is refused, because each range has its own sequence.

See also

On this page