Chain of Responsibility
Intermediate · 15 min read · Low-Level Design, Behavioral Patterns
Passing a request along a chain of handlers.
The idea in plain words
A request passes along a line of handlers. Each handler either deals with it or hands it to the next one. The sender only talks to the first handler and never needs to know who finally handled it.
The problem
An API request must pass several checks before reaching business logic: authentication, rate limiting, input validation, caching, then the actual handler. If the controller does all of this inline, it grows into a tangle of nested ifs; reordering checks or reusing them for another endpoint means copying code.
The solution
Turn each step into a handler object with a common interface and a reference to the next handler. The client sends the request to the first handler; the chain does the rest.
Design decisions
Where you've seen it
Use in LLD case studies
Pros and cons
Comparisons
Interview questions
- How is Chain of Responsibility different from Decorator?
- Design an ATM's cash dispenser for ₹2000, ₹500 and ₹100 notes.
Further reading
Key takeaways
- Chain of Responsibility passes a request through handlers that may process it, forward it, or stop it.
- Two common forms: middleware pipelines (each handler acts and forwards) and first-capable-wins chains.
- Decide what happens if no handler handles the request, and build chains in one place.
- Servlet filters, Spring Security and HTTP interceptors are everyday examples.