Strong Caching and Conditional Caching
Strong Caching
Definition: Strong caching refers to the browser's ability to fetch resources directly from the local cache without any interaction with the server during the cache validity period.
Characteristics:
- Cache Control Headers:
Cache-Control: max-age=<seconds>
: Specifies the maximum duration (in seconds) the resource can be cached.Expires: <date>
: Specifies the expiration date and time for the resource.
- Behavior:
- No Server Interaction: During the cache validity period, the browser retrieves the resource from the local cache without contacting the server.
- Improved Performance: Significantly improves performance since no server requests are made.
- Suitable For: Static resources that do not change frequently, such as images, CSS files, and JavaScript files.
Example Response Headers:
HTTP/1.1 200 OK
Cache-Control: max-age=3600
Expires: Wed, 21 Oct 2025 07:28:00 GMT
Content-Type: application/json
{ "id": 1, "name": "John Doe" }
Conditional Caching
Definition: Conditional caching, also known as negotiated caching, involves the browser sending requests to the server to validate cached resources. The server determines whether the cached resource is still valid.
Characteristics:
- Cache Control Headers:
ETag
: A unique identifier for the resource.Last-Modified
: The date and time when the resource was last modified.
- Request Headers:
If-None-Match
: Sent by the browser, containing the ETag value to validate the resource.If-Modified-Since
: Sent by the browser, containing the last modified date to validate the resource.
- Behavior:
- Server Interaction: Each request involves server interaction to validate the resource.
- Freshness Check: If the resource has not changed, the server responds with
304 Not Modified
, allowing the browser to use the cached version. - Performance: Slightly lower performance compared to strong caching due to server validation.
- Suitable For: Resources that change frequently but still need to be cached.
Example Request and Response Headers: First Request Response:
HTTP/1.1 200 OK
ETag: "abc123"
Last-Modified: Tue, 20 Apr 2023 07:28:00 GMT
Content-Type: application/json
{ "id": 1, "name": "John Doe" }
Subsequent Request:
GET /resource HTTP/1.1
Host: example.com
If-None-Match: "abc123"
If-Modified-Since: Tue, 20 Apr 2023 07:28:00 GMT
Server Response:
HTTP/1.1 304 Not Modified