What is distributed caching and why is it used in system design?
Distributed caching is a method to store data across multiple cache servers to improve data retrieval speed and reduce load on databases. It helps systems scale and handle more users by keeping frequently accessed data closer to the application.
Click to reveal answer
beginner
Name two popular distributed caching systems and one key difference between them.
Redis and Memcached are popular distributed caching systems. Redis supports data persistence and advanced data structures, while Memcached is simpler and focuses on fast in-memory key-value storage without persistence.
Click to reveal answer
intermediate
How does cache invalidation work in distributed caching?
Cache invalidation is the process of removing or updating cached data when the original data changes. In distributed caching, this ensures that stale data is not served. Techniques include time-to-live (TTL), explicit invalidation commands, or event-driven updates.
Click to reveal answer
intermediate
What is cache sharding and why is it important?
Cache sharding splits the cache data across multiple servers based on keys. It helps distribute load evenly, improves scalability, and avoids a single cache server becoming a bottleneck.
Click to reveal answer
intermediate
Explain the difference between cache-aside and write-through caching strategies.
Cache-aside means the application loads data into the cache only when needed (on cache miss). Write-through means data is written to the cache and the database at the same time. Cache-aside reduces unnecessary cache writes, while write-through ensures cache and database are always consistent.
Click to reveal answer
Which of the following is a key feature of Redis but not Memcached?
AData persistence
BIn-memory key-value storage
CSimple caching
DDistributed cache
✗ Incorrect
Redis supports data persistence to disk, while Memcached does not.
What does cache sharding help to achieve?
ABackup cached data
BEncrypt cached data
CCompress cached data
DDistribute cache data across servers
✗ Incorrect
Cache sharding splits data across multiple servers to balance load and improve scalability.
In cache-aside strategy, when is data loaded into the cache?
AOnly on cache miss
BEvery time data is written
CPeriodically by the cache server
DNever
✗ Incorrect
Cache-aside loads data into cache only when the application requests data not found in cache.
Which technique helps prevent serving stale data in distributed caches?
ALoad balancing
BData compression
CCache invalidation
DData encryption
✗ Incorrect
Cache invalidation removes or updates outdated cached data to keep cache fresh.
Memcached is best described as:
AA file storage system
BA simple, fast in-memory key-value store without persistence
CA message queue system
DA persistent database
✗ Incorrect
Memcached is designed for fast, simple caching without data persistence.
Describe how distributed caching improves system performance and scalability.
Think about how having data closer to the application helps.
You got /5 concepts.
Explain the differences between Redis and Memcached and when you might choose one over the other.
Consider features and use cases.
You got /5 concepts.
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
Step 1: Understand the role of distributed cache
Distributed caches store frequently accessed data in fast memory to reduce the number of database queries.
Step 2: Identify the main benefit
This reduces the load on the database and speeds up data retrieval for repeated requests.
Final Answer:
To reduce database load by storing frequently accessed data in memory -> Option A
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
Step 1: Recall Redis command for setting data
Redis uses the command SET key value to store a value under a key.
Step 2: Verify other options
GET retrieves data, STORE and PUT are not valid Redis commands.
Final Answer:
SET key value -> Option A
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
Step 1: Analyze the cache set operation
The key 'user:1' is set with the value 'Alice' in the cache.
Step 2: Analyze the cache get operation
Retrieving 'user:1' returns the stored value 'Alice', which is then printed.
Final Answer:
'Alice' -> Option B
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
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.
Step 2: Eliminate incorrect options
Memcached supports key-value storage, does not depend on database status, and does not use SQL queries.
Final Answer:
The cache keys are expiring too quickly due to a low TTL setting -> Option C
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
Step 1: Identify concurrency challenges in distributed cache
When multiple servers update the same key, race conditions can cause inconsistent data.
Step 2: Choose atomic update methods in Redis
Redis supports transactions and Lua scripts to ensure updates happen atomically, preventing conflicts.