What is the purpose of the Cache-Control header in HTTP responses?
The Cache-Control header tells browsers and other clients how to store and reuse the response. It controls caching behavior like how long to keep the data or whether to cache it at all.
Click to reveal answer
beginner
Explain what an ETag header does in HTTP responses.
An ETag is a unique identifier for a specific version of a resource. It helps clients check if the resource has changed since the last request, enabling efficient caching and conditional requests.
Click to reveal answer
intermediate
What does the directive Cache-Control: no-cache mean?
It means the client must revalidate with the server before using a cached copy. The cached response can be stored but must be checked for freshness before reuse.
Click to reveal answer
intermediate
How does the client use the If-None-Match header with ETag?
The client sends If-None-Match with the ETag value it has. The server compares it to the current ETag. If they match, the server replies with 304 Not Modified, saving bandwidth.
Click to reveal answer
beginner
Why are Cache-Control and ETag headers important for REST APIs?
They improve performance by reducing unnecessary data transfer and server load. Cache-Control manages how responses are cached, and ETag helps detect changes to resources efficiently.
Click to reveal answer
What does the Cache-Control: max-age=3600 header instruct the client to do?
AImmediately revalidate the response
BNever cache the response
CCache the response forever
DCache the response for 3600 seconds before revalidating
✗ Incorrect
The max-age=3600 directive tells the client to consider the cached response fresh for 3600 seconds (1 hour) before checking with the server again.
Which header is used by the client to tell the server the version of the resource it has cached?
ACache-Control
BIf-None-Match
CETag
DLast-Modified
✗ Incorrect
If-None-Match is sent by the client with the ETag value to ask the server if the resource has changed.
What response status code does the server send if the resource has not changed when using ETag validation?
A200 OK
B404 Not Found
C304 Not Modified
D500 Internal Server Error
✗ Incorrect
The server sends 304 Not Modified to tell the client the cached version is still valid.
If a response has Cache-Control: no-store, what does it mean?
AThe response must not be cached anywhere
BThe response can be cached indefinitely
CThe response can be cached but must be revalidated
DThe response is private
✗ Incorrect
no-store means the response must not be stored in any cache.
Which header helps reduce bandwidth by avoiding sending unchanged resources?
AETag
BCache-Control
CContent-Type
DAuthorization
✗ Incorrect
ETag allows the server to tell the client if the resource has changed, avoiding resending unchanged data.
Describe how Cache-Control and ETag headers work together to improve API performance.
Think about caching rules and how the client checks if data changed.
You got /5 concepts.
Explain the difference between Cache-Control: no-cache and Cache-Control: no-store.
Focus on whether caching is allowed or not.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the Cache-Control header in an HTTP response?
easy
A. To indicate the response content type
B. To encrypt the response data for security
C. To specify the server's IP address
D. To tell browsers how long to keep the response data
Solution
Step 1: Understand the role of Cache-Control
The Cache-Control header instructs browsers and caches how long they can store the response data before requesting it again.
Step 2: Compare options with Cache-Control purpose
Only To tell browsers how long to keep the response data correctly describes this behavior. Other options describe unrelated functions.
Final Answer:
To tell browsers how long to keep the response data -> Option D
Quick Check:
Cache-Control = Duration for caching [OK]
Hint: Cache-Control controls data storage time in browsers [OK]
Common Mistakes:
Confusing Cache-Control with security headers
Thinking Cache-Control sets content type
Assuming Cache-Control reveals server info
2. Which of the following is the correct syntax to set a Cache-Control header to cache content for 3600 seconds?
easy
A. "Cache-Control: max-age=3600"
B. "Cache-Control: timeout=3600"
C. "Cache-Control: expires=3600"
D. "Cache-Control: duration=3600"
Solution
Step 1: Recall Cache-Control max-age syntax
The correct way to specify caching time in seconds is using max-age=seconds.
Step 2: Check each option's correctness
Only "Cache-Control: max-age=3600" uses max-age=3600, which is valid. Others use invalid directives.
Final Answer:
"Cache-Control: max-age=3600" -> Option A
Quick Check:
max-age sets cache time [OK]
Hint: Use max-age to set cache time in seconds [OK]
Common Mistakes:
Using 'timeout' instead of 'max-age'
Confusing Cache-Control with Expires header syntax
What happens when the browser sends a request with header If-None-Match: "abc123"?
medium
A. Server returns 304 Not Modified if content unchanged
B. Server returns full content ignoring ETag
C. Server returns 500 Internal Server Error
D. Server always returns 200 OK with new ETag
Solution
Step 1: Understand ETag and If-None-Match interaction
The ETag header is a unique identifier for the content version. The browser sends If-None-Match with the ETag value to check if content changed.
Step 2: Interpret Cache-Control and server response
With max-age=0 and must-revalidate, the browser asks server to validate content. If ETag matches, server responds with 304 Not Modified, saving bandwidth.
Final Answer:
Server returns 304 Not Modified if content unchanged -> Option A
Quick Check:
ETag + If-None-Match = 304 if unchanged [OK]
Hint: ETag + If-None-Match = 304 Not Modified if same [OK]
Common Mistakes:
Assuming server always sends full content
Confusing 304 with error codes
Ignoring Cache-Control directives
4. A developer sets the response header as Cache-Control: max-age=abc. What is the likely result?
medium
A. The browser caches the response for 'abc' seconds
B. The server returns a syntax error
C. The browser ignores the Cache-Control header and does not cache
D. The browser caches the response indefinitely
Solution
Step 1: Analyze max-age value validity
The max-age directive requires a numeric value in seconds. 'abc' is invalid.
Step 2: Understand browser behavior on invalid Cache-Control
Browsers typically ignore invalid Cache-Control headers and treat the response as non-cacheable.
Final Answer:
The browser ignores the Cache-Control header and does not cache -> Option C
Quick Check:
Invalid max-age causes no caching [OK]
Hint: max-age must be a number, else ignored [OK]
Common Mistakes:
Thinking server returns error for header syntax
Assuming browser caches with invalid value
Confusing max-age with other headers
5. You want to optimize a REST API to reduce bandwidth by sending full data only when it changes. Which combination of headers should you use in the response?
hard
A. Cache-Control: max-age=0 and no ETag
B. Cache-Control: no-cache and ETag with unique value
C. Cache-Control: no-store and no ETag
D. Cache-Control: max-age=3600 and no ETag
Solution
Step 1: Identify headers that enable conditional requests
ETag provides a unique identifier for content versions, allowing clients to ask if content changed.