How To Prevent Cache Stampedes
Think cache invalidation and naming things are hard? Cute.
One of the biggest issues about ledgers, and money software in general, is performance. Numbers have to add up with total precision, and that often requires the kind of complex logic that makes operations slow down.
So you build a cache right?
In fact the CQRS pattern we talked about a couple of weeks ago is just that, to some degree: the write team continually builds materialized views of the state, which is just a fancy way of saying “pre-calculated results”. A cache.
Performant ledgers always involve some form of cache.
But here’s the problem: not everyone understands that caching has a subtle form of race condition built in. And, if you’re not careful, you might think you’re releasing your database from unnecessary read load, when in reality you’re not.
A Newsletter For The Engineers That Keep Money Moving
Designing payment systems for interviews is easy. Designing them for millions of transactions is not.
On Youtube, you’ll find tons of tutorials that show you how to design payment systems “to ace your interview”. Good, but you already have the job. Are these designs sound? Are they scalable?
You’re not looking for what works in theory. You’re looking for what works.
After nearly a decade building and maintaining large-scale money software, I’ve seen what works (and what doesn’t) about software that moves money around. In The Payments Engineer Playbook, I share one in-depth article every Wednesday with breakdowns of how money actually moves: topics range from what databases to use, to payment orchestration, even fraud and ledgers.
And I show you how top fintech companies build their systems.
If you’re an engineer or founder who needs to understand money software in depth, join close to 2,000 subscribers from companies like Shopify, Modern Treasury, Coinbase or Flywire to learn how real payment engineers plan, scale, and build money software.
In today’s article, I’m going to describe a common issue with cache layers called cache stampede. This article covers:
What cache stampede is and why it happens
Working code you can run on your laptop to reproduce it
Common approaches to avoid them
Let’s jump in.
Keep reading with a 7-day free trial
Subscribe to The Payments Engineer Playbook to keep reading this post and get 7 days of free access to the full post archives.