The difference between redis and memcache

Compare:

1. Redis single-threaded, memcache multi-threaded. Generally, the number of requests per second is not too large, and this problem can be ignored when choosing.

2. Redis is persistent (supports master-slave master-slave backup), and memcache data can only be placed in memory. In the face of machine power failure or restart, redis has little impact, and data loss is less or even not lost; and memcache is completely lost, so there is generally a single point of problem (although high availability can be achieved by some means).

3. Data consistency: redis is a single thread, so for a transaction, you can execute commands in sequence. Memcache provides a CAS mechanism (you can get the key version number first, and then call client.cas(key, newValue, casValue); you can ). Generally speaking, there are few problems with data consistency, but when multiple clients may modify a certain value at the same time (such as the remaining amount of money received from a red envelope), in order to ensure that the original value has changed during modification, here It is necessary to ensure data consistency.

4. The storage problem is finally converted into byte storage, but the value stored in the redis toolkit generally needs to be converted into String, and memcache has a toolkit to directly store Object (which needs to be serialized), but it must be serialized into byte format in the end. .

5. Storage format, redis has many data types, such as String, Hash, List, Set, Sorted Set. Among them, Hash (that is, similar to HashMap structure storage) abandons the operations of serialization and deserialization like memcache, and directly modifying a specific attribute, which is really convenient\(^o^)/; Sorted Set can be used to achieve Sort leaderboards and other functions.

6. Redis has advanced functions of "subscription-publishing system".

 7. Redis can repair data through the following commands, and restarting the server will automatically update and load. If the server is wrong, it is more stable to use the diff command.

redis-check-aof --fix appendonly.aof

 

 

For expansion, when sharding, it can be divided by range or consistent hash. Common implementations are 1. Fragmentation on the client side. 2. Fragmentation through a proxy, such as Twemproxy. 3. Query routing:

Consistent hash:

Definition: Consistent hashing is a special hashing algorithm. After using the consistent hashing algorithm, the change in the number of slots (size) of the hash table only requires remapping of {\displaystyle K/n} K/n keywords on average, where {\displaystyle K} K is the keyword's number, {\displaystyle n} n is the number of slots. However, in a traditional hash table, adding or removing a slot requires remapping of almost all keys.

        When expanding the capacity of some cached clusters, the consistent hash algorithm is often mentioned, which generally refers to adding a node with minor changes. If the hash value is positioned as 1-2 32 , a circle is formed, and the nodes of the cluster are divided into circles and placed in the corresponding position. When the request comes, the hash value is obtained according to the fixed value of the request ip or user information to find the nearest node access. (It can be understood as searching according to the binary tree rules). In this case, adding new nodes only affects half of the data between the original two old nodes. In addition, in order to prevent the hash value from being concentrated on a certain node, you need to implement the hash value calculation yourself, such as KETAMA_HASH of memcache.

        However, if the capacity of the web server load balancing machine is expanded, because no session is saved, the remainder algorithm can be used directly. In addition, load balancing includes : DNS domain name resolution load balancing (delay), data link layer load balancing (LVS), IP load balancing (SNAT), HTTP redirection load balancing (rare), and reverse proxy load balancing (nginx). Load balancing algorithms include: round robin algorithm, hash algorithm (ie consistent hash), weighted algorithm (ie weighted round robin), minimum connection algorithm, response speed algorithm, random algorithm (ie random value, and random weight probability can also be configured). Among them, the requests are mainly processed according to the number of requests, the number of connections, and the custom configuration weight.

 

Reverse proxy load balancing: nginx is commonly used, see an installation and configuration steps, take it when you have time.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326270507&siteId=291194637