redis interview summary (a)

1. Project cache is how to use? Why use a cache? Improper use of caching What are the consequences?

Face questions analysis

Why use a cache?

Caching, has two main purposes: high performance , high concurrency .

high performance

Assume such a scenario, you have an operation, a request came, Hangchihangchi you all kinds of mess operations mysql, check out half a result, time-consuming 600ms. But the results may not become the next few hours, or can be changed without immediate feedback to the user. So this time I supposed to?

Cache ah, toss the result of 600ms check out, throw cache, a key corresponding to a value, next time someone check, do not go mysql toss 600ms, and from the cache, by a key to check out a value directly, 2ms get. 300 times performance increase.

That is the result of complex operations for some time-consuming need to check out, and change back very determined, but there are a lot of read request, then check out the results directly in the cache, the cache is directly read like the back.

High concurrency

mysql database so heavy, simply did not designed to allow you to play high concurrent, although you can also play, but natural support is not good. mysql single supported to 2000QPSbe easy to start the alarm.

So if you have a system, a second peak of over 10,000 requests, that a single mysql will definitely die. You can only this time on the cache, the cache put a lot of data, do not put mysql. Caching feature simple, it means key-valueoperation, the amount of concurrent easy one second stand-alone support tens of thousands of hundreds of thousands, support high concurrency so easy. Stand-alone carrying amount is several times the concurrent mysql stand-alone.

Cache memory is gone, it is natural memory support high concurrency.


2.redis and memcached What is the difference? What redis threading model? Why redis single-threaded able to support high-concurrency?

Analysis of test sites

This is the time to ask redis, fundamental issues now, redis basic principles and characteristics of an internal, redis is actually a single-threaded model work , if you do not know this, that redis play back when, out of the Would not it be a problem do not know anything?

It is also possible the interviewer will ask you the difference between memcached and redis, but memcached the early years of the major Internet companies are commonly used caching scheme, in recent years, but now are basically redis, memcached with a little company.

Face questions analysis

memcached and redis what's the difference?

redis support complex data structures

Compared memcached for redis, it has more data structure that can support a richer data manipulation. If a cache can support more complex structures and operations, redis would be a good choice.

redis native support for cluster mode

In redis3.x version will be able to support cluster mode, but no native memcached cluster model, relies on the client to write data to achieve the cluster carved pieces.

Performance Comparison

Due to the higher performance memcached redis only mononuclear, polynuclear and may be used memcached, the average on each of the core than at redis small data storage. In the above 100k data, memcached performance than redis, although redis also recently optimize storage performance on large data, but compared to memcached, or slightly less.

redis threading model

redis internal use event handlers file file event handler, the file event handler is single-threaded, so it is called redis single-threaded model. It uses a plurality of IO multiplexer mechanism simultaneously listening socket, to select the corresponding event handler for processing according to the event on the socket.

Event handler file structure contains four parts:

  • Multiple socket
  • IO multiplexing program
  • File event dispatcher
  • Event handler (connection acknowledgment processor, the command processor request, reply command processor)

A plurality of socket may have different concurrent operations, each corresponding to a different event file, but the program will monitor the plurality of IO multiplexing socket, socket events will be generated queued in a queue, each event dispatcher remove an event from the queue, the event corresponding to the event processor for processing.

Point of view of a client and redis communication process:



Socket01 client request to the server socket redis connection is established, this time will generate a server socket AE_READABLEafter the event, listening to the IO multiplexing program generated event server socket, pressed into the event queue. File event dispatcher to acquire the event from the queue, to the connection response processor . The processor creates a connection response socket01 can communicate with the client, and the socket01 of AE_READABLEevents associated with the command requesting processor.

Assuming that the client sends a set key valuerequest, then redis socket01 the generated AE_READABLEevent, the IO multiplexing program press event queues, the event dispatcher acquired at this time from the queue to the event, since the front socket01 AE_READABLEevent It has been associated with a command requesting processor, so the event will be an event dispatcher to command processor to process the request. The processor reads the command request socket01 key valueand completed their memory key valuesettings. After the operation is complete, it will socket01 of AE_WRITABLEevents associated with the command reply processor.

If at this time the client is ready to receive the results returned, then redis in socket01 will generate an AE_WRITABLEevent, also pressed into the queue, the event dispatcher to find the associated command processor reply, reply by the command processor to enter this socket01 a result of the operation, for example ok, after the lifting of socket01 of AE_WRITABLEevents associated with a command processor reply.

This completes one communication.

Why redis single-threaded model can efficiently so high?

  • Pure memory operation
  • The core is based on the non-blocking IO multiplexing mechanism
  • Instead, a single-threaded multi-threaded avoid the frequent context switching problem

