Free — no signup required

The Specialist: Gorilla (Time-Series Compression)

2 min read

The Problem with Metrics

Imagine a sensor recording temperature every second:

10:00:01 -> 20.5°C
10:00:02 -> 20.6°C
10:00:03 -> 20.6°C
10:00:04 -> 20.5°C

Storing full timestamps (64-bit integers) and full floating-point numbers (64-bit doubles) for billions of data points is incredibly wasteful because the values change very little from second to second. A generic compression algorithm like Zstd will help somewhat, but it doesn't understand the mathematical structure of the data. A specialist algorithm that understands the data's domain can do far better.

The Gorilla Approach

Facebook developed the Gorilla algorithm (described in their 2015 VLDB paper) specifically for time-series data. It uses two domain-specific encoding tricks:

1. Delta-of-Deltas for Timestamps

Instead of storing the full 64-bit timestamp, Gorilla stores the difference from the previous timestamp (the "delta"). If data arrives every 60 seconds, every delta is 60. Then it stores the difference of the differences (the "delta-of-delta"). If the interval is perfectly regular, every delta-of-delta is 0. Storing a stream of zeros compresses to almost nothing — a few bits per timestamp instead of 64 bits.

Raw timestamps:    1000, 1060, 1120, 1180
Deltas:            60,   60,   60
Delta-of-deltas:   0,    0

2. XOR Encoding for Floating-Point Values

Gorilla compares the binary (IEEE 754) representation of the current value with the previous value using an XOR operation. If 20.6 is very similar to 20.5, most of the bits in their binary representations are identical. XOR of identical bits produces 0. The result is a number with a long run of leading zeros, which can be encoded in very few bits using variable-length encoding.

20.5 in binary: 0100000000110100100000000000000000000000000000000000000000000000
20.6 in binary: 0100000000110100110011001100110011001100110011001100110011001101
XOR result:     0000000000000000010111001100110011001100110011001100110011001101
                ^^^^^^^^^^^^^^^^ (leading zeros — encode only the meaningful bits)

In practice, Gorilla achieves compression ratios of 12:1 or better on typical metrics data, compared to 2–3:1 for generic algorithms on the same data.

Where You'll See It

Gorilla's encoding is used in Facebook's Gorilla TSDB, InfluxDB, Prometheus (TSDB), and TimescaleDB. Understanding it explains why time-series databases can store billions of data points efficiently — they're not using generic compression; they're exploiting the mathematical structure of the data itself.

This is one of 18 chapters

Every chapter is free — Kubernetes, Terraform, SRE, distributed systems, and more — with fast daily review built in.

Sign up free