Illustration of a CDN for ecommerce: product cards served from an edge globe while the shopping cart bypasses to the origin server

Online stores have two kinds of pages: ones every visitor sees the same way, and ones that are about one person's basket. A CDN for ecommerce speeds up the first kind a great deal. Applied carelessly to the second, it can show one shopper another shopper's cart.

What makes a store harder to cache than a blog?

The difference is state. Wikipedia's article on HTTP cookies describes how it works: the contents of a shopping cart are usually stored in a database on the server, and the server sends the browser a cookie with a unique session identifier, which comes back with every request so the server knows which cart to show.

That cookie is the hazard. If a CDN caches a response that carries Set-Cookie for one visitor and replays it to the next, the second visitor inherits the first one's session. Stores that skip the CDN to avoid that are safe, but slow. The earlier fix, 4.5 s to 2.1 s for a Magento store, is documented in a 2012 guest post by Kris Kayyal on this blog, How To Achieve a 2 Second Page Load with a Magento eCommerce Store. It combined several changes, and faster delivery of static files was one of them.

NetDNA, the CDN whose archive this site keeps, handled the split with pull zones: static files come through a separate zone hostname, and the store itself stays on the origin. The Magento CDN guide shows the setup, and our Magento CDN comparison shows where it fits.

Which URLs can the edge keep?

Say a shop in Bristol sells 1,200 products with 6 images each, at about 90 KB per image. That's 1,200 x 6 = 7,200 images, or 648,000 KB, about 648 MB (7,200 x 90 KB). Those files are identical for every visitor, so they're ideal edge content, and they usually make up most of a page's weight.

  • Cache for a long time: product and category images, CSS, JavaScript, fonts, logos (all with fingerprinted or versioned names).
  • Cache briefly, for anonymous visitors only: category pages and the homepage, with a lifetime of a minute or two.
  • Never cache: cart, checkout, account pages, order confirmation, search results containing personal data, and any response that sets a session cookie.

The reason for the middle tier's short lifetime is stock and price: a category page cached for a day can advertise a product that sold out at noon.

Why a separate static hostname helps twice

Serving static files from cdn.shop.example instead of www.shop.example removes a quiet cost. Browsers attach cookies to every request for the host that set them. Say the shop's cookies add up to 1.5 KB and a page needs 60 static files: that's 60 x 1.5 KB = 90 KB of uploaded cookie data per page view, or 720 kilobits. On a 5 Mbps uplink, 720 / 5,000 is about 0.14 s, which is 144 ms of upload that does nothing. A separate hostname that never sets cookies sends none of it.

It also makes the caching rule easy to enforce: nothing on the static hostname sets cookies, so nothing on it can leak a session. The static zone can use the long lifetimes from our Cache-Control guide without exceptions.

How do you keep carts out of the cache?

For the dynamic pages, the origin should say so explicitly. Setting Cache-Control: private, no-store on cart, checkout and account responses tells shared caches not to keep them. Where the CDN also fronts the HTML, a rule that bypasses the cache whenever a session cookie is present adds a second layer. EdgeRules are meant for this: matching on a path or header and choosing the caching behaviour for it.

Cross-origin issues can follow the move. Fonts and API calls on the static hostname need their Access-Control-Allow-Origin header, or the storefront loses its typography after launch.

A ten-minute check before you trust the setup

Three requests reveal almost every mistake:

curl -sI https://cdn.shop.example/media/catalog/shirt-001.jpg | grep -iE 'cache-control|set-cookie|age'
curl -sI https://www.shop.example/checkout/cart/ | grep -iE 'cache-control|set-cookie|age'
curl -sI https://www.shop.example/ | grep -iE 'cache-control|set-cookie|age'

The image should show a long cache-control, no set-cookie, and an age that grows on repeat requests. The cart should show private or no-store and no age at all. If the homepage returns a set-cookie alongside a public lifetime, that's the combination to fix before anything else, because it's the one that ships one shopper's session to the next.

Related: Magento CDN setup, PrestaShop CDN, purging after a catalogue update.