0
0
Rest APIprogramming~20 mins

Idempotency keys for safe retries in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Idempotency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when retrying a POST request with the same idempotency key?

Consider a REST API that uses idempotency keys to prevent duplicate resource creation. The client sends a POST request with an idempotency key header. The server stores the response for that key.

What will the server return if the client retries the same POST request with the same idempotency key?

Rest API
POST /orders HTTP/1.1
Idempotency-Key: abc123
{
  "item": "book",
  "quantity": 1
}

# Retry with the same Idempotency-Key: abc123
AThe server ignores the idempotency key and processes the request normally.
BThe server creates a new order again and returns a new order ID.
CThe server returns an error indicating the idempotency key is already used.
DThe server returns the original response for the first request without creating a new order.
Attempts:
2 left
💡 Hint

Think about why idempotency keys are used in APIs.

🧠 Conceptual
intermediate
1:30remaining
Why are idempotency keys important in REST APIs?

Which of the following best explains the main purpose of idempotency keys in REST APIs?

ATo allow safe retries of requests without creating duplicate effects.
BTo encrypt the request payload for security.
CTo speed up the server response time by caching.
DTo authenticate the client making the request.
Attempts:
2 left
💡 Hint

Think about what happens when a client retries a request due to network issues.

🔧 Debug
advanced
2:30remaining
Identify the bug in this idempotency key handling code snippet

Review the following pseudocode for handling POST requests with idempotency keys. What is the bug?

Rest API
def handle_post(request):
    key = request.headers.get('Idempotency-Key')
    if key in cache:
        return cache[key]
    response = process_request(request)
    # Missing cache storage here
    return response
AThe code does not check if the idempotency key is missing.
BThe response is not stored in the cache after processing the request.
CThe cache lookup should happen after processing the request.
DThe code should delete the key from cache after returning the response.
Attempts:
2 left
💡 Hint

Think about what happens on the second request with the same key.

📝 Syntax
advanced
2:00remaining
Which option correctly implements idempotency key storage in Python?

Choose the code snippet that correctly stores and returns a cached response for an idempotency key.

Rest API
cache = {}

def handle_request(request):
    key = request.headers.get('Idempotency-Key')
    if key in cache:
        return cache[key]
    response = process(request)
    # Store response here
    return response
A
cache[key] <- response
return response
B
cache[key] == response
return response
C
cache[key] = response
return response
D
cache.add(key, response)
return response
Attempts:
2 left
💡 Hint

Remember how to assign values to dictionary keys in Python.

🚀 Application
expert
3:00remaining
How many unique responses are stored after these requests with idempotency keys?

A client sends these POST requests to the server with idempotency keys:

  1. POST with key 'x1'
  2. POST with key 'x2'
  3. POST with key 'x1' again (retry)
  4. POST with key 'x3'
  5. POST with key 'x2' again (retry)

Assuming the server correctly caches responses by idempotency key, how many unique responses are stored in the cache after these requests?

A3
B5
C2
D4
Attempts:
2 left
💡 Hint

Count unique keys used in the requests.