Memcached vs Redis, Which One to Pick?
Ranjeet Vimal
Investing / Acquiring SaaS biz | 2X Founder I Tech, Product, Growth
Memchached or Redis? Which one to use? This is the most common question arises in every technical discussion when we talk about performance improvement. When performance needs to be improved, caching is often the first step taken, and Memcached or Redis are typically the first places to turn. Which one will give us better performance? What are pros and cons of Redis and Memcached?
While designing any caching system we consider following points:
- Read/write speed.
- Memory usage.
- Disk I/O dumping.
- Scaling.
I'm writing this tutorial considering that you already know about basic of cache.
Similarities between Redis & Memchached :
Both Memcached and Redis serve as in-memory, key-value data stores, although Redis is more accurately described as a data structure store. Redis is In-memory data structure store, used as database, cache and message broker. Both Memcached and Redis belong to the NoSQL family of data management solutions, and both are based on a key-value data model. They both keep all data in RAM, which of course makes them supremely useful as a caching layer.
As of today every major feature and strength memcached offers are now a subset of redis's features and strengths. Any use case you might use memcached for redis can solve equally well. They are both lightning fast as volatile caches. While that's all that memcached is its only the tip of the redis iceberg. Memcached is a volatile in-memory key/value store. Redis can act like one (and do that job as well as memcached), but it is a data structure server.
Why Memcached?
Memcached could be preferable when caching relatively small and static data, such as HTML code fragments. Memcached's internal memory management, is more efficient in the simplest use cases because it consumes comparatively less memory resources for metadata.
Memcached's memory management efficiency diminishes quickly when data size is dynamic, at which point Memcached's memory can become fragmented. Also, large data sets often involve serialized data, which always requires more space to store. If you are using Memcached then data is lost with a restart and rebuilding cache is a costly process.
Another scenario in which Memcached has an advantage over Redis is in scaling. Because Memcached is multithreaded, you can easily scale up by giving it more computational resources. Redis, which is mostly single-threaded, can scale horizontally via clustering without loss of data. Clustering is an effective scaling solution, but it is comparatively more complex to set up and operate. Memcached does not support replication.
Memcached is very good to handle high traffic websites. It can read lots of information at a time and give you back at a great response time. Redis can also handle high traffic on read but also can handle heavy writes as well.
Why Redis?
Redis has five primary data structures to choose from, opening up a world of possibilities to the application developer through intelligent caching and manipulation of cached data. Because of its data structures ( Stores data in a variety of formats: list, array, sets and sorted sets ) Redis as a cache gives you a lot of power and greater efficiency overall. Caches employ a mechanism called data eviction to make room for new data by deleting old data from memory. Memcached's data eviction mechanism employs a Least Recently Used algorithm and somewhat arbitrarily evicts data that's similar in size to the new data.
Redis allows for fine-grained control over eviction, letting you choose from six different eviction policies. Redis supports both lazy and active eviction, where data is evicted only when more space is needed or proactively. Memcached, on the other hand, provides lazy eviction only.
Here are a few of the features that redis offers to be used as a "real" data store instead of just a cache.
- Powerful data types and powerful commands to leverage them. Hashes, Sorted Sets, Lists, and more.
- Persistence to disk, by default.
- Transactions with optimistic locking (WATCH/MULTI/EXEC)
- Pub/sub. Extremely fast.
- Values up to 512MB in size (memcached limited to 1MB per key)
- Lua scripting (as of 2.6)
- Built in clustering (as of 3.0)
- Extremely fast at everything
The powerful data types are particularly important. They allow redis to provide a fantastic shared queue (lists), a great messaging solution (pub/sub), a good place for storing sessions (hashes), and a compelling place for high score tracking (sorted sets). These are just a few examples that scratch the surface.
Conclusion
The performance and memory usage of Redis compared to memcached is relatively similar. Unless you already have a large investment in memcached, going forward redis is the obvious solution.Not only Redis is better option, it enables whole new types of use cases and usage patterns.
Some Example Applications where Redis can be extremely usefull:
E-Commerce Applications : Most of the e-commerce applications are ready heavy. Redis can speed up your page load. You can store all your configurations in Redis reading these configuration from memory will be very fast. You can also store full page cache in Redis since its per key storage is pretty high. You can also store sessions into redis.
IOT Applications: In IoT applications, iot devices send data very frequently to server like few thousands data per seconds. You can push all these high volume raw data to Redis to process before storing it to any persistent storage.
Real Time Analytics : One can implement a real time analytics engine on memcached and backed to a database but redis is really good at counting lists and sets of things. Of all of redis's features, its ability to do key/value to sorted sets is where it exceeds memcached -- and counting something like page clicks per sets of pages and then summing those numbers together into analytics which can be pumped via a worker into a bigger analytics engine is one place where the Redis choice is the right one.
One last thing: regardless of the choice, a caching system is not a database.Man cannot live on caching alone. A system needs caching and a database.
Link: https://www.ranjeetvimal.com/memcached-vs-redis-one-pick/
Research Assistant @CSULB, Ex Software Developer at Tradeindia.com-Infocom Network Ltd
2 年Good Explanation Cleared all Caching doubts sir
Cloud Integration Senior Tech Manager
4 年Hi Vimal This is good article clearly explaining the difference b/w REDIS and MEMCACHED. Which is costlier in long term for similar data usage. These 2 works only for RDS right? These cannot act like CACHE for NOSQL databases.
Customer - Team - Product | SAFe Agile POPM & CSM
4 年Good article! Thanks for the insights!
Engineering Manager @ Maersk GSC | DDD,TDD,Micro-services,Azure
4 年While writing the detailed article on Redis, I got to know about this post, and it helps a lot to get a lot of information about the Redis.
Project leader at Mylangroup
4 年Thanks