Bird
0
0

Analyze this server-side code handling idempotency keys:

medium📝 Predict Output Q4 of 15
Rest API - Advanced Patterns
Analyze this server-side code handling idempotency keys:
if key in cache:
    return cache[key]
else:
    response = execute_operation()
    cache[key] = response
    return response
What occurs if the client sends the same request twice with the same key?
AThe operation executes once; the second request returns cached response
BThe operation executes twice, causing duplicate effects
CThe server returns an error on the second request
DThe cache is cleared after the first request
Step-by-Step Solution
Solution:
  1. Step 1: Check key presence

    If the key exists in cache, the stored response is returned immediately.
  2. Step 2: On first request

    The operation executes and result is cached.
  3. Step 3: On second request

    The cached response is returned without re-executing the operation.
  4. Final Answer:

    The operation executes once; the second request returns cached response -> Option A
  5. Quick Check:

    Cache prevents duplicate processing on repeated keys [OK]
Quick Trick: Cached response returned on repeated keys [OK]
Common Mistakes:
MISTAKES
  • Assuming operation runs multiple times
  • Expecting errors on repeated keys
  • Believing cache clears automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes