Backend Development › Backend Basics
Request Lifecycle
What happens between a request arriving and a response leaving.
A request goes through several steps between reaching your server and the response leaving it. The exact order depends on the framework, but it usually looks like this:
- The web server accepts the connection and parses the HTTP request.
- Middleware runs: logging, authentication, parsing the body and similar checks.
- The router matches the method and path to a handler.
- The handler runs. It validates input, applies business logic, queries the database or calls other services.
- The response is built and sent back to the client.
request -> web server -> middleware -> router -> handler -> database
|
response <-----------------------------------------+
Knowing the steps tells you where to look when something is slow or wrong. Timing or logging each step shows whether the time goes to the database, an external call or your own code.
The classic mistake is doing slow work inline, such as sending an email or calling a third-party API, so the user waits for it. Move that work into a background job or a queue, as covered in background jobs and message queues, and respond as soon as the essential work is done. The request handler is the step where most of your own code runs.