When designing a multi-core OLTP index, pair ART + OLC with 3-epoch EBR and shrink hysteresis to prevent cache-line bouncing and allocator thrashing.
TL;DR30-second simplified summary▾
Embedding a 64-bit OLC version word in ART's 20-byte header keeps reads write-free, while 3-epoch EBR safely frees obsolete nodes after grow/shrink without per-hop memory fences.
free() a replaced node immediately.R & E · Requirements, Memory Budget & Latency Math.
With 500M keys and a 500 ns p99 lookup budget, you can afford at most 4–5 DRAM cache misses (~70–90 ns each) per lookup—leaving zero room for lock contention.
Illustrative Memory Sizing: At ~48–60 bytes per key including inner nodes + 8-byte leaf pointers, 500M keys occupy ~24–30 GB of DRAM for inner structure plus leaf payloads—easily fitting inside a single 256 GB–512 GB dual-socket server, compared to ~100+ GB for a naive 256-way trie.
Shared RWLock Contention vs. Optimistic Lock Coupling (OLC)
- Readers dirty shared cache line
- 64 cores serialize at root
- <2M ops/sec ceiling
- Zero memory writes on reads
- Linear scaling across 64 cores
- <500 ns p99 lookup
S · Storage & Cache-Aligned Node Headers.
Every ART inner node starts with a compact 16-byte header so the CPU fetches the version word, node type, and compressed prefix in the exact same 64-byte cache line.
Header layout: uint64_t version_lock_obsolete (8B: bit 0 = lock, bit 1 = obsolete, bits 2..63 = monotonic version), uint8_t type (Node4/16/48/256), uint8_t count, uint16_t prefix_len, and uint8_t prefix[8] (8B inline compressed prefix). Total header = 20 bytes.
20B OLC Header + Adaptive Child Payload
H & A · High-Level Engine & OLC Traversal Contract.
Every lookup or range scan begins by registering the worker thread's current epoch (EbrGuard) and reading the root node's version.
During traversal, before dereferencing child = parent.find_child(byte), the reader checks parent.read_unlock_or_restart(v_parent). Only after confirming parent was untouched does it load v_child and descend—this is 'lock coupling' without holding any read locks!
Epoch-Guarded Optimistic Descent & Version Validation
D · Detailed Design: Node Grow/Shrink & Epoch Reclamation.
A writer cannot call delete old_node immediately after swapping a Node4 for a Node16, because an optimistic reader might still be inspecting old_node before its validation check.
With 3-epoch EBR (e, e-1, e-2), each worker keeps a local limbo list of retired nodes tagged with the epoch e when they were retired. Periodically, a thread scans all active threads' announced epochs; once min_active_epoch >= e + 2, every node retired in epoch e is guaranteed unreachable by any reader and is freed in batch.
Active -> Obsolete + Retired -> Epoch Advance -> Safe Free
- Problem
- How to safely reclaim retired ART nodes without slowing down optimistic readers.
- Naive Solution
- Immediately
free()replaced nodes or use per-pointer Hazard Pointers on every hop. - Why It Fails
- Immediate
free()causes segfaults on concurrent OLC readers; Hazard Pointers require an expensive atomic store + memory fence (MFENCE) at every single tree level. - Alternatives
- Reference counting vs. Hazard Pointers vs. 3-Epoch Based Reclamation (EBR).
- Engineering Decision
- Use 3-Epoch Based Reclamation (EBR) with hysteresis on node shrink and a bounded-epoch stall check.
- Trade-Offs
- Amortizes reclamation overhead to once per operation batch (zero fences inside tree traversal), assuming workers do not block inside an epoch guard.
During a bulk delete workload, a worker thread enters an EBR epoch guard, then makes a slow synchronous HTTP call to an external service before exiting the guard. What happens to the 64-core OLTP index?
E & D · Evaluation, Assumptions, Limitations & Glossary.
This design assumes all index data fits in DRAM on a cache-coherent NUMA server and that threads exit EBR guards within microseconds.
Assumptions & Limitations: Memory estimates (~24–30 GB for 500M keys) assume moderate key prefix sharing and exclude WAL/MVCC tuple payloads. Under extreme skew where 64 writers continuously resize the exact same Node4/Node16, OLC read restarts can spike; falling back to a brief shared read lock after K=8 restarts guarantees bounded tail latency.
OLC Workers -> ART Adaptive Nodes -> EBR Limbo -> Slab Allocator
Practice: Make the trade-off yourself.
Why does an OLC-based ART engine use a shrink hysteresis threshold (e.g., grow Node16->48 at 17 keys, shrink Node48->16 at 12 keys)?