Kai Ole Hartwig
6 min read

Three S3 requests per image

The move of TYPO3 media to an S3 driver was done, checked and live. Then came the question whether anyone had compared the speed. Nobody had. The answer from the access log was unpleasant, and its cause sat not in the driver but in the web server. Part 9 of “The Ops Log”.

TL;DR

TL;DR: Caddy's file_server treats an S3 filesystem like a disk. For every file it asks for existence and size, opens it and seeks back to the start to deliver it. On S3 that is three requests at 30 to 40 milliseconds each, and again on the next visit. The FUSE mount before it had a disk cache and took 4 milliseconds. A first-party Caddy module fetches each file once, keeps it locally and only asks about changes. In the pod: 3 to 5 instead of 72 to 100 milliseconds.

What happened

Four TYPO3 installations had moved their media from the mounted bucket to a FAL driver; the story is in TYPO3 media without a mount. Delivery of the files went to the Caddy in the pod with an S3 filesystem module, the way the Caddy documentation suggests. Every check was green: files there, headers right, no errors.

Nobody had looked at latency. The edge proxy writes duration and cache status for every request to its log, and Loki held fourteen days of it. Same objects, before and after:

Request at the edge proxymountS3 filesystem
cache miss, median81 ms247 ms
cache miss, 95th percentile178 ms285 ms
the same 900-byte image4 ms259 ms
the same 1.6 MB image68 ms282 ms

Size did not matter. A fixed surcharge of around 200 milliseconds per file, however small. And the edge cache barely helped: only one request in ten on /fileadmin was a hit, because bots and first-time visitors fetch every address exactly once.

The cause

Measured in the pod, directly against Caddy, with no edge and no network in between: 73 to 85 milliseconds for the small image, 51 for a file that does not exist, 3 for a file from local disk. A single S3 access from the pod costs 28 to 42 milliseconds in the same region. So every file carried two to three S3 accesses.

The source of the module and of Caddy's file_server explains the number. file_server works the way one writes it for a disk:

  1. Stat: does the file exist, how large is it? The module sends a HeadObject.
  2. Open: open the file. The module starts a GetObject.
  3. ServeContent seeks to the end and back to the start to determine size and ranges. A filesystem on S3 cannot seek back. The module aborts the running download and starts a second GetObject.

Three round trips, each correct, a problem together. A file that does not exist costs two: a HeadObject and a listing, in case it is a directory. And nothing is kept between two requests. The FUSE mount had done exactly that: what was read once lay on the node's disk.

None of this is a bug in the module. An fs.FS that answers every method with a network call is correct. It just does not know what file_server intends to do with it.

The fix

The answer is a first-party filesystem module for Caddy, built around the behaviour of file_server: caddy-fs-s3cache.

The test against a real bucket found one more bug the unit tests had not seen: a never-fetched file with S3 down answered 400 instead of 500, because file_server treats every filesystem error it does not recognise as a bad request. Since version 1.0.1 upstream errors pass through bare and become 500 or 503.

The pilot ran on the smallest tenant, same pod type, same files, once before and once after the switch:

Measurementgeneric modulewith cache
small image in the pod, hot72 to 100 ms3 to 5 ms
small image in the pod, cold250 ms44 ms
file that does not exist40 ms12 to 27 ms
cache miss at the edge proxy70 to 88 ms1 to 12 ms

A day later the module was the class default for all four installations, and the generic module left the runtime image. How the first-party module got into that image without a detour through GitHub is in Ops Log 10.

The lesson

Two things. First: correctness was checked, speed was not, and both were measurable within an hour from the same logs. From now on a change in the request path gets a before-and-after number before it is called done. Second: a virtual filesystem over a network has to know what its caller does. file_server calls Stat, Open and two Seeks, and an adapter that passes each of those calls to the network one by one is correct and slow. The abstraction is not wrong. It is just not free.

Frequently asked questions

What happens to a replaced file?+

It appears at the latest after the revalidation interval, one minute by default. Image variants with a checksum in the name do not have the problem.

Why not let TYPO3 deliver the files?+

Then every image would pass through PHP. The web server delivers files orders of magnitude cheaper, and the URLs stay as they are.

Would a bigger edge cache have been enough?+

No. The first request for every address always reaches the origin, and with bots and first-time visitors almost every request is the first. The cache has to sit where the request originates.

Conclusion

The measurement came late, but it came, and it traced the problem to three lines of source. The result is a small module that brings back the disk the mount had, without the privileged pod the mount needed. Anyone letting Caddy or FrankenPHP serve files from S3 should count once how many requests a file costs. The answer is rarely one.

Next step

How many S3 requests does an image cost you?

I measure the delivery path of your media from the edge to the bucket and show where the time goes.

Book a slot →

Or email me directly: mail@ole-hartwig.eu

About the author

Photo of Kai Ole Hartwig.

Kai Ole Hartwig

Freelance DevSecOps consultant · OnlyOle Consulting

Programming since 2002 – self-taught, set up my own business with KO-Web in 2012. Over 100 projects, with a focus on security, performance, automation and quality. Today freelance: DevSecOps consulting, training and software development.