Dry! Redis interview must know will be

What kinds of support Redis data types?

  • String : the basic data types, binary safe string, maximum 512M
  • List : list of strings to maintain order of addition according to the order
  • SET : unordered strings, there is no duplicate elements
  • the SET sorted : a collection of strings sorted
  • the hash : Key / value pairs that one kind

Redis is a single process or single-threaded?

Redis is single-threaded process of, Redis queuing technologies will become concurrent access serial access, eliminating the overhead of traditional database serial control.

Why Redis is single-threaded?

Multithreading involves locks, and designed to be multi-threaded processing thread switching and consuming CPU. Redis because the CPU is not the bottleneck, Redis bottlenecks are most likely to be the machine's memory or network bandwidth. You can not play a single-threaded multi-core CPU performance, but can be solved by turning single Redis instance.

Redis advantage

  • **high speed. ** Because the data is stored in memory, similar to HashMap, HashMap advantage is to find and operation time complexity is O (1)
  • Support for rich data types , support string, list, set, sorted set , hash
  • Support services , operations are atomic, so-called atomic changes to the data that is either all executed or not executed all
  • Feature-rich : 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 Redis and memcached

  • Memcached all values ​​are simple strings, Reids as its replacement, support richer data types
  • Redis is much faster than memcached
  • Redis can persist their data
  • Redis data backup data backup support, that master / slave mode

Redis key expiration time and permanent, respectively, how to set?

EXPIREAnd PERSISTcommand.

What Redis major consuming physical resources?

RAM.

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.

Talk about the concept Redis hash slot?

Each node of the cluster is not used Redis the hash consistency, but introduces the concept of a hash slot, 16384 clusters Redis hash slots, each key by the CRC16 checksum of modulo 16384 to determine which channel is placed, the cluster responsible for part of hash slots.

Redis what's the use in the pipeline?

A request / response server can be realized even if the old process a new request has not been a request response. This allows you to send multiple commands to the server without waiting for a reply, and finally read the answer in one step.

This is the Pipeline (pipelining), it is a technique widely used for decades. For example, many POP3 protocol has been implemented to support this function, greatly accelerate the download new messages from the server process.

How to understand Redis affairs?

A transaction is a single isolation Operation: All commands are serialized transaction performed sequentially. During execution of a transaction, the command will not be sent to other client requests interrupted.

A transaction is an atomic operation: commands in the transaction are either all executed or not executed all.

Redis transaction-related commands and which of?

MULTIEXECDISCARDWATCH

Redis memory recovery mechanism

Redis memory recovery focused on two major aspects:

  1. Redis expiration policy : Delete key value of the expiration time
  2. Redis elimination strategy : memory usage reaches the limit on maxmemory trigger data out of memory

Redis expiration policy

Redis expiration policy usually has the following three:

  1. Timing expiration of each set the expiration time of the key need to create a timer to the expiration time will be removed immediately. This strategy can immediately clear stale data, memory was very friendly; but it will take a lot of CPU resources to the disposal of obsolete data, thus affecting the cached response time and throughput.

  2. Inert expired only when accessing a key, will determine whether the key has expired, expired is cleared. This strategy can maximize the saving CPU resources, but then the memory is very unfriendly. Extreme cases, a large number of expired key not be accessed again, so that will not be cleared up a lot of memory.

  3. Regular expired at regular intervals, scans expires dictionary database of a certain number of a certain number of key, and clear which expired key. The former strategy is a compromise between the two. By adjusting the scanning timing of each scan interval and time-consuming defined, it may cause the CPU and memory resources to achieve optimum balancing effect in different situations.

Redis use both the inert expired and expire regularly two kinds expiration policy.

Redis, which has several data elimination strategy

In Redis, allowing the user to set the maximum memory size to use server.maxmemorywhen rising Redis memory data set size to a certain size and they will perform data out of the policy

  • the LRU-volatile : from stale data is set to pick and choose out of the least recently used
  • ttl-volatile : from stale data is set to expire focus on the selection of data out of
  • Random-volatile : the selection of centralized data eliminated from any data set expired
  • the LRU-AllKeys : data from the data set selected out of the least recently used
  • Random-AllKeys : from centralized data out of the selection of arbitrary data
  • noenviction : prohibit out of data

