Cloudflare significantly reduced memory usage in its Pingora-based load-balancing service by optimizing its consistent hashing implementation. This article details the mathematical and engineering approaches used to reclaim over 100TB of RAM globally, focusing on improvements to hash distribution, weight-based allocation, and memory layout for hash points.
Read original on Cloudflare BlogOperating at Cloudflare's scale means even minor inefficiencies can lead to enormous resource waste. The article highlights how their internal load-balancing service, Pingora Backend Router (PBR), experienced excessive memory usage due to its consistent hashing library, `pingora-ketama`. This prompted an investigation into how to optimize memory while maintaining performance and equitable resource distribution across thousands of servers globally. The goal was to find a balance between efficient resource usage and the inherent statistical properties of consistent hashing.
Consistent hashing distributes requests across servers such that adding or removing servers minimizes key remappings. Cloudflare uses it to route cacheable requests by URL, ensuring only one copy of a file is stored per data center. The core concept involves mapping server and task hashes onto a number line (or ring). Tasks are assigned to the nearest server to their left. A key challenge is achieving an even distribution of workload, as simple hashing can lead to highly unequal ranges for servers due to the random nature of hash outputs.
Consistent Hashing Fundamentals
Consistent hashing maps both data items (e.g., cache keys, URLs) and servers (nodes) to the same hash space (often visualized as a ring). A data item is assigned to the first server encountered clockwise on the ring from the item's hash. This design minimizes data movement when servers are added or removed, improving cache hit rates and reducing load on origin servers.
To mitigate uneven distribution, the standard practice is to assign multiple hash points (virtual nodes) to each server. While each individual hash segment remains random, the Law of Large Numbers dictates that the sum of many segments assigned to a server will tend to average out, leading to a more uniform workload distribution. For example, increasing from 1 hash per server to 160 hashes per server can reduce the coefficient of variation (error margin) from ~99% to ~8% in a 100-server setup. Cloudflare also uses weighted consistent hashing (like the Ketama algorithm) to distribute requests proportionally to server capacities (e.g., disk space for storage-intensive services, CPU for compute-intensive ones), requiring even more hash points.
A crucial architectural consideration for such a change is migration. Altering the hash ring changes request routing, which could invalidate a large portion of cached content and overwhelm origin servers. Cloudflare avoided a 'single global flip' by carefully orchestrating the transition, emphasizing that memory optimizations must not compromise system stability or performance during deployment.