3.redis what are data types? Which are used in a scene more appropriate?

Face questions analysis

redis are the following data types:

  • string
  • hash
  • list
  • set
  • sorted set

string

This is the simplest type, is an ordinary set and get, do simple KV cache.

Set college su

hash

This is a structure similar to the map, this is to be generally structured data, such as an object (if the object is not nested other objects ) to cache redis, and then each read and write the cache, they can on the operation of the hash in a field .

hset person name bingo
hset person age 20
hset person id 1
hget person name

person = {
    "name": "bingo",
    "age": 20,
    "id": 1
}

list

list is an ordered list, which can play a lot of tricks.

For example, by storing a list of some type of list data structure, something similar to the fan list, review the list of articles and the like.

For example, by lrange commands, read elements within a closed interval, based on the list can implement paging query, this is a great feature, based on simple, high-performance redis page, you can do a similar kind of microblogging continue to drop down tab things, high performance, go from page to page.



# 0 starting position, -1 end position, end position is - 1, represents the last position in the list, that is, to see all. 
mylist Lrange 0 - . 1

Example, can put forward a simple message queue, into the dislike list head, tail, where it comes out from the list.

lpush mylist 1
lpush mylist 2
lpush mylist 3 4 5

# 1
rpop mylist

set

set unordered collection, automatically de-emphasis.

Set directly on the system need to re-thrown to the data, automatically give weight to, and if you need some quick global data de-duplication, of course, you can go heavy on jvm memory of HashSet, but if you deploy a system on multiple machines? Redis was set based deduplication globally.

Can be set to play an intersection, union, difference-based operations, such as the intersection of it, you can put two people in a whole list of fans intersection, take a look at two people who in common is that? Right.

The two fans are placed in two large V set, the two set to do intersection.

# ------- ------- operating a the SET 
# add elements 
Sadd MySet 1 

# View all the elements 
smembers MySet 

# determine whether to include a value 
sismember MySet 3 

# remove an / some elements of 
Srem MySet 1 
MySet Srem 2  . 4 

the number of elements # view 
SCard MySet 

# delete a random element 
SPOP MySet 

# ------- ------- operating a plurality of set 
# to move a set of element to another set 
SMOVE yourSet MySet 2 

# find the intersection of two set 
Sinter yourSet MySet 

# request set and two sets 
SUNION yourSet MySet 

# yourSet elements not required in the MySet 
sdiff yourSet mySet

sorted set

sorted set is sort of set, but can be re-sorted to write into the time to a fraction, automatically sorted by score.

Board Zadd 85 zhangsan 
Zadd Board 72 Lisi 
Zadd Board 96 wangwu 
Zadd Board 63 is zhaoliu 

# Get the top three user (default is ascending, descending it is necessary to rev) 
zrevrange Board 0  . 3 

# acquires a user ranking 
zrank board zhaoliu

4.redis expiration policies are what? What mechanisms are out of memory? LRU handwritten code to achieve what?

Analysis of test sites

Even if you do not know the problem, come up on the ignorant, the answer does not come out, you write that line of code, written redis granted that the data will certainly exist, the latter causes the system to a variety of bug, who is responsible ?

There are two common problems:

  • How data is written to redis gone?

Some students may encounter, often lose some data in redis production environment, get a hold of, then, it may be gone. My God, classmates, you ask this question to explain redis you will not use right ah. redis is cached, you gave when storage is not it?

Shajiao cache? When using memory cache. Memory is unlimited right, memory is very precious and limited, disks are cheap and lots of. It may be a machine on the memory of dozens of G, but there are a few T of hard disk space. redis memory to be mainly based on high-performance, concurrent read and write operations.

Now that memory is limited, such as redis can only use 10G, if you write the data entered, 20G, and would we supposed to? Of course, the data will get rid of 10G, 10G and then retain the data of the. What data that kill? What data retention? Of course, is to get rid of infrequently accessed data, the common data retention.

  • Data clearly expired, how can they occupy the memory?

This is determined by the expiration policy redis.

Face questions analysis

redis expiration policy

redis expiration policy is: periodically delete inert + Delete .

The so-called periodically delete , refers to the redis default is every 100ms on a random sample of some set an expiration time of the key, to check whether it has expired, expired delete.

Redis put a 10w assume a key, are set expiration time, you every few hundred milliseconds, checks 10w a key, that redis basically died, cpu load will be very high, consumption in your checking expired key on. Note that this 100ms to traverse all of the key is not set the expiration time intervals, so that the performance of a disaster . In fact redis is every 100ms random sample of some of the key to check and delete.

But the problem is, periodically delete key may cause a lot of back to the time and has not been removed, it zezheng it? So it is inert deleted. This means that you get a key time, redis will check that the key if you set an expiration time does it expire? If this time has expired will be deleted and will not give you anything in return.

