skip to content

For a fashion catalogue app's photos, should the server offer fixed size buckets or any requested width, and how does each affect CDN hit rate?

level: middleimportance: must knowfreq 42%

answer

  1. every width is its own object
  2. few keys, many hits
  3. round up, accept a little waste
  4. an open resize endpoint invites abuse
  5. the bucket list lives on the server

basics

~20 s

Offer a short list of size buckets and have the app round up to the next one. Any-width resizing scatters each photo across many cache keys, so most requests miss, and an open resize endpoint invites abuse.

solid answer

~50 s

Each distinct width is a separate object to resize, store and cache. Because the installed app knows its exact pixel width, an any-width endpoint tempting the app to ask for 513, 517 or 573 splits one photo across dozens of keys, each paying its own miss at every edge and in every app's disk cache. A short list of buckets, say 320, 480, 640, 960, 1280 and 1920, concentrates traffic on a few objects that stay warm. The price is rounding up, at most about 1.5x the width and 2.25x the pixels with that ladder. An open `?w=` endpoint also lets anyone request thousands of unique widths, each forcing a resize at the origin; allow only the listed widths or snap others to a listed bucket, usually the next larger one. Keep the list server-owned so new buckets ship without an app release.

go deeper

for a junior

Remember that every distinct width is a separate cached object, so a short list of sizes keeps caches warm while exact widths scatter them.

for a middle

Explain the hit-rate arithmetic for buckets against any width, the waste rounding up costs for a given ladder, and why an open resize endpoint invites cache-busting.

for a senior

Keep the list server-owned, snap unknown widths at the edge, reuse larger cached variants on the device, and plan for builds that still request old buckets.

for a principal

Treat the ladder as a tunable policy: pick step ratios from traffic data, weigh waste against keys and origin cost, and decide who may add a bucket.

## Two ways to offer sizes The image server for a fashion catalogue app can expose sizes in two ways: - **Any width.** The app sends the exact pixel width it computed, for example `w=513`, and the server resizes on demand. - **Size buckets.** The server offers a fixed list, for example 320, 480, 640, 960, 1280 and 1920 pixels, and the app rounds its computed width **up** to the next bucket. Any width looks ideal for an installed app, because the app knows its exact pixels and could ask for a perfect fit. The cost appears in the caches. ## Why every distinct width is a cache problem A cache stores responses by key, and the width is part of the URL, so each distinct width of the same photo is a **separate object**. Each one is resized once at the origin, stored, and fetched as a miss the first time any edge location sees it. The same is true in each app's own disk cache: a 513-pixel copy does not help a request for 517 pixels. A worked example, with assumed numbers: one product photo is viewed 1,200 times at one edge location during its cache lifetime. 1. **Any width.** The device population produces, say, 40 distinct cell widths across phones and tablets, times three surfaces (grid, gallery, zoom), so up to 120 distinct keys. Even if every key is requested, the first request for each one misses: up to 120 misses out of 1,200, so the hit rate is at most 90 %, and in practice lower, because rare widths are evicted before their second request. 2. **Buckets.** Two or three buckets carry nearly all grid traffic and one or two carry the gallery, so perhaps 6 keys in use: about 6 misses out of 1,200, a hit rate around 99.5 %. The difference is not just origin load. A miss is slower for the user on a weak signal, because the edge has to fetch or resize before answering. ## The price of rounding up Buckets cost some extra pixels. With the ladder above, adjacent steps differ by a factor of 1.33 to 1.5, so the worst case sends a variant about 1.5 times the needed width, which is about 2.25 times the pixels; the average waste is much smaller. A cell that needs 513 pixels gets 640. Choosing the ladder is a trade between this waste and the number of keys, and the server team can tune it once it sees which buckets carry the traffic. | Choice | Keys per photo | Hit rate | Bytes per request | Abuse surface | Change needs an app release? | |---|---|---|---|---|---| | Any width | Dozens to hundreds | Lower; rare widths always miss | Exact fit | Unbounded unique widths | No, but the cache never warms | | Fixed buckets, list in the app build | A handful | High | Up to about 2.25x the pixels with steps of 1.5x | Bounded | Yes, and old builds keep the old list | | Fixed buckets, list from the server | A handful | High | Same as above | Bounded | No | ## The open resize endpoint An endpoint that resizes to any width is an invitation to **cache-busting**: anyone can request `w=1001`, `w=1002`, `w=1003` and so on, and every request misses, forces a resize at the origin and fills the cache with objects nobody else will use. The usual defences are: - accept only the widths on the published list, and **snap** any other width to a listed bucket (usually the next larger one) at the edge instead of resizing it; - if the server must produce custom sizes for some surfaces, sign the transformation URLs so only the app's own backend can mint them; - rate-limit transformation misses per client. ## Where the bucket list lives If the list is compiled into the app, every change waits for a release, and builds installed two years ago keep requesting the old widths forever. Put the list on the server instead: either each image object in the API response carries its `available_widths`, or the app fetches a small configuration with a URL template and the allowed widths. Then adding a bucket for a new tablet layout is a server change. Removing one is the careful direction: old builds may still request it, so the edge should keep **snapping** that width to a neighbour rather than returning an error. ## The app's own cache sits in front An installed app adds one more layer: its disk cache. Before requesting a 640 variant for the grid, the app can check whether it already holds the 960 or 1280 variant of the same photo from the gallery, and scale that down instead of downloading anything. Buckets make this lookup simple, because the app only has to check a handful of known widths. How the device decodes and caches those bytes is the image loader's concern; the variant choice is what stays here.

  • The app already has the 1280-pixel variant of a photo cached from the gallery. The grid needs 640. What should it do?
    Use the cached 1280 variant, scaled down to the cell, and request nothing. It costs a larger decode than the 640 file would, so the loader should downsample it, but it saves a network round trip and data on cellular. Buckets make the check cheap: the app looks for a known width at or above 640.
  • A product team wants a new 2400-pixel bucket for zoomable detail shots on large tablets. What has to change?
    If the list is server-owned, the server adds 2400 to the allowed widths for zoomable images and the app's existing round-up rule picks it when the pixels call for it. No release is needed, and older builds simply never ask for it.
  • How does the edge treat a request for a width that is not on the list?
    It snaps the width to the nearest bucket, typically the next larger one, and serves that cached object, or redirects to its URL. It does not resize to the exact width, which would let anyone fill the cache and load the origin with unique sizes.

  • Any-width resizing gives the best hit rate because every response is a perfect fit.
  • The CDN caches one copy of a photo whatever width the URL asks for.
  • Rounding up to a bucket doubles the bytes of every single request.
  • An open resize endpoint is safe because each width is cached after the first request.
  • Widths hardcoded in the build can be retired once the next release ships.