Bird
Raised Fist0
Redisquery~10 mins

Cache invalidation strategies in Redis - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a cache key with an expiration time in Redis.

Redis
redis.set('user:123', 'data', [1]=3600)
Drag options to blanks, or click blank then click option'
Aex
Bexpire
Ctimeout
Dttl
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'expire' which is a separate command, not a parameter here.
Using 'ttl' which returns time to live, not sets it.
2fill in blank
medium

Complete the code to delete a cache key in Redis.

Redis
redis.[1]('user:123')
Drag options to blanks, or click blank then click option'
Aremove
Bdel
Cdelete
Dunset
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'del' which is the Redis protocol command but not the Python client method.
Using 'remove' or 'unset' which are not Redis client methods.
3fill in blank
hard

Fix the error in the code to check if a cache key exists in Redis.

Redis
exists = redis.[1]('user:123')
Drag options to blanks, or click blank then click option'
Ahas
Bexists
Ccontains
Dcheck
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'has' or 'contains' which are not Redis commands.
Using 'check' which is not a Redis command.
4fill in blank
hard

Fill both blanks to set a cache key with a value and expiration time using Redis pipeline.

Redis
pipe = redis.pipeline()
pipe.set('session:[1]', '[2]')
pipe.expire('session:123', 1800)
pipe.execute()
Drag options to blanks, or click blank then click option'
A123
Bactive
Cuser
Dinactive
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching key suffix and expire key.
Using 'inactive' instead of 'active' for session state.
5fill in blank
hard

Fill all three blanks to implement a cache invalidation strategy that deletes a key and logs the action.

Redis
def invalidate_cache(redis, key):
    redis.[1](key)
    log_message = f"Cache key [2] invalidated at [3]"
    print(log_message)
Drag options to blanks, or click blank then click option'
Adel
Bkey
Cdatetime.now()
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'del' instead of 'delete' for Redis client method.
Not using the key variable in the log message.
Not including current time in the log.

Practice

(1/5)
1. What is the main purpose of cache invalidation in Redis?
easy
A. To keep cached data fresh and accurate
B. To increase the size of the cache
C. To slow down data retrieval
D. To permanently store data without changes

Solution

  1. Step 1: Understand cache invalidation

    Cache invalidation means removing or updating cached data to avoid stale information.
  2. Step 2: Identify the main goal

    The goal is to keep the cached data fresh and accurate so users get the latest information.
  3. Final Answer:

    To keep cached data fresh and accurate -> Option A
  4. Quick Check:

    Cache invalidation = fresh data [OK]
Hint: Cache invalidation means keeping data fresh [OK]
Common Mistakes:
  • Thinking cache invalidation increases cache size
  • Believing it slows down data retrieval
  • Assuming cache data never changes
2. Which Redis command is used to manually delete a cached key for invalidation?
easy
A. EXPIRE
B. GET
C. SETEX
D. DEL

Solution

  1. Step 1: Identify commands for cache invalidation

    Manual invalidation requires deleting keys, so the command must remove keys.
  2. Step 2: Match command to action

    DEL command deletes keys immediately, while EXPIRE sets a timeout, SETEX sets value with expiration, GET retrieves value.
  3. Final Answer:

    DEL -> Option D
  4. Quick Check:

    Delete key = DEL command [OK]
Hint: DEL deletes keys instantly for invalidation [OK]
Common Mistakes:
  • Confusing EXPIRE with immediate deletion
  • Using GET which only reads data
  • Thinking SETEX deletes keys
3. Consider this Redis usage:
SET user:1 "Alice" EX 10
What happens after 10 seconds?
medium
A. The key user:1 remains forever
B. The key user:1 is deleted automatically
C. The key user:1 value changes to null
D. The key user:1 expires but stays in memory

Solution

  1. Step 1: Understand the EX option in SET

    EX 10 means the key expires after 10 seconds automatically.
  2. Step 2: Identify what happens on expiration

    When a key expires, Redis deletes it from memory automatically.
  3. Final Answer:

    The key user:1 is deleted automatically -> Option B
  4. Quick Check:

    EX sets expiration = auto delete [OK]
Hint: EX sets auto-delete time for keys [OK]
Common Mistakes:
  • Thinking key stays forever after expiration
  • Believing value changes to null instead of deletion
  • Assuming expired keys remain in memory
4. You wrote this Redis command to expire a key:
EXPIRE user:2 0
What is the problem with this command?
medium
A. It sets expiration to zero seconds, effectively deleting the key
B. It deletes the key immediately
C. It sets the key to never expire
D. It causes a syntax error

Solution

  1. Step 1: Understand EXPIRE with zero seconds

    EXPIRE key 0 removes any existing expiration timeout (equivalent to PERSIST command).
  2. Step 2: Identify the problem

    The intent was to expire the key, but this sets it to never expire instead.
  3. Final Answer:

    It sets the key to never expire -> Option C
  4. Quick Check:

    EXPIRE 0 = remove expiration [OK]
Hint: EXPIRE 0 removes expiration (never expire) [OK]
Common Mistakes:
  • Thinking it deletes the key immediately
  • Believing it causes a syntax error
  • Assuming zero seconds expires immediately
5. You want to keep cached product data fresh but avoid deleting keys too often. Which strategy best balances speed and data correctness in Redis?
hard
A. Use a long expiration time and rely on manual invalidation when data changes
B. Never expire keys and update them only manually
C. Delete keys immediately after every read
D. Set a short expiration time and manually delete keys on updates

Solution

  1. Step 1: Understand trade-offs of expiration and manual deletion

    Short expiration causes frequent reloads; manual deletion alone risks stale data.
  2. Step 2: Choose balanced approach

    Long expiration reduces reloads, manual invalidation on updates keeps data correct without overhead.
  3. Final Answer:

    Use a long expiration time and rely on manual invalidation when data changes -> Option A
  4. Quick Check:

    Balance speed and correctness = long expiry + manual invalidation [OK]
Hint: Long expiry plus manual delete balances freshness and speed [OK]
Common Mistakes:
  • Choosing very short expiration causing overhead
  • Never expiring keys causing stale data
  • Deleting keys after every read causing slow performance
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