Memchached alsoIs Redis?

Which one should I use? This is one of the most common questions in every technical discussion when we talk about improving performance. Whenever performance needs to be improved, adopting caching is often the first step. In the meantime, choosing Memcached or Redis is usually the first consideration. Which gives us better performance? What are their advantages and disadvantages?

When designing any caching system, we consider the following:

  • Read/write speed

  • Memory usage

  • Disk I/O dump.

  • Scalability.

I wrote this tutorial with the knowledge that you already know the basics of caching.

Similarities between Redis & Memchached:

Both Memcached/Redis provide memory-based, key-value data storage, although Redis is more accurately called structured data storage. Redis is an in-memory structured data store for databases, caches, and message brokers. Both (Memcached/Redis) belong to the NoSQL family of data management schemes and are key-value based. They both hold data in memory, which of course makes them particularly useful as a caching layer.

As of today, every major feature that Memcached offers and its benefits is a subset of Redis features and capabilities. Redis can be used equally anywhere Memcached might be used in a use case. They are lightning fast caches. Memcached provides just the tip of the iceberg of what Redis has. Memcached is a key-value store based on volatile memory. Redis can do this just as well (just as well as Memcached), but it is still a structured data server.

Why Memcached?

Memcached may be preferable when the cache is relatively small and you use static data, such as HTML code snippets. Memory management within Memcached is more efficient in the simplest use cases because its metadata consumes relatively less memory resources.

When the data size is dynamic, Memcached’s memory management efficiency degrades quickly, and Memcached memory becomes fragmented. Also, large data sets often involve data serialization, which requires more space to store. If you use Memcached, the data will be lost with a reboot, and rebuilding the cache is an expensive process.

Another scenario where Memcached has an advantage over Redis is scalability. Because Memcached is multithreaded, you can easily scale it by giving it more computing resources. Redis is single-threaded and can scale horizontally with clustering without loss. Clustering is an effective extension scheme, but it is relatively complex to configure and operate. Memcached does not support replication (automatically copying data from one machine to another).

Memcached is ideal for dealing with high-traffic sites. It can read a large amount of information at once and return it with excellent response time. Redis can handle heavy writes as well as high-traffic reads.

Why Redis?

Redis has five main data structures to choose from. By intelligently caching and processing cached data, it opens up a whole new world of possibilities for application developers. Redis offers more power and overall better efficiency as a caching system because of its data structure (storing data in multiple formats: lists, arrays, collections, ordered collections). Caching uses a mechanism called “data reclamation” to make room for new data by removing old data from memory. Memcached’s data reclamation mechanism uses the Least Recently Used (LRU) algorithm, but it is somewhat arbitrary to recycle data that is approximately the size of the new data.

Redis allows fine-grained control of recycling, allowing you to choose from six different recycling strategies. Redis supports both lazy (passive) and active recycling, with data being reclaimed only when more space is needed or active activation is required. Memcached, on the other hand, only supports lazy collection.

Here are some features provided by Redis that can be used for “real” data storage, not just caching.

  • Powerful data types and powerful command support to take advantage of them. Hash, ordered sets, lists, etc.

  • Default disk persistence support

  • Transaction support with optimistic locks (WATCH/MULTI/EXEC)

  • Publish/subscribe, very fast

  • Up to 512MB key value size limit (Memcached key value is limited to 1MB)

  • Lua Script support (2.6 and later)

  • Built-in cluster support (3.0 and later)

  • Everything was very fast

Strong data types are especially important. They allow Redis to provide an excellent shared queue (list), a great messaging solution (PUB/SUB), a great place to store session information (Hashes), and an impressive high-score tracking area (sorted sets). They are simply examples of usage that can be explored.

conclusion

Redis is quite similar in performance and memory usage compared to Memcached. Unless you’ve already invested a lot of money in Memcached, Redis is the obvious solution to move forward. Not only is Redis the better choice, it also supports new types of use cases and usage patterns.

Some sample applications Redis might be useful for:

E-commerce apps: Most e-commerce apps are heavy, and Redis can speed up your page loading. You can store all the configuration files in Redis and get them out of memory very quickly. You can also store a full page cache in Redis because of its large key value capacity. You can also store session information in Redis.

Iot applications: In iot applications, iot devices send data to servers very frequently, such as thousands of messages per second. You can push these high-volume raw data to Redis before storing them in any persistent storage.

Real-time analysis: You can implement a real-time analysis engine on Memcached, backed by a database. But Redis is very good at counting lists and lists of things. Of all the Redis features, it has the ability to sort key values more than Memcached, to count the number of clicks on a set of pages, and to aggregate those numbers into an analytics system. This data can be fed into larger analytics engines by staff, and Redis is one of the right decisions to make in these applications.

One last thing: no matter what you choose, a caching system is not a database. You can’t just cache, the system needs both the cache and the database.


This is my comment. Overall, Redis features far better than Memcached and is a next-generation product. The choice of which to use seems clear. However, it must be noted that Redis’s single-threaded design also comes with some important pitfalls. Redis has data persistence, which, combined with the single-threaded nature of Redis, is a high risk area for Redis failures. The default RDB persistence blocks threads, making Redis unable to respond to normal requests and prone to numerous request errors on high-traffic sites. This is called “emergent” in the system, and of course it’s the bad one. Of course, later Redis also developed the AOF persistence mode (it is not opened by default, it must be manually opened), which alleviates the Redis persistence problem to some extent. Redis forks a child process to handle persistence separately. However, the fork function is not without cost. It also has the problem of consuming memory resources and affecting the main program to respond to requests. Blocking is a Redis application nightmare, just like Node.js. Therefore, when there is a failure, you can analyze the cause from this Angle, and it may be solved quickly.

Original: https://acejoy.com/2018/03/20/memcached-vs-redis- choose which one? /

By Ranjeet Vimal

Click on the menu “wechat group” to join the group and communicate with your partners!