In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the differences between Redis and Memcached". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
To analyze their differences, we should compare them mainly from the following aspects:
Thread model
Data structure
Elimination strategy
Pipelines and transactions
Persistence
High availability
Clustering
Thread model
To talk about performance, their service models must be analyzed.
Memcached uses multithreading model to process requests, and based on IO multiplexing technology, the main thread receives the request and distributes it to sub-threads.
The advantage of this is that when one request is time-consuming, it does not affect the processing of other requests.
Of course, the disadvantage is that the multithreaded switching of CPU must have performance loss, and at the same time, multithreading must be locked when accessing shared resources, which will degrade the performance to a certain extent.
Redis also uses IO multiplexing technology, but it uses a single-thread model to process requests, from receiving requests to processing data in a single thread. It is recommended to take a look at this article, "is Redis single-threaded or multithreaded? ".
This means that with Redis, once a request takes a long time to process, the entire Redis will be blocked, and the next request will not be processed until the request is returned, and complex and time-consuming operations must be avoided when using Redis.
The advantage of single thread is that it has less context switching loss of CPU and no lock competition of multi-thread access resources, but the disadvantage is that it can not take advantage of the multi-core performance of CPU.
Because Redis is an in-memory database, its access speed is very fast, so its performance bottleneck lies not in CPU, but in memory and network bandwidth, which is the main reason why the author adopts the single-thread model. At the same time, single thread is very friendly to program development and convenient to debug. The development of multithreaded programs will inevitably increase the difficulty of debugging.
Therefore, when the data of our business using key is relatively large, the access performance of Memcached is better than Redis. If the key data is small, there is not much difference between the two.
Strictly speaking, Redis's single thread refers to the thread that processes the request, and there are other threads working on it, such as other threads used to process time-consuming tasks asynchronously.
Redis6.0 further improves multithreading by using multithread when receiving and sending requests, which further improves the processing performance.
Data structure
Memcached supports a single data structure and only supports operations of type string. And the size limit for value must be below 1MB, and the expiration time cannot exceed 30 days.
Redis supports a wealth of data structures, in addition to the commonly used data types string, list, hash, set, zset, you can also use geo, hyperLogLog data types.
When using Memcached, we can only serialize the data and write it to Memcached. Then read the data from Memcached, and then de-serialize it into the format we need, which can only be "saved and fetched".
Redis can use different operation methods for different data structures, which is very flexible.
List: you can easily build a linked list or use it as a queue
Hash: flexibly manipulate the fields we need, such as "whole storage zero fetching", "zero saving integer fetching" and "zero saving zero fetching".
Set: construct a non-repeating set, and easily perform subtraction and union operations
Zset: build a ranking, or a list with weights
Geo: used for map-related services, identifying the coordinates of two locations and calculating their distances
HyperLogLog: use very little memory to calculate UV
In a word, because Redis provides such a rich data structure, it shines brilliantly in the field of in-memory database in recent years, which provides great convenience for our business development. Follow the official account Java technology stack to get more detailed usage tutorials of data types.
Elimination strategy
Memcached must set the memory limit of the entire instance. When the data reaches the upper limit, the LRU elimination mechanism will be triggered, giving priority to the elimination of data that is not commonly used.
But there are some problems with its data elimination mechanism: newly written data may be eliminated first, which is mainly caused by its own memory management design mechanism.
Redis has no limit that the memory limit must be set, and if the memory is used sufficiently, Redis can use enough memory. It is recommended to take a look at "what if the Redis memory is full"
At the same time, Redis provides a variety of phase-out strategies:
Volatile-lru: obsolete according to LRU mechanism from expired key
Allkeys-lru: obsolete according to LRU mechanism in all key
Volatile-random: randomly phase out key in expired key
Allkeys-random: randomly phasing out key in all key
Volatile-ttl: give priority to the elimination of key that is about to expire recently
Volatile-lfu: obsolete according to LFU mechanism in all key
Allkeys-lfu: obsolete according to LFU mechanism in expired key
We can use different data elimination strategies for business scenarios.
Pipelines and transactions
Redis also supports the pipeline function. The client packages and sends multiple commands to the server at one time, and the server processes the commands sent by the client in turn. This reduces the number of network IO trips back and forth and provides high access performance.
In addition, it also supports transactions, and the transactions mentioned here are not as strict as the transaction model of MySQL, which is unique to Redis.
General affairs will be used in conjunction with the pipeline, and the client packages and sends multiple commands to the server at a time, and identifies that these commands must be executed in strict order and cannot be interrupted by other clients. Before executing the transaction at the same time, the client can tell the server that a certain key will perform relevant operations later. If the client changes the key before operating the key, then the current client will abandon the entire transaction operation when executing these commands to ensure consistency.
Persistence
Memcached does not support data persistence, and if the Memcached service goes down, all data on this node will be lost.
Redis supports persistence of data on disk. RDB and AOF are available:
RDB: snapshot the data from the entire instance to disk for full persistence
AOF: persist every write command to disk and persist incrementally
Redis uses these two methods to cooperate with each other to ensure data integrity and minimize data loss caused by service downtime.
High availability
Memcached has no master-slave replication architecture and can only be deployed on a single node. If the node goes down, all the data of that node will be lost. The business needs to be compatible with this situation, and when one node is not available, write the data to other nodes to reduce the impact on the business.
Redis has a master-slave replication architecture, and two nodes form a master-slave architecture, which can synchronize master data in real time and improve the availability of the whole Redis service.
At the same time, Redis also provides the sentinel node, when the master node is down, actively promote the slave node to the master node and continue to provide services. A series of tutorials such as how to integrate Redis Sentinel with Spring Boot can follow the official account Java technology stack search to read.
The master and slave nodes can also provide read-write separation function to further improve the performance of program access.
Clustering
Both Memcached and Redis are clustered by multiple nodes to provide services, but their mechanisms are also different.
The clustering of Memcached is that the client uses the consistent hashing algorithm to send data to the specified node, and when one node goes down, other nodes will share the request of that node.
Redis clustering uses each node to maintain part of the virtual slot, through the key hash calculation, the key is mapped to the specific virtual slot, and this slot is then mapped to the specific Redis node.
At the same time, each Redis node contains at least one slave node, which forms the master-slave architecture, which further improves the high availability capability of each node.
When adding or going offline, you need to manually trigger data migration and re-hash slot mapping.
Redis's official clustering solution is Redis cluster, which uses a decentralized design. There are also third-party clustering solutions that adopt the centralized design proxy approach, such as Codis and Twemproxy.
Summary
A comparative analysis is made from the above aspects, and the summary is as follows.
Overall, Redis provides a wealth of functionality, and its performance is roughly the same as that of Memcached, which is why it has taken the lead in in-memory databases in recent years.
This is the end of the content of "what are the differences between Redis and Memcached". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.