performance · queue · ecowell · database · system-design · reliability

Good Queue: Putting a Front Door in Front of the Database

2 December 2026 · 3 min read

Our API was slow while its CPU sat idle (see the earlier latency write-up). Almost all of a request’s time was spent waiting on the hosted database over the network. When many people used the system at once, requests piled up behind each other with no order, no limit, and no way to tell a person who was waiting from a background job that could wait for hours.

Requests lining up in order at a single gate to the database

Animated demo: ducks standing for requests wait in line and pass a gate one at a time on their way to the database

Good Queue is our answer: a gateway that every database call must pass through. It does not make the database faster. It makes the waiting fair, bounded and visible.

The core idea

Every call goes through one function:

dbGate.run({ lane: 'interactive', deadlineMs: 8000, fn: () => db.query(...) })

That single choke point lets us enforce four things in one place.

1. Lanes

  • interactive: a person is looking at a spinner.
  • background: notification fan-out, reports, polling.

Interactive work goes first. Background work is capped to a share of the slots, so a heavy report cannot starve a customer opening their booking.

2. A fixed number in flight

Only N calls (default 16, tunable by environment variable) talk to the database at once. The rest wait in FIFO order within their lane. Without a cap, 50 users meant hundreds of simultaneous calls all slowing each other down. With a cap, each call finishes faster and the line moves predictably.

3. Deadlines

Every call carries a deadline (8 s interactive, 30 s background). If a request has already waited past its deadline before it starts, we drop it instead of sending it to the database, because nobody is still waiting for that answer.

4. Shedding, loudly

If a lane’s queue is full, we fail immediately with an honest 503 QUEUE_BUSY and a Retry-After hint. A fast, truthful “busy” beats a 30-second hang that ends in a timeout.

The rule that overrides all of it

The owner set one rule on day one of this work, and it shaped every design choice:

No data loss. Sales, accounts, installation, inventory, service and customer data must never be lost, dropped, duplicated or silently changed. We can be slower. We cannot be wrong.

Concretely, this means the “drop it” tools above apply only to reads:

  • A write the server accepted is committed, or the user is clearly told it failed. We never shed or expire a write after it started.
  • We never batch or coalesce writes across users.
  • Writes retry automatically only with an idempotency key, so a retry can never double-record a payment.
  • Work created by a write (notifications, audit rows, PDFs, messages) is saved in the same transaction as the write (an outbox). A crash cannot leave a payment recorded with its notification forgotten.
  • Caches may serve slightly stale reads, but every write invalidates them, and writes carry an expected version so a stale read cannot overwrite newer data.
  • Summary tables are derived. They are never the source of truth.

Shedding is acceptable for a dashboard refresh. It is never acceptable for “Confirm payment”.

Tricks that only apply to reads

  • Batching: identical reads arriving within a few milliseconds share one query, and many per-id lookups become one in (...) query (the DataLoader pattern).
  • Auto-retry once on transient failures (timeout, connection reset, 502/503) with a little random jitter, so a hiccup does not reach the user.
  • Short caching for authorised, branch-filtered reads, invalidated on writes.

How you know it is working

A super-admin-only metrics endpoint exposes in-flight count, queue length per lane, wait-time percentiles, batch hit ratio, shed count and retry count. If you cannot see the queue, you cannot trust it.

Takeaways

  1. A queue in front of a slow dependency turns unbounded waiting into bounded, explainable waiting.
  2. Separate people from background work, or background work will eventually hurt people.
  3. Decide up front which operations may be dropped (reads) and which may never be (writes). Write the rule down before writing code.
  4. Fail fast and honestly. A clear “busy, retrying” message earns more trust than a spinner that never ends.

AI use: Written with AI assistance from my own notes and project facts, then reviewed by me. Images are AI-generated. How I use AI

Loading reactions…

Working on something similar? Let's talk.

Same problem, a question about this article, a collaboration, or an open-source project you would like me to contribute to. Pick whichever door you like: