Bird
Raised Fist0
FastAPIframework~10 mins

Request timing middleware in FastAPI - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Request timing middleware
Incoming HTTP Request
Middleware Start Timer
Pass Request to Endpoint
Endpoint Processes Request
Middleware Stops Timer
Add Timing Info to Response
Send Response Back to Client
The middleware starts a timer when a request arrives, lets the endpoint handle the request, then stops the timer and adds the elapsed time to the response.
Execution Sample
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)
    duration = time.time() - start
    response.headers["X-Process-Time"] = str(duration)
    return response
This middleware measures how long each request takes and adds that time in seconds to the response headers.
Execution Table
StepActionTimer Value (seconds)Response Header AddedNotes
1Request received by middlewarestart = current time (e.g. 100.0)NoTimer starts
2Middleware calls endpoint handlerstart = 100.0NoWaiting for endpoint response
3Endpoint processes requeststart = 100.0NoEndpoint runs user code
4Endpoint returns responsestart = 100.0NoResponse ready to send back
5Middleware calculates durationduration = current time - start (e.g. 0.123)NoElapsed time computed
6Middleware adds header 'X-Process-Time'duration = 0.123Yes: 'X-Process-Time': '0.123'Timing info added to response
7Response sent to clientduration = 0.123YesRequest cycle complete
💡 Request processing finished; response sent with timing header
Variable Tracker
VariableStartAfter Step 2After Step 5Final
startundefined100.0 (time at request start)100.0100.0
durationundefinedundefined0.123 (elapsed seconds)0.123
response.headers['X-Process-Time']undefinedundefinedundefined'0.123'
Key Moments - 2 Insights
Why do we call 'await call_next(request)' inside the middleware?
Because 'call_next(request)' runs the next step (the endpoint) and returns its response. We must wait for it to finish to measure total time, as shown in execution_table step 2 and 3.
When is the timing header added to the response?
After the endpoint finishes and returns the response, the middleware calculates duration and adds the header before sending back, as in execution_table step 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at step 5?
AThe endpoint processes the request
BThe middleware starts the timer
CThe middleware calculates how long the request took
DThe response is sent to the client
💡 Hint
Check the 'Action' and 'Timer Value' columns at step 5 in the execution_table
At which step is the 'X-Process-Time' header added to the response?
AStep 2
BStep 6
CStep 4
DStep 7
💡 Hint
Look at the 'Response Header Added' column in the execution_table
If the endpoint takes longer, how does the 'duration' variable change in variable_tracker?
AIt becomes larger
BIt becomes smaller
CIt stays the same
DIt becomes zero
💡 Hint
Duration measures elapsed time, so longer endpoint processing means larger duration value in variable_tracker
Concept Snapshot
Request timing middleware in FastAPI:
- Use @app.middleware("http") decorator
- Start timer before calling next handler
- Await call_next(request) to get response
- Calculate elapsed time after response
- Add timing info to response headers
- Return modified response
This tracks how long each request takes.
Full Transcript
This visual execution trace shows how FastAPI request timing middleware works. When a request arrives, the middleware records the current time. It then calls the next handler (the endpoint) and waits for it to finish. After the endpoint returns a response, the middleware calculates how much time passed. It adds this duration as a header named 'X-Process-Time' to the response. Finally, it sends the response back to the client. The execution table breaks down each step, showing when the timer starts, when the endpoint runs, and when the header is added. The variable tracker follows the timer start and duration values. Key moments clarify why we await the next handler and when the header is added. The quiz tests understanding of these steps. This middleware helps measure request processing time simply and clearly.

Practice

(1/5)
1. What is the main purpose of a request timing middleware in FastAPI?
easy
A. To convert JSON data to Python objects
B. To handle user authentication automatically
C. To serve static files faster
D. To measure how long each HTTP request takes to process

Solution

  1. Step 1: Understand middleware role

    Middleware runs code before and after each request to add extra features.
  2. Step 2: Identify timing middleware purpose

    Request timing middleware specifically measures the time taken to process requests.
  3. Final Answer:

    To measure how long each HTTP request takes to process -> Option D
  4. Quick Check:

    Request timing = measure duration [OK]
Hint: Middleware timing measures request duration [OK]
Common Mistakes:
  • Confusing timing middleware with authentication
  • Thinking it serves static files
  • Assuming it parses JSON data
2. Which of the following is the correct way to define a request timing middleware in FastAPI?
easy
A. @app.middleware('websocket')\nasync def timing_middleware(request, call_next):\n pass
B. @app.route('/middleware')\ndef timing_middleware(request):\n start = time.time()\n return 'Done'
C. @app.middleware('http')\nasync def timing_middleware(request, call_next):\n start = time.time()\n response = await call_next(request)\n duration = time.time() - start\n response.headers['X-Process-Time'] = str(duration)\n return response
D. def timing_middleware(request):\n start = time.time()\n return 'Middleware running'

