Bird
Raised Fist0
Redisquery~20 mins

Why caching patterns matter in Redis - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Caching Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What happens when you cache data without expiration?

Consider a Redis cache where keys are set without expiration time. What is the likely outcome after continuous data updates?

Redis
SET user:123 "John Doe"
# No expiration set
AThe cache will automatically clear old keys to save space.
BThe cache will grow indefinitely, possibly causing memory issues.
CRedis will throw an error when memory is full.
DThe cached data will expire after 24 hours by default.
Attempts:
2 left
💡 Hint

Think about what happens if keys never expire and new data keeps adding.

🧠 Conceptual
intermediate
2:00remaining
Why use cache aside pattern?

Which benefit best explains why the cache aside pattern is used in caching?

AIt allows the application to control when to load data into the cache, reducing stale data.
BIt ensures the cache is always updated automatically without application logic.
CIt forces Redis to handle all database writes directly.
DIt disables caching for write-heavy applications.
Attempts:
2 left
💡 Hint

Think about who controls the cache loading in cache aside.

📝 Syntax
advanced
2:00remaining
Which Redis command sets a key with expiration in one step?

Choose the Redis command that sets a key "session:1" with value "active" and expires after 60 seconds.

ASET session:1 active EX 60
BEXPIRE session:1 60 SET active
CSET session:1 active PX 60000
DSETEX session:1 60 active
Attempts:
2 left
💡 Hint

Look for the command that combines setting and expiration in one.

❓ optimization
advanced
2:00remaining
How to avoid cache stampede in Redis?

When many clients request the same missing cache key simultaneously, what is a good strategy to prevent all from querying the database at once?

AUse a distributed lock to allow only one client to load data and others wait.
BIncrease Redis memory to handle more keys.
CDisable caching for hot keys to avoid stale data.
DSet very short expiration times to refresh cache frequently.
Attempts:
2 left
💡 Hint

Think about controlling who loads data when cache is missing.

🔧 Debug
expert
3:00remaining
Why does this Redis cache update fail to reflect new data?

Given this sequence:
1. Cache key "product:10" set with old data.
2. Database updated with new product info.
3. Cache key deleted.
4. Application reads cache key "product:10" expecting new data.
Why might the application still see old data?

ARedis does not support deleting keys, so cache remains unchanged.
BThe cache was deleted before the database update, so stale data was re-cached.
CThe cache deletion and database update were not atomic, causing race condition.
DThe application is reading from the database, not the cache.
Attempts:
2 left
💡 Hint

Consider timing between cache deletion and database update.

Practice

(1/5)
1. Why is caching important in Redis when building applications?
easy
A. It slows down the application by adding extra steps
B. It permanently saves all data without expiration
C. It deletes all data immediately after storing
D. It stores data temporarily to make data retrieval faster

Solution

  1. Step 1: Understand caching purpose

    Caching temporarily stores data to reduce the time needed to get it again.
  2. Step 2: Identify correct caching behavior in Redis

    Redis caching speeds up apps by storing data for quick access, not permanent saving or immediate deletion.
  3. Final Answer:

    It stores data temporarily to make data retrieval faster -> Option D
  4. Quick Check:

    Caching = Temporary fast storage [OK]
Hint: Caching means temporary storage for speed, not permanent save [OK]
Common Mistakes:
  • Thinking caching saves data permanently
  • Believing caching slows down the app
  • Confusing caching with immediate data deletion
2. Which Redis command correctly sets a key with an expiration time of 60 seconds?
easy
A. GET key EX 60
B. SET key value EX 60
C. DEL key 60
D. EXPIRE key SET 60

Solution

  1. Step 1: Recall Redis syntax for setting key with expiration

    The correct command is SET with EX option to set expiration in seconds.
  2. Step 2: Check each option's correctness

    SET key value EX 60 uses SET key value EX 60 which is valid. Others misuse commands or order.
  3. Final Answer:

    SET key value EX 60 -> Option B
  4. Quick Check:

    SET with EX sets key and expiration [OK]
Hint: Use SET with EX to set expiration in seconds [OK]
Common Mistakes:
  • Using GET instead of SET to set values
  • Misplacing EXPIRE and SET commands
  • Trying to delete keys with expiration time
3. Given this Redis command sequence:
SET user:1 "Alice" EX 10
GET user:1

What will the GET command return if run 15 seconds after the SET?
medium
A. nil
B. Empty string
C. Error: key expired
D. "Alice"

Solution

  1. Step 1: Understand expiration effect

    SET with EX 10 means key expires after 10 seconds, so after 15 seconds key is gone.
  2. Step 2: Predict GET result after expiration

    GET on expired key returns nil (no value), not error or empty string.
  3. Final Answer:

    nil -> Option A
  4. Quick Check:

    Expired key returns nil on GET [OK]
Hint: GET returns nil if key expired before access [OK]
Common Mistakes:
  • Expecting old value after expiration
  • Thinking GET returns error on expired keys
  • Confusing empty string with nil
4. You wrote this Redis code:
SET session:123 "active"
EXPIRE session:123 0

What is the problem with this code?
medium
A. EXPIRE command syntax is incorrect
B. Key will never expire because 0 means infinite
C. Expiration time 0 deletes the key immediately
D. SET command must include expiration time

Solution

  1. Step 1: Understand EXPIRE with 0 seconds

    EXPIRE with 0 seconds deletes the key immediately.
  2. Step 2: Identify the effect on the key

    After EXPIRE session:123 0, the key is deleted immediately, which may be unintended.
  3. Final Answer:

    Expiration time 0 deletes the key immediately -> Option C
  4. Quick Check:

    EXPIRE 0 deletes key immediately [OK]
Hint: EXPIRE 0 deletes key immediately [OK]
Common Mistakes:
  • Thinking EXPIRE 0 removes expiration (never expires)
  • Assuming EXPIRE syntax is wrong
  • Believing SET must always have expiration
5. You want to cache user profile data in Redis but ensure it updates if the user changes their info. Which caching pattern is best?
hard
A. Set key with expiration and delete key on user update
B. Set key without expiration and never delete it
C. Set key with very long expiration and ignore updates
D. Do not cache user data at all

Solution

  1. Step 1: Understand caching with data freshness

    To keep data fresh, cache keys should expire and be deleted when data changes.
  2. Step 2: Evaluate options for update handling

    Set key with expiration and delete key on user update uses expiration and deletes key on update, ensuring fresh data. Others risk stale data or no caching.
  3. Final Answer:

    Set key with expiration and delete key on user update -> Option A
  4. Quick Check:

    Expire + delete on update keeps cache fresh [OK]
Hint: Expire keys and delete on update to keep cache fresh [OK]
Common Mistakes:
  • Not deleting keys on data change
  • Using no expiration causing stale cache
  • Avoiding caching user data unnecessarily
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