0
0
FastAPIframework~8 mins

Custom middleware creation in FastAPI - Performance & Optimization

Choose your learning style9 modes available
Performance: Custom middleware creation
MEDIUM IMPACT
This affects the request-response cycle speed and server throughput by adding processing steps before and after handling requests.
Adding custom logic to process requests and responses
FastAPI
from fastapi import FastAPI
from starlette.middleware.base import BaseHTTPMiddleware
import asyncio

app = FastAPI()

class EfficientMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request, call_next):
        # Non-blocking async processing
        await asyncio.sleep(0.1)  # does not block event loop
        response = await call_next(request)
        return response

app.add_middleware(EfficientMiddleware)
Uses async sleep to avoid blocking event loop, allowing other requests to be processed concurrently.
📈 Performance GainNon-blocking delay improves throughput and reduces request latency under load.
Adding custom logic to process requests and responses
FastAPI
from fastapi import FastAPI
from starlette.middleware.base import BaseHTTPMiddleware

app = FastAPI()

class SlowMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request, call_next):
        # Heavy synchronous processing
        import time
        time.sleep(0.1)  # blocks event loop
        response = await call_next(request)
        return response

app.add_middleware(SlowMiddleware)
Blocking the event loop with synchronous sleep delays all requests, reducing throughput and increasing latency.
📉 Performance CostBlocks event loop for 100ms per request, increasing response time and reducing concurrency.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Blocking synchronous middlewareN/AN/AN/A[X] Bad
Async non-blocking middlewareN/AN/AN/A[OK] Good
CPU-heavy synchronous middlewareN/AN/AN/A[X] Bad
CPU work offloaded to thread poolN/AN/AN/A[OK] Good
Rendering Pipeline
Middleware runs during the server's request-response cycle before and after the main handler. It adds processing steps that can delay response generation.
Request Handling
Response Generation
⚠️ BottleneckSynchronous or CPU-heavy middleware blocks the event loop, delaying all requests.
Optimization Tips
1Avoid synchronous blocking code in middleware to keep the event loop free.
2Use async/await and non-blocking operations inside middleware.
3Offload CPU-intensive tasks to threads or background workers.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of using synchronous blocking code in FastAPI middleware?
AIt blocks the event loop, increasing request latency and reducing throughput.
BIt increases the size of the response payload.
CIt causes layout shifts in the browser.
DIt improves CPU utilization.
DevTools: Performance
How to check: Record a server request in DevTools Performance panel, look for long tasks blocking the main thread or event loop.
What to look for: Long blocking tasks or delays before response indicate middleware blocking; short async tasks indicate good performance.