The BackendDeeper

Kayavak / The Backend / BE·44 Backend Fundamentals / Deeper

Deeper · The Backend

Backend Fundamentals

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

In 1999 Dan Kegel posed the C10k problem: how can one server handle ten thousand simultaneous clients? With a thread or process per connection, memory and context switching ran out long before that. The answers shaped backend frameworks for two decades: event loops on non-blocking sockets (epoll, kqueue), used by nginx (2004), Node.js (2009) and Netty; lightweight threads in the runtime (Erlang's processes, Go's goroutines); and now Java's virtual threads, which bring the cheap-thread approach to the JVM.

Meanwhile the shape of a backend converged across languages: a listener, a chain of middleware, a router, handlers, services holding business rules, and repositories talking to storage. Spring, ASP.NET Core, Express, Django, Gin and Axum differ in style and agree on that structure, because it separates the HTTP concerns from the business from the storage.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

A request passes through layers, each with one job: protocol at the edge, cross-cutting concerns in middleware, business rules in services, storage behind repositories. Keep each layer ignorant of the ones beyond its neighbours.

Question Answer
What is it? The common architecture and runtime models of server applications
Problem it solves Handling many concurrent requests correctly, with code that stays organised as it grows
Parts Listener, middleware/filters, routing, handlers, services, repositories, configuration, health checks, graceful shutdown
How it works Each request is handled on a thread, task or event-loop callback; layers transform and pass it along
Assumptions Handlers don't block event-loop threads; business rules don't depend on HTTP details
When they change One blocking call on an event loop stalls every connection it serves
Where it fails Shutdown that drops in-flight requests; error responses that leak internals; layers that bypass each other
Related ideas I/O models (Foundations, ch08); concurrency (ch38); API design (ch45)

What if?

What if a server stopped instantly on every deploy?

Reason it through, then open

Requests in progress would be cut off mid-response; clients would see errors or, worse, not know whether a payment went through. Graceful shutdown stops accepting new connections, tells the load balancer it's leaving, lets in-flight requests finish within a deadline, then closes resources. Rolling deploys depend on it.

What if business rules lived in controllers?

Reason it through, then open

The same rule would be copied into every entry point (HTTP, a message consumer, a scheduled job) and drift apart; tests would need HTTP requests to check arithmetic. Services keep rules in one place that every entry point calls, and can be tested directly.

Where the boundary is

Aspect Thread per request (incl. virtual threads) Event loop
Code style Sequential, blocking calls Callbacks, promises, reactive streams
One slow call Blocks only its own thread Blocks everything on that loop, if it blocks
Debugging Normal stack traces Fragmented stacks
Examples Spring MVC, Go, Java with virtual threads Node.js, Netty, WebFlux, Vert.x

How sure are we?

Claim How it's known Where from
The C10k problem was posed by Dan Kegel in 1999 Historical His web page of that name
Backend frameworks share the same layered shape Model Observed across frameworks; details vary
Virtual threads handle many blocking connections cheaply Measured JEP 444 and the volume's labs

Still open

With virtual threads, whether reactive programming on the JVM still pays for its complexity is actively debated; it keeps an edge for streaming and backpressure.

What would change your mind? If an event-loop service and a virtual-thread service showed no difference in throughput or latency for your workload, choose the simpler code.

Recall

What was the C10k problem?

Serving ten thousand simultaneous clients when a thread or process per connection was too expensive.

What does graceful shutdown do?

Stops accepting new work, finishes in-flight requests within a deadline, then closes resources.

Why keep business rules out of controllers?

So every entry point shares one tested implementation instead of drifting copies.

What's the danger of blocking on an event loop?

It stalls every connection served by that loop.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.