Redis Face Test Summary (1)February 28, 2018 17:42:21 lsx 丨 Mr. Pen read: 3568 more Personal Classification: A summary of the questions(1) What is Redis?
Redis is a high-performance, memory-based Key-value database. (Free to add, with an understanding error or inadequacy, please correct)
(2) Characteristics of Reids
Redis is essentially a key-value type of in-memory database, much like memcached, where the entire database is loaded in memory for operation, and the database data is periodically flush to the hard disk for storage by asynchronous operations. Because it is pure memory operation, Redis has excellent performance and can handle more than 100,000 read and write operations per second, which is the fastest known performance Key-value DB.
The great feature of Redis is not only performance, but also the greatest charm of redis is the ability to save a variety of data structures, and the maximum limit for a single value is 1GB, unlike memcached, which can only hold 1MB of data, so Redis is used to implement many useful functions. For example, using his list to do FIFO doubly linked list, to achieve a lightweight high-performance Message Queuing service, with his set can do high-performance tag system and so on. In addition, Redis can also set expire time for key-value, so it can also be used as a function-enhanced version of the memcached.
The main drawback of Redis is that database capacity is limited by physical memory and cannot be used as a high-performance read-write for massive data, so Redis is the most suitable scenario for high-performance operations and operations with smaller data volumes.
(3) data types supported by Redis
Redis is differentiated by Key-value's single-valued different types, and the following are the supported types:
Strings
Lists
Sets the intersection and the set
Sorted Set
Hashes
(4) Why does Redis need to put all the data in memory?
Redis reads the data into memory for the fastest read-write speed and writes the data asynchronously to disk. So Redis has the characteristics of fast and data persistence. If you do not put the data in memory, the disk I/O speed severely affects the performance of Redis. With memory getting cheaper today, Redis will be more and more popular.
If you set the maximum amount of memory used, you cannot continue inserting new values after the data has reached the memory limit.
(5) Redis is single-process single-threaded
Redis leverages queue technology to turn concurrent access into serial access, eliminating the overhead of traditional database serial control
(6) virtual memory
When your key is small and value is large, the effect of using a VM is better. Because this saves more memory.
When your key is not small, consider using some very important methods to turn a large key into a large value, such as you might consider combining key,value into a new value.
Vm-max-threads This parameter, you can set the number of threads to access the swap file, set the best not to exceed the machine's core number, if set to 0, then all the operation of the swap file is serial. This can result in a long delay, but with good assurance of data integrity.
The performance of virtual memory is also good when you test yourself. If you have a large amount of data, consider a distributed or other database
(7) Distributed
Redis supports master-slave mode. Principle: Master synchronizes data to slave, and slave does not synchronize the data to master. Slave is connected to master at startup to synchronize data.
This is a typical distributed read-write separation model. We can use master to insert data and slave to provide retrieval services. This can effectively reduce the number of concurrent accesses for a single machine
(8) Read-Write separation model
By increasing the number of slave db, the read performance can grow linearly. To avoid a single point of failure in Master DB, the cluster generally uses two master DB for dual-machine hot standby, so the read and write availability of the entire cluster is very high.
The flaw in the read-write separation architecture is that each node must hold the full data, whether it be master or slave, and if, in large amounts of data, the cluster's scalability is limited to the storage capacity of a single node, and for write-intensive types of applications, The read-write separation architecture is not appropriate.
(9) Data fragmentation model
In order to solve the defect of the read-write separation model, the data fragmentation model can be applied.
Each node can be viewed as a separate master, and then the data is fragmented through the business.
Combining the above two models, each master can be designed as a model consisting of a master and multiple slave.
(Ten) Redis's recycling strategy
VOLATILE-LRU: Pick the least recently used data from the set of data sets (Server.db[i].expires) that have expired time
Volatile-ttl: Select the data that will expire from the set of expired data sets (Server.db[i].expires)
Volatile-random: Choose data culling from any data set (Server.db[i].expires) that has an expiration time set
ALLKEYS-LRU: Pick the least recently used data culling from the dataset (Server.db[i].dict)
Allkeys-random: Choose data culling from data set (SERVER.DB[I].DICT)
No-enviction (expulsion): Prohibition of eviction data
1. What are the benefits of using Redis?
(1) Fast, because the data exists in memory, similar to the advantage of Hashmap,hashmap is that the time complexity of finding and Operating is O (1)
(2) Support rich data type, support string,list,set,sorted Set,hash
(3) Support transactions, operations are atomic, so-called atomicity is to change the data is either all executed, or all do not execute
(4) Rich features: can be used for caching, messages, press key to set the expiration time, after expiration will be automatically deleted
2. What are the advantages of Redis compared to memcached?
(1) memcached All values are simple strings, redis as its replacement, supports richer data types
(2) Redis is much faster than memcached
(3) Redis can persist its data
3. Redis common performance issues and solutions:
(1) Master should not do any persistent work, such as RDB memory snapshots and aof log files
(2) If the data is more important, a slave turns on AOF backup data and the policy is set to synchronize once per second
(3) for the speed of master-slave replication and the stability of the connection, master and slave preferably within the same LAN
(4) Try to avoid adding from the library to the most stressful main library
(5) Master-slave replication do not use the graphic structure, with a unidirectional list structure more stable, that is: Master <-Slave1 <-Slave2 <-Slave3 ...
This kind of structure is convenient to solve single point of failure problem, realize slave to master replacement. If master hangs up, you can enable Slave1 to do the master immediately, and the rest will be unchanged.
4. mysql has 2000w data, only 20w of data in Redis, how to ensure that the data in Redis are hot data
Related knowledge: When the Redis memory dataset size rises to a certain size, it will implement a data-phase-out strategy. Redis offers 6 kinds of data-culling strategies:
VOLTILE-LRU: Pick the least recently used data from the set of data sets (Server.db[i].expires) that have expired time
Volatile-ttl: Select the data that will expire from the set of expired data sets (Server.db[i].expires)
Volatile-random: Choose data culling from any data set (Server.db[i].expires) that has an expiration time set
ALLKEYS-LRU: Pick the least recently used data culling from the dataset (Server.db[i].dict)
Allkeys-random: Choose data culling from data set (SERVER.DB[I].DICT)
No-enviction (expulsion): Prohibition of eviction data
5. What are the differences between Memcache and Redis?
1), storage mode
Memecache all of the data in memory, after the power outage will be suspended, the data can not exceed the memory size.
Redis is partially present on the hard drive, which guarantees the data's durability.
2), Data support type
Memcache support for data types is relatively straightforward.
Redis has complex data types.
3), using the underlying model is different
They are not the same as the underlying implementation and the application protocols that communicate with the client.
Redis builds its own VM mechanism directly, because the normal system calls system functions, it wastes a certain amount of time to move and request.
4), Value size
Redis can be up to 1GB, while Memcache is only 1MB
6. What are the common performance issues with Redis? How to solve?
1). Master Write memory snapshot, save command dispatch Rdbsave function, will block the work of the main thread, when the snapshot is large, the performance impact is very large, will intermittently pause service, so master should not write memory snapshot.
2). Master AoF Persistence, if you do not rewrite the aof file, the performance impact of this persistence is minimal, but the aof file will continue to grow, aof file over the General Assembly to affect the recovery speed of master restart. Master should not do any persistent work, including memory snapshots and aof log files, in particular, do not enable memory snapshots to persist, if the data is more critical, a slave open aof backup data, the policy is synchronized once per second.
3). Master calls bgrewriteaof rewrite aof file, aof in the time of rewriting will occupy a large amount of CPU and memory resources, resulting in service load is too high, there is a short service pause phenomenon.
4). Redis Master-slave replication performance issues, for master-slave replication speed and connection stability, slave and master preferably in the same LAN
7. The most suitable scenario for Redis
Redis is best suited for all data in-momory scenarios, although Redis also provides persistence, but actually more of a disk-backed function, compared to the traditional sense of persistence there is a big difference, then you may have questions, It seems that Redis is more like an enhanced version of memcached, so when to use memcached, when to use Redis?
If you simply compare the differences between Redis and memcached, most of them will get the following ideas:
1, Redis not only supports simple k/v type of data, but also provides the storage of data structures such as List,set,zset,hash.
2, Redis support data backup, that is, Master-slave mode of data backup.
3, Redis support data persistence, you can keep the in-memory data on the disk, restart the time can be loaded again for use.
(1), session cache
One of the most common scenarios for using Redis is the session cache. The advantage of using a Redis cache session over other storage (such as memcached) is that Redis provides persistence. When maintaining a cache that is not strictly consistent, if the user's shopping cart information is lost, most people will be unhappy, now, they will do this?
Fortunately, with Redis's improvements over the years, it's easy to find out how to properly use Redis to cache your session's documentation. Even the well-known business platform Magento offers Redis plugins.
(2), full page cache (FPC)
In addition to basic session tokens, Redis offers a simple FPC platform. Back to the consistency issue, even though the Redis instance was restarted, because of the persistence of the disk, the user will not see a drop in page loading speed, which is a great improvement, similar to PHP native FPC.
Again, for example Magento, Magento provides a plug-in to use Redis as the full-page cache backend.
In addition, for WordPress users, Pantheon has a very good plug-in Wp-redis, this plugin can help you to load the fastest speed you have visited the page.
(3), queue
One of the great advantages of Reids in the memory storage engine area is the provision of list and set operations, which enables Redis to be used as a good Message Queuing platform. The operation that Redis uses as a queue is similar to the push/pop operation of a local program language (such as Python) on a list.
If you quickly search for "Redis queues" in Google, you'll soon be able to find lots of open source projects that are designed to use Redis to create very good back-end tools to meet a variety of queue requirements. For example, celery has a background where redis is used as a broker, which you can view from here.
(4), leaderboard/Counter
Redis's operation of incrementing or decreasing numbers in memory is very well implemented. The set (set) and the ordered set (Sorted set) also make it very simple to perform these operations, and Redis just provides the two data structures exactly. So, we're going to get the top 10 users from the sorted collection – What we call "User_scores", we just need to do it like this:
Of course, it is assumed that you are doing an incremental sort based on your user's score. If you want to return users and users ' scores, you need to do this:
Zrange User_scores 0 Withscores
Agora Games is a good example of what you can see in Ruby, which uses Redis to store data.
(5), Publish/Subscribe
Last (but certainly not least) is the Publish/Subscribe feature of Redis. There are really a lot of usage scenarios for publish/subscribe. I've seen people use it in social networking connections, as a publish/subscribe-based scripting trigger, and even use Redis's publish/Subscribe feature to build chat systems! (No, it's true, you can check it out).
Of all the features that Redis provides, I feel this is the least of the people who like it, although it provides users with this versatile
Summary of Redis Common face questions