redis series --Redis interview summary

 

Make advertising, selling things to help a friend, something super cheap yo [] clothes, shoes, etc. , factory direct shipping, is lower than the market price! ! Generally cheaper than the market price of 3-7 fold [are] brand goods, if you are interested, you can scan two-dimensional code bottom of the screen, thanks attention! ! !

Micro letter

  • What is redis

        Redis is a memory-based high-performance key-value database.

  • Reids features

        Is the Key-Value Redis essentially a type of memory database, the memcached like, operates the entire database in memory to load all of them, the asynchronous operation by periodically flush the database data to be stored on the hard disk. Because it is pure memory operation, Redis performance is very good, can handle more than 100,000 times per second read and write operations, are known to the fastest performance of Key-Value DB.

        Redis not only excellence of performance, Redis greatest charm is to support a variety of data structures stored, in addition to the maximum limit of a single value is 1GB, unlike memcached data can only be saved 1MB, so Redis can be used for many useful function, for example to do with his List FIFO doubly linked list to achieve a lightweight, high-performance message queuing service, with his Set can do high-performance tag systems. In addition Redis can also set expire time of deposit of the Key-Value, it can also be used as a function enhanced version of memcached to use.

        The main disadvantage of Redis database capacity by limiting physical memory, reading and writing massive data can not be used in high-performance, and therefore the appropriate scene Redis mainly confined to the small amount of data and high-performance computing operations.

  • Redis data types supported

        Redis to distinguish between different types of single-valued by Key-Value, the following is supported types: String, List, Set, Sorted Set, hash

  • Why redis need to put all the data into memory

        Redis To achieve the fastest read and write speed data into memory, and by asynchronously writing data to disk. Therefore, fast and redis data persistence features. If you do not put the data in memory, disk I / O speed to seriously affect the performance of redis. In memory getting cheaper today, redis will become increasingly popular.

        If you set the maximum memory used, the data has been recorded can not continue to insert a new value after the number reached memory limit.

  • Redis is single-threaded single-process

        redis queuing technologies will become concurrent access serial access, eliminating the overhead of traditional database serial control.

  • Virtual Memory

        When your key small and great value when using VM effect will be better. It saves memory is relatively large; when your key is not small, consider using some very big key method will become a great value, for example, you might consider a combination of key, value into a new value.

        vm-max-threads of this parameter, you can set the number of threads to access swap files, settings should not exceed the number of core machine, if set to 0, then all operations on the swap file is sequential. It may result in relatively long delays, but there is a good guarantee data integrity.

        When his tests found that virtual memory performance is also good. If large amounts of data, consider a distributed or other databases.

  • distributed

        redis support master-slave mode. Principle: Master data will be synchronized to the slave, and the slave does not synchronize data to the master. It connects master to synchronize data when Slave start. This is typical of a distributed model for separate read and write. We can use the master to insert the data, slave to provide search services. This can effectively reduce the number of concurrent access to a single machine

  • Model separate read and write

        By increasing the number of Slave DB, read performance can increase linearly. To avoid single points of failure of the Master DB, generally using two cluster Master DB do hot standby, so the availability of the entire cluster of reading and writing are very high.

        Defects separate read and write architecture that whether it is Master or Slave, each node must preserve the integrity of the data, if in the case of large amount of data, scalability cluster storage capacity is limited to a single node, but also for write-intensive type of application, the architecture is not suitable for separate read and write.

  • Model data pieces

        In order to separate read and write address the shortcomings of the model, the data model can be applied fragments come. Each node can be thought of are independent master, and implement service data piece through. The above combination of the two models, each of the master may be designed to model consists of a plurality of master and slave thereof.

  • Redis recovery strategy

volatile-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

  • What are the benefits of using Redis there

(1) faster because the data stored in memory, similar to HashMap, HashMap advantage is the time complexity of finding and operations are 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, will be automatically deleted after the expiration

  • What are the advantages compared to redis memcached

All values ​​(1) memcached are simple strings, redis as its replacement, support richer data types

(2) redis much faster than memcached

(3) redis can persist its data

  • redis common performance problems and solutions

(1) Master best not to do any persistent work, such as RDB memory snapshots and log files AOF

(2) if the data is more important, a Slave open AOF backup data, policy is set to sync once per second

(3) from the master copy to the speed and stability, Master and Slave connection is preferably the same LAN

(4) try to avoid an increase from the library a lot of pressure on the main library

(5) Do not use the master copy from FIG like structure, with a more stable way linked list structure, 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.

  • There 2000w MySQL 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 policy " Redis recovery strategy ."

  • Redis what are the common performance problems

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

  • redis most appropriate scene

(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) full-page cache (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) a 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
  • Why redis need to put all the data into memory

        Redis To achieve the fastest read and write speed data into memory, and by asynchronously writing data to disk. Therefore, fast and redis data persistence features. If you do not put the data in memory, disk I / O speed to seriously affect the performance of redis. In memory getting cheaper today, redis will become increasingly popular. If you set the maximum memory used, the data has been recorded can not continue to insert a new value after the number reached memory limit.

Published 107 original articles · won praise 184 · views 210 000 +

Guess you like

Origin blog.csdn.net/qq_22172133/article/details/86502836