r/programming • • Aug 27 '26

How we saved 100 terabytes of memory by optimizing 1.1.1.1’s DNS cache

https://blog.cloudflare.com/dns-cache-memory-optimization-1111/
2.1k Upvotes

53 comments sorted by

572

u/hypertesto Aug 27 '26

I always enjoy this kind of posts from Cloudflare.
Boxing the enum variants it's something I'd never taught about

106

u/Ok_Stomach6651 Aug 27 '26

Yes, they are really underrated coding masters in the world, and important thing is they always belive in sharing internal things.

17

u/floodyberry Aug 28 '26

"maybe we shouldn't store multiple half empty vectors per entry when we have billions of entries" or "unioning a common 4 byte field with a very rare 136 byte field is wasting memory" isn't "coding master" territory

14

u/spennnyy Aug 29 '26

Seriously. It's kind of amazing that such low hanging fruit of optimization had not already been picked earlier for a team with such high volume traffic.

4

u/jwakely Aug 30 '26

Yeah, these are the sort of optimisations you'd expect to be done before you scale up the working set to one terabyte, not waiting until it's in production with hundreds of terabytes. They've been blogging about this BigPineapple architecture since at least February 2023. I guess the price of DRAM forced them to revisit this.

1

u/pro_hodler Sep 24 '26

Unfortunately in modern world the ignorant managers are always throwing hardware at the problem and never ever dedicate any budget to performance optimization. They just repeat "Premature optimization is evil" mantra

2

u/Ok_Stomach6651 Sep 24 '26

Actually these types of solutions only comes where money lacks and scaling expectations go beyond limits, google shifted more than half of networking in to user space to handle context switching delay, again google discovering big table which became foundation of nosql

158

u/User_00000 Aug 27 '26

It's a pretty common pattern, so much so that there's even a clippy lint for it. That said this and the lint usually only applies for a rather large difference between the variants (the default threshold is 200 bytes between the smallest and biggest variant).

49

u/hypertesto Aug 27 '26

It was an interesting concept, i humbly admit that as a jvm ecosystem worker, this kind of low level stuff is something I never touch so the idea was totally new to me :-)

18

u/syklemil Aug 28 '26 edited Aug 28 '26

Yeah, it comes up sometimes as a hurdle in learning Rust as well, because it winds up with a bunch of different ways of storing what feels like the same data to the user, so there are some blog posts out there like When should I use String vs &str where the first recommendation is just "don't think about it and just use String", and then branching out into some simple cases of &str.

