The hard part of application development is assigning cost to a tile request.

Project date Platform engineering · Tile serving

A web map is drawn from tiles, small square pieces fetched one request at a time. Each organization on Sarkoa has an allowance of tile requests, so every request has to be counted against somebody. Open a web map once and your browser fires off a dozen or more tile requests before you’ve even touched the mouse. If you try to meter that the obvious way - writing a row to Postgres on every tile - you immediately turn a casual map view into a write storm, and every viewer ends up waiting on your billing database just to see a map load.

The trick is to not ask the database every time. Instead a counter asks Postgres once for a reserved block - “put me down for the next 200 tiles” - counts those down in its own memory, and only goes back when the block runs out. Like buying a strip of bus tickets instead of paying the driver each ride.

My first attempt gave each Cloudflare Worker isolate its own little batch of 200 reserved requests, flushing back to Postgres every 25 requests or 1.5 seconds. (Cloudflare runs my tile code as many small copies at once, in datacentres all over the world. Each copy is an isolate, and isolates don’t share memory.) That worked fine until you think about how that network actually behaves: traffic hits different datacentres, so different isolates end up holding separate allowances for the same user at the same time. The next day I moved the counter into a single Durable Object per organization. A Durable Object is a small program with its own memory that Cloudflare guarantees exists exactly once, so every copy of the tiler, wherever it runs, draws from one shared bucket.

Who gets billed?

Before counting anything, the tiler has to know whose quota to dock:

  • Public map or embed: the publisher pays. A random person clicking a shared link doesn’t have a Sarkoa account.
  • Private file: the signed-in viewer’s organization pays for its own access.

From there, every tiler asks that organization’s Durable Object for permission. Most of the time it just ticks down the reserved block in memory and moves on. When the block runs dry, it goes to Postgres for a fresh block, and it flushes usage back every 100 requests or five seconds. You still hit Postgres occasionally, just orders of magnitude less often.

Making sure the math doesn’t lie

One easy trap with reserved blocks is over-counting. Say Postgres hands the counter a block of 5 requests, and only 2 tiles get served. The counter should show 3 left in reserve and 2 actually consumed - not 5 consumed just because 5 were reserved.

A reserved block of five tile requests: two served and waiting to be reported, three left, and one reservation call to the database. Below, the counter’s three answers: HTTP 429 when the publisher is over its allowance, the tile served when the database is unreachable and unreported usage is under its cap, and a temporary service error once that cap is reached.

I also wanted a clean line between “this account is out of quota” and “my database is having a bad day.” Out of quota returns a clean HTTP 429, the web’s standard “too many requests” answer. If Postgres is unreachable, the counter lets a capped buffer of unreported usage pile up so it can reconcile later; only when that buffer fills up does it throw a temporary service error. I verified those edge cases against simulated storage (test names, settings, and block trace).

Failing open when billing breaks

That still leaves one ugly failure mode: what if the Durable Object itself times out or errors?

I made a deliberate call here: fail open. A system fails open when a broken check lets things through, and fails closed when a broken check blocks everything. If the counter returns an actual refusal (HTTP 429 or buffer full), the tile gets blocked. If the counter can’t be reached at all, the tiler logs the error and serves the map anyway.

The alternative is letting a hiccup in my accounting code blank out every customer’s live maps. I’d much rather eat the cost of a few uncounted tiles during an outage than break someone’s embedded map because my billing layer blinked.

All posts