0
0
FastAPIframework~8 mins

Request timing middleware in FastAPI - Performance & Optimization

Choose your learning style9 modes available
Performance: Request timing middleware
MEDIUM IMPACT
This affects the server response time and perceived page load speed by measuring how long each request takes to process.
Measuring request processing time without blocking response
FastAPI
from fastapi import FastAPI, Request
import time
import asyncio

app = FastAPI()

@app.middleware("http")
async def timing_middleware(request: Request, call_next):
    start = time.time()
    response = await call_next(request)
    end = time.time()
    duration = end - start
    # Schedule logging asynchronously without blocking
    asyncio.create_task(async_log(duration))
    return response

async def async_log(duration: float):
    print(f"Request took {duration:.4f} seconds")
Logging is done asynchronously after response is sent, so it does not delay the client.
📈 Performance GainNon-blocking logging saves ~1-5ms per request, improving server response time
Measuring request processing time without blocking response
FastAPI
from fastapi import FastAPI, Request
import time

app = FastAPI()

@app.middleware("http")
async def timing_middleware(request: Request, call_next):
    start = time.time()
    response = await call_next(request)
    end = time.time()
    duration = end - start
    # Blocking logging inside middleware
    print(f"Request took {duration:.4f} seconds")
    return response
Logging inside middleware blocks the response until logging completes, adding latency to every request.
📉 Performance CostBlocks response for logging duration, increasing server response time by ~1-5ms per request
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Blocking synchronous logging in middleware000[X] Bad
Asynchronous logging after response000[OK] Good
Rendering Pipeline
Request timing middleware runs during the server's request handling phase before sending the response. It measures processing time but does not affect browser rendering directly.
Server Processing
Response Delivery
⚠️ BottleneckBlocking synchronous operations inside middleware delay response delivery.
Core Web Vital Affected
LCP
This affects the server response time and perceived page load speed by measuring how long each request takes to process.
Optimization Tips
1Avoid blocking operations inside request timing middleware to keep server response fast.
2Use asynchronous tasks for logging or heavy work after sending the response.
3Monitor server response times in DevTools Network tab to detect middleware delays.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of synchronous logging inside request timing middleware?
AIt blocks the response, increasing server response time.
BIt causes layout shifts in the browser.
CIt increases the bundle size significantly.
DIt reduces the number of DOM nodes.
DevTools: Network
How to check: Open DevTools, go to Network tab, reload the page, and check the Time column for server response duration.
What to look for: Look for lower server response times indicating faster middleware processing.