Get key time, if the key at this time has expired, delete, does not return anything.

But in fact this is a problem, if you delete regularly missed a lot of back key, then you have no time to investigate, also did not go inert delete, then what will happen? If the accumulation of a large number of expired key in memory, causing memory block redis exhausted, zezheng?

The answer is: go out of memory mechanisms .

Memory elimination mechanism

redis memory elimination mechanism are the following:

  • noeviction: When the memory is not sufficient to accommodate the new data is written, the new write operation will complain that this is generally no one with it, it was disgusting.
  • LRU-AllKeys : When insufficient memory to accommodate the new data is written in the key space , the key is removed the least recently used (this is the most common of).
  • allkeys-random: When the memory is not sufficient to accommodate the new data is written in the key space randomly remove a key, this is generally not people use it, why should random, the key is certainly the least recently used to kill ah .
  • volatile-lru: When insufficient memory to accommodate the new data is written in a provided space expiration time of the key , the key is removed the least recently used (this is generally not appropriate).
  • volatile-random: When insufficient memory to accommodate the new data is written in a provided space expiration time of the key , the random removal of a key.
  • volatile-ttl: When the memory is insufficient to hold the new data is written in a provided space key expiration time , there is an earlier expiration date of the priority key removed.

Handwritten a LRU algorithm

You can spot the most original handwriting LRU algorithm, that code is too big, it seems unrealistic.

Not seeking my own handmade from the ground up to create their own LRU, but at least you know how to use the existing data structure to achieve JDK version of a Java LRU.

class the LRUCache <K, V> the extends a LinkedHashMap <K, V> {
     Private Final int the CACHE_SIZE; 

    / * * 
     * the maximum number of data to be passed in buffer 
     * 
     * @param cacheSize cache size 
     * / 
    public the LRUCache ( int cacheSize) {
         // to true let linkedHashMap expressed in accordance with the access order to sort, on the recent visit of the head, the oldest accessed on the tail. 
        Super (( int ) Math.ceil (cacheSize / 0.75 ) + . 1 , 0.75f , to true ); 
        the CACHE_SIZE = cacheSize; 
    } 

    @Override 
    protectedthe removeEldestEntry Boolean (of Map.Entry <K, V> eldest) {
         // when the map data is larger than a specified number when the cache, automatically delete the oldest data. 
        return size ()> the CACHE_SIZE; 
    } 
}

5. How to ensure redis high concurrency and high availability? redis copy of the master-slave principle tell us about it? Sentinel Principle redis can tell us about it?

Analysis of test sites

In fact, I ask this question, mainly test you, redis stand-alone can carry tall concurrency? If the stand-alone expansion could not carry on how to carry more concurrent? redis will not hang? Now that would hang redis redis is that how to ensure high availability?

In fact, some of the issues for all projects in which you certainly have to consider, if you have not thought about it, do you think that is too small to problems in production systems.

Face questions analysis

If you use redis caching technology, it will certainly have to consider how to use the machine Cadogan redis, redis ensure high concurrency, there is how to ensure their future redis not hang directly died, that redis availability.

redis achieve high concurrency mainly depends on master-slave architecture , a master multi-slave, in general, in fact, many projects would be sufficient, a single master used to write data, stand-alone tens of thousands of QPS, from more than used to query data from multiple instances We can provide 10w per second QPS.

After a while if you want to achieve high concurrency, hold large amounts of data, then you need to redis clusters, using redis clusters, can provide hundreds of thousands of concurrent read and write per second.

redis availability, call the shots if the architecture deployed, then add the sentinel can be, can be achieved, any one of them is down, then the switchover can.

6.redis persistence What are the different ways? Different mechanisms of persistence are what advantages and disadvantages? How specific underlying persistence mechanism is implemented?

Analysis of test sites

redis if only just inside the data cached in memory, if redis is down and then restarted, the data on the memory of all lost ah. You have to use persistence mechanism redis while writing data to memory, asynchronous slowly write data to disk file, for persistence.

If redis downtime restart automatically from disk before loading the persistence of some of the data on it, maybe a little lost data, but at least not all the data are lost.

This fact, are some of the problems you might encounter redis production environment for, that is, if the redis hung up and then restart, memory data is not lost on the whole? You can not restart the time to restore the data?

Face questions analysis

Persistence is mainly to do disaster recovery, data recovery, can also be classified into a highly available link go, such as whole redis you hung up, then redis is not available, you have to do is make redis becomes available, become available as soon as possible.

Restart redis, stack it outside as soon as possible to provide services, if not do data backup, this time redis started, ah is not available, the data is gone.

