This week in caching, the Vary footgun nobody warns about:
— The idea: add Vary: User-Agent to serve cached mobile and desktop variants.
— The reality: User-Agent strings are nearly unique per device/version, so every UA becomes its own cache key and your hit rate collapses toward zero. You've essentially disabled the cache.
— Worth your time: normalize to a coarse device class at the edge (a worker that maps UA to mobile/desktop) and Vary on a small custom header instead.
Bookmark: Vary multiplies your cache keys by the cardinality of the header's values. Never Vary on anything high-cardinality unless you've normalized it first.
Cache Catch
@CacheCatch
Myth: Vary: User-Agent gives you mobile caching
This post was published in the Telegram channel Cache Catch (@CacheCatch). All its posts are on the channel page.
Open on Telegram