How an 8-Byte Mistake Cost 100 Terabytes of Memory

We’ve all been there. You inherit a massive codebase, start poking around, and find a variable that does absolutely nothing. It’s just sitting there, taking up space. Usually, it’s harmless. A few bytes lost to the void. But what if that useless variable was duplicated 250 billion times?

Abstraction is a luxury. At scale, it becomes a tax.

Cloudflare recently detailed how they saved 100 terabytes of RAM on their 1.1.1.1 DNS resolver. You’d think this required a revolutionary new compression algorithm or a complete rewrite in a lower-level language. It didn’t. They just deleted a field that tracked capacity.

Let’s look at the Rust code. They were using a Vec to store DNS records. A Vec has three components: a pointer to the data, the length of the data, and the capacity (how much memory is allocated). But here’s the kicker: DNS cache responses are immutable. Once they are written, they are never modified. The capacity field is literally dead weight. Eight bytes of nothing, multiplied by 250 billion.

The most dangerous code in your system isn’t the code that breaks. It’s the code that works perfectly but serves no purpose.

The comments on this post are a goldmine of the engineer’s itch—the subtle thrill of dissecting a technical post to find overlooked design flaws. One reader pointed out the elephant in the room: 250 billion entries isn’t a cache. It’s a primary data store masquerading as a cache. A cache is supposed to be a small, fast layer. When your ‘cache’ requires 100TB of RAM just to hold the structural overhead, the abstraction has completely leaked.

Another commenter, a C-programmer, noted that they could have optimized further by putting the record data right after the CacheEntry members instead of allocating memory separately. It’s the kind of deep system mastery that makes you realize how much performance we leave on the table in our higher-level languages.

When your cache outgrows your database, you aren’t caching. You’re just hoarding.

This is why system programming still matters. We live in an era of infinite cloud resources and lazy abstractions. We just spin up another node when we run out of memory. But at the extreme edge of the internet, the bytes still bleed. The most profound gains didn’t come from sophisticated data structures. They came from questioning assumptions—like ‘we never modify cached responses’—and removing the dead weight.

First-principles thinking trumps band-aid optimizations every single time. Don’t just compress your data. Question why the data is there in the first place.

Before you optimize the algorithm, try deleting the dead weight. The best code is no code.

FAQ

Q: If it was so useless, why wasn't it caught in code review?

A: Because abstractions like Vec are treated as black boxes. We assume the compiler or the standard library knows best, so we stop questioning the underlying memory layout. It takes a first-principles review to realize the structure doesn't fit the use case.

Q: What's the practical implication for my own codebase?

A: Stop reaching for complex compression algorithms first. Audit your data structures for dead weight. At scale, removing a single unused field is infinitely more valuable—and easier—than a 10% algorithmic speedup.

Q: Is this actually a cache if it holds 250 billion entries?

A: No. As the comments pointed out, a 250-billion-entry 'cache' is a primary data store. The abstraction has completely leaked, but it's acting as a necessary performance hack at Cloudflare's scale. The line between cache and database no longer exists.

📎 Source: View Source