Use scalable streams

Use scalable streams

Create a scalable stream, append to it by key, read across its ranges, and split or merge them.

A scalable stream spreads its records over several ranges behind one address, and each range has its own sequence. A routing key picks the range: records with the same key land in the same range, in the order you appended them, and records with different keys have no order between them. Use one when you need more write throughput than a plain stream gives, and order only per key. Scalable streams, Keys and ranges, and Bookmarks explain the ideas. The API reference has create and append, and the CLI has nova stream create and the range commands.

Create a scalable stream

The kind is fixed when you create the stream. A scalable stream opens with one range unless you ask for more, up to 256:

st, err := c.CreateStream(ctx, client.StreamOptions{
	Bucket:        "demo",
	Key:           "orders",
	Kind:          client.Scalable,
	InitialRanges: 4, // four ranges, each with its own sequence
})
if err != nil {
	return err
}

fmt.Println(st.Key, st.Kind)
Output
orders scalable

Append by key

AppendByKey / appendByKey appends records under one routing key. Each range numbers its own records from 0:

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

for _, user := range []string{"user-1", "user-2", "user-1"} {
	record := client.AppendRecord{Body: []byte("order for " + user)}

	res, err := c.AppendByKey(ctx, stream, user, record)
	if err != nil {
		return err
	}

	// A key always lands in the same range, in order.
	fmt.Println(user, "at", res.Start.SeqNum, "in its range")
}
Output
user-1 at 0 in its range
user-2 at 0 in its range
user-1 at 1 in its range

A producer appends by key too, with p.AppendByKey, and keeps one session per range it writes to.

The result names the range the records landed in (Range / range). The SDK adds the key to each record as a nova-routing-key header, which a reader sees. A record appended without a key lands in a range that Nova picks at random.

Read across the ranges

A read covers every range, one range after another, and each record names its range. The bookmark it returns holds a place in every range. It is an opaque string: save it, and resume from it later:

page, err := c.Read(ctx, stream, client.FromBeginning(), 100)
if err != nil {
	return err
}

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

// The bookmark holds a place in every range. Save its text, and resume from it later.
saved := page.Bookmark.String()

bookmark, err := client.ParseBookmark(saved)
if err != nil {
	return err
}

next, err := c.Read(ctx, stream, client.FromBookmark(bookmark), 100)
if err != nil {
	return err
}

fmt.Println(len(next.Records), "new records")
Output
order for user-1 at 0
order for user-1 at 1
order for user-2 at 0
0 new records

A subscription covers every range too, and picks up the new ranges a split opens without starting over. Start from the beginning, the tail, a time, or a bookmark. A sequence number or a tail offset names one range's position, so a scalable stream refuses them. To replay one range from a position you logged, replace that range's place in a bookmark with Bookmark.At / bookmarkAt. Bookmark.Places / placesOf lists the places.

Split and merge ranges

A split turns one range into two, so its keys spread over more ranges. A merge does the reverse, for two or more neighbouring ranges. You ask for the change, and a background process on the cluster carries it out within about 30 seconds:

listing, err := admin.ListRanges(ctx, stream)
if err != nil {
	return err
}

fmt.Println(len(listing.Ranges), "ranges")

// Split the first range at the middle of its span.
change, err := admin.SplitRange(ctx, stream, listing.Ranges[0].ID, nil)
if err != nil {
	return err
}

fmt.Println("split accepted:", change.Accepted)
Output
4 ranges
split accepted: true

The range calls belong to the admin client, client.NewAdmin, which needs a gRPC address and an admin credential. MergeRanges takes the ranges to merge.

A split seals the old range and opens two new ones, each starting its sequence at 0. Readers finish the sealed range before they read its successors, so each key keeps its order. A stream holds at most 256 ranges, counting the sealed ones that splits and merges leave behind. A split or merge that would go past that is refused: the result says it was not accepted, and why.

Nova also splits a range on its own when it takes more than 100 MiB a second for a minute, 130 MiB on Express. An operator can change both thresholds.

What a scalable stream refuses

Positions are per range, so the calls that name one position on the whole stream are refused:

CallGo, TypeScript, HTTPInstead
An append with a match sequence numberErrMatchOnScalable, RecordTooLargeError, 400Order work per key, or fence the stream
A read or subscription from a sequence number or tail offsetErrPositionPerRange, PositionPerRangeError, 400 with reason: position_per_rangeStart from a bookmark, the beginning, the tail, or a time
A trim to a sequence numberErrPositionPerRange, PositionPerRangeError, 400 with reason: position_per_rangeTrim to a bookmark or a time

A tail check answers a bookmark, with every open range at its tail. One fencing token covers every range, including ranges a later split opens. A fence that fails part way may already have installed the token; run it again with the same token to finish it.

See also

On this page