Skip to main content

Running a whole product on Cloudflare

Updated 8 min readMarkdown
Running a whole product on Cloudflare

Stagd gives every pull request its own running preview of an app. The previews run on our customers' compute. Stagd itself, the API, the dashboard, the webhooks and the edge that routes every preview's URL, runs entirely on Cloudflare.

This is why we built it there, what it looks like, and what we'd tell anyone thinking of doing the same.

Why Cloudflare

Four reasons:

  • Everything in one place. A database, object storage, queues, background jobs, cron, WebSockets, headless browsers, email, DNS and TLS for wildcard domains, under one account, one bill and one deploy tool. For a small team, every extra provider is another dashboard, another set of keys and another thing that breaks at night.
  • Cheap. Workers bill for CPU time, not for a server waiting on requests. A product with a few users costs almost nothing to run, and nothing runs while nobody's using it.
  • It scales without us. No instance sizes, no autoscaling rules, no region to choose. If launch day goes well, nothing has to be resized.
  • Stability. Stagd sits in the path of every preview's URL, so when it's down, every customer's previews are down with it. We wanted Stagd spread across many places, not on one server in one region. Cloudflare's network already carries a large part of the web, and our Workers run on all of it.

What runs where

Three Workers, talking to each other over service bindings with plain TypeScript, no RPC framework in between:

  • The server: the API, sign-in, the dashboard as static assets, and everything a preview's life needs.
  • Hooks: takes GitHub's webhooks and puts them on a Queue at once, so a delivery isn't lost while the server deploys or has a bad minute.
  • The edge: every preview's URL, *.stagd.run, checks who may open it and forwards to the right compute.

Behind them:

  • D1 for all the data, one database per environment, split into global tables and tables owned by a workspace, so a workspace can move to its own database later.
  • Durable Objects for state that lives longer than a request: live updates pushed to each workspace's open dashboards, and the connection to each customer's relay.
  • Workflows for long jobs that must finish: deploying a preview (build, restore the database, start the services, wait until it's live) and deleting a workspace, each step retried on its own.
  • R2 for backups, previews' thumbnails and the volumes of sleeping previews.
  • Analytics Engine for each preview's CPU and memory over time.
  • Browser Run to take a screenshot of each preview as it goes live.
  • Cron triggers for the sweep every minute that puts idle previews to sleep and expires old ones.
  • Cloudflare Tunnel to reach previews on compute behind a firewall.
  • Email for sign-in codes and invites.

What worked

Building was fast. The platform did most of the infrastructure work for us: there was nothing to provision. The CLI and the dashboard only talk to the server through one API contract, generated for TypeScript, so the server can change underneath them without either noticing.

Durable Objects are the best part. Each workspace has one object that every open dashboard listens to, so a change shows up everywhere at once. Our customers' relays keep a long-lived connection to an object of their own, and we run a small multiplexing protocol over it in TypeScript. On a server, those would have been a pub/sub system and a pool of long-lived connections to build and look after.

Workflows fit long jobs well. A preview deploy takes a minute or two and touches several outside systems. Writing it as steps that are retried on their own, and that survive a deploy of our own code in the middle, removed a whole class of "stuck in Building" bugs.

Queues in front of webhooks. GitHub doesn't retry a failed delivery on its own. A tiny Worker that only puts them on a queue means our server can be down for a deploy without missing a pull request.

What hurt

We can't preview Stagd with Stagd. This is the one that hurts the most. Stagd builds previews from a compose file and runs them as containers. Stagd itself is Workers, D1, Durable Objects, Workflows and queues, and none of that fits in a container. wrangler dev runs it locally, and Cloudflare can upload a Worker's version with its own URL, but a full copy of the system per pull request, with its own database, objects and workflows, is something we'd have to assemble ourselves. So the product that gives every pull request a preview is reviewed the old way: locally and on staging. Making Stagd work for apps like itself is on our list.

D1 is fast, until it isn't. Most queries take a few milliseconds. Now and then one takes seconds. Since every page in the dashboard checks the session first, one slow query was a blank page. We now keep who's signed in for 30 seconds per Worker instance, the dashboard asks again when the session check hangs, and the server logs any lookup slower than a second, so we can tell it happening.

Some context doesn't survive. We tag each request's errors and analytics with the workspace and preview through AsyncLocalStorage. A Workflow's steps are called back outside the context they were started in, so for a while their events and errors were silently dropped. Nothing failed: data just went missing. Each step now goes back into its context explicitly.

Error messages can point the wrong way. One morning every deploy, to staging and to production, failed with "You need to enable Analytics Engine" (error 10089). Analytics Engine was working fine, and our live Workers were writing to it. The real cause was that the account had dropped from Workers Paid to Workers Free. Upgrading the plan fixed it. If you see 10089 on a project that worked yesterday, check the plan first.

Headless browsers and busy pages. Browser Run waits for a page's network to go quiet before it takes a screenshot. A page with a WebSocket, polling or hot reload never goes quiet, so the screenshot failed. Telling it to take the page as it stands after 15 seconds fixed it.

Shared mail reputation. Sign-in codes sent through a shared sending IP landed in Junk at Microsoft 365 for a while, even with SPF, DKIM and DMARC passing. We wrote about that separately.

Would we do it again?

Yes, for sure. Everything in one place, a bill that grows with use rather than with servers, and nothing to resize when it gets busy: for a small team building a product like this, that's worth more than the rough edges above. The one we most want gone is that Stagd can't preview itself yet, and that's ours to fix.

What's next

Cloudflare runs containers now too, started and addressed from a Worker like any other binding. That opens a door we may go through: some parts of Stagd in another language, where a long-running process or a library from another ecosystem fits better than a Worker, while the rest stays on Workers, without giving up having everything in one place.

The same goes for the database. If D1's occasional slow queries or its size limits start to pinch, Postgres through Hyperdrive, Cloudflare's connection pooling and query caching in front of a regular Postgres database, is the obvious next step.

What's open

The server described here is closed, but what talks to it isn't. stagdhq/stagd is open source under Apache 2.0: the CLI, the relay that connects private clusters and Docker hosts, and the API contract (protobuf, served over Connect) that the dashboard, the CLI and the MCP server all use. Anything the dashboard can do, a script can do from the same contract.

The server may well be opened too one day. For now it's changing too fast, and we'd rather open it once it's in a shape others can build on.

If you're thinking of it

  • Design around Durable Objects early. Anything that needs one writer, a live connection or fan-out is simpler as an object than as rows and locks.
  • Put a queue in front of anything you can't afford to drop. It's a dozen lines.
  • Treat D1 latency as variable. Cache what every request needs, and log slow queries from the start.
  • Write long jobs as Workflows, and carry your request context into each step yourself.
  • Keep an eye on the plan. Some errors about a missing feature really mean a missing plan.

Stagd gives every pull request its own preview, with its own database and a link in the review, on compute you already have. Try it, or tell us about your own Cloudflare setup at hello@stagd.dev.

About the author

  • Software developer in Norway. I build developer tools and the platforms underneath them, mostly in Go, TypeScript and Rust, and write about what broke and how it got fixed.

No comments yet. Be the first to comment.