Bird
Raised Fist0
Redisquery~20 mins

Cache-aside pattern in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Cache-Aside Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Cache Miss Handling in Cache-Aside Pattern

In the cache-aside pattern, what happens when the requested data is not found in the cache?

AThe cache automatically fetches data from the database and updates itself without application intervention.
BThe application returns an error immediately without querying the database.
CThe application fetches data from the database, stores it in the cache, then returns it.
DThe cache deletes the missing key and waits for the database to push data.
Attempts:
2 left
💡 Hint

Think about who is responsible for loading data into the cache when it is missing.

❓ query_result
intermediate
2:00remaining
Result of Cache-Aside Data Retrieval Sequence

Given the following sequence in a cache-aside pattern using Redis:
1. Application checks Redis key 'user:123'.
2. Key is missing.
3. Application queries database and gets user data: {"id":123, "name":"Alice"}.
4. Application stores this data in Redis under 'user:123'.
5. Application reads 'user:123' from Redis.
What is the output of the final Redis read?

A{"id":123, "name":"Alice"}
Bnull
C"user:123"
DAn error because Redis cannot store JSON
Attempts:
2 left
💡 Hint

Consider what data was stored in Redis and what is returned when reading that key.

📝 Syntax
advanced
2:00remaining
Identify the Correct Redis Command to Store Data in Cache-Aside Pattern

Which Redis command correctly stores the user data JSON string '{"id":123, "name":"Alice"}' under the key 'user:123'?

ASET user:123 '{"id":123, "name":"Alice"}'
BGET user:123 '{"id":123, "name":"Alice"}'
CPUT user:123 '{"id":123, "name":"Alice"}'
DSTORE user:123 '{"id":123, "name":"Alice"}'
Attempts:
2 left
💡 Hint

Remember the Redis command to set a key's value.

❓ optimization
advanced
2:00remaining
Optimizing Cache Expiration in Cache-Aside Pattern

To prevent stale data in a cache-aside pattern, which approach optimizes cache expiration for user data in Redis?

ANever expire cached keys to ensure data is always available.
BSet a TTL (time-to-live) on cached keys to automatically expire after a fixed time.
CManually delete keys only when the database is updated, without TTL.
DUse Redis transactions to lock keys indefinitely.
Attempts:
2 left
💡 Hint

Think about automatic ways to keep cache fresh without manual intervention.

🔧 Debug
expert
3:00remaining
Diagnosing Cache Inconsistency in Cache-Aside Pattern

An application uses the cache-aside pattern with Redis. After updating user data in the database, the cache is not updated, causing stale data to be served. What is the most likely cause?

AThe cache key expired too soon, causing stale data.
BRedis automatically updates cached data when the database changes.
CThe database update failed silently, so cache was correct.
DThe application did not invalidate or update the Redis cache after the database update.
Attempts:
2 left
💡 Hint

Consider the responsibilities of the application in cache-aside pattern after database writes.

Practice

(1/5)
1. What is the main idea behind the cache-aside pattern in Redis?
easy
A. Automatically update cache whenever the database changes without application intervention
B. Load data into cache only when the application requests it and cache misses occur
C. Preload all data into cache before any application requests
D. Use cache only for temporary data that does not come from the database

Solution

  1. Step 1: Understand cache-aside pattern basics

    The cache-aside pattern means the application loads data into cache only when it is needed, not before.
  2. Step 2: Compare options with this concept

    Load data into cache only when the application requests it and cache misses occur matches this idea exactly, while others describe different caching strategies.
  3. Final Answer:

    Load data into cache only when the application requests it and cache misses occur -> Option B
  4. Quick Check:

    Cache-aside = Load on demand [OK]
Hint: Cache-aside means load cache only on demand [OK]
Common Mistakes:
  • Thinking cache is preloaded with all data
  • Assuming cache updates automatically without app logic
  • Confusing cache-aside with write-through caching
2. Which Redis command is typically used to check if a key exists in cache before loading from the database in cache-aside pattern?
easy
A. DEL
B. GET
C. SET
D. EXISTS

