Skip to the content.
Designing Request-Handler Layouts in Python | AI Systems Design From Scratch

Connect with Amin Boulouma Official

🏠 Documentation Hub πŸ“ Engineering Blog πŸ’» GitHub Repository

Designing Request-Handler Layouts

Amin Boulouma, Software Engineer

In Python web development, mapping incoming HTTP requests to specific logic is the backbone of any framework. Using a dictionary-based routing table, specifically structured as self._routes[HTTP_METHOD][URL_PATH] = Handler_Callback, is a highly efficient and performant way to manage application traffic.

The Routing Architecture

This layout treats your application state as a nested map. This structure allows for $O(1)$ constant-time lookup complexity when dispatching requests.

Implementation Example

By organizing routes this way, you decouple the routing logic from the business logic, allowing your handlers to remain clean and focused.

class Router:
    def __init__(self):
        # Structure: { METHOD: { PATH: CALLBACK } }
        self._routes = {
            "GET": {},
            "POST": {},
        }

    def add_route(self, method, path, callback):
        self._routes[method.upper()][path] = callback

    def dispatch(self, method, path):
        # O(1) lookup
        handler = self._routes.get(method.upper(), {}).get(path)
        if handler:
            return handler()
        return "404 Not Found"

# Usage
router = Router()
router.add_route("GET", "/index", lambda: "Hello World")

print(router.dispatch("GET", "/index"))

Strategic Advantages

  1. High-Speed Dispatch: By using dictionary nesting, you avoid iterating through lists of regular expressions, which is essential for low-latency systems.
  2. Type Safety and Clarity: Explicitly defining the method (GET/POST) as a primary key prevents logic errors where a POST request might accidentally trigger a GET handler.
  3. Extensibility: You can easily extend this pattern to support dynamic path variables (e.g., /user/{id}) by adding a secondary lookup mechanism for regex patterns when the exact path isn’t found.

Routing Pattern Comparison

Structure Lookup Complexity Maintainability Best For
Nested Dict ([M][P]) $O(1)$ High High-performance APIs
Linear List/Regex $O(N)$ Moderate Complex routing requirements
If/Else Ladder $O(N)$ Low Very small prototypes

Best Practices

Connect with Amin Boulouma Official