How I set up caching for an APK download site
APK files are big, change often and arrive with a pile of channel parameters. Cache too long and users get old builds; cache too short and the origin melts. Here is the setup we landed on after migrating a batch of download sites.
Last month we moved a batch of download sites onto our network, and the first two days were basically a fight with caching.
The problem is a classic. Each APK is tens of megabytes, and every download link carries something like ?channel=ad7&t=1795852901. Every channel and every click has different parameters. With query strings in the cache key you are not really caching at all, so every request went back to the origin. The origin was one ordinary cloud server. At 9 p.m. its uplink maxed out and users saw downloads freeze at 30%.
My first move was the obvious one: ignore the query string and cache for seven days. The next morning the customer called. They had shipped a new version and users were still downloading the old one.
One look at the origin explained it. Their APK lives at a fixed URL and gets rebuilt on a schedule. The same /app/latest.apk is regenerated every few minutes, Last-Modified is always a few minutes ago, and the response says max-age=0. Cache that for a week and you are handing out stale builds.
What we settled on
A separate cache rule for .apk: ignore query strings, cache for 60 seconds. When it expires the node does not download the whole file again. It asks the origin "has this changed?" with the ETag, and if not, the origin answers 304. That costs a few hundred bytes.
Sixty seconds sounds short, but for a download site it is plenty. At peak the same file gets pulled hundreds of times a minute and only one of those goes to the origin. Comparing the same domains in the same time window before and after the move, byte hit ratio went from about 62% to about 72%, and origin traffic dropped by almost a quarter. Not dramatic, but that server finally stopped paging people at night.
Two smaller things.
Range requests: mobile download managers love fetching in chunks, so the logs are full of 206s. As long as the cache key ignores parameters, ranged requests hit the cache too. Nothing special to do, just do not disable Range on the origin.
Purging after a release: if a new version means a new file name, cache as long as you like and never purge. If you overwrite a fixed URL like the site above, purge that URL after shipping instead of waiting out the 60 seconds. Before a big release you can also preload the file to the edge so every node does not hit the origin at the same moment.
There is no magic number for download-site caching. Go look at how the origin actually updates its files, then decide. That beats copying a "recommended config" from a forum every time.