Keys and ranges

Keys and ranges

On a scalable stream, each record's routing key picks the range that stores it, and keeps that key's records in order.

On a scalable stream, each record's routing key decides which range stores it. Records with the same key always go to the same range, in the order you append them. This append sent records for two keys in one request. Nova cut it into two parts, one per range, and says where each landed:

POST /v1/streams/demo/clicks/records, with keys user-7 and user-8
{
  "start": {
    "seq_num": 0,
    "timestamp_ms": 0
  },
  "end": {
    "seq_num": 0,
    "timestamp_ms": 0
  },
  "tail": {
    "seq_num": 0,
    "timestamp_ms": 0
  },
  "parts": [
    {
      "range": "01a2047f-166b-758d-99f8-1d26db61d80b",
      "records": [
        0
      ],
      "start": {
        "seq_num": 2,
        "timestamp_ms": 1795371774748
      },
      "end": {
        "seq_num": 3,
        "timestamp_ms": 1795371774748
      },
      "tail": {
        "seq_num": 3,
        "timestamp_ms": 1795371774748
      }
    },
    {
      "range": "01a2047f-166b-758e-a781-f56a6c95c953",
      "records": [
        1
      ],
      "start": {
        "seq_num": 3,
        "timestamp_ms": 1795371774748
      },
      "end": {
        "seq_num": 4,
        "timestamp_ms": 1795371774748
      },
      "tail": {
        "seq_num": 4,
        "timestamp_ms": 1795371774748
      }
    }
  ]
}

Each part lists the indexes of the request's records it holds, and their positions in its range. When an append has more than one part, its own start, end, and tail are zero: read the positions from the parts.

Keys choose ranges

A routing key is any string, such as a user ID or a device ID. Nova hashes it to a number between 0 and 2³² − 1. Each open range owns one span of those numbers, and a record goes to the range whose span holds its key's hash. A record carries its key in the nova-routing-key header, which the SDKs set for you, and a read returns it there.

Records with the same key are in one range, in append order. Nova promises no order between records with different keys, even while they share a range. An append with no key at all goes whole to one range picked at random. Use a key whenever order matters.

Each range numbers its own records 0, 1, 2, … with no gaps. There is no sequence across the stream, so a position always comes with its range: every record a read returns names its range.

Many keys in one request

An SDK append takes one routing key for all its records, so it is one batch in one range: stored whole or not at all. A producer keeps the appends for many keys in flight at the same time, and each gets its own result.

The API also accepts one request with records for several keys. Nova cuts it into one part per range, and stores each part as a batch of its own. Records without a key go with the first part. The whole request must fit in one batch's limits, 1,000 records and 1 MiB by default. Because the parts are separate batches, some can be stored while another fails. The error then lists the parts that landed and the ones to send again.

Splits and merges

By default, Nova splits a range that keeps taking more than 100 MiB/s for a minute, or 130 MiB/s on Express. The range closes, and two new ranges, its children, divide its span. Each key then goes to the child whose half holds its hash. When a range and a neighbouring range have both had no writer for about five minutes, Nova merges them into one new range.

You do not reconnect for either. The SDK learns the new ranges and sends each append to the right one. A subscription delivers all of a closed range's records before it starts on the ranges that replaced it, so each key keeps its order across a split. An operator can also split or merge ranges by hand; see the range commands.

A stream holds at most 256 ranges, including closed ranges that still hold records. A split that would pass that limit does not happen, and the busy range keeps its span.

How it works: Scalable streams inside.

See also

On this page