0
0
Djangoframework~8 mins

Cache invalidation strategies in Django - Performance & Optimization

Choose your learning style9 modes available
Performance: Cache invalidation strategies
HIGH IMPACT
This affects page load speed by controlling how fresh cached content is served and how often the server recomputes or fetches data.
Serving dynamic content with cache that updates when data changes
Django
from django.core.cache import cache

def get_data():
    data = cache.get('my_data')
    if not data:
        data = expensive_db_query()
        cache.set('my_data', data)
    return data

def update_data(new_data):
    save_to_db(new_data)
    cache.delete('my_data')  # invalidate cache immediately
Invalidates cache immediately when data changes, ensuring fresh content without waiting for timeout.
📈 Performance GainReduces stale content delivery and avoids unnecessary cache expiration spikes.
Serving dynamic content with cache that updates when data changes
Django
from django.core.cache import cache

def get_data():
    data = cache.get('my_data')
    if not data:
        data = expensive_db_query()
        cache.set('my_data', data, timeout=3600)  # fixed 1 hour timeout
    return data
Using a fixed timeout can serve stale data for up to an hour even if the underlying data changes immediately.
📉 Performance CostCauses poor user experience due to stale content; may increase server load when cache expires simultaneously.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Fixed timeout cache invalidationN/AN/AMay delay fresh content load[X] Bad
Manual cache key deletionN/AN/AMultiple cache calls increase server load[!] OK
Batch cache key deletion (delete_many)N/AN/AFewer cache calls, consistent invalidation[OK] Good
Cache versioningN/AN/AAutomatic fresh content with minimal overhead[OK] Good
Rendering Pipeline
Cache invalidation affects how quickly fresh content is available to the browser, impacting the server response time and thus the browser's rendering start.
Server Response
Critical Rendering Path
Resource Fetching
⚠️ BottleneckServer Response time due to cache misses or stale data forcing recomputation
Core Web Vital Affected
LCP
This affects page load speed by controlling how fresh cached content is served and how often the server recomputes or fetches data.
Optimization Tips
1Avoid fixed timeout invalidation for frequently changing data.
2Use cache.delete_many to delete multiple related cache entries efficiently.
3Implement cache versioning to automatically handle data updates.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a downside of using a fixed timeout cache invalidation strategy?
AIt always deletes cache immediately after data changes.
BIt reduces server load by never recomputing data.
CIt can serve stale content until the timeout expires.
DIt automatically updates cache keys with data changes.
DevTools: Network
How to check: Open DevTools, go to Network tab, reload page, and check response headers for cache status (e.g., X-Cache or Age headers).
What to look for: Look for cache hits (fast responses) vs cache misses (longer server times) to verify cache effectiveness and invalidation.