Solution

  1. Step 1: Check middleware decorator and signature

    FastAPI HTTP middleware uses @app.middleware('http') and async def with (request, call_next).
  2. Step 2: Verify timing logic and response modification

    It records start time, awaits call_next(request), calculates duration, adds header, and returns response.
  3. Final Answer:

    Correct async HTTP middleware with timing and header addition -> Option C
  4. Quick Check:

    @app.middleware('http') + call_next + timing [OK]
Hint: Use @app.middleware('http') with async and call_next [OK]
Common Mistakes:
  • Using @app.route instead of @app.middleware
  • Missing async or call_next parameter
  • Using websocket middleware for HTTP requests
3. Given this middleware code snippet, what will be added to the response headers after a request is processed?
import time
from fastapi import FastAPI
app = FastAPI()

@app.middleware('http')
async def add_process_time_header(request, call_next):
    start_time = time.time()
    response = await call_next(request)
    process_time = time.time() - start_time
    response.headers['X-Process-Time'] = str(process_time)
    return response
medium
A. A header named 'Content-Length' with the size of the response
B. A header named 'X-Process-Time' with the request processing duration in seconds
C. A header named 'X-Request-ID' with a unique request identifier
D. No headers are added by this middleware

Solution

  1. Step 1: Analyze header addition in middleware

    The code adds 'X-Process-Time' header with the calculated process_time value.
  2. Step 2: Confirm header content meaning

    This header holds the duration in seconds the request took to process.
  3. Final Answer:

    A header named 'X-Process-Time' with the request processing duration in seconds -> Option B
  4. Quick Check:

    Header 'X-Process-Time' = duration seconds [OK]
Hint: Look for response.headers assignment for header name [OK]
Common Mistakes:
  • Confusing header names added by middleware
  • Assuming no headers are added
  • Thinking it adds request ID or content length
4. Identify the error in this FastAPI request timing middleware code:
import time
from fastapi import FastAPI
app = FastAPI()

@app.middleware('http')
def timing_middleware(request, call_next):
    start = time.time()
    response = call_next(request)
    duration = time.time() - start
    response.headers['X-Time'] = str(duration)
    return response
medium
A. Missing async keyword and missing await before call_next(request)
B. Using time.time() instead of datetime.now()
C. Response headers cannot be modified in middleware
D. Middleware should be defined with @app.route decorator

Solution

  1. Step 1: Check function signature and async usage

    Middleware must be async and await call_next(request) because call_next is async.
  2. Step 2: Identify missing await and async

    Code lacks async def and await, causing runtime errors.
  3. Final Answer:

    Missing async keyword and missing await before call_next(request) -> Option A
  4. Quick Check:

    Async + await call_next required [OK]
Hint: Middleware must be async and await call_next(request) [OK]
Common Mistakes:
  • Forgetting async keyword on middleware function
  • Not awaiting call_next(request)
  • Using wrong decorator like @app.route
5. You want to create a request timing middleware that logs the duration only if it exceeds 0.5 seconds. Which code snippet correctly implements this behavior?
hard
A. @app.middleware('http')\nasync def timing_middleware(request, call_next):\n start = time.time()\n response = await call_next(request)\n duration = time.time() - start\n if duration > 0.5:\n print(f'Request took {duration:.3f} seconds')\n return response
B. @app.middleware('http')\ndef timing_middleware(request, call_next):\n start = time.time()\n response = call_next(request)\n duration = time.time() - start\n if duration > 0.5:\n print('Slow request')\n return response
C. @app.middleware('http')\nasync def timing_middleware(request, call_next):\n response = await call_next(request)\n duration = time.time()\n if duration > 0.5:\n print('Request slow')\n return response
D. @app.middleware('http')\nasync def timing_middleware(request, call_next):\n start = time.time()\n response = await call_next(request)\n duration = start - time.time()\n if duration > 0.5:\n print('Request slow')\n return response

Solution

  1. Step 1: Confirm async middleware and await call_next

    Middleware must be async and await call_next(request) to work properly.
  2. Step 2: Check timing calculation and conditional logging

    Duration is end time minus start time; log only if duration > 0.5 seconds.
  3. Step 3: Verify correct duration calculation and print statement

    Code with start = time.time(), await call_next, duration = time.time() - start, if duration > 0.5: print(f'Request took {duration:.3f} seconds') correctly calculates duration and prints formatted message conditionally.
  4. Final Answer:

    Async middleware with correct timing and conditional logging if duration > 0.5s -> Option A
  5. Quick Check:

    Async + await + correct timing + conditional print [OK]
Hint: Use async, await, and check duration > 0.5 before logging [OK]
Common Mistakes:
  • Missing async or await in middleware
  • Calculating duration incorrectly (start - end)
  • Logging unconditionally or not at all