WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Coding Interview Essentials · Understanding System Design · question 97 of 120

How would you design a distributed cache?

📕 Buy this interview preparation book: 120 Coding Interview Essentials questions & answers — PDF + EPUB for $5

Designing a distributed cache involves multiple considerations including consistent hashing, scalability, master-slave architecture, maintaining the replication factor, and ensuring fault tolerance.

1. **Consistent Hashing**: In a distributed cache system, many servers might be setup to cache incoming requests. One primary task is to decide which server will cache which request. A popular technique to decide this is to use consistent hashing.

Consistent hashing is a way to assign keys to nodes in such a way that when nodes are added or removed, only a minimal amount of key reassignments happen. In other words, it ensures that addition or removal of one node only affects the keys of that node and doesn’t affect the keys in other nodes. This is done by creating a hash ring where each node gets a position on the ring based on its hashed value.

When a key needs to be found in the cache, it is hashed and found on the ring. If a node has failed, the key will be found at the next node in the ring.

2. **Scalability**: Scalability is paramount in distributed caching as it should be possible to scale the number of servers up or down according to demand. This can be achieved by adding more servers to the hash ring of a consistent hashing algorithm.

3. **Master-Slave Architecture**: A master-slave architecture can be used to avoid single point of failure. In this setup, all the write operations go to the master node which then replicates the data to its slave nodes. Read operations can go to any of the master node or the slave nodes. This architecture provides load balancing and redundancy.

4. **Maintaining the Replication Factor**: Since caches are not a reliable data store, data is usually replicated across multiple cache servers to ensure that it is available even if one of the cache servers goes down. Setting up the replication factor might depend on the trade-off between cost and reliability that a system is willing to maintain.

5. **Fault Tolerance**: To make the system more reliable, the cache servers may keep a backup of the data in a persistent store. This data is only accessed when it’s not available in any of the cache servers. One of the cache servers will be responsible to keep this data synced with the cache.

The process for adding new servers, caching data, and fetching data is fairly linear:

- New servers are hashed and placed on the hash ring.

- Incoming data or request is hashed and assigned to a cache server based on the hash ring.

- When looking for data, if data is not available in the assigned server, the system moves in the clockwise direction on the hash ring to find an available server containing the data.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Coding Interview Essentials interview — then scores it.
📞 Practice Coding Interview Essentials — free 15 min
📕 Buy this interview preparation book: 120 Coding Interview Essentials questions & answers — PDF + EPUB for $5

All 120 Coding Interview Essentials questions · All topics