Server racks in a data center, representing database reliability and recovery.
← Back to Blog

Database Circuit Breaker, Simply Explained

A tiny mental model for protecting your app when the database is slow or failing.

May 11, 20262 min read

The Simple Idea

A database circuit breaker is a small safety switch for your app.

When the database keeps failing, the app stops hitting it for a short time.

Why It Helps

Without it, every request keeps pushing the already tired database.

With it, your app fails fast, protects the database, and gets time to recover.

How To Think About It

Closed means normal: send requests to the database.

Open means pause: do not call the database for a few seconds.

Half-open means test: allow one request and see if the database is healthy again.

Where To Use It

Use it around risky calls like login, checkout, search, reports, or heavy dashboard queries.

Do not hide the problem forever. Log it, alert on it, and return a friendly fallback when possible.

src/lib/db-circuit-breaker.ts
let openUntil = 0;async function getUser(id: string) {  if (Date.now() < openUntil) {    return { ok: false, reason: "DB is cooling down" };  }  try {    return { ok: true, user: await db.user.findUnique({ where: { id } }) };  } catch {    openUntil = Date.now() + 10_000;    return { ok: false, reason: "DB failed, try again soon" };  }}