awesome-everything RU
↑ Back to the climb

Backend Architecture

Request lifecycle: code reading

Crux Read real handler, middleware, streaming, and deadline-propagation snippets, predict the lifecycle failure, and pick the highest-leverage fix a senior would make first.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

Lifecycle bugs are diagnosed in the code on the hot path and in the order things are wired. Read each snippet, predict where it fails in production, and choose the fix a senior engineer reaches for first.

Goal

Practise the loop you run in every lifecycle incident: read the wiring or the hot path, predict the stop that breaks, and reach for the highest-leverage fix — order, backpressure, serialization, or a propagated deadline.

Snippet 1 — the middleware chain

const app = express();

app.use(express.json());                 // body parser, no size limit
app.post("/admin/wipe", wipeHandler);     // <-- registered here
app.use(rateLimit({ max: 100 }));         // rate limiter
app.use(requireAuth);                     // auth: 401 if no valid token
app.use(errorHandler);                    // catches downstream throws
Quiz

Which problems does this registration order create, and what is the fix?

Snippet 2 — the streaming export

function exportCSV(rows, res) {
  res.setHeader("Transfer-Encoding", "chunked");
  for (const row of rows) {
    res.write(toCSVLine(row));   // return value ignored
  }
  res.end();
}
Quiz

This passes every test but OOMs in production under one slow client. What is the bug and the correct rewrite?

Snippet 3 — the response builder

app.get("/orders", async (req, res) => {
  const orders = await db.query("SELECT * FROM orders"); // unbounded
  res.status(200).json({ ok: true, data: orders });      // JSON.stringify
});
Quiz

As the orders table grows, this endpoint degrades and occasionally stalls all other requests. Which two lifecycle stops are at fault, and what is the fix?

Snippet 4 — the deadline that is not propagated

async function getProfile(userId) {
  // each client sets its own fixed 1s timeout; nothing is passed down
  const user = await usersClient.get(userId, { timeoutMs: 1000 });
  const prefs = await prefsClient.get(userId, { timeoutMs: 1000 });
  const feed = await feedClient.get(userId, { timeoutMs: 1000 });
  return { user, prefs, feed };
}
Quiz

The entry point promises a 1 s SLA, but this function can take ~3 s when downstreams are slow. Why, and what is the correct shape?

Recap

Every lifecycle incident is read in the wiring or the hot path: middleware registered after a route never runs, so order is a security boundary; a write() loop that ignores the false return OOMs under slow clients, so use pipeline(); an unbounded query plus generic JSON.stringify blocks the loop, so paginate and use a schema serializer; and fixed per-hop timeouts do not compose, so propagate a deadline with min(local, remaining). Diagnose the stop, apply the highest-leverage fix, then verify under the same load.

Continue the climb ↑Request lifecycle: instrument and harden a 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.