Web Framework Internals interview question · Most asked
What is the middleware 'onion' model?
Summary
Each middleware wraps the rest of the pipeline: it runs code on the way in, calls the next layer, then runs code on the way out with the response. Request-phase code runs in registration order and response-phase code in reverse order. Laravel, Django, Express/Koa, Starlette, ASP.NET Core and PSR-15 middleware all follow it.
Longer Answer
The model explains why a logging middleware placed outermost sees every response (including errors produced inside), why middleware can short-circuit by returning a response without calling the next layer, and why order determines what each middleware can see.
Key Points
- Wraps the next layer: in, next, out.
- In: registration order; out: reverse.
- Can short-circuit.
- Order defines visibility.
Deep dive: Request Lifecycle: The Django Request Lifecycle and Middleware Order
Explanation
What interviewers look for
Clear explanation with an ordering consequence.
More most-asked Web Framework Internals questions
- What is a request lifecycle, and why should a backend engineer know it for their framework?
- What layers does a request pass through before reaching application code, and how should their timeouts relate?
- Where should authentication, authorisation, validation and error formatting live in a web framework?
- Walk through the Laravel request lifecycle and say where you would add code that must run on every request.
- How does Express handle a request, and how are errors routed?
- What is the NestJS request lifecycle?
- Describe the Spring MVC request flow and where exceptions are handled.
- Walk through the Django request lifecycle.