Solution

  1. Step 1: Identify command to check key presence

    In Redis, EXISTS checks if a key is present in cache.
  2. Step 2: Match commands with their purpose

    GET retrieves value, SET stores value, DEL deletes key. Only EXISTS checks presence.
  3. Final Answer:

    EXISTS -> Option D
  4. Quick Check:

    Check key presence = EXISTS [OK]
Hint: Use EXISTS to check if cache key is present [OK]
Common Mistakes:
  • Using GET to check existence (GET returns null if missing)
  • Confusing SET with checking commands
  • Using DEL which deletes keys
3. Consider this Redis cache-aside pseudocode:
if not redis.exists('user:123'):
    data = db.query('SELECT * FROM users WHERE id=123')
    redis.set('user:123', data)
else:
    data = redis.get('user:123')

What happens if the key user:123 is missing in Redis?
medium
A. Redis automatically loads data from database
B. Data is fetched from Redis but will be empty
C. Data is fetched from the database and stored in Redis cache
D. An error occurs because key does not exist

Solution

  1. Step 1: Analyze the if condition for cache miss

    If user:123 does not exist in Redis, the code queries the database for data.
  2. Step 2: Understand cache population after miss

    After fetching from database, it stores the data in Redis with set for future requests.
  3. Final Answer:

    Data is fetched from the database and stored in Redis cache -> Option C
  4. Quick Check:

    Cache miss triggers DB fetch and cache store [OK]
Hint: Cache miss means fetch DB and store in cache [OK]
Common Mistakes:
  • Assuming Redis auto-loads data from DB
  • Expecting error on missing key
  • Thinking Redis returns empty data silently
4. You have this cache-aside code snippet in Redis:
if redis.get('product:456') is None:
    data = db.query('SELECT * FROM products WHERE id=456')
    redis.set('product:456', data)
else:
    data = redis.get('product:456')

But the cache never updates even after database changes. What is the likely problem?
medium
A. The cache is never invalidated or updated after DB changes
B. The Redis GET command is incorrect
C. The database query syntax is wrong
D. The key name 'product:456' is invalid in Redis

Solution

  1. Step 1: Identify cache update mechanism

    The code only sets cache if key is missing but does not update or invalidate cache after DB changes.
  2. Step 2: Understand cache staleness cause

    Without cache invalidation or update logic after DB writes, cache remains stale and does not reflect DB changes.
  3. Final Answer:

    The cache is never invalidated or updated after DB changes -> Option A
  4. Quick Check:

    Cache stale if no invalidation after DB update [OK]
Hint: Cache must be invalidated after DB updates [OK]
Common Mistakes:
  • Assuming GET command causes update
  • Blaming DB query syntax without evidence
  • Thinking Redis keys are invalid without reason
5. You want to implement cache-aside pattern in Redis for a user profile service. Which approach correctly handles cache misses and updates when user data changes?
1. On read: check cache, if miss fetch DB and set cache
2. On write: update DB, then delete cache key to force reload on next read

Why is deleting the cache key after DB update preferred over updating cache directly?
hard
A. Deleting cache key avoids race conditions and stale data issues
B. Updating cache directly is impossible in Redis
C. Deleting cache key improves Redis memory usage drastically
D. Updating cache directly causes Redis to crash

Solution

  1. Step 1: Understand cache update strategies

    Deleting cache key after DB update forces next read to reload fresh data, avoiding stale cache.
  2. Step 2: Recognize race condition prevention

    Direct cache update risks race conditions if multiple writes/read happen concurrently; deleting key is safer.
  3. Final Answer:

    Deleting cache key avoids race conditions and stale data issues -> Option A
  4. Quick Check:

    Delete cache key after DB write to avoid stale data [OK]
Hint: Delete cache key after DB update to avoid stale data [OK]
Common Mistakes:
  • Thinking Redis cannot update cache values
  • Believing direct cache update is always better
  • Assuming deleting cache key crashes Redis
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