Middleware Pipeline

Middleware in RouteMQ provides a powerful way to process messages before they reach your route handlers. The middleware pipeline follows the familiar "onion" pattern where each middleware wraps the next layer.

Middleware Concept

┌─────────────────────────────────────────────────────────────┐
│                    Middleware Pipeline                      │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐         │
│  │    Auth     │  │ Rate Limit  │  │   Logging   │         │
│  │ Middleware  │  │ Middleware  │  │ Middleware  │         │
│  │     ┌───────┼──┼─────────────┼──┼─────────────┼───────┐ │
│  │     │       │  │             │  │             │       │ │
│  │     │       │  │             │  │             │       │ │
│  │     │   ┌───┼──┼─────────────┼──┼─────────────┼────┐  │ │
│  │     │   │   │  │             │  │             │    │  │ │
│  │     │   │   │  │             │  │             │    │  │ │
│  │     │   │   │  │             │  │             │    │  │ │
│  │     │   │   │  │             │  │ ┌───────────┼────┼──┼─┤
│  │     │   │   │  │             │  │ │           │    │  │ │
│  │     │   │   │  │             │  │ │  Handler  │    │  │ │
│  │     │   │   │  │             │  │ │           │    │  │ │
│  │     │   │   │  │             │  │ └───────────┼────┼──┼─┤
│  │     │   │   │  │             │  │             │    │  │ │
│  │     │   │   │  │             │  │             │    │  │ │
│  │     │   │   └──┼─────────────┼──┼─────────────┼────┘  │ │
│  │     │   │      │             │  │             │       │ │
│  │     │   └──────┼─────────────┼──┼─────────────┼───────┘ │
│  │     │          │             │  │             │         │
│  └─────┼──────────┼─────────────┼──┼─────────────┼─────────┘
│        │          │             │  │             │
│     Request    Request       Request          Request
│     ────────▶  ────────▶     ────────▶        ────────▶
│        │          │             │  │             │
│     Response   Response      Response         Response
│     ◀────────  ◀────────     ◀────────        ◀────────
└─────────────────────────────────────────────────────────────┘

Creating Middleware

Basic Middleware Structure

All middleware must extend the Middleware base class:

Middleware with Configuration

Middleware Registration

Route-Level Middleware

Apply middleware to specific routes:

Group-Level Middleware

Apply middleware to route groups:

Combined Middleware

Group middleware combines with route-specific middleware:

Built-in Middleware Examples

Authentication Middleware

Request ID Middleware

Validation Middleware

Middleware Execution Order

Chain Processing

Middleware executes in the order specified:

Early Termination

Middleware can stop processing by not calling next_handler:

Context Manipulation

Adding Data to Context

Middleware can add data for downstream processing:

Modifying Payload

Error Handling in Middleware

Graceful Error Handling

Error Recovery

Dependency Injection in Middleware

Access to Shared Resources

Performance Considerations

Async Operations

Always use async for I/O operations:

Memory Efficiency

Avoid storing large objects in context:

Testing Middleware

Unit Testing

Integration Testing

Best Practices

Middleware Design

  1. Single Responsibility: Each middleware should handle one concern

  2. Fail Fast: Validate early and return clear error messages

  3. Context Hygiene: Only add necessary data to context

  4. Async First: Use async/await for all I/O operations

  5. Error Handling: Handle errors gracefully and provide useful messages

Performance Guidelines

  1. Minimize Processing: Keep middleware logic lightweight

  2. Cache Results: Cache expensive operations when possible

  3. Connection Pooling: Reuse database and Redis connections

  4. Lazy Loading: Only load data when needed

Security Considerations

  1. Input Validation: Validate all input data

  2. Authentication: Verify user identity before processing

  3. Authorization: Check permissions for specific operations

  4. Rate Limiting: Prevent abuse and DoS attacks

  5. Logging: Log security-relevant events

Next Steps

Last updated