skip to content

How would you set a team's Hyperdrive rules for query caching and origin connection limits when several Workers share one PostgreSQL database?

level: principalimportance: nice to knowfreq 14%

answer

  1. classify reads by freshness
  2. one cached, one fresh config
  3. limits add up per config
  4. soft limit needs headroom
  5. short transactions free the pool

basics

~20 s

Classify reads by how stale they may be, give cache-tolerant reads a cached config with a deliberate max_age and route the rest through a cache-disabled one, then budget each config's soft origin connection limit so their sum stays under the database's cap.

solid answer

~40 s

I start from the reads, not the tool. Public, high-volume reads such as the product listing go through a cached config with an explicit `max_age` the product owner signs off; anything that decides access or must reflect a user's own write goes through a cache-disabled config, which still pools connections. Then the connection budget: each Hyperdrive config has its own pool with an `origin_connection_limit` (minimum 5, about 20 on Free and about 100 on Paid), and the limit is soft, so the sum across every config on that database must sit well below its `max_connections`. I keep the number of configs small, require short transactions and per-request clients, watch `cacheStatus` and pool metrics, and name an owner for config changes and the break-glass pool restart.

go deeper

for a junior

Know that each Hyperdrive config has its own cache settings and its own limit on connections to the database.

for a middle

Explain why reads are split between cached and cache-disabled configs, and why the connection limit is soft.

for a senior

Size the origin connection limits across configs against the database's cap, and spot pool exhaustion from metrics.

for a principal

Own the policy: the default binding for new code, who signs off max_age, how many configs exist, and who may restart a pool.

## The decision a lead owns Hyperdrive gives every Worker two knobs that affect correctness and the database's health: **query caching** (on by default, 60-second `max_age`, 15-second `stale_while_revalidate`, no invalidation on writes) and the **origin connection limit** of each config. Left to individual services, teams end up with stale reads in places that matter and with more origin connections than the database accepts. The lead's job is to set defaults, decide where the exceptions go, and make the trade-offs visible. There is no single right answer; what follows are the levers and how to weigh them. ## Caching rules Classify reads by **how stale they may be**, not by table: | Read class | Examples | Config | | --- | --- | --- | | Tolerates a short delay | product listing, search, public pages, dashboards | cached, explicit `max_age` | | Must reflect the latest write | the editor's own view after saving, cart totals | cache-disabled | | Decides access or money | sessions, permissions, billing state, admin settings | cache-disabled | Questions to settle with the team: - **What is the default for new code?** Defaulting to the cache-disabled binding is safe but leaves load on the database; defaulting to the cached one is fast but shifts the burden onto reviewers to spot reads that need freshness. One workable split is a cached default for read-only services and a fresh default for services that also write. - **Who signs off on `max_age`?** The staleness a product page tolerates is a product decision. Write the chosen value down per config rather than relying on the 60-second default. - **How do ORMs fit?** If a library owns the SQL, it needs its own client built from the right binding; make that wiring part of the service template. - **What counts as cacheable?** Queries with `NOW()` or other `STABLE` or `VOLATILE` functions are not cached, so the review checklist includes binding time values as parameters. ## Connection budget Each Hyperdrive config keeps **its own pool** and its own `origin_connection_limit`, set with `wrangler hyperdrive update <id> --origin-connection-limit <n>` or in the dashboard. Facts that shape the budget: 1. Every config allows at least **5** connections; the maximum depends on the plan, about **20** per config on Free and about **100** on Paid, and higher limits can be requested. 2. The limit is **soft**: during network trouble Hyperdrive may open extra connections, and a client that cannot reach an existing pool can cause a new pool with its own allocation. 3. Configs add up. A cached and a cache-disabled config for each of three services means six pools against one database. 4. Hyperdrive does not limit client connections from Workers; what runs out is origin connections, held for a whole query or transaction. So the rule becomes: sum the limits of every config on the database, keep that sum comfortably below the database's own connection cap after its other clients, and prefer **fewer shared configs** (one cached, one fresh per database) over one pair per service. Accounts are also capped at 10 configs on Free and 25 on Paid, which favours sharing anyway. ## Usage rules that protect the pool - Create a client per request and never hold a driver pool at module scope. - Keep transactions short; no external calls inside `BEGIN` … `COMMIT`. - Do not hold clients open in long-lived Durable Objects without a clear need. - Place a Worker near the database only for requests that run several sequential queries. ## Operating it Decide who watches what. The analytics expose `cacheStatus` (hit ratio and why queries were uncacheable) and pool metrics such as open connections and waiting clients; a rising waiting-clients number or `Failed to acquire a connection from the pool.` errors mean the budget or the transaction habits need attention. Restarting a config's pool from the dashboard is a break-glass action after a failover, it drops in-flight queries and requires the Hyperdrive Admin role, so name who may do it. On the Free plan, remember that cached and uncached queries count equally toward the 100,000 daily queries. ## The trade-off in one line More caching means less database load and faster pages at the cost of bounded staleness; more configs mean cleaner separation at the cost of connection headroom. The policy should say, per read class and per database, which side the team chose and who revisits it.

  • The database allows 200 connections and other services use 80. How would you split Hyperdrive's share?
    Leave headroom for the soft limit and for maintenance, say a Hyperdrive total of around 80. With one cached and one cache-disabled config, give the cached config fewer connections, since hits never reach the database, and the cache-disabled config more, then adjust from pool metrics such as waiting clients.
  • When would you disable caching for a whole database instead of keeping two configs?
    When most reads must be fresh, for example an admin back office or a write-heavy workflow service. Two clients everywhere then add complexity for little gain, and a single cache-disabled config still gives edge connection setup and pooling.

  • Leave every config on the 60-second default; staleness is the cache's problem, not ours.
  • Hyperdrive's origin connection limit is a hard cap, so the sum can equal the database's maximum.
  • Two configs on the same database share one pool, so splitting costs no connections.
  • Give each service its own pair of configs; the database will scale its connections.
  • Cached queries do not count toward the Free plan's daily query allowance.