Background work

Background work

How Nova indexes acknowledged records, regroups them by stream, and deletes storage nobody needs.

An append is acknowledged while its records sit in a shared object with records of other streams. Background work turns that into long-term history. It records where each record is, copies each stream's records into objects of its own, and deletes what trims, retention, and deleted streams leave behind. None of it delays an append, and none of it scans a bucket.

From transit to stream objectsmetadata: how readers find recordsobject storage: where records are savednothing yetno metadata is writtenbefore the acktransit bundlea shared object withmany streams' recordsspan refswhere each stream'srecords sit in ittransit bundleunchanged; any nodecan now read itabsorbcatalog rowlists stream objects;replaces the span refsstream objectone stream's records,in standard storage,written oncedenorm copiescleanup jobs donecatalog row stays untila trim or retentionbundle deleted10 min after everystream is copied out;stream object staysackabsorbdenormclean uptransit window: denorm is due 5 min after 8 MiB more, or at the age limit
The top row shows metadata; the bottom row shows stored objects. Read from left to right.

The parts

PartWhat it does
IndexerRuns on the leader. After each acknowledgement it records in the metadata store where each stream's records are, a step called absorb.
AuditorThe background process that moves bytes and deletes objects. It runs inside every node by default.
Transit bundleA WAL slot's object: one flight, with records of many streams
Stream objectAn object that holds consecutive records of one stream, in the standard bucket
DenormThe job that copies a stream's records from transit bundles into stream objects
Due-queueA queue of pending jobs in the metadata store, ordered by the time each one is due
Read graceThe wait before an object nobody references is deleted, 10 minutes by default

From writes to stored history

  1. The leader acknowledges a flight once it is stored. The flight is one transit bundle, shared by every stream it holds.
  2. The indexer absorbs the flight. It writes one span ref per stream, pointing into the bundle, and advances its ledger's checkpoint, the first slot it has not absorbed. From now on any node can read the records. Absorb also files a denorm job for later.
  3. When the job is due, denorm reads each stream's share of the bundles and writes it as a stream object. It then replaces the stream's span refs with one catalog row that lists the new objects. Reads switch to the stream objects at that moment.
  4. Once denorm has copied every stream out of a slot, the trim-check job deletes the slot from every bucket, after the read grace.
  5. Later, retention or a trim moves the stream's watermark. Records below it are unreadable at once.
  6. When the retention limit passes a stream object's last record, the auditor deletes the object, after the read grace. A stream object that holds records on both sides stays.

Who does which work

The work is split in two by what it touches:

The serving side doesThe auditor does
Absorb: span refs and the ledger checkpoint. It moves no bytes.Every byte move: denorm, garbage collection, and other rewrites
A logical trim: moving a stream's watermarkEvery routine delete of WAL slots, stream objects, and transit bundles
Ledger recovery that an open or a read needs right awayLedger recovery for streams nobody opens, and for nodes that are gone
Deleting a stream logically: fence, release, tombstoneCleaning up deleted streams, retired nodes, and objects nobody recorded

The serving side stays cheap and predictable: a burst of background work cannot slow an append. Every delete goes through one place, with one set of crash rules. There is one exception. A slot that was written above a fenced ledger's cut, and never acknowledged, is deleted by the ledger recovery that finds it.

How the auditor finds work

Nova never lists a bucket or scans the metadata store to find work. Each step that creates work files a job for it in a due-queue, in the same metadata update, with the time it becomes due. Absorb files a denorm job, denorm files a trim-check, and retention files a check of each object it empties. The auditor reads its share of the due-queues every 30 seconds, due jobs first.

JobWhat it does
DenormCopies a stream's records from transit bundles into stream objects
Trim-checkDeletes WAL slots whose records denorm has copied
Retention, retention checkTrims records older than a stream's age limit, and checks byte limits
ReapDeletes an object that no ref names, in two stages
Garbage collectionRewrites a stream object that holds many trimmed records, keeping the live ones
Teardown, final teardownCleans up a deleted stream. The final purge waits until every log that could hold its records is indexed past the delete.
RecoveryIndexes the ledger of a node that stopped
Star reclaimDeletes what a decommissioned node left in storage
OrphanDeletes objects that were written but never recorded, found through write-intent records
Range intent, range split, range mergeCarry out the splits and merges of scalable streams, and clean up after an interrupted one

Before a job writes an object, it records its intent in the metadata store. A crash between the upload and the metadata update leaves an intent that the orphan job finds, an hour later by default. The auditor finds every object through a ref or an intent that names it.

The transit window

A stream's records in transit bundles form its transit window. A long read there touches many shared objects, so denorm copies them into stream objects. Two rules decide when:

  • When a stream's indexed bytes pass another 8 MiB, its job becomes due 5 minutes later. The wait groups more records into each copy.
  • Otherwise, the job becomes due at an age limit after absorb: 24 hours for Standard, and 1 hour for Express, because express storage costs more per byte.

When denorm keeps up, a busy stream's transit window holds about 8 MiB plus 5 minutes of writes, and a quiet stream's holds at most its age limit. Denorm writes stream objects of up to about 256 MiB.

A slot can be deleted only when every stream in it has been copied out. One quiet stream could therefore keep a busy ledger's slots for a whole day. A cap prevents that. When 4 GiB of a ledger's slots wait behind its oldest pending job, and that job is at least 5 minutes old, the ledger's pending denorm jobs all become due at once.

A lagging denorm never slows appends. It costs storage, because slots stay longer, and it raises alerts on the age of the denorm backlog. A node that runs only the auditor downloads each transit bundle once to its local disk and copies every stream's share from there, instead of one ranged read per stream.

Removing finished slots

The auditor deletes slots by position, without reading them. Each ledger keeps two positions: up to where its slots are deleted, and up to where every pending denorm job is finished. Trim-check waits the read grace, then deletes every slot between the two, from every bucket of the class. The grace protects readers that still use a footer they cached. The auditor deletes the slots of open and stopped ledgers alike.

Reads stop first, deletes come later

A trim, a retention limit, or a deleted stream makes records unreadable at once. The bytes go later:

Retention or trim Watermark moves Reads below it fail at once Auditor finds expired objects Read grace, 10 min Objects deleted
A record is gone for readers at once, and from storage after the read grace.

Storage is billed until the delete, usually minutes after a retention limit passes, and longer when the auditor is behind. At most one stream object per stream holds records on both sides of the watermark, and it stays until the watermark passes its last record. For a deleted stream, the final purge also waits until every node's log is indexed past the moment of the delete, so no late record of the stream is left behind.

An explicit trim past the retention limit

The auditor reclaims storage below the point that a stream's retention limits set. When an explicit trim goes further, the objects in between stay until an age limit reaches them, or until the stream is deleted. On a stream with no retention limit, a trim frees no storage.

What bounds the cost

The auditor works within budgets, so cleanup cannot crowd out the serving path. By default each auditor runs 2 jobs at once, makes at most 50 storage requests a second, and moves at most 64 MiB/s through its node. Deletes share a budget of 1,000 keys a second per bucket across the whole fleet. Several auditors share the work through a ring in the metadata store, and each job takes a lease while it runs.

When an auditor crashes, its leases expire and another auditor runs the job again. Every step is safe to repeat: a re-run after a crash at any point reaches the same result. When auditors fall behind, storage grows and the backlog's age rises, but appends and reads continue. See Auditor delays.

Go deeper

Back to the concepts: Retention and Trimming.

On this page