A chunk: 120 points, packed
A CHUNK (the new term) is a fixed-size bundle of consecutive points from one series — bit-packed into one blob with one header — and Prometheus closes it at 120 samples or 2 hours, whichever comes first, because that is where Gorilla's 1.37 B/point compression averages out (vs 16 B/point naive: a ~12× win).
We have two encodings, each almost magical alone. A chunk is where they live together.
Scene 05
A chunk: 120 points, packed
- Watch
- Try it
- Predict
- Capture
New term this scene: CHUNK — a fixed-size bundle of compressed points from one series. Watch 120 samples accumulate, the bitstring pack in lockstep, and the chunk close. Then the bars draw and the 12× ratio appears.
Highlighted lines are the ones running in the diagram right now.
class Chunk:# --- header (16 B, written once) ---series_id: uint64 # which seriesfirst_ts: int64 # anchor for delta-of-deltafirst_value: float64 # anchor for XOR# --- body (bit-packed, grows append-only) ---bits: BitBuffer # (ts_bits, val_bits) pairssamples: int = 0 # cap at SAMPLES_CAP (120)span_min: int = 0 # cap at TIME_CAP_MIN (120)closed: bool = False # immutable once true
def append(chunk, ts, value):dod = (ts - chunk.last_ts) - chunk.last_deltachunk.bits.write(encode_dod(dod)) # ~1 bit on cadencexor = float_bits(value) ^ chunk.last_value_bitschunk.bits.write(encode_xor(xor)) # 1 bit if unchangedchunk.samples += 1chunk.span_min = (ts - chunk.first_ts) // 60if chunk.samples >= SAMPLES_CAP \or chunk.span_min >= TIME_CAP_MIN:close_chunk(chunk)
def close_chunk(chunk):chunk.bits.flush_byte_aligned() # body ends on byte boundarychunk.closed = True # no more appends, ever# one chunk = the unit of compression / mmap / I/Ofile = chunks_dir / f"{chunk.series_id}-{chunk.first_ts}"file.write(chunk.header || chunk.bits)mmap_readonly(file) # query path reads via mmap# head opens a fresh chunk for this series and keeps scrapingreturn Chunk(series_id=chunk.series_id)
Where this sits in Build a Prometheus-style time-series database
Scene 05 of 12. Bundle ~120 consecutive points into a single bit-packed blob. Gorilla: 16 B/point → 1.37 B/point — about 12× compression.
Up next. A chunk is great when it's full. But points arrive one at a time — where does a not-yet-full chunk live, and what happens if the process crashes mid-chunk?
All 12 scenes in Build a Prometheus-style time-series database · Every curriculum