Bird
Raised Fist0
HLDsystem_design~20 mins

Distributed caching (Redis, Memcached) in HLD - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Distributed Cache Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Architecture
intermediate
2:00remaining
Designing a distributed cache with Redis

You need to design a distributed caching layer using Redis for a web application that handles millions of users. Which architecture component is essential to ensure data consistency and high availability?

ADeploy Redis as a single instance on a powerful server
BUse Redis Sentinel for automatic failover and monitoring
CUse Memcached with no replication for simplicity
DStore cache data only on the application server memory
Attempts:
2 left
💡 Hint

Think about how Redis handles failover and keeps the cache available during node failures.

❓ scaling
intermediate
2:00remaining
Scaling Memcached for large datasets

Your application uses Memcached to cache user session data. The dataset is growing beyond the capacity of a single Memcached server. What is the best approach to scale Memcached horizontally?

AUse Redis instead because Memcached cannot scale horizontally
BIncrease the RAM of the single Memcached server to hold all data
CUse consistent hashing to distribute keys across multiple Memcached nodes
DStore all data in a single Memcached node and replicate it to others
Attempts:
2 left
💡 Hint

Consider how Memcached clients decide which server to use for a given key.

❓ tradeoff
advanced
2:00remaining
Choosing between Redis and Memcached for session caching

Your team must choose between Redis and Memcached for caching user sessions. Which tradeoff is most important when deciding?

ARedis supports data persistence and complex data types, Memcached is simpler and faster for pure key-value caching
BMemcached supports transactions, Redis does not
CRedis cannot scale horizontally, Memcached can
DMemcached supports data persistence, Redis does not
Attempts:
2 left
💡 Hint

Think about features and use cases of Redis and Memcached.

🧠 Conceptual
advanced
2:00remaining
Cache invalidation strategies in distributed caching

Which cache invalidation strategy is best suited for a distributed cache to maintain data freshness without overwhelming the backend database?

AWrite all updates only to the cache and never to the database
BAlways delete cache entries immediately after every database update
CNever expire cache entries and rely on manual cache clearing
DUse time-to-live (TTL) expiration combined with lazy loading on cache miss
Attempts:
2 left
💡 Hint

Consider balancing freshness and load on the database.

❓ estimation
expert
3:00remaining
Estimating cache capacity for a high-traffic system

Your system expects 10 million active users, each with an average session size of 2 KB. You want to cache 30% of active sessions simultaneously in Redis. Estimate the minimum memory required for the Redis cache, considering 20% overhead for metadata and replication.

AApproximately 7.2 GB
BApproximately 4.8 GB
CApproximately 12 GB
DApproximately 3.6 GB
Attempts:
2 left
💡 Hint

Calculate total data size, then add overhead.

Practice

(1/5)
1. What is the primary purpose of using a distributed cache like Redis or Memcached in a system?
easy
A. To reduce database load by storing frequently accessed data in memory
B. To permanently store all application data for backup
C. To replace the database entirely for all data storage needs
D. To increase the size of the database by adding more servers

Solution

  1. Step 1: Understand the role of distributed cache

    Distributed caches store frequently accessed data in fast memory to reduce the number of database queries.
  2. Step 2: Identify the main benefit

    This reduces the load on the database and speeds up data retrieval for repeated requests.
  3. Final Answer:

    To reduce database load by storing frequently accessed data in memory -> Option A
  4. Quick Check:

    Distributed cache reduces DB load = C [OK]
Hint: Cache stores frequent data to reduce DB hits [OK]
Common Mistakes:
  • Thinking cache permanently stores all data
  • Confusing cache with database backup
  • Assuming cache replaces the database
2. Which of the following is the correct way to set a key-value pair in Redis using its command syntax?
easy
A. SET key value
B. GET key value
C. STORE key value
D. PUT key value

Solution

  1. Step 1: Recall Redis command for setting data

    Redis uses the command SET key value to store a value under a key.
  2. Step 2: Verify other options

    GET retrieves data, STORE and PUT are not valid Redis commands.
  3. Final Answer:

    SET key value -> Option A
  4. Quick Check:

    Redis set command = SET key value [OK]
Hint: Remember Redis uses SET to store key-value [OK]
Common Mistakes:
  • Confusing GET with SET
  • Using non-existent commands like STORE or PUT
  • Mixing Redis commands with SQL syntax
3. Consider this simplified Redis usage in code:
cache.set('user:1', 'Alice')
name = cache.get('user:1')
print(name)

What will be printed?
medium
A. None
B. 'Alice'
C. user:1
D. Error

Solution

  1. Step 1: Analyze the cache set operation

    The key 'user:1' is set with the value 'Alice' in the cache.
  2. Step 2: Analyze the cache get operation

    Retrieving 'user:1' returns the stored value 'Alice', which is then printed.
  3. Final Answer:

    'Alice' -> Option B
  4. Quick Check:

    Set then get returns stored value = 'Alice' [OK]
Hint: Get returns value set by set command [OK]
Common Mistakes:
  • Expecting key name instead of value
  • Assuming None if not careful with key format
  • Thinking it causes an error without context
4. A developer tries to use Memcached but notices cache misses even after setting keys. Which of these is a likely cause?
medium
A. The database is down, so cache cannot be used
B. Memcached does not support key-value storage
C. The cache keys are expiring too quickly due to a low TTL setting
D. Memcached requires SQL queries to retrieve data

Solution

  1. Step 1: Understand Memcached key expiration

    Memcached keys have a TTL (time to live). If TTL is too low, keys expire quickly causing cache misses.
  2. Step 2: Eliminate incorrect options

    Memcached supports key-value storage, does not depend on database status, and does not use SQL queries.
  3. Final Answer:

    The cache keys are expiring too quickly due to a low TTL setting -> Option C
  4. Quick Check:

    Low TTL causes cache misses = A [OK]
Hint: Check TTL settings if cache misses occur [OK]
Common Mistakes:
  • Thinking Memcached lacks key-value support
  • Assuming database status affects cache directly
  • Confusing Memcached with SQL databases
5. You are designing a high-traffic web app that uses Redis as a distributed cache. To ensure data consistency when multiple servers update the same cache key, which approach is best?
hard
A. Use Memcached instead because it handles concurrency automatically
B. Allow all servers to update cache keys without coordination
C. Disable caching for keys that are frequently updated
D. Use Redis transactions or Lua scripts to perform atomic updates

Solution

  1. Step 1: Identify concurrency challenges in distributed cache

    When multiple servers update the same key, race conditions can cause inconsistent data.
  2. Step 2: Choose atomic update methods in Redis

    Redis supports transactions and Lua scripts to ensure updates happen atomically, preventing conflicts.
  3. Step 3: Evaluate other options

    Allowing uncoordinated updates risks inconsistency; disabling caching reduces performance; Memcached lacks built-in atomic scripting.
  4. Final Answer:

    Use Redis transactions or Lua scripts to perform atomic updates -> Option D
  5. Quick Check:

    Atomic updates ensure consistency = D [OK]
Hint: Use Redis atomic commands for safe concurrent updates [OK]
Common Mistakes:
  • Ignoring concurrency causing stale data
  • Thinking Memcached handles concurrency automatically
  • Disabling cache unnecessarily reduces speed
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