Cutting 100k entries to 10k saves Cloudflare 100 TB of RAM


Featured image Cutting 100k entries to 10k saves Cloudflare 100 TB of RAM

Cloudflare’s Masterstroke: How a Smarter Hash Map Saved 100 Terabytes of RAM

In the world of massive-scale computing, where efficiency is the ultimate currency, Cloudflare recently pulled off a staggering feat: saving 100 terabytes of RAM by fine-tuning its core hash-mapping algorithm. This wasn’t just a minor tweak; it was a deep dive into the mathematical architecture that powers their colossal data caching system, revealing how simple algebraic adjustments can yield astronomical resource savings.

At the heart of Cloudflare’s business is the ability to serve content directly from memory rather than repeatedly fetching it from live sites—a process that requires an incredibly complex system for routing and caching. To manage this, the company utilizes its open-source Pingora framework and the Ketama algorithm to map arbitrary URLs to the appropriate cache servers. The challenge lies in the sheer scale: in an environment where backend servers constantly appear and disappear, maintaining this mapping accurately and efficiently becomes exponentially difficult.

The original method for routing URLs involved hashing the URL and mapping it to a server. Initially, engineers instinctively tried to map each URL to a single server. However, this created a significant vulnerability. If a server unexpectedly went offline, all the requests routed to it had to be redirected, potentially overloading neighboring servers and causing cascading failures.

The solution required a sophisticated adjustment to the mathematical approach. The team realized that simply hashing a URL was insufficient. They developed a more robust system by generating multiple hashes for each server and randomly mixing them together. This ensured that if one server vanished, incoming requests could be redistributed among the remaining servers in an egalitarian and balanced manner, eliminating single points of failure and load imbalance.

This ingenious method required a massive amount of memory, multiplying the demands as they introduced layers of weighting and architectural restrictions. One machine was running with up to 100,000 server hashes, ballooning the required RAM. Faced with this immense memory footprint, Cloudflare engineers applied statistics and algebra to find the sweet spot. They discovered that a mere 10% of the original number of hashes was sufficient to deliver nearly identical results, with error rates barely changing even when scaling up to tens of thousands.

By implementing this optimized approach, the team managed to significantly reduce the computational overhead. Furthermore, by judiciously wrangling Rust’s data structures to save just two bytes per entry in the hash-server map, they managed to squeeze out billions of records from the system. This optimization, applied thoughtfully as a separate code path, allowed Cloudflare to save approximately 100 TB of RAM, proving that even the most complex systems benefit from a little applied mathematical brilliance.

You may also like: