# Migrating a monolith to the edge without downtime

URL: https://eoxlabs.dev/engineering/monolith-to-edge-migration
Last updated: 2026-11-26
Published: 2026-11-26
Publisher: EOXLABS (https://eoxlabs.dev)

Section: [Engineering notes](https://eoxlabs.dev/engineering)

EOXLABS moves a server-rendered monolith to the edge by placing a Cloudflare Worker in front of it as a routing façade, then moving one route at a time behind that façade while the monolith keeps serving everything else. The monolith can take any route back until the last one has moved, so there is never a cutover night.

## Why use the strangler fig pattern?

In a [2004 post](https://martinfowler.com/bliki/OriginalStranglerFigApplication.html), Martin Fowler described the alternative to a rewrite: "gradually create a new system around the edges of the old, letting it grow slowly over several years until the old system is strangled." His [2024 rewrite of the article](https://martinfowler.com/bliki/StranglerFigApplication.html) names the price: "transitional architecture to allow the new and legacy system to coexist, code that will go away once the modernization is complete."

Big-bang rewrites tend to fail at cutover, because every undocumented behavior surfaces on the same night. A strangler fig migration spreads that discovery across many small releases, each one reversible. The transitional code is real work: the façade, the comparison harness, the session bridge. We plan it as a deliverable and delete it at the end.

## What does the routing façade look like?

A Worker attached to a route runs [in front of the existing application server](https://developers.cloudflare.com/workers/configuration/routing/routes/) on a proxied hostname. Calling `fetch()` on the incoming request sends it on to the origin set in the zone's DNS. The façade is a few lines:

```ts
const MOVED = [/^\/api\/catalog(\/|$)/, /^\/status$/];

export default {
  async fetch(request, env, ctx) {
    ctx.passThroughOnException(); // an uncaught error falls back to the monolith
    const { pathname } = new URL(request.url);
    const atEdge = env.EDGE_ROUTES === 'on' && MOVED.some((re) => re.test(pathname));
    return atEdge ? handleAtEdge(request, env, ctx) : fetch(request);
  }
} satisfies ExportedHandler<Env>;
```

`passThroughOnException()` is a useful safety net with two documented limits. It covers uncaught exceptions only, not exceeded CPU or memory limits. And because bodies stream, a request whose body was already read may reach the origin without it. Move read routes first. For write routes, catch errors and return a 5xx rather than counting on pass-through.

## How does edge code reach the existing database?

[Hyperdrive](https://eoxlabs.dev/stack/hyperdrive) sets up connections near the Worker and keeps a pool of connections [near the origin database](https://developers.cloudflare.com/hyperdrive/concepts/how-hyperdrive-works/), so the edge can query the monolith's database without moving it. Two defaults deserve attention during a migration.

First, [query caching is on by default](https://developers.cloudflare.com/hyperdrive/concepts/query-caching/): eligible read-only results are cached for a default `max_age` of 60 seconds, and Hyperdrive does not invalidate them when the application writes. The monolith usually reads its own writes; an edge route through a cached configuration may not. We run two configurations against the same database: a cached one for catalog-style reads, and one created with `--caching-disabled` for sessions, permissions and anything read right after a write.

Second, the pool runs in transaction mode. `SET` lasts only for one transaction or query, and session-level advisory locks such as `pg_advisory_lock` are not supported. Monolith code that leans on session state needs changing before its queries can move.

If the database itself is moving to [Neon Postgres](https://eoxlabs.dev/stack/neon-postgres), [logical replication](https://neon.com/docs/guides/logical-replication-guide) with Neon as the subscriber copies a snapshot and then streams changes, so the move becomes a replica promotion instead of a dump and restore.

## How do sessions survive the move?

A user should never notice which system rendered a page.

- **One issuer.** Only the monolith issues and revokes sessions until a late, separate step. The edge validates the same cookie against the same store, through the cache-disabled configuration.
- **Identical cookie attributes.** Same name, `Domain`, `Path`, `SameSite` and `Secure`. A path mismatch logs people out on the first edge-served page.
- **Revocation reaches both sides.** Any edge-side session cache must expire faster than the delay you would accept for a revoked session to keep working.
- **Issuance moves last**, as its own step with its own rollback.

## How do you prove an edge route is right before users see it?

Shadow traffic. The façade serves the response from the monolith, then calls the edge handler inside `ctx.waitUntil()`, compares the two, and logs any difference. `waitUntil` work can run for [up to 30 seconds after the response](https://developers.cloudflare.com/workers/runtime-apis/context/), shared across all `waitUntil` calls in that request.

- Shadow only `GET` and `HEAD`. A shadowed `POST` is a duplicate order.
- Compare status, key headers and a normalized body hash, with timestamps, CSRF tokens and request IDs stripped. Log the diff, not the body, because bodies carry personal data.
- Shadowing doubles database reads for sampled requests. Watch the pool.
- Promote a route when its mismatches are zero, or each remaining one is explained, across a full business cycle that includes month-end jobs.

## Who owns the data while it moves?

Every table has exactly one writer at any moment, and the design record names it. The other side reads. Ownership moves in a single deploy, only after the reads are proven.

## Why are dual writes so risky?

Writing the same change to two stores from application code has no atomic commit across them. If the first write succeeds and the second fails, the stores diverge. Retries double-apply unless every write carries an idempotency key. Concurrent requests can land in different orders on each side.

| Approach | Consistency | How it fails | When to use it |
|---|---|---|---|
| Dual write from application code | None across stores | Silent divergence after partial failure or reordering | Avoid |
| Single writer, other side reads through | The owner's data is the truth | Cross-region read latency | The default for most steps |
| Single writer plus transactional outbox | Change and event commit in one transaction | Consumers must be idempotent | Moving one domain's ownership |
| Single writer plus database replication | Replica lags the source | Lag; schema changes need coordination | Moving a whole database |

## What does the runbook look like, step by step?

1. Inventory every route, job and cron, with traffic and write paths. Rollback: nothing has changed yet.
2. Attach the Worker as a pure pass-through and record latency and errors. Rollback: detach the route; traffic reaches the origin directly.
3. Bind Hyperdrive to the existing database, cached and uncached. Rollback: remove the bindings; nothing depends on them yet.
4. Build the first read route at the edge and run it in shadow mode. Rollback: switch shadowing off.
5. Serve that route to a share of traffic with a gradual deployment, then all of it. Rollback: roll back the Worker version, or flip `EDGE_ROUTES`.
6. Repeat steps 4 and 5 per read route, smallest blast radius first. Rollback: per route, as in step 5.
7. Bridge sessions so edge routes accept the monolith's sessions. Rollback: route authenticated pages back to the monolith.
8. Move write ownership one domain at a time through an outbox or replication, never dual writes. Rollback: hand ownership back while the monolith's write path is still deployed.
9. Move session issuance and background jobs. Rollback: re-enable the monolith's issuer and scheduler, which stay deployable until step 10.
10. Retire the monolith after a quiet period with zero origin traffic. Rollback: restore from a verified, immutable snapshot taken before this step.

[Gradual deployments](https://developers.cloudflare.com/workers/configuration/versions-and-deployments/gradual-deployments/) route a percentage of requests to a new version. By default each request is routed independently, so one user can hit both versions unless version affinity is set, and only one version of each Durable Object runs at a time. [Rollbacks](https://developers.cloudflare.com/workers/configuration/versions-and-deployments/rollbacks/) reach any of the 100 most recently published versions, but they do not change connected resources or data. Every schema change must therefore be backward compatible: expand first, contract only after the old code is gone.

This runbook is the core of EOXLABS [legacy-to-edge migration](https://eoxlabs.dev/capabilities/legacy-to-edge-migration) work, the kind of project that begins as a Maintain On Course engagement on [the start page](https://eoxlabs.dev/start). Related notes: why latency budgets punish cross-region queries in [sub-50 ms edge latency](https://eoxlabs.dev/engineering/sub-50ms-edge-latency), what changes once code is [edge-native rather than edge-deployed](https://eoxlabs.dev/engineering/edge-native-vs-edge-deployed), and how to keep that final snapshot safe in [ransomware-resilient backups](https://eoxlabs.dev/engineering/ransomware-resilient-backups). The platform underneath is [Cloudflare Workers](https://eoxlabs.dev/stack/cloudflare-workers); more field notes are on the [engineering index](https://eoxlabs.dev/engineering).

— DL

## Related

- [Legacy-to-Edge Migration](https://eoxlabs.dev/capabilities/legacy-to-edge-migration): Maintain On Course: EOXLABS migrates live systems to Cloudflare's edge one route at a time, keeps the existing database reachable through Hyperdrive, and keeps every step reversible.
- [Hyperdrive](https://eoxlabs.dev/stack/hyperdrive): Hyperdrive lets edge code query a central Postgres without a new connection per request, and EOXLABS splits cached and fresh reads and keeps transactions short.
- [Cloudflare Workers](https://eoxlabs.dev/stack/cloudflare-workers): Cloudflare Workers is the request layer of EOXLABS systems, and this page covers how EOXLABS structures a Worker, the limits behind each design choice, and where a Worker is the wrong tool.
- [Strangler Fig Pattern](https://eoxlabs.dev/glossary/strangler-fig-migration): The strangler fig pattern migrates a legacy system piece by piece behind a routing façade, so the business keeps running while functionality moves to the new system.
- [Edge-native vs edge-deployed](https://eoxlabs.dev/engineering/edge-native-vs-edge-deployed): What separates an edge-native system from one that only runs its handlers at the edge, with a comparison table and the architecture smells that give an edge-deployed system away.

## More engineering notes

- [One engineering company, many industries](https://eoxlabs.dev/engineering/one-company-many-industries)
- [Durable Object alarms instead of background jobs](https://eoxlabs.dev/engineering/durable-object-alarms)
