0
0
Flaskframework~8 mins

Response caching strategies in Flask - Performance & Optimization

Choose your learning style9 modes available
Performance: Response caching strategies
HIGH IMPACT
Response caching affects how fast the server can deliver content and reduces load on the server and network, improving page load speed.
Serving frequently requested data in a Flask app
Flask
from flask import Flask, jsonify, make_response
import time
app = Flask(__name__)
cache = {}

@app.route('/data')
def data():
    if 'result' in cache and time.time() - cache['time'] < 60:
        response = make_response(jsonify(cache['result']))
        response.headers['Cache-Control'] = 'public, max-age=60'
        return response
    result = expensive_computation()
    cache['result'] = result
    cache['time'] = time.time()
    response = make_response(jsonify(result))
    response.headers['Cache-Control'] = 'public, max-age=60'
    return response
Caches the result in memory and sets HTTP cache headers to allow browsers and proxies to reuse the response for 60 seconds.
📈 Performance GainReduces server CPU load and response time by serving cached data; saves 200+ ms per repeated request
Serving frequently requested data in a Flask app
Flask
from flask import Flask, jsonify
app = Flask(__name__)

@app.route('/data')
def data():
    # No caching, recomputes every request
    result = expensive_computation()
    return jsonify(result)
Every request triggers the expensive computation and database calls, increasing server response time and CPU load.
📉 Performance CostBlocks rendering for 200+ ms per request, increases server CPU usage linearly with requests
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
No caching, recompute every requestN/A (server-side)N/AIncreases LCP due to slow response[X] Bad
Server-side caching with Cache-Control headersN/A (server-side)N/AFaster LCP due to quick response[OK] Good
Rendering Pipeline
When response caching is used, the server can quickly return cached content without recomputing data, reducing server processing time and network latency. The browser can also reuse cached responses, skipping network requests.
Server Processing
Network Transfer
Browser Rendering
⚠️ BottleneckServer Processing time for generating dynamic content
Core Web Vital Affected
LCP
Response caching affects how fast the server can deliver content and reduces load on the server and network, improving page load speed.
Optimization Tips
1Cache expensive computations on the server to reduce response time.
2Use Cache-Control headers to enable browser and proxy caching.
3Set appropriate max-age values to balance freshness and performance.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main benefit of using server-side response caching in Flask?
ATriggers more DOM reflows in the browser
BReduces server processing time and speeds up response delivery
CIncreases the size of the HTML sent to the client
DDisables browser caching completely
DevTools: Network
How to check: Open DevTools, go to Network tab, reload the page, and inspect the response headers for Cache-Control and Age headers.
What to look for: Look for Cache-Control with max-age and Age header indicating cached response; faster response times indicate good caching.