What kinds of support Redis persistence mode

RDB persistence principle is to Redis data recorded in the memory dump to the timing RDB file on disk. After the specified time interval the memory snapshot of a dataset is written to disk, the actual operation is fork a child process, the first set of data written to a temporary file, write successfully, and then replace the previous file, binary compressed storage.

AOF (append only file) persistent principle is Redis operation log to additional write to files. Record processed by the server in the form of logs every write, delete, query operation is not recorded, the recorded text, you can open the file to see the detailed operating record. When the server restart will re-execute these commands to restore the original data. AOF command to save an additional protocol Reids each write operation to the end of the file. Redis also on the AOF files for background rewritten, so that the volume will not AOF file is too large.

Redis two kinds of advantages and disadvantages of persistent way?

RDB persistence

  • Advantages: RDB file compact, small size, network transmission speed, for the whole amount of replication; recover much faster than AOF. Of course, compared with the AOF, one of the most important advantages RDB is a relatively small impact on performance
  • Disadvantages: fatal flaw RDB files in persistent mode with its data snapshot determines the inevitable can not do real-time persistence, and data increasingly important today, a large number of data loss is often unacceptable, and therefore AOF persistence called mainstream. Further, a RDB needs to meet specific file format compatibility is poor.

AOF persistence and persistence RDB corresponds AOF advantage is that the second-level support persistence, good compatibility, the disadvantage is large files, recovery is slow, high-impact performance

How to choose the way Redis persistence strategy?

Before introducing the persistence strategy, we must first understand whether or RDB AOF, persistent open are to pay the price performance of. RDB persistence contrast, on the one hand is the main process will be blocked when Redis bdsave carrying fork operation, on the other hand, the child process to write data to the hard disk IO will also bring pressure; for AOF persistent, writing data to the hard drive greatly increased frequency (under everysec strategy of seconds), IO greater pressure settings may result in additional obstruction AOF file. In addition, similar to the RDB basave AOF rewrite the file, IO pressure and blocking the child process when the problem will fork. In contrast, due to the higher AOF writing data to the hard disk frequency, so the impact on the main process Redis performance will be even greater.

In the actual production environment, the amount of data depending on the circumstances, the application of data security requirements, budget constraints, etc., there will be a variety of persistence strategy; do not use any such persistence, or use RDB AOF one kind or a RDB and colleagues turned AOF persistence and so on. In addition, persistent policy must be selected together with the main from the viewpoint of Redis, also has the function as the primary backup data from the persistent replication, host and slave master and slave may be independently selected persistence scheme.

Why do Redis partition?

Zoning allows Redis manage more memory, Redis will be able to use all of the machine's memory. If there are no partitions, you can only use up to a machine's memory. Redis partition the computing power by simply adding computers to get increase exponentially, Redis network bandwidth will also increase computer and network cards and doubled.

Redis cluster master is how to copy from a model?

To cluster is not still available in case of failure of a communication node portion or most nodes, the cluster is replicated from a master model, each node has a copy of the N-1

Redis cluster will write to lose it? why?

Redis does not guarantee strong data consistency, which means that in practice the cluster may be lost when operating under certain conditions

How is replicated between cluster Redis

Asynchronous replication.

How do Redis memory optimization

Whenever possible, use a hash table (hashes), hash table (which is to say a small number list stored) memory used is very small, so you should be as your data model abstraction to a hash table inside, such as your web system there is a user object, not to the user's name, last name, email, password, set up a separate key, but users should put all this information is stored in a hash table.

Redis common usage scenarios

  • Session Sharing (single sign-on)
  • Page Caching
  • queue
  • Ranks / Calculator
  • Publish / Subscribe

Which architectural patterns Redis there? Talk about their characteristics?

single vision

Features: Simple

Problems:

  1. Memory capacity is limited
  2. Limited processing power
  3. Unable to availability