People will generally get into stuff like &'lifetime str, Box<str> and Cow<'lifetime, str> as they need to, similar to how they'll start thinking about which allocator they're using when they need to (and I think it's common enough in Rust to wish that picking an allocator was a bit more explicit and hands-on, like in Zig, and that the Allocator trait would stabilize).

I suspect most people can write Rust kinda sorta like it was Java (although they're absolutely different languages) and not have to think about or even encounter stuff like Box<str>, and just leave it as an option for people working on stuff that needs something approaching Cloudflare scale and latency, similar to getting into the weeds on JVM tuning.

edit Though I'll also add that my Java education included something in that territory in discussions about int vs Integer.

8

u/aoeudhtns Aug 28 '26

It's still a concern in Java for folks working with native memory. Java even has arenas now to help out with that. Admittedly, a niche scenario.

A little less niche is dealing with the GC -- weak references, soft references, and phantom references. I have used weak references to automagically clean up things when a reference goes out of scope, and I have used soft references to allow data to be reclaimed that I know I can cheaply re-load back into memory. I'm not sure I've ever used a phantom reference.

7

u/syklemil Aug 28 '26

Yeah, and working with a GC doesn't mean people are entirely free from thinking about memory, as anyone who's had to deal with OOM events and GC thrashing will know, only the toolkit there will be different kinds of voodoo than what will show up in a Rust article.

FWIW some of that stuff also shows up in Rust. The RAII logic it uses should feel pretty familiar to practitioners coming from GC languages, and so to avoid stuff like reference loops turning into a memory leak you'll see recommendations to use Weak.

(There's also PhantomData, but having a look at PhantomReference in Java I suspect the two are false friends or convergent etymology or something.)

4

u/aoeudhtns Aug 28 '26

There's a degree of sameness yet they're quite different. I'm no expert with PhantomData but the docs look like it has something to do with lifetime ownership in a struct (although it also says this is no longer necessary, so...)

In Java you'd use a PhantomReference to receive notification that some referent's lifetime has passed. The docs say that this is typically done for some kind of cleanup, although IME Weak is more commonly used for this because Weak is about-to-be-GC'd and Phantom is has-been-GC'd. Weak has the gotcha that you can accidentally prevent GC; Phantom has the gotcha that you can't get the referent back if it is holding any data relevant to the cleanup, because it's already gone.

4

u/steveklabnik1 Aug 28 '26

They're totally different, PhantomData is basically an API to control variance.

1

u/Worth_Trust_3825 Aug 28 '26

edit Though I'll also add that my Java education included something in that territory in discussions about int vs Integer.

valhalla is in early preview. the discussion is soon to be irrelevant

17

u/admalledd Aug 28 '26

Darn that high quality JVM JIT /s :)

1

u/Rare_Instance_8205 Aug 28 '26

Hi, as someone new to programming can you tell me what should I need to learn before I fully understand this article and start thinking like these guys did?

122

u/KryptosFR Aug 27 '26

That was an interesting read. Easy to understand even if you don't know what DNS records are.

45

u/Ok_Stomach6651 Aug 27 '26

Yes, these blogs are surpringly easy to understand and indepth

24

u/Pseudanonymius Aug 27 '26

Very interesting. Would love to be one of the people working on it, amazing Engineering. 

35

u/BoredTech127001 Aug 28 '26

So I'm hearing you have some memory for sale then... I'm interested.

18

u/danted002 Aug 28 '26

They are reinvesting the recouped memory into the cache itself to serve DNS even faster.

21

u/authentic_developer Aug 28 '26

Neat writeup. Worth flagging that a lot of these memory tricks only pay off once you're at Cloudflare's request volume, at a smaller scale the extra indirection from boxing variants can actually hurt cache locality more than it helps. Profiled something similar once on a much smaller cache and the "obvious" optimization made things slower until the working set got big enough to matter.

22

u/rdtsc Aug 28 '26

once you're at Cloudflare's request volume

Reducing a services's working set from 10 to 5 GB is useful even if you only have a single box. And on a smaller scale you might have smaller boxes, too, so again worthwhile.

6

u/CherryLongjump1989 Aug 28 '26

If this is about trading memory for time, it’s absolutely plausible that the relative value of one or the other becomes relatively more important at scale.

7

u/ExF-Altrue Aug 28 '26

A clean and interesting read as always!

7

u/HRApprovedUsername Aug 28 '26

Who’s paying for all the bots in this comment section

1

u/floodyberry Aug 28 '26

Good read! Very interesting and cool. I always enjoy your comments.

18

u/DeliciousIncident Aug 28 '26

The guy who used DNS as free storage must be furious now with his data being gone!

6

u/invisi1407 Aug 28 '26

Why would his data be gone? This is just the cache, not the DNS zone storage itself.

12

u/DeliciousIncident Aug 28 '26 edited Aug 28 '26

It's just a joke on the title - "saved 100 terabytes", and people abusing low level network protocols, like ICMP Echo, or in this case DNS, for storing data. Think more like DNSFS, which does in fact store the data in the DNS resolver cache, rather than the DNS zone.

13

u/Zamaamiro Aug 28 '26

Cloudflare has some of the best engineers in the business.

2

u/Roang_zero1 Aug 28 '26

Good read. The savings are incredible at all levels, but this really highlights how small savings can have a massive impact at scale.

2

u/r1shi Aug 28 '26

Boom Boom Sebastiaan! nice article.

2

u/Revolutionary_Ad7262 Aug 29 '26

I would like to see x32 ABI in action for it or some slot based pointers, which are independent of the machine word

3

u/smp46 Aug 28 '26

Cloudflare blogs are always interesting, cool that they're still finding ways to optimise an older service.

-1

u/Rare_Instance_8205 Aug 28 '26

Hi, as someone new to programming can anyone please tell me what should I need to learn before I fully understand this article and start thinking like these guys did?

-6

u/NamedBird Aug 28 '26

Neat article.

(Though i think we need a DNS 2.0 or something to really make it more efficient.)

5

u/invisi1407 Aug 28 '26

Why'd we need DNS 2.0? Cloudflare as a DNS provider has a ridiculously large number of zones. This is just caching responses to lower the response latency.

5

u/snowtax Aug 28 '26

I would argue that DNS is the most efficient set of globally distributed databases on the planet with caching built in at several levels including your own computer. I have no idea how that could be done any better.

2

u/NamedBird Aug 28 '26

Well, i'm not saying that it's bad, but the protocol is quite old. And while it's very efficient with it's global distribution, i believe it could still be improved upon. I'd like to see a better caching mechanisms that does not rely on fetching everything (A+AAAA+HTTPS+CAA+TXT+DNSSEC) again when the TTL expires, separation of records per service and integrating security mechanisms that aren't as fragile as DNSSEC.

If you can make that work, there should be less "it's always DNS" problems. 🙃
And it would allow for some interesting follow-up things like using DANE to sidestep CA's.

1

u/snowtax Aug 29 '26

One of the reasons for short timeouts is load balancing and fail-over. If a server goes down on you just want to distribute load across multiple servers, a monitoring system can change the IP address. That only works with short (maybe 5 minute DNS timeouts) to make the client refresh the address. That’s just one means of load-balancing and fail-over, but that one is useful when you need redundancy across data centers which may be on different continents.

501

u/[deleted] Aug 27 '26

[removed] — view removed comment

286

u/YeOldeMemeShoppe Aug 27 '26 edited Aug 27 '26

My stats are very outdated (like 10-12 years back), but at Twitter, when it was limited to 140 characters, each tweets in the database would take 10-15 kilobytes demoralized denormalized.

Every storage in any system is a trade off between speed and space.

Edit: autocorrect strikes again.

217

u/rariety Aug 27 '26

Why were they so mean to those kilobytes? Poor things.

93

u/Johnnyhiveisalive Aug 27 '26

It's the metadata that they planned to sell to advertiser's that was the real dataset.. the characters they stored for free was their payment for that data. Device, user, time, location, weather, who knows what other data points

28

u/LambdaLambo Aug 28 '26

Yeah that makes sense I’d be pretty upset if I were those KBs

57

u/bzbub2 Aug 27 '26

if anyone wants some good old twitter war stories this is a really funny video with twitter engineers reminiscing on all the crazy bugs https://www.youtube.com/watch?v=swtlHP58ak8 wish they made more videos. i am astonished to see it only got 1.2k views it is so good lol

9

u/superxpro12 Aug 27 '26

I really appreciate the zero indexed nerd joke

6

u/keloidoscope Aug 28 '26 edited Aug 28 '26

Oxide and Friends hosted Jason Hoffmann, whose company Joyent used to provide hosting for Twitter in the early days, and he had some classic war stories of how early Twitter got to learn some hard lessons about scaling, and why Ruby exceptions weren't a great control structure choice for hot code paths, especially combined with a deep call stack...

https://youtu.be/rSVhwjFOIyU

edit: added Joyent context

4

u/Nine99 Aug 28 '26

Well, it's not like Twitter engineers where watching others, either. I sent them an easily fixable, easily exploitable bug with security implications, and they didn't even answer. That was pre-Elon.

48

u/SirDale Aug 27 '26

I’m sure anything under musk’s control would be even more demoralised now…