awesome-everything RU
↑ Back to the climb

Backend Architecture

Putting it together: code and incident reading

Crux Read real backend snippets — a blocked event loop, an unbounded pool acquire, a non-atomic idempotency check, and a breaker with no timeout — and pick the fix a senior would make first.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

Composed failures are diagnosed in code and logs, not in the abstract. Read each snippet, predict how it behaves under load, and choose the highest-leverage fix — the one that closes the interaction, not just the local symptom.

Goal

Practise the loop you run in every incident: read the hot path, spot which mechanism’s bad behaviour will feed the next, and reach for the fix that stops the cascade at its source.

Snippet 1 — the handler that blocks the loop

// Node.js, single event loop, thousands of concurrent requests
app.post("/report", (req, res) => {
  const body = req.body;                       // already parsed
  const hash = crypto.pbkdf2Sync(              // SYNCHRONOUS, ~80ms
    body.token, body.salt, 200000, 64, "sha512"
  );
  const pdf = renderPdfSync(body.rows);        // SYNCHRONOUS, ~120ms CPU
  res.send({ ok: true, digest: hash.toString("hex"), pdf });
});
Quiz

Under concurrent load this handler tanks p99 for every endpoint, not just /report. Why, and what is the highest-leverage fix?

Snippet 2 — the pool acquire with no bound

// Go: a fixed pool of 50 connections, no acquisition timeout
func (h *Handler) Charge(ctx context.Context, req ChargeReq) error {
    conn := h.pool.Acquire()          // blocks indefinitely if pool is empty
    defer h.pool.Release(conn)
    // provider call has no timeout of its own either
    resp, err := h.provider.Call(conn, req)
    if err != nil {
        return err
    }
    return h.repo.Save(conn, resp)
}
Quiz

The provider slows to 4s per call. What does this code do to the whole service, and what is the first fix?

Snippet 3 — the non-atomic idempotency check

-- Application does: check-then-insert, two separate statements
SELECT result FROM charges WHERE idempotency_key = $1;     -- step A: not found
-- ... handler proceeds to charge the card ...
INSERT INTO charges (idempotency_key, result) VALUES ($1, $2);  -- step B
Quiz

A client retries the same POST and two requests run concurrently. With this check-then-insert idempotency, what happens and how do you fix it?

Snippet 4 — the breaker that never trips

const breaker = new CircuitBreaker(callProvider, {
  errorThresholdPercentage: 50,   // open at 50% errors
  resetTimeout: 30000,            // half-open after 30s
  // no `timeout` option set — calls can hang indefinitely
});

async function charge(req) {
  return breaker.fire(req);       // awaits callProvider with no deadline
}
Quiz

The provider stops returning — calls hang for minutes rather than erroring. Why does this breaker fail to protect the service, and what is the fix?

Recap

Every snippet is one mechanism whose flaw becomes the next mechanism’s catastrophe: synchronous CPU on the event loop stalls all concurrency; an unbounded pool acquire turns a slow downstream into a total stall; a non-atomic idempotency check double-charges under a retry race; and a breaker with no call timeout stays blind to hung calls. The fix is always the one that stops the interaction at its source — move work off the loop, fail fast on acquire, enforce dedup atomically, and give the breaker a timeout — not a knob that defers or amplifies the problem.

Continue the climb ↑Putting it together: build a resilient backend service
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.