What is Caching
Caching is a mechanism that temporarily stores previously retrieved data and reuses it for subsequent accesses. This significantly improves performance.
Benefits of Caching: Reduced response times, lower server load, bandwidth savings, improved user experience
Types and Layers of Cache
- Browser Cache (Client-side)
- CDN Cache (Edge)
- Reverse Proxy Cache
- Application Cache
Cache-Control Headers
| Directive | Description |
|---|---|
max-age=seconds | Specifies cache expiration in seconds |
s-maxage=seconds | Expiration for shared caches |
no-cache | Must validate before use |
no-store | Do not cache |
private | Only browser can cache |
public | Can be cached anywhere |
immutable | Content will not change |
Common Patterns
# Static assets (cache for 1 year)
Cache-Control: public, max-age=31536000, immutable
# HTML pages (do not cache)
Cache-Control: no-cache, no-store, must-revalidate
# API responses (cache for 5 minutes)
Cache-Control: private, max-age=300
Cache Busting
<!-- Include hash in filename -->
<link rel="stylesheet" href="/css/styles.a1b2c3d4.css">
<script src="/js/app.e5f6g7h8.js"></script>
Summary
Caching is a crucial technology for dramatically improving web performance. With an appropriate caching strategy, you can achieve both improved user experience and reduced server costs.
← Back to list