Master-slave replication

Redis replication (Replication) function allows the user to create any number of copies of the server based on a Redis server, wherein the server is a master is copied (Master), and by copying replicas was created out of the server from the server ( slave). Mainly from the primary network connection between the server properly, both master and slave server will have the same data, the main server will have to happen to them to synchronize data updates from the server, which has been the main guarantee of the same data from the server.

problem:

  1. We can not guarantee availability
  2. Does not solve the master wrote pressure

sentinel

Redis sentinel a distributed system is monitored from redis master server, and automatically failover the primary server offline. Three features:

  • Monitoring (Monitoring): Sentinel will continue to check your server if the primary server and from functioning properly.

  • Remind (Notification): When monitoring a Redis server problem, Sentinel can send notifications to the administrator or other applications via the API.

  • Automatic failover (Automatic failover): when a primary server is not working properly, Sentinel will automatically start a failover operation.

Features:

  1. Ensure high availability
  2. Monitoring each node
  3. Automatic failover

Disadvantages:

  1. Master-slave mode, switching takes time, data will be lost
  2. Does not solve the master wrote pressure

Cluster (proxy type)

Twemproxy is an open source Twitter memcache and redis a quick / lightweight proxy server; Twemproxy is a fast single-threaded proxy program, supported Memcached ASCII protocol and redis agreement.

Features:

  1. A variety of hash algorithms: MD5, CRC16, CRC32, CRC32a, hsieh, murmur, Jenkins
  2. Support failed node automatically deleted
  3. Sharding rear transparent business logic slice, read and write operations and a single side of the same operation Redis

Disadvantages:

  1. It added a new proxy, needs to maintain its high availability.
  2. need to implement failover logic, which itself can not support automatic transfer failure poor scalability, capacity for scaling requires manual intervention

Cluster (direct type)

From then redis 3.0 Supported redis-cluster clustering, Redis-Cluster-free central structure, each of the data storage node and the entire state of the cluster, every node and all other nodes are connected.

Features:

  1. None center architecture (which affect the performance bottleneck node does not exist), less a proxy layer.
  2. Slot in accordance with the data stored a plurality of distributed nodes, inter-node data sharing, data distribution can be dynamically adjusted.
  3. Scalability, linearly expandable to 1000 nodes, the nodes may be dynamically added or deleted.
  4. High availability, some node is not available, the cluster is still available. Make backup copies of data by increasing the Slave
  5. Failure to achieve automatic failover, between nodes exchange status information through gossip protocol, complete with Slave to the voting mechanism to enhance the role of Master.

Disadvantages:

  1. Resource isolation is poor, the situation is prone to influence each other.
  2. Asynchronous data replication, does not guarantee strong data consistency

Used Redis distributed lock on it, it is how to achieve?

Setnx scramble to acquire a lock, then grabbed, and then add a lock to expire expiration prevent lock forget released. If you perform before expire after setnx process to restart unexpectedly crash or maintained, then what happens? There are very complex instruction set of parameters, this should be able to setnx and expire at the same time a synthesis instruction to use!

Used Redis what to do asynchronous queue, how do you use? What are the disadvantages?

Use the list as a queue-like structure, rpush production news, lpop consumer news. Lpop when no message to a proper sleep will try again.

Disadvantages:

  • In the case of the consumer off the assembly line, production will be lost messages, message queues have to use professional as rabbitmq and so on.
  • Many times a consumer can not produce it?
  • Use pub / sub topic subscriber mode, you can achieve the 1: N message queue.

What is Cache penetrate? How to avoid?

Cache penetration

General caching system, are in accordance with the key to cache query, if the corresponding value does not exist, they should go to find the back-end systems (such as DB). Some deliberately malicious request query key is not present, a large amount of the request, it will cause a lot of pressure on the back-end systems. This is called caching penetration.

How to avoid?

  1. The query result is empty situation also cache, cache time shorter, or the key data corresponding cache clean up after the insert.

  2. The key does not exist for a certain filter. We can put all the possible key into a large Bitmap by the bitmap filter query.