Is likely to say that a large number of requests over, all cache can not hit, can not find the data in redis years, this time dead, appeared cache avalanche problems. Not all requests redis hit, will go to the mysql database to find the source of this data, all of a sudden mysql undertake high concurrency, and then hung up ...

If you do redis persistence, backup and recovery program to achieve enterprise-class degree, even if your redis failure, and can also back up data, recover quickly, once restored to provide services immediately.

redis persistence of two ways

  • RDB: RDB persistence mechanism, is carried out on the data redis periodic persistence.
  • AOF: AOF mechanism for each write command as a log to append-onlythe log file in a write mode, at the time of restart redis, can play back to reconstruct the entire data set in the write command log AOF.

By RDB or AOF, can be redis memory data to be persisted to disk above, then you can back up data to other places, such as Ali cloud and other cloud services.

If redis hung up the data on the disk and memory on the server are lost, you can copy the data back from the cloud before serving, put the specified directory, then start again redis, redis persistent data file automatically based on the data, to restore data in memory, continue to provide services.

If you use RDB and AOF two kinds of persistence mechanism, so when redis restart, use the AOF to reconstruct the data, because the AOF data more complete .

RDB advantages and disadvantages

  • RDB will generate multiple data files, each data file represents the data of a certain moment in redis, this way of multiple data files, very suitable for cold standby , you can send this file to complete a number of remote data secure storage up, such as Amazon's S3 cloud service up in the country may be on the ODPS Ali cloud storage distributed to the scheduled backup policy to back up data regularly in redis.

  • RDB read and write redis provide external services, the impact is very small, can make redis maintain high performance , because redis main process requires only fork a child process, so that the child process to perform disk IO operations to RDB persistence can be.

  • AOF relative persistence mechanism, the process to restart and restore redis more quickly and directly based on the RDB data files.

  • If you want to redis failure when, as little as possible loss of data, then the RDB no good AOF. In general, RDB data snapshot files, are every 5 minutes, or longer generation time, this time you have to accept the redis Once the process down, then lost the last five minutes of data.

  • RDB in each fork child process to execute when RDB snapshot data files generated, if the data file is particularly large, may result in suspension of service provided to clients milliseconds, or even seconds.

AOF advantages and disadvantages

  • AOF can better protect data is not lost, AOF will generally every second, once executed by a background thread fsyncoperations, data loss of up to 1 second.
  • AOF log file append-onlymode write, so there is no disk seek overhead, write performance is very high, and the file is not easily damaged, even if the end of the file is damaged, and very easy to fix.
  • AOF log file is too large even when the background rewriting operation occurs, it will not affect the read-write client. Because in rewritethe time log, and will guide them to compress, to create a minimum number of log data you need to recover it. Then create a new log file when the old log file is written as usual. When the log file after the new merge ready, and then exchange the old and new log files.
  • AOF command log file is recorded in a very readable way, this feature is very suitable for emergency recovery of accidentally deleted disastrous . For example, someone accidentally use flushallthe command to clear all the data, as long as this time the background rewritehas not happened, then you can immediately copy AOF documents, the last flushallcommand to be deleted, then the AOFput files back, you can restore mechanism, automatically restore all the data.
  • For the same data is, AOF log files are usually larger than the RDB data snapshot files.
  • After AOF open, backed write QPS QPS will be lower than write RDB support because AOF usually configured per second fsynconce the log file, of course, once per second fsync, performance is still very high. (If you write in real time, then the QPS will drop, redis performance will be greatly reduced)
  • When the previous bug AOF happened, AOF is through the log records, data recovery, no recovery of data out exactly the same. So, similar to the AOF this more complex commands based on the log / merge / playback mode, based on the RDB than each persistent file a complete snapshot of the data the way some of the more fragile, prone to bug. But AOF is to avoid bug rewrite process caused so each rewrite is not performed merge is based on the old command log, but to re-build instructions based on the data in memory at the time , this will be much better robustness.

RDB and AOF in the end how to choose

  • Do not just use RDB, because that would cause you to lose a lot of data;
  • Do not just use the AOF, because that there are two problems: First, you do cold standby by AOF, do not RDB cold standby to recover faster; second, RDB data snapshot is generated every time simple and crude, more robust, this bug can be avoided AOF complex backup and recovery mechanisms;
  • redis support open simultaneously open two kinds of persistent way, we can use the integrated AOF and RDB two kinds of persistence mechanism, with AOF to ensure that data is not lost, as the first choice for data recovery; RDB to do with varying degrees of cold standby, when AOF files are missing or damaged unavailable, you can also use RDB for fast data recovery.




Guess you like

Origin www.cnblogs.com/wcgstudy/p/11408367.html