Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between Memcached and Redis?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

What is the difference between Memcached and Redis? for this question, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Memchached or Redis?

Which one should I use? This is the most common problem in every technical discussion when we talk about improving performance. Caching is often the first step whenever performance needs to be improved. At the same time, choosing Memcached or Redis is usually the first place to consider. Which can provide us with better performance? What are their advantages and disadvantages?

When designing any caching system, we consider the following points:

Read / write speed

Memory usage

Disk IPUBO dump.

Flexibility.

I wrote this tutorial considering that you already know the basics of these caches.

Similarities between Redis and Memchached:

Both Memcached/Redis provide memory-based, key-value data storage, although Redis is more precisely a structured data store. Redis is a structured data store in memory for databases, caches, and message agents. Both (Memcached/Redis) belong to the NoSQL family in the data management scheme and are based on key-value storage. They all hold data in memory, which of course makes them particularly useful as cache layers.

As of today, every major feature and benefit provided by Memcached is a subset of the functions and features of Redis. Redis can be used equivalently in any use case where Memcached may be used. They are all lightning fast caches. What Memcached offers is just the tip of the iceberg where Redis has features. Memcached is a key-value memory based on volatile memory. Redis can do the same (as well as Memcached), but it is still a structured data server.

Why Memcached?

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

When the data size is dynamic, the memory management efficiency of Memcached decreases rapidly, and the memory of Memcached will be fragmented. Moreover, large data sets often involve data serialization and always require more space to store. If you use Memcached, 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 extend it by giving it more computing resources. Redis is single-threaded and can scale horizontally through a lossless cluster. Cluster is an effective expansion scheme, but it is relatively complex to configure and operate. Memcached does not support replication (data is automatically copied from one machine to another).

Memcached is ideal for dealing with high-traffic websites. It can read a large amount of information at one time and return in excellent response time. Redis can handle not only high-traffic reads, but also heavy writes.

Why Redis?

Redis has five main data structures to choose from. Through the intelligent caching and processing of cached data, it opens up a new world of possibilities for application developers. Because of its data structure (using multiple formats to store data: lists, arrays, collections, ordered collections), Redis as a caching system provides more capabilities and overall better efficiency. Caching uses a mechanism called data collection to make room for new data by deleting old data from memory. Memcached's data collection mechanism uses the LRU (the least recently used by Least Recently Used-) algorithm, but it is a bit arbitrary to retrieve data about the size of the new data.

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

Here are some features that redis provides that can be used for "real" data storage, not just caching.

Powerful data types and powerful command support that can be utilized. Hashes, ordered collections, lists, etc.

Default disk persistence support

Transaction support using optimistic locks (WATCH/MULTI/EXEC)

Publish / subscribe function, extremely fast

Up to the upper limit of 512MB key size (Memcached each key value is limited to 1MB size)

Lua scripting support (version 2.6 and above)

Built-in cluster support (version 3.0 or above)

Everything is very fast.

Strong data types are particularly important. They allow Redis to provide an excellent shared queue (list), a great messaging solution (pub/sub), a good place to store session information (hashes), and a strikingly high score tracking area (sorted sets). They are just examples of usage that can be obtained from a simple discussion.

Conclusion

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

Some sample applications where Redis can be useful:

E-commerce applications: most e-commerce applications are heavy, and Redis can improve your page loading speed. You can store all the configuration files in Redis, and it will be very fast to read these configuration information from memory. You can also store a complete page cache in Redis because of its large key capacity. You can also save session information to Redis.

Internet of things applications: in Internet of things applications, Internet of things devices send data to the server very frequently, such as thousands of messages per second. You can push these high-capacity raw data to Redis before storing them in any persistent memory.

Real-time analysis: a real-time analysis engine can be implemented on Memcached, backed by database. But Redis is very good at statistical lists and a range of things. Of all the Redis features, it has more ability to sort key values than Memcached, and calculates data such as the number of clicks on a set of pages, and then aggregates these numbers into the analysis system. This data can be entered into a larger analysis engine by the staff, and choosing Redis in these applications is one of the right decisions.

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

This is my comment. Generally speaking, the functional features of Redis are much better than Memcached, and it is completely the next generation of products. It seems clear which one to use. However, it must be pointed out that the single-threaded design of Redis also brings some important hidden dangers. Redis has the function of data persistence, which, combined with the single-threaded feature of Redis, has become an area with high incidence of Redis failures. Default RDB persistence blocks threads, making Redis unable to respond to normal requests, and is prone to a large number of request errors on high-traffic websites. This is called "emergence" in the system, and of course this is a bad one. Of course, later, Redis also developed an AOF persistence method (which is not turned on by default and needs to be opened manually), which slows down the persistence problem of Redis to some extent. Redis fork a child process to handle persistence alone. However, the fork function is not without cost, it also has the problem of consuming memory resources and affecting the response of the main program to requests. Blocking is a nightmare for Redis apps, just like Node.js. Therefore, when there is a fault, we can analyze the reasons from this point of view, and may be able to solve it quickly.

The answer to the question about what is the difference between Memcached and Redis is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report