openstreetmap/operations

Review cache headers served by render servers

Opened this issue · 5 comments

We want to review the cache related headers served by the render servers to make sure we're getting the best out of our frontend caches.

Currently the only such headers mod_tile adds are Expires and Cache-Control: max-age both of which report the same expiry time though in different way.

The core of it's algorithm is to take the current age of the tile and multiple that by ModTileCacheLastModifiedFactor which we have configured as 0.2 currently, so a tile that is five days old will have an expiry of one day and a tile that is thirty days old will have an expiry of six days.

It does also clamp the expiry to be no more that ModTileCacheDurationMax which is seven days in our configuration, and clamps the minimum expiry in three buckets:

  • A low zoom bucket configured using ModTileCacheDurationLowZoom which is 6 days for us and covers zoom 0 to 9
  • A medium zoom bucket configured using ModTileCacheDurationMediumZoom which is 1 day for us and covers zoom 10 to 13
  • A high zoom bucket configured using ModTileCacheDurationMinimum which is 3 hours for us and covers zoom 14 to 19

Some random jitter (up to 3 hours) is also added.

Tiles which are known to be dirty when served are given a randomised expiry up to ModTileCacheDurationDirty which we have as 15 minutes.

Because we know when low zoom tiles will be next rerendered, we should override z0-12 to be a couple hours after the weekly rerender completes.

Header Value TBC 1 TBC 2 TBC 3
etag "d4ffa790e405f161c794639c86f84e7f"
cache-control max-age=17265, stale-while-revalidate=604800, stale-if-error=604800
expires Fri, 29 Dec 2023 20:35:47 GMT
x-tilerender nidhogg.openstreetmap.org
content-type image/png
accept-ranges bytes
date Fri, 29 Dec 2023 15:48:45 GMT
age 44
x-served-by cache-lcy-eglc8600053-LCY
x-cache HIT
x-cache-hits 1
content-length 19647

Unfortunately mod_tile doesn't provide a way to set a fixed expiry and as it also doesn't set the Last-Modified header there's no way to have apache override it to last modified + one day for low zoom tiles :-(

Last-Modified is probably the first thing to get working, so we need mod_tile changes.

It appears that Last-Modified headers were removed here when the ETag header was "fixed" to be the md5sum of the tile rather than the metatile.

I can't think of any reason not to re-enable these two lines:
https://github.com/openstreetmap/mod_tile/blob/007fee044cffbbe35fdeac4167f8c5bd4379596e/mod_tile.c#L521-L522

Of course, it will only provide useful information for storage backends which support it.