Since November I’ve published 34 apps, watch faces and data fields for Garmin watches under Silva Dev Labs. They name the mountain you point your wrist at, draw the aircraft overhead, show offline maps and ski pistes, and plot the nearest earthquake’s seismogram.
None of that fits on a watch. Behind them sit 14 Cloudflare Workers, all TypeScript, with 3 R2 buckets, 4 D1 databases and one Durable Object class. This note is about what the watch forces you to do, and why Workers and R2 turned out to be a good shape for it.
The watch is the constraint
A Garmin app talks to the internet through the paired phone, with makeWebRequest, and gets JSON back. Everything else follows from how little the watch can take. These numbers are all from comments in my own code, where each one explains a design decision:
- One response is 8–16 KB, depending on the device and the kind of app. A watch face accepts 8 KB. Too big and the app gets error
-402. - Storage holds 32 KB per value, so an offline map tile has to come in under 30,000 bytes.
- JSON decimals become single-precision floats, about 2 m of error at 180°. 32-bit integers arrive exactly, so positions travel as integer microdegrees.
- A polygon can have at most 64 points, because that’s all
Dc.fillPolygontakes. - The watchdog allows 240,000 instructions per callback. A terrain panorama is out of the question on the device.
- A watch face’s background service runs every 5 minutes, for about 30 seconds.
- No WebSockets.
- 96 KB of memory on smaller watches. One watch face crashed with 2.2 KB free.
So the server’s job is not “be an API”. It is to do the heavy work and hand the watch something tiny, already shaped for how the watch will use it.
Pack answers for the watch, not for a browser
JSON is the only transport, so everything binary rides inside it as base64: a place in Refill is 14 bytes, a quake in Tremor is 22, and True Sunset’s skyline is a run of int16 angles. Where it has to stay JSON, Skyward uses arrays instead of objects, because the watch parses them in far less memory. The header comment in near.ts is the whole spec:
// Answer (arrays, not objects: a watch parses them in far less memory):
// { "v":1, "t":epoch s, "src":"adsb.lol (ODbL)", "r":km, "c":count in the circle,
// "ac":[[hex, callsign, lat·1e5, lon·1e5, altFt|null, gsKt|null, track|null, ...], ...],
// "ty":[[designator, name], ...],
// "rt":[[flight, airline, fromCode, fromCity, ...], ...] }
Aircraft types and routes are de-duplicated into lookup tables that rows point into. A typical answer is about 3 KB, down from 34 KB raw.
Let nearby users share one upstream call
Live data comes from public sources (adsb.lol for aircraft, USGS and EMSC for earthquakes, Nominatim for place names) that I want to be a polite client of. The trick used across the Workers is to round the user’s position until nearby users produce the same cache key.
Skyward fetches aircraft per 0.2° tile (about 22 km), with a radius that covers any point in the tile, and caches it for four seconds:
export const TILE = 0.2;
const MARGIN_KM = 17; // farthest a point can be from its tile's centre, and a little
const BUCKETS_NM = [15, 30, 60, 100]; // upstream radius steps
const POS_TTL = 4; // seconds
Each answer is then cut down to the user’s own circle and sorted nearest first. Two people at the same airport cost one upstream request. It also honours upstream Retry-After. Other Workers round harder: reverse geocoding is cached for 30 days per 0.05° cell, and terrain answers are cached per roughly 10 m.
The cache layer is caches.default plus a small in-memory copy per isolate, with the write pushed off the response path:
ctx.waitUntil(api.put(req, res).catch(() => undefined));
Ten of the Workers also use the Rate Limiting binding, keyed per phone IP. The limits range from 4 per minute for generating a new offline map pack to 300 per minute for Streetwise’s small 3D building cells.
The planet in one R2 object
Wrist Map draws offline vector maps. The source is a single Protomaps basemap.pmtiles file for the whole planet, about 129 GB, stored in R2 for roughly $2 a month. Nothing is pre-cut per region. The Worker reads exactly the bytes it needs with ranged gets:
class R2Source implements Source {
constructor(private bucket: R2Bucket, private key: string) {}
getKey() { return this.key; }
async getBytes(offset: number, length: number): Promise<RangeResponse> {
const obj = await this.bucket.get(this.key, { range: { offset, length } });
if (!obj) throw new Error(`missing ${this.key}`);
return { data: await obj.arrayBuffer(), etag: obj.etag };
}
}
The decoded vector tiles are then re-encoded into a compact format the watch can draw, and cached for 7 days. The cache key is the part I’m happiest with:
// What the watch tiles are made from: the format, the encoder and the
// basemap file. In every cache key, so a new basemap or encoder never meets
// tiles or chunks cached from the old one.
return `${TILE_VERSION}.${ENCODER_REV}.${reader!.etag}`;
Upload a new planet file and its R2 etag changes. Bump the encoder and its revision changes. Either way, stale tiles simply stop being addressed. There is nothing to purge.
Offline packs go to the watch in chunks of up to 12 KB, which is 16 KB once base64’d. Each chunk carries a hash of the tile it starts in and of the tile it ends inside. If the next chunk’s starting hash doesn’t match, the map changed between the two requests, and the watch fetches that tile again from its start instead of stitching two versions together. Chunks are cached too, so a retried chunk costs nothing.
Two other Workers read the same bucket with read-only bindings: Pistes lays ski runs over the basemap, and Streetwise builds 3D buildings from it. Uploading 129 GB with wrangler r2 object put was too slow, so it goes up with rclone.
Terrain: 6 GB, one ranged read per tile
Peak Horizon names the mountains you’re looking at. Its bucket holds a world elevation model (Copernicus GLO-90) plus peaks, places, water and snow masks: about 26,500 land cells, roughly 6 GB in 53,000 objects, inside R2’s free 10 GB.
Each elevation file holds 60×60 tiles, compressed with a plane predictor and Rice coding (3.1–3.8× at the near level). They sit behind a uint32 offset table, so once the table is cached in the isolate, any tile is a single ranged read.
The panorama itself, 360 directions by 15 rows, is ray-marched on the server, because the watch’s instruction watchdog would never allow it. It takes about 16 ms cold and 3–4 ms warm.
Once that terrain was in R2, other apps got it almost for free. True Sunset casts 1,440 rays against the same data to find when the sun actually drops behind the ridge, not the official sunset time. Refill reads the same bucket to find water near you. Native Wear OS and watchOS versions read the raw objects through a /v3 route cached at the edge.
The few stateful parts
Most Workers are stateless functions over R2 and the cache. Three things needed more:
- Group Beacon shows where the rest of your group is. Each 6-digit group code is one Durable Object (
Room, SQLite-backed), and an alarm deletes it after 4 hours. Phones can use a WebSocket. Watches can’t, so they poll. - Usage counts live in D1. Every app says hello when it opens. The Worker stores a SHA-256 of the device ID (never the ID itself), plus the model, firmware, app version and country from
request.cf. It always answers{ok: true}: bookkeeping must never fail the watch. - Open Dial, a customisable watch face, keeps designs in D1, capped at 4 KB packed. It long-polls for up to 25 s to fit inside the face’s 30-second background window, and a daily cron cleans up.
Picking a map area on a watch screen is miserable, so for that the watch hands the job to the phone. It draws a QR code from raw bit modules, and a page served by the same Worker takes over.
What I’d change
Honest notes, since I’d want to read these in someone else’s post:
- Code is copied between Workers, not shared. The tile encoder, geometry and hashing helpers started in Wrist Map and were copied into Pistes, and the terrain codec lives in three repos. That was fast for the first ten apps and is now the main source of drift. The fix is a small internal package, versioned like any other dependency.
- Testing is uneven. Open Dial has about 47 tests. Several Workers have none, or only an end-to-end script. Skyward’s tests run against recorded upstream responses, which is the pattern I’d copy everywhere.
- Deploys are mostly manual. One Worker deploys from git with Workers Builds; the other thirteen are
wrangler deployfrom my machine. With 14 Workers that’s 14 chances to forget.
Each of those is a tooling problem more than an app problem, which is roughly why I find the tooling side interesting.
In numbers
| Apps, watch faces and data fields | 34 |
| Workers (TypeScript) | 14 |
| R2 buckets | 3 (terrain ≈ 6 GB, planet map ≈ 129 GB, ski areas) |
| D1 databases / Durable Object classes | 4 / 1 |
| Workers with rate limiting | 10 |
| Largest single response a watch face accepts | 8 KB |
| Skyward answer, packed vs raw | ≈ 3 KB vs 34 KB |
| Panorama compute, cold / warm | ≈ 16 ms / 3–4 ms |
All the apps are at links.silvadevlabs.com, a page that is itself a Worker building the list live from the store, with a cached snapshot as a fallback.