Kayavak / The Backend / BE·00 Start Here: What a Backend Is / Deeper
Deeper · The Backend
Start Here: What a Backend Is
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?
The first web servers only sent files. To answer with something computed (search results, a shopping basket) servers needed to run a program per request: the Common Gateway Interface (NCSA, 1993) started a new process for each one, which was simple and slow. Java servlets (1997) kept a program running and handled each request on a thread, and enterprise Java (J2EE, 1999) added transactions, messaging and remote objects, at a large cost in complexity. Spring (2002–04), Rails (2004) and Django (2005) made server programming lighter; Node.js (2009) showed one thread with an event loop could serve many connections.
Underneath every one of those, the backend's job hasn't changed: receive a request over the network, apply the business's rules, read or change stored data, and answer, for many people at once, without losing anything.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
A backend is the part of a system you control. Clients are untrusted and unreliable; the backend is where rules are enforced, data is kept consistent, and work for many users is shared fairly.
| Question | Answer |
|---|---|
| What is it? | Long-running programs that answer requests over a network and own the system's data and rules |
| Problem it solves | One trustworthy place to check permissions, apply business rules and store data, for every client |
| Parts | A listener, middleware, routing, handlers, services with the business rules, repositories, the database, other services |
| How it works | Each request is parsed, checked, handled (often on its own thread) and answered with a status code and body |
| Assumptions | Handlers are stateless between requests; state lives in databases and caches |
| When they change | State kept in one server's memory breaks when you run two servers behind a load balancer |
| Where it fails | Slow dependencies, overload, partial failures, which most of this volume is about |
| Related ideas | HTTP and sockets (Foundations, ch09); threads (Foundations, ch08); databases (Databases) |
What if?
What if you trusted the client to enforce the rules?
Reason it through, then open
Anyone can send any request with a few lines of code or the browser's developer tools, bypassing your interface completely. Prices, permissions, limits and validation enforced only in the frontend are suggestions. The backend must re-check everything that matters, because it's the only code you actually control.
What if each server kept users' sessions in memory?
Reason it through, then open
With one server it works. With two behind a load balancer, a user's next request may land on the other one, which has never heard of them. Restarting a server logs everyone out. Stateless servers, with sessions in a shared store or in signed tokens, can be added, removed and restarted freely.
Where the boundary is
| Aspect | Frontend | Backend |
|---|---|---|
| Runs on | Users' devices | Machines you operate |
| Trusted? | No | Yes: the only place rules are enforced |
| Serves | One person | Everyone at once |
| Failure looks like | One broken screen | Everyone affected |
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| CGI started a process per request | Historical | NCSA's 1993 specification |
| Clients can't be trusted to enforce rules | Model | A security principle; every client can be modified or bypassed |
| Stateless handlers make scaling easier | Model | True in general; some systems (games, chat) need state close to users |
Still open
How much logic belongs on servers versus edge locations and clients keeps shifting with each generation of tools. The constant is that authoritative data and rules need one place to live.
What would change your mind? If clients could prove they ran unmodified code, some checks could move to them. Attestation technologies try this, with significant privacy costs.
Recall
Why must the backend re-check rules the frontend already checks?
Clients can be modified or bypassed; only the backend is under your control.
Why keep backend handlers stateless?
So any server can handle any request, and servers can be added, removed or restarted freely.
How did servlets improve on CGI?
They kept one program running and handled requests on threads instead of starting a process per request.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.