HTTP Caching
Fundamentals
-
What is HTTP caching?
HTTP caching is a mechanism that stores previously retrieved resources so they can be reused for subsequent requests.
Benefits:
- Faster response times
- Reduced server load
- Reduced network traffic
- Improved application performance
-
How does HTTP caching work?
HTTP caching uses response headers to determine how resources should be stored, reused and validated.
The primary caching headers are:
Cache-ControlExpiresETag
Types of Caching
-
What are the different types of caching?
Type Description Client-side Stores resources on the user’s device. Server-side Stores reusable resources on the server. Shared Allows multiple components or users to benefit from a common cache.
HTTP Caching Headers
-
What is Cache-Control?
The
Cache-Controlheader defines caching rules for HTTP requests and responses.Directive Description max-ageSpecifies how long a response remains fresh. no-cacheRequires validation before reusing a cached response. no-storePrevents the response from being stored. must-revalidateRequires validation before reusing a stale response. -
What is the Expires header?
The
Expiresheader specifies a date and time after which a cached response becomes stale. -
What is an ETag?
An Entity Tag (ETag) identifies a particular version of a resource. It allows clients to validate cached responses without unnecessarily downloading the resource again.
Caching Strategies
-
What are the different caching strategies?
Freshness-based caching
Resources are reused for a specified period before becoming stale.
Validation-based caching
The client checks with the server whether its cached resource is still valid.
Fallback-based caching
A default or alternative resource is served when the requested cached resource is unavailable.
Performance Optimization
-
How can caching performance be improved?
- Consider resource sizes and cache storage capacity.
- Avoid unnecessary cache evictions.
- Use Content Delivery Networks (CDNs) to serve resources closer to users.
- Select appropriate caching headers and expiration policies.
Reference
HTTP Caching by Code with Sloba (codewithsloba.com).