What if your website could magically skip sending the same data twice?
Why Last-Modified and If-Modified-Since in Rest API? - Purpose & Use Cases
Imagine you run a website that shows news articles. Every time a visitor opens your page, their browser asks your server for the full list of articles, even if nothing has changed since their last visit.
This means your server sends the same data again and again, wasting time and internet data. Visitors wait longer, and your server works harder for no good reason.
Using Last-Modified and If-Modified-Since headers, the server tells the browser when the content was last changed. The browser then asks only if the content is newer than what it has. If not, the server replies: "Nothing new," saving time and data.
GET /news // Server sends full news list every time
GET /news If-Modified-Since: Wed, 21 Oct 2020 07:28:00 GMT // Server replies 304 Not Modified if no changes
This makes websites faster and lighter by avoiding sending unchanged data repeatedly.
When you refresh your favorite blog, your browser uses these headers to check if new posts exist. If not, it loads instantly without downloading everything again.
Manual repeated data sending wastes time and bandwidth.
Last-Modified and If-Modified-Since let servers and browsers talk efficiently.
This improves speed and reduces unnecessary data transfer.
Practice
Last-Modified header in a REST API response?Solution
Step 1: Understand the role of Last-Modified header
TheLast-Modifiedheader indicates the date and time when the resource was last changed on the server.Step 2: Compare options with the header's purpose
Only To tell the client when the resource was last changed correctly describes this purpose. Other options describe unrelated information.Final Answer:
To tell the client when the resource was last changed -> Option AQuick Check:
Last-Modified = resource last change time [OK]
- Confusing Last-Modified with Content-Length
- Thinking it shows server current time
- Mixing it up with Content-Type header
If-Modified-Since header in an HTTP request?Solution
Step 1: Recall HTTP header date format
HTTP headers use the RFC 1123 date format likeSun, 01 Jan 2023 12:00:00 GMTfor date/time values.Step 2: Match options to correct format
If-Modified-Since: Sun, 01 Jan 2023 12:00:00 GMT matches the correct header syntax and date format. Other options use invalid syntax or date formats.Final Answer:
If-Modified-Since: Sun, 01 Jan 2023 12:00:00 GMT -> Option DQuick Check:
If-Modified-Since uses RFC 1123 date format [OK]
- Using ISO 8601 instead of RFC 1123 format
- Using '=' instead of ':' in header
- Using arrow '->' instead of colon
Last-Modified: Wed, 10 May 2023 10:00:00 GMT, what will the server respond if the client sends If-Modified-Since: Wed, 10 May 2023 09:00:00 GMT?Solution
Step 1: Compare If-Modified-Since with Last-Modified
The client'sIf-Modified-Sincedate is earlier (09:00) than the server'sLast-Modifieddate (10:00).Step 2: Determine server response based on dates
Since the resource was modified after the client's date, the server sends full data with HTTP 200 OK.Final Answer:
HTTP 200 OK with resource data -> Option CQuick Check:
If-Modified-Since < Last-Modified -> 200 OK [OK]
- Thinking server sends 304 when resource changed after client date
- Confusing 400 Bad Request with conditional headers
- Assuming server error on date mismatch
If-Modified-Since: Tue, 09 May 2023 10:00:00 GMT but the server always responds with HTTP 200 OK and full data, ignoring the header. What is the likely error in server handling?Solution
Step 1: Understand expected server behavior
The server should compareIf-Modified-Sincewith resource'sLast-Modifiedand respond 304 if not changed.Step 2: Identify why server always sends 200 OK
If server ignores the header and always sends full data, it likely does not check theIf-Modified-Sinceheader properly.Final Answer:
Server does not check If-Modified-Since header before sending data -> Option AQuick Check:
Ignoring If-Modified-Since means always 200 OK [OK]
- Blaming client for server ignoring header
- Thinking wrong Last-Modified format causes 200 OK
- Confusing Content-Type header with caching headers
Last-Modified and If-Modified-Since. Which approach correctly implements this optimization?Solution
Step 1: Understand how Last-Modified and If-Modified-Since work together
The server sendsLast-Modifiedheader with the resource's last change date. The client sendsIf-Modified-Sinceheader on next request to ask if resource changed since that date.Step 2: Implement correct server logic for optimization
The server comparesIf-Modified-Sincewith resource's last modified date and returns HTTP 304 Not Modified if unchanged, saving bandwidth.Final Answer:
Send Last-Modified header with resource date; on request, check If-Modified-Since and respond 304 if unchanged -> Option BQuick Check:
Send Last-Modified + check If-Modified-Since -> 304 optimization [OK]
- Sending If-Modified-Since from server instead of client
- Ignoring If-Modified-Since header in server logic
- Using Last-Modified only on POST requests
Ready to prove it?
Pass the test and this pattern moves unknown
