Skip to main content
← PROBLEMS / ARCHITECTURE
An end-to-end domain learning journey

Design a Sub-2ms OLAP Segment
Filtering Engine with Roaring Bitmaps.

Scanning 10B-row columnar arrays on every ad-hoc cohort query saturates memory bandwidth.
Start the journey2 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

Maintain per-segment Roaring Bitmap inverted indexes and evaluate predicates in ascending cardinality order (smallest bitmap first) with container-level early skipping.

TL;DR30-second simplified summary
▾

Cardinality-ordered Roaring intersections + lazy result materialization cut query latency from seconds to < 2 ms.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
Roaring Bitmaps
16-bit high/low hybrid containers (Array, Bitset, Run).
Columnar Segment Partitioning
Splitting 10B rows into immutable 65,536-row or 1M-row granules/segments.
Full walkthrough · 2 stages
01
L4 · FUNDAMENTALS

R & H — Cost-Based Predicate Ordering & Chunk Skipping.

OLAP Roaring Inverted Index Filter Pipeline

SQL / Cohort QueryCardinality PlannerRoaring ContainerANDMatched Row IDs(<2ms)3 predicatessparsest firstAVX2 / galloping

If plan='PRO' matches 500k users (200 chunks) and active=1 matches 8B users (60,000 chunks), always intersect the smallest cardinality bitmap first. Any 16-bit high chunk missing from the smallest bitmap is skipped in O(1) without ever reading the large bitmap's containers from disk/RAM.

02
L4 · FUNDAMENTALS

D — Cardinality-Ordered Multi-Bitmap Intersection in Python.

Cardinality-Ordered Early-Exit Intersection

Engineering Decision Framework
Problem
How should an OLAP engine handle high-cardinality columns (e.g., user_id with 1B unique values) vs. low-cardinality columns (country)?
Naive Solution
Build a Roaring Bitmap inverted index on every column including user_id.
Why It Fails
Indexing 1B unique values creates 1B single-element bitmaps, bloating index metadata.
Alternatives
Roaring inverted index on low/mid cardinality + Bloom/MinMax skipping on high cardinality,Roaring index on all columns,Full columnar scan
Engineering Decision
Use Roaring Bitmap indexes for cardinality <= 1M per segment, and MinMax/Bloom filters for high-cardinality IDs.
Trade-Offs
Keeps index size < 5% of raw data while preserving sub-2ms boolean cohort filtering.
Decision Exercise · Make the Call

Why does sorting predicates by ascending cardinality speed up multi-way Roaring intersections?

Sorting predicates by len(bm.chunks) and intersecting chunk-by-chunk ensures intermediate bitmaps only shrink:

from typing import Dict, List, Set

def intersect_roaring_predicates(bitmaps: List[Dict[int, Set[int]]]) -> Dict[int, Set[int]]:
    if not bitmaps:
        return {}
    ordered = sorted(bitmaps, key=lambda b: sum(len(v) for v in b.values()))
    acc = {hi: set(lo) for hi, lo in ordered[0].items()}
    for nxt in ordered[1:]:
        next_acc = {}
        for hi, lo_set in acc.items():
            if hi in nxt:
                inter = lo_set & nxt[hi]
                if inter:
                    next_acc[hi] = inter
        acc = next_acc
        if not acc:
            break  # Early exit on empty intersection
    return acc
Recommended Learning Flow