Saving 100TB of RAM Through Data Structures and Math
Cloudflare published details on how optimizing its consistent hashing library saved over 100 terabytes of RAM globally through struct padding adjustments and math-backed hash reductions.
Understanding Struct Padding Optimization
According to the source, Cloudflare reclaimed memory within the pingora-ketama consistent hashing library used by the Pingora Backend Router (PBR) by modifying hash-point structures.
- Reported Fact: Rust alignment rules require a struct's total size to be a multiple of its largest field—in this case, a 4-byte hash. Changing an index pointer from a 32-bit integer (
u32) to a 16-bit integer (u16) did not immediately reduce memory due to these alignment rules. - Concrete Example: To bypass this limitation without using controversial packed structures, developers stored the hash and the 16-bit index as a 6-byte raw byte array (
[u8; 6]). They then implemented custom getter methods usingfrom_ne_bytesto read the values out of the array. According to the source, this change reduced consistent hashing memory usage by 25%.
Reducing Hash Counts via Probability
The source states that Cloudflare used mathematical analysis of the coefficient of variation and the birthday paradox to evaluate its consistent hashing configuration.
- Reported Fact: The analysis showed that generating high numbers of hashes per server yielded diminishing returns in error reduction while significantly increasing the probability of hash collisions, which cause random drops in request volume contributions.
- Concrete Example: Because dozens of feature-combination rings created excessive memory use, the team determined they could decrease the number of hashes generated for each server by 90% without incurring appreciable error.
Managing Migrations Safely
To prevent cache invalidation storms and surges in origin traffic during the rollout, Cloudflare avoided a global percentage switch.
- Reported Fact: PBR carried both the old ketama ring and the new smaller ring in memory simultaneously. A migration framework routed requests based on the request hash to determine which ring to use, providing a clean rollback path.
- Concrete Example: The migration was rolled out progressively by starting with small validation locations and moving through larger groups of data centers before decommissioning the old rings entirely.
Why Developers Might Care
- Low-Level Memory Tuning: Developers can use raw byte arrays and custom getters to bypass strict compiler padding rules when memory optimization is critical.
- Algorithmic Review: Statistical modeling of expected value, standard deviation, and coefficients of variation can expose over-provisioned parameters in routing algorithms, such as excessive virtual nodes.
- Safe Production Migrations: The post provides a blueprint for replacing core infrastructural routing algorithms safely by running dual implementations concurrently and scoping rollouts geographically rather than globally.
Limitations
- Domain Specificity: The source notes that these massive memory savings rely on Cloudflare's scale—operating thousands of servers and dozens of feature-combination rings. Smaller applications may not see proportional gains.
- Implementation Complexity: Exchanging standard typed structs for raw byte arrays (
[u8; 6]) and manual byte-slicing getters trades code readability and developer ergonomics for memory efficiency.
Source: Cloudflare Blog