Charm on the edge, but 1–2 seconds a screen. And I knew why

EngineeringCloudflare WorkersPostgresHyperdriveTanStack Start

Syahmi RafsanjaniCo-founder, CTO

Charm ran on Cloudflare Workers, near every user, and still opened a new Postgres connection for every request. Moving it next to the database fixed the speed for nothing extra. We replaced it anyway, for scope.

Every screen in Charm took about one to two seconds to open. The orders list, an order, the calendar: tap, wait, then the page.

Charm was our order system from December 2025. Staff entered every WhatsApp order into it and sent the customer a confirmation link. It ran as a TanStack Start app on Cloudflare Workers, which run your code close to whoever opens the page. On paper, that’s about as fast as hosting gets.

I already knew why it was slow. The more interesting question is why knowing didn’t fix it.

One connection, opened and closed every time

Charm’s database was Postgres, on our own server. The Worker talked to it through a small piece of middleware that every server call ran through. Here it is, simplified.

src/middleware/dbMiddleware.ts · simplified
const client = postgres(process.env.DATABASE_URL, {
  max: 1, // Required for Cloudflare Workers
  connect_timeout: 10,
})
try {
  return await next({ context: { db: drizzle(client, { schema }) } })
} finally {
  await client.end({ timeout: 5 })
}

Every request opened a brand-new connection to Postgres, with a pool of one. It ran its queries, then closed the connection.

Opening a Postgres connection isn’t free. There’s a TCP handshake, then a TLS handshake, then Postgres’s own start-up, all before the first query. On a long-running server you pay that once and keep the connection. In a Worker, without help, you pay it on every request. And a Worker running near the user is, by design, not running near your database.

One request in Charm on Workers. The handshakes cross the same distance as the queries.

Why we couldn’t use the usual fix

Cloudflare has a product for exactly this. Hyperdrive keeps warm connections to your database and hands one to the Worker, so the handshakes don’t happen per request.

We couldn’t use it. An SSL issue with how our database was hosted stopped Hyperdrive connecting. So I knew from the start that the per-request connection was the cost. I knew the tool that would remove it. I just couldn’t plug it in.

I tried to hide the wait instead. In December 2025 I added Suspense boundaries and prefetching. Table rows became links that preload their page, and calendar days fetch their orders on hover or touch. It couldn’t hide the wait.

Moving next to the database

In July 2026 I gave Charm a Docker build target and moved it off Workers, onto our VPS, next to its database. The build commit is dated 3 July. We also suspected the Workers free plan’s CPU limit might have played a part, but I never confirmed that.

Here’s the interesting part. The per-request connection stayed. The middleware above still opens and closes a connection for every request. But now the database was close by, so each handshake was short. Charm became fairly fast. And it cost nothing extra: the VPS was already running our database.

That answers the opening question. Knowing the cause wasn’t enough, because the cause wasn’t the code. It was the distance between the code and the data. Prefetching and Suspense worked on the wrong end of the problem.

The lesson

Put your server next to its database before you tune anything else.

What I’d tell anyone putting an app on the edge

  • Ask where the database lives. Edge code with a database in one city still pays the trip to that city.
  • Count the handshakes, not just the queries. A new connection per request can cost more than the query itself.
  • Check the pooler works before you commit. If it can’t connect, you’re back to raw connections.
  • Try moving the server first. It’s cheaper than a rewrite, and it may be all you need.

So Charm got fast, and we still replaced it. Not for speed: like the backends in Part 2, it was built for many florists, and we were one. The next system was built for Fleur Babes alone. Then I built far more of it than a floral studio needed.

Syahmi RafsanjaniCo-founder, CTO

Is your app fast on paper and slow in the hand? I'm happy to take a look.

Book a free callSystem review · build work · fractional CTO
Syahmi RafsanjaniCo-founder, CTO