Latest / The CTO Podcast with Fexingo: Technical Leadership, Architecture, and Engineering Org / How Shopify Handles Black Friday Traffic With Static Caching
Transcript
- Lucas: Every year on Black Friday, Shopify merchants collectively process something like a billion dollars in sales within a single day. Behind those checkout buttons, there is a massive infrastructure challenge: how do you keep storefronts loading in under a second when traffic spikes tenfold or more? Luna: And the interesting part is that most of that traffic is read-only — people browsing products, looking at images, reading descriptions. They aren't checking out every time. Lucas: Exactly. So the core strategy Shopify uses is static caching at the edge. They serve the majority of storefront pages from CDN nodes — think Fastly or Cloudflare — rather than hitting their Ruby on Rails application servers. The goal is to absorb maybe 90 percent of read requests before they ever touch the origin. Luna: Ninety percent is huge. But the tricky part is invalidation. If a merchant changes a price or marks something as out of stock, you can't serve a stale page for hours. Lucas: Right, and that's where the cache invalidation system comes in. Shopify uses a technique they call 'cache tags' or 'cache surrogates'. Each page gets tagged with identifiers — like product ID, collection ID, merchant ID. When a merchant updates a product, Shopify sends a purge request to the CDN for all pages that carry that product's tag. The CDN invalidates those cached responses and fetches fresh ones on the next request. Luna: So instead of purging the entire storefront cache, they only wipe the affected pages. That limits the blast radius. Lucas: Exactly. And it keeps the cache hit rate high because most pages remain valid. But there is a subtlety around cache stampedes. Imagine a popular product page gets invalidated right at the start of a flash sale. Suddenly thousands of shoppers hit the same page simultaneously, all experiencing a cache miss, and they all request the page from the origin at once. Luna: A stampede can take down the origin if it's not prepared. How do they handle that? Lucas: Shopify uses what's called request collapsing at the CDN level. The CDN recognizes that multiple requests for the same URL are coming in while the cache is empty. Instead of forwarding all of them to the origin, it sends just one and holds the others. When the response comes back, it populates the cache and then serves all the waiting requests from that single response. Luna: That's a standard CDN feature, but it only works if the origin response time is fast enough. If the backend takes two seconds to render the page, those waiting requests are all delayed by two seconds. Lucas: Right, and that's why Shopify also invests in making the uncached response as fast as possible. They use techniques like edge side includes — ESI — to assemble pages from cached fragments. The product description might be a fragment that rarely changes, while inventory status is a separate fragment that updates more frequently. The CDN can assemble the page from these fragments without hitting the origin for every piece. Luna: So the page itself becomes composable from cached parts. That's clever. Lucas: And it ties back to how they think about the edge. The CDN isn't just a reverse proxy — it's an active part of the application architecture. Shopify also uses something called 'surrogate keys' or 'cache tags' to manage relationships between resources. For example, if a merchant updates their logo, that change might affect the storefront header on every page. With a single purge request for the 'store-logo' tag, the CDN invalidates all pages that include that header fragment. Luna: That granularity is what allows them to keep the cache warm even during rapid updates. It's not all-or-nothing. Lucas: Exactly. And this approach predates the current wave of edge computing platforms. Shopify has been doing this for years, refining it through each Black Friday. Their public postmortems from 2018 and 2019 talk about how they improved cache hit rates from around 85 percent to over 95 percent by tuning these tag-based purges. Luna: I remember reading about the year they had a cache poisoning issue — where a bug caused the CDN to serve a stale page even after a purge. They had to add a version hash to the cache key to force fresh responses. Lucas: Yeah, that was a good lesson. They added a deployment identifier to the cache key so that every time they pushed new code, the entire cache effectively reset. It's a brute-force approach, but it works as a safety net. Luna: It's interesting because a lot of people think of caching as a set-it-and-forget-it thing. But Shopify treats it as an ongoing optimization problem. Lucas: And that's the kind of mindset that separates a black friday ready architecture from one that crumbles. If these conversations are useful for what you're building or running — whether it's your own side project or a team at work — listener support is what keeps this show ad-free and independent. Luna: Yeah, it's a small thing that makes a big difference. If you've gotten value from episodes like this one, you can throw a few dollars into the pot at buy me a coffee dot com slash fexingo. Lucas: And honestly, every contribution lets us spend more time digging into the details that matter. So back to Shopify's edge strategy — there's one more layer I want to mention. Luna: What's that? Lucas: They also use stale while revalidate. The CDN can serve a slightly stale cached page while it fetches a fresh version in the background. That way, the user never sees a loading spinner. The page renders instantly, and a few seconds later the next visitor gets the updated version. Luna: That's a great trade-off for a storefront where being a few seconds stale on inventory isn't critical. The alternative is a long spinner, which kills conversion. Lucas: Exactly. And Shopify has data showing that even a 100-millisecond increase in page load time correlates with a measurable drop in revenue. So they prioritize perceived performance over absolute freshness in many cases. Luna: That's a philosophical choice, really. For a checkout page you'd want strict freshness, but for a product listing, stale while revalidate is fine. Lucas: Right, and they apply different caching policies to different endpoints. The cart and checkout are never cached — those are dynamic. But the homepage, collection pages, product pages — those can be cached for a minute or two, with stale while revalidate extending that window. Luna: So the key takeaway for anyone building a read-heavy system is: invest in edge caching, use tag-based invalidation, and don't be afraid of serving slightly stale content if it keeps your site fast. Lucas: Exactly. And start small. You don't need to build Shopify's entire system from day one. Even adding a CDN with a simple time-based cache for your static assets can offload a huge amount of traffic. The difference between a site that handles a traffic spike and one that doesn't is often just a well-configured cache. Luna: And if you want to go deeper, Shopify has published several engineering blog posts about their Black Friday preparations. They're surprisingly transparent about what went wrong and how they fixed it. Lucas: Yeah, their 2023 postmortem on cache invalidation slowdowns is a great read. They traced a 50-millisecond increase in purge time to an inefficient database query in their invalidation service. That kind of detail is gold. Luna: It shows that even at Shopify's scale, the fundamentals matter. Index your queries, monitor your purge latency, and test your cache behavior under load. Lucas: Right. And that's the note I want to leave on. Caching is not glamorous, but it's the backbone of every high-traffic e-commerce site. Get it right, and you can handle Black Friday without breaking a sweat. Luna: Or at least without breaking your servers.