2020PHP interview -Redis articles

A, Redis data type

1. string character.

Structured 2.hash hash object. key can not be repeated

3.list queue lpush rpop lpop rpush  

4. set set value can not be repeated

5. zset ordered set plus fraction (sorting) on ​​the basis of the set.

 

Two, Redis affairs

1.multi (transaction starts), exec (transaction execution), watch (monitoring of a few keys have not been modified other clients, if there is any key is modified, exec is going to be a direct return nil, not the enforcement branch), discard (cancel the transaction immediately, empty transaction queue all commands).

2.redis watched_keys database will exist a dictionary, the dictionary key key is monitored, the value of a linked list recorded in the client monitors the key, once the key to be monitored have changes, the key corresponding to the client opens REDIS_DIRTY_CAS identify, on behalf of the client's transaction security is destroyed. This is achieved watch command.

3. Why redis transaction does not support rollback?

  Because this does not match the sophisticated features and redis design. And redis affairs programming error error cause is usually only occurs in the development environment, the production environment can be avoided.

4.redis transaction command execution results wrong?

  redis will not execute interrupt the transaction, will continue to perform the subsequent operation command. exec when the error is identified command will come out and give the reason.

5. redis transactional persistence, persistence influenced by the way, as long as the use of aof snapshot mode, and the value is always appendfsync option, the program will always invoke synchronization (sync) function after executing the command, the command data really saved to the hard disk.

  ps: If only because of the way rdb words, redis server only done under specific storage conditions BGSAVE command and executed asynchronously BGSAVE command can not guarantee that the data transaction first time is saved to the hard disk.

 

Three, Redis cache and cache penetrate avalanche

  Cache penetration:

  Some malicious requests, deliberately large number of inquiries key does not exist.

    How to avoid:

    1. key query result is empty situation also cache, cache shorter duration settings, or when the key is inserted, to clean up the cache update.

    2. advance filter over certain key does not exist, all possible key into a large bitmap in the filter, or use the Bloom filter.

  Cache Avalanche:

  The same time, a large cache of collective failure.

    How to avoid:

    1. a large number of cache invalidation db lock or enqueue control, control the number of concurrent.

    2. Make L2 cache. A1 for short-term cached copy. A2 for long-term original cache. When A1 failure, access A2.

    3. Try a different key set different validity, let's aging cache node to even things up.

 

Four, Redis threading model

Single-threaded, the event manager based on the file (file event handler), I / O multiplexer, a plurality of simultaneously listening socket, according to the event type socket, assigned to the corresponding event handler for processing.

 

Five, Redis expiration policy

1. periodically delete:

  Each 100ms randomly selected key has expired, expired is deleted. (Ps: if every check all the key will be stuck.)

2. inert Delete:

  Get a key when the key has expired inspection, expired is deleted.

 

If you do not regularly delete to delete, and no request to obtain certain key, the key that would have been occupied memory. More and more memory, memory triggers elimination mechanism.

 

Six, Redis memory elimination mechanism

1.noeviction: not out of the data, write error.

2.allkeys-random: Select any data out from all the data.

3.allkeys-lru: selected key from all the least recently used data were eliminated.

4.volatile-random: Select any data out from the data provided in the validity period.

5.volatile-lru: Select the key will be the least recently used data from the phase-out period set in.

6.volatile-ttl: pick the shortest survival time from key data set in the period were eliminated.

 

Seven, Redis and Memcached difference

1.memcached supports only strings. redis supports 5 types of diversification.

2.memcached does not support persistent. redis support RDB and AOF snapshots.

3. Overall, less memory usage redis few.

 

Eight, Redis cluster at what time not available

1. hang any master, no slave master and the current from the library, resulting in a hash slot incomplete.

2. More than half of the master die.

 

Nine, Redis hash slot

Did not like the concept of consistency hash ring, Redis hash cluster total of 16,384 slots after each key by CRC16 check of 16384 modulo to decide on which slot. Each node of the cluster responsible for a portion of the slot, you can easily add and remove nodes.

 

Ten, Redis persistence mode

1.RBD: fork child process, using a copy-on-write copy-on-write technology to record data to memory RDB file.

2.AOF: The Redis operation log to additional write to files.

ps: redis 4.0 supports mixed persistence. When redis instance restart, use RDB rebuild most of the memory. Then use the AOF replay snapshot, revert to the state before the restart.

 

Eleven, AOF how to reduce their file size

bgrewriteaof rewrite aof, rewrite success will be replaced.

 

Twelve, Redis master-slave synchronization

1. The pre-synchronization, the master node to a first bgsave, while the recording operation of the subsequent modifications to the memory buffer. The subsequent rdb file is completely synchronized to the node, constructing a memory. Then notify the master node in the buffer memory to modify the operation of the synchronization reproduced from the node, is completed.

 

Thirteen, Redis queue

pub / sub mode if consumers offline data producers backlog, if this time is not real-time aof, data will be lost.

 

Fourth, database, double the cache write consistency

To update the database, and then delete the cache. If you have to do the follow-up to the key query, check the cache does not exist, go check the database to a write cache.

 

Guess you like

Origin www.cnblogs.com/camouflage/p/12369655.html