0
0
Flaskframework~8 mins

Before_request as middleware alternative in Flask - Performance & Optimization

Choose your learning style9 modes available
Performance: Before_request as middleware alternative
MEDIUM IMPACT
This affects the request handling speed and server response time by controlling when and how middleware logic runs before each request.
Running code before each request to check authentication
Flask
from flask import Flask, request
app = Flask(__name__)

@app.before_request
def fast_check():
    auth = request.headers.get('Authorization')
    if not auth:
        return 'Unauthorized', 401
The check is immediate and non-blocking, allowing the server to respond quickly without delay.
📈 Performance GainReduces request blocking time from 500ms to near 0ms, improving INP significantly.
Running code before each request to check authentication
Flask
from flask import Flask, request
app = Flask(__name__)

@app.before_request
def heavy_check():
    # Simulate heavy processing
    import time
    time.sleep(0.5)  # blocks request handling
    if not request.headers.get('Authorization'):
        return 'Unauthorized', 401
The before_request function blocks the request handling for 0.5 seconds every time, delaying response start.
📉 Performance CostBlocks request handling for 500ms per request, increasing INP and slowing server response.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Heavy blocking code in before_requestN/A (server-side)N/AN/A[X] Bad
Lightweight immediate checks in before_requestN/A (server-side)N/AN/A[OK] Good
Rendering Pipeline
The before_request function runs before the main request handler, affecting how fast the server can start processing and sending a response. Heavy or blocking operations here delay the entire request lifecycle.
Request Handling
Response Start
⚠️ BottleneckBlocking or slow operations inside before_request delay response start and increase input latency.
Core Web Vital Affected
INP
This affects the request handling speed and server response time by controlling when and how middleware logic runs before each request.
Optimization Tips
1Avoid heavy or blocking operations inside before_request functions.
2Keep before_request logic simple and fast to improve server response time.
3Use asynchronous or deferred processing for expensive tasks outside before_request.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of using a heavy before_request function in Flask?
AIt causes layout shifts on the client side.
BIt increases the size of the HTML sent to the browser.
CIt blocks the server from starting the response quickly, increasing input latency.
DIt slows down CSS rendering in the browser.
DevTools: Network
How to check: Open DevTools Network tab, reload the page, and check the Time to First Byte (TTFB) and response start times for requests.
What to look for: Long TTFB or delayed response start indicates slow server-side middleware like before_request blocking the response.