Bird
Raised Fist0
Rest APIprogramming~20 mins

Validation-based caching in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Validation-based caching Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this HTTP cache validation example?

Consider a REST API that uses ETag headers for validation-based caching. The server responds with an ETag value "abc123" for a resource. A client sends a GET request with header If-None-Match: "abc123". What will the server respond?

Rest API
HTTP/1.1 304 Not Modified
ETag: "abc123"

AHTTP 404 Not Found
BHTTP 500 Internal Server Error
CHTTP 304 Not Modified with no body
DHTTP 200 OK with full resource body
Attempts:
2 left
💡 Hint

Think about what the server does when the client's ETag matches the current resource's ETag.

🧠 Conceptual
intermediate
1:30remaining
Which HTTP header is used by clients to validate cached resources?

In validation-based caching, clients send a header to check if their cached resource is still valid. Which header is it?

AIf-Modified-Since
BCache-Control
CAuthorization
DContent-Type
Attempts:
2 left
💡 Hint

It is a header that asks the server if the resource has changed since a certain date.

🔧 Debug
advanced
2:30remaining
Why does this validation-based caching code always return full data instead of 304?

Given this simplified server code snippet in Python Flask, why does the server never return 304 Not Modified?

Rest API
from flask import Flask, request, jsonify, make_response
app = Flask(__name__)

@app.route('/data')
def data():
    etag = '"xyz789"'
    if request.headers.get('If-None-Match') == etag:
        return make_response('', 304)
    response = make_response(jsonify({'value': 42}))
    response.headers['ETag'] = etag
    return response
AThe response body is always sent regardless of ETag
BThe server does not check the If-Modified-Since header
CThe ETag header is not set in the response
DThe If-None-Match header is compared incorrectly (missing quotes)
Attempts:
2 left
💡 Hint

Check how the ETag value is formatted and compared with the header.

📝 Syntax
advanced
2:00remaining
Which option correctly sets the ETag header in this Node.js Express code?

Choose the correct way to set the ETag header in an Express.js response.

Rest API
app.get('/resource', (req, res) => {
  const etagValue = 'abc123';
  // Set ETag header here
  res.send('Hello World');
});
Ares.set('ETag', etagValue);
Bres.setHeader('etag', etagValue);
Cres.header('ETag', etagValue);
Dres.etag = etagValue;
Attempts:
2 left
💡 Hint

Express response objects have a method to set headers with case-insensitive names.

🚀 Application
expert
1:30remaining
How many items are cached when using validation-based caching with ETag and If-None-Match?

A REST API serves 3 different resources, each with unique ETags. A client caches all 3 resources and sends requests with If-None-Match headers for each. How many resources will the client avoid downloading if all ETags match?

A0
B3
C2
D1
Attempts:
2 left
💡 Hint

If the client's ETag matches the server's, the server responds with 304 Not Modified.

Practice

(1/5)
1. What is the main purpose of validation-based caching in REST APIs?
easy
A. To encrypt data during transmission
B. To speed up the server response by skipping validation
C. To store data permanently on the client side
D. To check if the data has changed before downloading it again

Solution

  1. Step 1: Understand validation-based caching concept

    It is used to avoid downloading data again if it has not changed.
  2. Step 2: Identify the main purpose

    The main goal is to check if data changed before downloading again to save bandwidth and time.
  3. Final Answer:

    To check if the data has changed before downloading it again -> Option D
  4. Quick Check:

    Validation-based caching = check data change before download [OK]
Hint: Remember: caching avoids re-downloading unchanged data [OK]
Common Mistakes:
  • Thinking caching stores data permanently
  • Confusing caching with encryption
  • Assuming caching skips validation
2. Which HTTP header does the client send to ask the server if the cached data is still valid?
easy
A. If-Modified-Since
B. ETag
C. If-None-Match
D. Cache-Control

Solution

  1. Step 1: Identify client headers for validation

    The client uses headers to tell the server what version it has.
  2. Step 2: Match header for ETag validation

    The If-None-Match header is sent with the ETag value to check if data changed.
  3. Final Answer:

    If-None-Match -> Option C
  4. Quick Check:

    Client asks with If-None-Match header [OK]
Hint: Client sends If-None-Match with ETag to validate cache [OK]
Common Mistakes:
  • Confusing If-Modified-Since with If-None-Match
  • Using Cache-Control header for validation
  • Thinking ETag is sent by client
3. Given this server response to a client request with If-None-Match: "abc123" header:
HTTP/1.1 304 Not Modified
ETag: "abc123"

What does this response mean?
medium
A. The data has changed and new data is sent
B. The data has not changed; client should use cached data
C. The server does not support caching
D. The client must resend the request without ETag

Solution

  1. Step 1: Understand HTTP 304 status meaning

    304 means Not Modified, so data is unchanged since last fetch.
  2. Step 2: Interpret ETag match

    ETag matches client's value, so server confirms data is still valid.
  3. Final Answer:

    The data has not changed; client should use cached data -> Option B
  4. Quick Check:

    304 Not Modified = use cached data [OK]
Hint: 304 status means no new data, use cache [OK]
Common Mistakes:
  • Thinking 304 means new data sent
  • Confusing 304 with 200 OK
  • Assuming server rejects caching
4. A client sends a request with header If-None-Match: "xyz789". The server responds with:
HTTP/1.1 200 OK
ETag: "abc123"
{ "data": "new content" }

What is the likely problem in this interaction?
medium
A. The server sent a different ETag, indicating data changed, so 200 OK is correct
B. The server should respond with 304 Not Modified instead of 200 OK
C. The client sent an invalid ETag header
D. The server response is missing the If-None-Match header

Solution

  1. Step 1: Compare client and server ETags

    Client sent "xyz789", server responded with "abc123" which is different.
  2. Step 2: Understand server response code

    Since ETags differ, data changed, so server correctly sends 200 OK with new data.
  3. Final Answer:

    The server sent a different ETag, indicating data changed, so 200 OK is correct -> Option A
  4. Quick Check:

    Different ETag means data changed, send 200 OK [OK]
Hint: Different ETag means new data, so 200 OK is correct [OK]
Common Mistakes:
  • Expecting 304 when ETags differ
  • Thinking client ETag must match server's
  • Confusing If-None-Match with If-Match
5. You want to implement validation-based caching in your REST API. Which sequence correctly describes the interaction between client and server?
hard
A. Client sends request with If-None-Match header; server replies 304 if data unchanged or 200 with new data and ETag if changed
B. Server sends ETag in request; client replies 304 if data unchanged or 200 with new data if changed
C. Client sends ETag in response; server sends If-None-Match header to validate cache
D. Server always sends 200 OK with data and never uses ETag

Solution

  1. Step 1: Identify correct client-server header roles

    Client sends If-None-Match with ETag to ask if data changed.
  2. Step 2: Understand server response logic

    Server replies 304 if data unchanged, or 200 with new data and new ETag if changed.
  3. Final Answer:

    Client sends request with If-None-Match header; server replies 304 if data unchanged or 200 with new data and ETag if changed -> Option A
  4. Quick Check:

    Client sends If-None-Match; server replies 304 or 200 [OK]
Hint: Client sends If-None-Match; server replies 304 or 200 with ETag [OK]
Common Mistakes:
  • Mixing up who sends ETag and If-None-Match
  • Thinking server sends If-None-Match header
  • Assuming server never sends 304
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard