Redis face questions set

Original link: https://my.oschina.net/u/1186503/blog/1632949

Redis 1. What are the benefits?

(1) faster because the data stored in memory, similar to HashMap, HashMap advantage is to find the time and complexity of the operation is O (1) 
(2) support for rich data types, support string, list, set, sorted set , hash 
(3) support services, operations are atomic, so-called atomic changes to the data that is either all executed or not executed all 
(4) rich feature set: can be used for caching, message, press the key to set the expiration time, after the expiration will be automatically deleted

2. redis memcached What are the advantages compared to?

All values (1) memcached are simple string, as an alternative Redis support richer data type 
(2) redis is much faster than the memcached 
(. 3) can persist its data Redis

3. redis common performance problems and solutions:

(1) Master best not to do any persistent work, such as RDB AOF memory snapshots and log files 
(2) if the data is more important, a Slave open AOF backup data, policy is set to sync once per second 
(3) In order to master-slave copy speed and stability, master and Slave in a preferably connected within the same local area network 
(4) to avoid a large increase from the library on the pressure in the main database 
(5) from the master do not replicate Fig structure, unidirectional list structure more stable, namely: Master <- Slave1 <- Slave2  <- slave3 ...
such a structure convenient single point of failure to solve the issue and realize the replacement of Master of Slave. If the Master hung up, you can turn immediately Slave1 do Master, the other unchanged.

4. MySQL there 2000w data, only the data stored in redis 20w, how to ensure that the data is hot data in redis

Knowledge: redis-memory data set size up to a certain size and they will perform data elimination strategy. redis offers six data out of the policy:

voltile-lru: selection of the least recently used data out of the set of data set expiration time (server.db [i] .expires) in

volatile-ttl: selection of data to be expired expiration time has been set out from the data set (server.db [i] .expires) in

volatile-random: the expiration time has been set from the data set (server.db [i] .expires) arbitrarily selected out of the data

allkeys-lru: selection of the least recently used data out from the data set (server.db [i] .dict) in

allkeys-random: selecting out data from the data set (server.db [i] .dict) any

no-enviction (expulsion): prohibits the expulsion data

5. Memcache difference with Redis what are?

1), storage

Memecache all the data exists in memory, it will hang after power failure, data can not exceed the memory size.

Redis has a part on the hard disk, this will ensure persistent data.

2) data type support

Memcache for data type support is relatively simple.

Redis complex data types.

3) using different underlying model

Application protocol for communication between them and between the underlying implementations of the client are not the same.

Redis VM build their own direct mechanism, because most of the system call system function, it will waste some time to move and requests.

4), value size

redis can be up to 1GB, while memcache only 1MB

6. Redis common performance problems are what? How to solve?

1) .Master write memory snapshot, save rdbSave command scheduling function, can block the main thread of the work, when the snapshot is relatively large impact on performance is very large, it will be suspended intermittently, so Master is best not to write a memory snapshot.

2) .Master AOF persistent, if not rewrite AOF documents, the impact of this persistent way on performance is minimal, but AOF files grow, AOF file too much effect on the speed of recovery Master restart. Master best not to do any persistent work, including memory snapshots and AOF log files, especially not enable memory snapshots are persistent, if the data is more critical, open a Slave AOF backup data, sync strategy once per second.

3) .Master call BGREWRITEAOF AOF rewrite the file, when AOF rewrite will account for a large amount of CPU and memory resources, resulting in service load is too high, a brief suspension of service phenomenon.

4). Redis replication master from performance problems, in order from the master copy speed and stability, the Slave and Master is preferably connected to the same LAN

7, redis most appropriate scene

Redis most suitable for all data in-momory scene, although Redis also provide lasting capabilities, but is actually more of a disk-backed function, with persistence in the traditional sense has a relatively large difference, then we will probably have questions, it seems more like an enhanced version of Redis Memcached, then when to use Memcached, Redis when to use it? 
If you simply compare the difference between Redis and Memcached, most will get the following points: 
1, Redis not only support simple k / v types of data, while also providing a storage list, set, zset, hash and other data structures. 
2, Redis supports the backup data, i.e., data backup master-slave mode. 
3, Redis supports data persistence, data in memory can be kept on disk, restart when you can load be used again. 
(1), the session cache (Session Cache)

The most common scenario is using Redis session cache (session cache). Redis caching session with than other storage (such as Memcached) has the advantage of: Redis provide persistence. When not strictly required to maintain a cache coherency, if the user's shopping cart information lost, most people will be unhappy, and now, they will do so?

Fortunately, with the improvement of Redis these years, it's easy to find the right document how to use Redis caching session. Even well-known business platform Magento also offers Redis plug-ins.

(2), page caching (FPC)

In addition to the basic session token, Redis also provides a very convenient platform for FPC. Back consistency, even restart Redis instance, because of the persistent disk, users will not see a decrease page loading speed, which is a great improvement, similar to PHP local FPC.

Magento again as an example, Magento offers a plug-in to use Redis as a full-page cache backend.

In addition, the WordPress user, Pantheon has a great plugin wp-redis, this plugin can help you with the fastest speed of loading pages you've visited.

(3), the queue

Reids a big advantage in the field memory storage engine is to provide a list and set operations, which makes Redis can serve as a good platform to use the message queue. Redis used as an operation queue, similar to native language (e.g., Python) push on the list / pop operations.

If you do a quick search for "Redis queues" in Google, you'll be able to find a large number of open source projects, the purpose of these projects is to use Redis create a very good back-end tools to meet the needs of various queues. For example, Celery has a background is to use Redis as a broker, you can go to see from here.

(4), charts / counter

Redis digital up or down in memory operations are implemented very well. Collection (Set) and an ordered set (Sorted Set) also allows us the time to perform these operations become very simple, Redis just provide just these two data structures. So, we need to sort the collection to get the highest ranked 10 users - what we call "user_scores", we just need to execute something like the following:

Of course, this assumes you are doing increasing user score based on your sort. If you want to return the user and the user's score, you need to perform:

ZRANGE user_scores 0 10 WITHSCORES

Agora Games is a good example, implemented in Ruby, its ranking is to use Redis to store data, you can see here.

(5), publish / subscribe

Last (but certainly not least) is Redis publish / subscribe functionality. Publish / Subscribe usage scenario is indeed very much. I have seen people use social networking connections, but also to build a chat system based publish / subscribe Script Triggers, and even publish Redis / subscribe functionality! (No, it's true, you can go to verify).

Redis offers all the features, I feel like this is the least of a person, although it provides users if this multifunction.

Reproduced in: https: //my.oschina.net/u/1186503/blog/1632949

Guess you like

Origin blog.csdn.net/choy9999/article/details/100591124