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

← All Web Framework Internals questions