When building an in-memory TSDB buffer, use Gorilla Delta-of-Delta + XOR per 2-hour chunk for O(1) streaming writes and ~11x lossless compression.
TL;DR30-second simplified summary▾
Delta-of-Delta collapses regular scrapes to 1 bit, while XOR window reuse keeps slowly changing float64 values compact inside 2-hour chunks.
float64 packs 1 sign bit, 11 exponent bits, and 52 mantissa bits—and nearby values share almost identical upper bits.P · Problem & Why Uncompressed Telemetry Blows Up RAM.
Every raw metric sample is 8 bytes for the Unix timestamp + 8 bytes for the float64 value = 16 bytes.
At 10M samples/sec, 2 hours (7,200 seconds) of hot operational metrics equals 72 billion points = 1.152 TB of raw DRAM before any index overhead. Compressing with gzip/Zstd across 10M separate series buffers either forces tiny 100-byte blocks (poor ratio) or delays flushing until buffers fill.
Raw 16-Byte Samples vs. Gorilla 1.37-Byte Bitstream
- 16 bytes per data point
- 1.15 TB DRAM for 2h @ 10M/s
- No bit-level redundancy removal
- ~1.37 bytes/point (~11.6x)
- ~98 GB DRAM for same 2h window
- O(1) streaming append & read
R · Representation: Delta-of-Delta Timestamp Encoding.
Suppose a Prometheus scraper polls a node every 60 seconds: t0=0, t1=60, t2=120, t3=181.
The first differences are 60, 60, 61. The second differences (Delta-of-Delta D) are D2 = 60 - 60 = 0 and D3 = 61 - 60 = +1. Gorilla uses a variable-length prefix code: '0' (1 bit) for D == 0; '10' + 7-bit signed D for [-63, 64]; '110' + 9-bit D for [-255, 256]; '1110' + 12-bit D for [-2047, 2048]; and '1111' + 32-bit D otherwise.
t_n -> First Delta -> Delta-of-Delta -> Variable Bit Bucket
I · Invariant: IEEE-754 XOR Floating-Point Compression.
Subtracting two float64 numbers (42.3 - 42.1) creates floating-point rounding noise across all 52 mantissa bits. Bitwise XOR (v_{n-1} ^ v_n) preserves exact lossless bit equality.
Gorilla computes xor = prev_bits ^ curr_bits. Case 1: xor == 0 -> emit '0' (1 bit). Case 2: xor != 0 -> emit '1'. If the non-zero meaningful bits fall inside the previous XOR's [leading_zeros, trailing_zeros] window, emit '0' + just those meaningful bits! Otherwise emit '1' + 5 bits (leading zeros, capped at 31) + 6 bits (meaningful bit count - 1) + the meaningful bits.
64-Bit Float XOR -> Zero Check -> Window Reuse vs. New Window
M · Mechanics at Scale: 2-Hour Blocks & Random-Access Limits.
Gorilla encoding is stateful and sequential: to decode point #500, you must decode points #1 through #499 from the start of the chunk.
Resetting chunks every 2 hours (e.g., 120 points at 60s scrape, or 720 points at 10s scrape) bounds decode latency to microseconds and caps blast radius if a chunk is evicted or corrupted. It also aligns cleanly with Prometheus/VictoriaMetrics block compaction.
Open Chunk (Append Bits) -> 2h Seal -> Flush to Block Store
- Problem
- How to store billions of streaming metric points in RAM with ~10x compression and sub-millisecond reads.
- Naive Solution
- Store raw 16-byte structs or run Zstd on every incoming point.
- Why It Fails
- Raw structs blow up DRAM by 11x; Zstd requires batching or burns CPU re-compressing tiny per-series buffers.
- Alternatives
- Raw arrays vs. Zstd batches vs. Gorilla Delta-of-Delta + XOR 2-hour chunks.
- Engineering Decision
- Use Gorilla streaming bit-compression per 2-hour chunk in RAM, then optionally apply Zstd/Parquet when flushing cold blocks to S3.
- Trade-Offs
- Sequential decode within a 2-hour chunk (mitigated by capping chunk duration at 2h) in exchange for O(1) append and ~1.37 B/point.
E · Edge Cases, Assumptions & When NOT to Use Gorilla.
Gorilla assumes timestamps per series arrive mostly in order at regular intervals and values change smoothly.
When NOT to use Gorilla: (1) High-entropy random floats or cryptographic hashes—XOR windows stay near 64 bits wide, making Gorilla larger than 16 bytes due to control bits! (2) Heavy out-of-order writes—an append-only bitstream cannot insert t_n < t_{n-1} mid-chunk without rewriting or using an out-of-order buffer.
Smooth Gauges (1.37 B/pt) vs. Out-of-Order / Random Entropy
A team uses Gorilla XOR compression on a time series of uniformly random 64-bit UUID hashes cast to floats. Compression ratio drops to 0.96x (worse than uncompressed!). Why?
Practice: Make the trade-off yourself.
In Gorilla timestamp compression, how many bits are needed to encode a timestamp whose Delta-of-Delta `D` equals `0`?