0
0
Rest APIprogramming~20 mins

Cache invalidation strategies in Rest API - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Invalidation 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 cache invalidation simulation?

Consider a REST API cache that stores user data. The cache invalidation is triggered when a user updates their profile. What will be the output of the following Python simulation code?

Rest API
cache = {'user_1': 'John Doe', 'user_2': 'Jane Smith'}

# User 1 updates profile
cache['user_1'] = 'John A. Doe'

# Invalidate cache for user_2
cache.pop('user_2', None)

print(cache)
A{'user_1': 'John A. Doe', 'user_2': 'Jane Smith'}
B{'user_1': 'John Doe', 'user_2': 'Jane Smith'}
C{'user_1': 'John A. Doe'}
D{'user_2': 'Jane Smith'}
Attempts:
2 left
💡 Hint

Think about what happens when you update a cache entry and then remove another.

🧠 Conceptual
intermediate
1:30remaining
Which cache invalidation strategy is demonstrated here?

A REST API cache is set to expire entries automatically after 10 minutes. Which cache invalidation strategy does this represent?

ATime-to-live (TTL) expiration
BWrite-through caching
CManual invalidation
DCache aside
Attempts:
2 left
💡 Hint

Think about automatic expiration based on time.

🔧 Debug
advanced
2:30remaining
Why does this cache invalidation code fail to remove the stale entry?

Look at the code below that tries to invalidate a cache entry after updating the database. Why does the stale cache entry remain?

Rest API
cache = {'item_1': 'old_value'}

def update_database():
    # Simulate database update
    return 'new_value'

new_val = update_database()

# Later code tries to remove stale cache but fails
if 'item_1' in cache:
    pass  # supposed to remove stale cache but does nothing

print(cache)
AThe cache invalidation code does not remove the entry; it only checks existence.
BThe cache dictionary is immutable, so assignment fails.
CThe cache entry is overwritten, so no stale entry remains.
DThe update_database function returns null, so cache is not updated.
Attempts:
2 left
💡 Hint

Look carefully at the 'if' block that is supposed to remove the cache entry.

📝 Syntax
advanced
1:30remaining
Which option causes a runtime error in cache invalidation code?

Identify the option that will cause a runtime error when trying to invalidate a cache entry in Python.

Rest API
cache = {'key1': 'value1', 'key2': 'value2'}
Acache.pop('key1')
Bdel cache['key2']
Ccache['key1'] = None
Dcache.remove('key1')
Attempts:
2 left
💡 Hint

Check which method is not valid for Python dictionaries.

🚀 Application
expert
3:00remaining
How many cache entries remain after this sequence of invalidations?

Given the following cache invalidation sequence, how many entries remain in the cache?

Rest API
cache = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

# Invalidate entries where value is even
cache = {k: v for k, v in cache.items() if v % 2 != 0}

# Add new entry
cache['e'] = 5

# Remove entry 'a'
cache.pop('a', None)

print(len(cache))
A1
B2
C4
D3
Attempts:
2 left
💡 Hint

Count entries after filtering, adding, and removing.