What is Cache avalanche? Ru avoid?

Avalanche cache when the cache server reboot or a large number of cache concentrated in one time period fail, so that when failure, back-end systems will bring a lot of pressure, causing the system to crash.

How to avoid?

  1. After a cache miss, by locking to control the number of threads or queue database read write cache. For example, a key for allowing only one thread to query the data and write cache, other threads wait.
  2. Do secondary cache, cache the original A1, A2 copy is cached, when A1 fails, access to A2, A1 cache expiration time is set short-term, A2 is set for long-term 3: different key, to set different expiry time for the cache invalidation point in time as uniform as possible

Cache Concurrency

Concurrent here refers to the problem of multiple concurrent redis the client at the same time set key cause. In fact, redis itself is a single-threaded operation, multiple client concurrent operations, in accordance with the principle of first come, first performed, first come first execution of the rest of the obstruction. Of course, another solution is to redis.set operation in a queue serializing it, it must be executed one by one.

Cache warming

After preheating the cache line is on the system, the relevant data is directly loaded into the cache buffer system.

This can be avoided when the user requests, first query the database, and then the data cache problem! Users to directly query cache data previously been preheated!

Solutions:

  1. Direct write cache to refresh the page, next time on-line manual;
  2. The amount of data can be loaded automatically when the project started;

Before the object is on-line system, the data is loaded into the cache.

How Redis recovery process work?

  1. The client performs a data write operation
  2. After redis server receives the write operation, the limit checking maxmemory, if the limit is exceeded, then clean out the partial data according to the corresponding policy
  3. Write operation has finished.

Redis is single-threaded, how to improve the utilization of multi-core CPU?

You can be deployed on the same server multiple instances of Redis, and treat them as different servers to use, at some point, in any case a server is not enough, so if you want to use multiple CPU, you can think about fragment (shard).

Modify the configuration will not restart Redis immediate effect it?

For running instance, there are many configuration options can be modified through the CONFIG SET command, without any form of execution restart. Starting Redis 2.2, you can switch to a snapshot from the AOF RDB persistence or otherwise, without the need to restart Redis. Retrieval CONFIG GET *command for more information. But occasionally restart is required, such as Redis program to upgrade to a new version, or when you need to modify the configuration parameters of the time is not currently supported by some CONFIG command.

Redis memory runs out what will happen?

If you reach the set limit, Redis write command returns an error message (but read command can also return to normal.) Or you can Redis as cache to use the configuration elimination mechanism, when Redis reach the memory limit may wash away the old contents.

Redis is distributed scale up early or late to do the re-do? why?

Since Redis is so lightweight (single instance only 1M memory), in order to prevent future expansion, the best way is to start to start more instances. Even if you have only one server, you can also outset Redis running in a distributed manner, using partitioning, start multiple instances on the same server. Setting the outset more than a few Redis instances, such as 32 or 64 examples, for most users, it can be cumbersome to operate, but to do this sacrifice is worth it in the long term. In this case, when your data is growing, requiring more Redis server, you need to do is simply Redis instance migration from one server to another server only (without regard to the issue of re-zoning). Once you add another server, you will need half your Redis instance migration from the first machine to a second machine.

Redis and other key-value store What is the difference?

Redis has a more complex data structure and provide atomic operations on them, which is different from the other databases evolutionary path. Meanwhile Redis data types are based on the basic data structure transparent to the programmer, without the need for additional abstraction. Redis running in memory, but can be persisted to disk, so that when the high-speed read and write memory tradeoffs different data sets, the data should not be greater than the amount of hardware memory. Another advantage of in-memory databases are, compared to the same complex data structures, operate in a very simple memory on the disk, so you can do a lot of internal Redis highly complex matter. Meanwhile, in terms of their compact disk format to generate additional way, because they do not require random access.

Watch for more sharing


Good text recommended:

Reproduced in: https: //juejin.im/post/5d04528ef265da1b8b2b5bbd

Guess you like

Origin blog.csdn.net/weixin_34143774/article/details/93164508