当前位置:网站首页>Analysis of similarities and differences between redis and memcached in cache use

Analysis of similarities and differences between redis and memcached in cache use

2022-06-24 14:35:00 brookwang

Understand their usage scenarios , We must first know their common ground and differences .

Common ground :

1.Memcached And Redis All belong to memory 、 Key value data storage scheme , All are nosql Outstanding representatives in the database .

2. Almost all major programming languages have support memcached and redis Client library , The easier , Very low threshold .

3. Both are data stored in memory , This also leads to almost the same characteristics ( And indicators ) performance 、 Data throughput and latency of the load .

4.Memcached And Redis The server can be easily installed in a few minutes , For learners, the cost is very low .

5.Memcached No authentication ,Redis There is no authentication by default .( Lack of authentication is also a reason to perform well under high loads , Of course, if someone knows the port and ip, The consequences are serious , This is also the current redis The biggest security risk , Many well-known Internet projects are not authenticated at present )

It's important , Differences :

1.Memcached Limit key names to 250 byte , The value is also limited to no more than 1MB, And it only applies to ordinary strings , Redis The key name and value of both support 512M,MC It is estimated that many people have encountered this pit .

2.Memcached Only string storage is supported , It almost limits its application scenario to just reading data ,Redis Support string , Hash , list , aggregate , Ordered set , Its application scenario expands instantly N times , Its rich internal functions and storage types support a variety of complex operations , There is a self increasing and self decreasing function in the list scene processing , The list can be queued in the queue processing scenario , Out of the team , Set functions can be added or deleted , Ordered sets can also be sorted , Calculation .

3.Memcached No data persistence scheme , Just restart , No data ,Redis It also provides optional and adjustable data persistence schemes ,RDB( snapshot ) and AOF( Copy ) Two kinds of , Administrators can control risks according to their needs , By setting... In the configuration file , keep redis Persist to disk for a certain period of time , Even restart when encountering , Power failure, etc , And keep the data still , This feature will be useful in some businesses redis As a persistent storage solution, it has a very positive significance .

4.Redis There is also a very important , Far better than Memcached Characteristics of ,Memcached No support for master-slave ,Redis It can provide replication function , Support the master and slave , The replication function can help the cache system to achieve a high availability configuration scheme , So as to continue to provide uninterrupted cache service for applications in case of failure , The master-slave configuration is also a standard configuration with high reliability .

5.Memcached Our data collection mechanism uses LRU( That is, the minimum recent use ) Algorithm ,Redis Adopt data recycling mechanism , It can delete old data from memory to provide the cache space necessary for new data .

6.Memcached No authentication ,Redis Although there is no authentication by default , But it supports authentication , Although almost no one has set up Authentication .

7.Memcached The maximum expiration time of is one month , Otherwise, it will fail to write ( I've stepped on this pit ),Redis There is no maximum expiration time limit yet , But because the caching mechanism is different , In less than 2.1.3 Of redis In the version , Only right key Set once expire.redis2.1.3 And later versions , You can do it many times key Use expire command , to update key Of expire time.

8.Redis A single thread ,Memcached Is a multithreaded , If the data structure is only KEY-VALUE, And VALUE Too much value for , It is best to Memcached, Because under single thread GET Oversized data , Can cause congestion , And so on Redis Slow sentences of ( Such as KEYS,HGETALL etc. ) It is better not to execute in the production environment .

9.Memcached The expiration policy of is lazy deletion ,Redis The expiration policy of is lazy deletion + Delete periodically .

(1) Expired deletion refers to the next GET If the value to is expired, it will be deleted .

(2) Periodic deletion refers to the random inspection of a specified quantity at regular intervals ( The default is 20) There is an expiration date KEY, If it expires, delete .

When Memcached When the used memory is greater than the set maximum memory usage , To make room for new data items ,Memcached It will start LRU Algorithm ( Recently at least use ) Obsolete data items .

So through the above analysis , although Redis And Memcached Both are excellent and proven caching solutions , Each have advantages and disadvantages .

10.Memcache Used Slab Allocator Memory allocation mechanism : According to the predetermined size , Splits allocated memory into blocks of a specific length , To completely solve the memory fragmentation problem .

Memcache The storage of involves slab,page,chunk Three concepts

1.Chunk For fixed size memory space , The default is 48Byte.

2.page Corresponding to the actual physical space ,1 individual page by 1M.

3. Same size chunk Also known as slab.

原网站

版权声明
本文为[brookwang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241426125401.html