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)0 first
1 second
0 more records; the tail is 2A 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
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))
}1 secondFollow 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))
}
}0 first
1 second
2 thirdCancel 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)the next record gets 2The 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)
}the first record left is 1A 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.