Images
AVIF at the edge: what we learned encoding two billion images
AVIF images are about half the size of JPEG, but encoding them is slow. How we chose encoder settings, kept tail latency down, and when we still serve WebP.
In June, AVIF conversion became generally available on every paid plan. Since then our image pipeline has produced a little over two billion distinct AVIF variants. Visitors whose browsers accept AVIF now download images that are, at the median, 52% smaller than the original JPEG and 31% smaller than WebP at matching visual quality. Getting there took most of a year, and most of that year went into making encoding fast enough, not making files small.
The problem with AVIF
AVIF is based on the AV1 video codec, and AV1 encoders were designed for video, where you encode once and decode millions of times. An image CDN encodes on demand, on the first request for a given size and quality, while a visitor waits. Our first prototype took over 900 milliseconds to encode a 1600-pixel-wide photo at default settings. A visitor will not wait that long, and neither should they.
Choosing a speed setting
AV1 encoders expose a speed setting that trades encoding time for compression. We ran a sweep across a corpus of 40,000 customer images, measuring output size at a fixed perceptual quality target (scored with SSIMULACRA2) and encoding time on our production hardware.
| Format and setting | Median size | vs JPEG | Encode time p50 |
|---|---|---|---|
| JPEG (original, q85) | 214 KB | – | – |
| WebP (q80) | 149 KB | −30% | 11 ms |
| AVIF speed 8 | 121 KB | −43% | 38 ms |
| AVIF speed 6 (default) | 103 KB | −52% | 71 ms |
| AVIF speed 4 | 101 KB | −53% | 164 ms |
Speed 6 sits at the knee of the curve. Going to speed 4 saves another 1.5% of bytes at more than twice the CPU cost. Speed 8 is fast but leaves a lot on the table. We use speed 6 for all on-demand encodes, with two threads per image and a per-node pool that caps total encoding threads so a burst of new images cannot starve request handling.
# encoder settings per quality bucket, after the SSIMULACRA2 sweep
[avif.profiles.default]
speed = 6 # libaom cpu-used; 4 was 2.3x slower for 1.5% smaller files
q_map = "ssim2" # map requested q=1..100 to a perceptual target
chroma = "420"
tune = "ssim"
threads = 2 # per request; the pool caps total threads per node
[avif.profiles.small] # images under 64x64 px
fallback = "webp" # AVIF headers cost more than they saveHiding the first encode
Even at 71 milliseconds median, the slowest 1% of encodes take over 600 milliseconds, mostly very large source images. For those, we do not make the visitor wait. If an AVIF encode has not finished in 150 milliseconds, we serve a WebP (which takes about 11 milliseconds) for this one request, mark it with a short TTL, and let the AVIF encode finish in the background. The next request gets AVIF. In practice, 0.6% of first requests receive the WebP fallback.
Encoded variants are stored at the shield as well as the edge. Since a new image tends to be requested from many cities at once, especially on a news homepage, request collapsing at the shield means each variant is encoded exactly once worldwide, not once per location.
Where WebP still wins
- Tiny images. Below roughly 64×64 pixels, AVIF's container overhead makes files larger than WebP. Icons and thumbnails fall back automatically.
- Animated images. Animated AVIF decoding is still inconsistent across browsers. Animated GIFs become animated WebP.
- Sharp-edged graphics. Screenshots and diagrams with flat colours and text compress better as lossless WebP or PNG. Our classifier detects them by colour count and edge density and keeps the lossless path.
Content negotiation
With fm=auto, we look at the Accept header and pick AVIF, then WebP, then the original format. The chosen format becomes part of the cache key, normalised to three classes so the many different Accept strings browsers send do not fragment the cache.
$ curl -s -o /dev/null -w '%{size_download} %{content_type}\n' \
-H 'Accept: image/avif,image/webp,*/*' \
'https://www.example.com/images/hero.jpg?w=1200&fm=auto'
103418 image/avifWhat is next
We are experimenting with JPEG XL for the browsers that support it, and with encoding the most requested variants in the background at a slower speed setting once they are clearly popular. Both follow the same principle as everything else in the pipeline: spend CPU where visitors are waiting only when it pays off immediately, and spend the rest of it later. The shield tier is what makes that affordable.