When people talk about the Performance Improvement of an application, the one integral factor that everyone considers is server-side caching. Identifying the right cache provider that suits the requirement is an integral part of adopting the server-side caching.
Redis and Memcached are widely used open-source cache providers across the world. Most of the Cloud providers support Redis and Memcached out of the box.
In this article, I would like to share similarities and differences between the Redis and Memcached and when do we need to go for Redis or Memcached.
Similarities between Redis vs Memcached
- Key-value pair data stores
- Supports Data Partitioning
- Sub-millisecond latency
- NoSQL family
- Supported by the Majority of programming languages and Cloud providers
Memcached: Supports only simple key-value pair structure
Redis: Supports data types like strings, lists, sets, sorted sets, hashes, bit arrays, geospatial, and hyper logs.
Redis allows you to access or change parts of a data object without having to load the entire object to an applicational level, modify it, and then re-store the updated version.
Memcached: Strictly in memory and extended to save key-value details into drive using an extension extstore
Redis: Can store the details to disk when the physical memory is fully occupied. Redis has the mechanism to swap the values that are least recently used to disk and the latest values into the physical memory.
Memcached: Can only store the data of size up to 1 MB
Redis: can store the data of size up to 512 MB (string values)
Memcached: Doesn’t support data persistence
Redis: Supports data persistence using RDB snapshot and AOF Log persistence policies
Cluster Mode (Distributed caching)
Memcached: Memcached doesn’t support the distributed mechanism out of the box. This can be achieved on the client-side using a _consistent hashing _ strategy
Redis: Supports distributed cache (Clustering)
Memcached: Supports multithreading and hence can effectively use the multiple cores of the system
Redis: Doesn’t support multi-threading
Memcached: can ve vertically scalable. Horizontal scalability is achieved from the client-side only (Using consistent hash algorithm)
Redis: Can be horizontally scalable
Memcached: Doesn’t support data replication
Redis: Supports data replication out of the box. Redis Cluster introduces the master node and slave node to ensure data availability. Redis Cluster has two corresponding slave nodes for redundancy.
- Least Recently Used (LRU)
- No Eviction (Returns an error if the memory limit has been reached when trying to insert more data)
- All keys LRU (Evicts the least recently used keys out of all keys)
- Volatile LFU (Evicts the least frequently used keys out of all keys)
- All keys random (Randomly evicts keys out of all keys)
- Volatile random (Randomly evicts keys with an “expire” field set)
- Volatile TTL (Evicts the shortest time-to-live and least recently used keys out of all keys with an “expire” field set.)
- volatile LRU (Evicts the least recently used keys out of all keys with an “expire” field set)
- volatile LFU (Evicts the least frequently used keys out of all keys with an “expire” field set)
Memcached: Doesn’t support transactions
Redis: Support transactions
Memcached is recommended when dealing with smaller and static data. When dealing with larger data sets, Memcached has to serialize and deserialize the data while saving and retrieving from the cache and require more space to store it. When dealing with smaller projects, it is better to go with the Memcached due to its multi-threading nature and vertical scalability. Clustering requires a considerable amount of effort to configure the infrastructure.
Redis supports various data types to handle various types of data. Its clustering and data persistence features make it a good choice for large applications. Additional features like Message queue and transactions allow Redis to perform beyond the cache-store.
In addition to the above-mentioned features, Redis supports the below features as well
- Message queuing support (Pub/sub)
- snapshots for data archiving/restoring purpose
- Lua scripting
Conclusion: Redis and Memcached both can perform very well as a Cache store. Which one to pick varies from project to project.
It is wise to consider the pros and cons of the providers right from the inception phase to avoid changes and migrations during the project.
Hope you enjoyed the article. Please share your thoughts/ ideas in the comments box. Thank you for reading it.