Question: When should one use a distributed cache?


A distributed cache should be used when:

  1. Scalability is a concern. If your application needs to handle a large volume of data that exceeds the capacity of a single machine's memory, a distributed cache can distribute the data across multiple nodes, thereby increasing the overall available memory.

    from rediscluster import RedisCluster # Assumes that you have a Redis cluster set up on localhost ports 7000-7005 startup_nodes = [{"host": "", "port": "7000"}, {"host": "", "port": "7001"}] rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True) rc.set('foo', 'bar') print(rc.get('foo')) # Outputs: 'bar'
  2. High Availability is required. Distributed caches often replicate data across multiple nodes, so if one node fails, the system can retrieve the cached data from another node.

    # Example configuration for a replicated distributed cache in Ehcache Java library <ehcache> <cacheManagerPeerProviderFactory class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory" properties="peerDiscovery=automatic, multicastGroupAddress=, multicastGroupPort=4446, timeToLive=32"/> </ehcache>
  3. To achieve Load Balancing. A distributed cache can distribute traffic and load evenly across a network, reducing the risk of any single node becoming a bottleneck.

  4. When Fault Tolerance is needed. If a node goes down in a distributed caching system, the system will still operate by serving requests from other available nodes.

  5. When Geographical distribution of data is required. For applications that are globally deployed, having a distributed cache can help reduce latency by storing data closer to where end-users are located.

In summary, if your application needs to handle high volumes of data efficiently, requires high availability, is distributed geographically, and needs fault tolerance, then a distributed cache is an excellent choice. However, it's essential to note that while distributed caches offer many benefits, they also add complexity to your system. Therefore, they should only be implemented when necessary.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.