[Redis] Things about redis interviews (1)

1. What is redis?

An open source memory database that can be used as a database, cache, message middleware, and can be persisted. Support multiple data types, string string, list list, set set, ordered set sorted set, hash hash, etc. Built-in replication, lua script support, transaction, LRU removal, and different levels of data persistence mechanisms. Provide high availability through sentinel mode and automatic partition cluster mode. 2. The common configuration of redis is configured in redis.conf by default, and the configuration file can be specified when redis is started.

 
 

127.0.0.1 192.168.0.1 the bind
protected the MODE NO-
Port 6379

# client closes the connection idle timeout
timeout 0
# daemon is started
daemonize yes
# number of database default DB0
Databases 16
# use quotation marks when password contains a space
requirepass redis5
# RDB save snapshots
the Save 600 100
# of available memory began to clear the key beyond
maxmemory 2GB
# Clear strategy
maxmemory-policy noeviction
# Lua execution timeout
Lua-time-limit 5000
# Enable the cluster model
cluster-Enabled yes
# specify the cluster configuration file
cluster-config-file nodes -6379.conf

3. Data type
string : the most basic redis value type, binary safe, can contain any data, such as pictures or serialized objects, a string can store up to 512M bytes of content. You can use the INCR command set ( incr/decr/incrby ) to use a numeric string as a counter, use the append command to append a string, use getrange/setrange for string random vector access, and use getbit/setbit bitmaps to implement bloom filters . list : The list is a simple list of strings, arranged in the order of insertion, you can add an element to the head (left) or tail (right) of the list. Common operations: lpush/rpush/lpop/rpop/lrange . A list can have up to 2 32 -1 elements. You can insert and delete data at both ends of the list very quickly, but if you try to access the middle element of a very large list, it is still very slow, because it is a time complexity of O(N) operating. set : A collection is an unordered collection of strings. You can add, delete, and test the existence of elements with O(1) time complexity (the time complexity is constant no matter how many elements in the set). Redis collections have excellent features that do not allow the same members to exist. Add the same element to the collection multiple times, and eventually only one of this element will exist in the collection. In fact, this means that before adding an element, you do not need to check whether the element already exists. hash : Hash is a mapping between string fields and string values, so they are perfect data types that represent objects. A hash can contain up to 2 32 -1  key-value pairs. Common commands: hget/ hset/hgetall/hmget/hmset/hmget . sorted set : The sorted set is similar to the set. The difference between them is that each member of the sorted set is associated with a score. This score is used to arrange the members in the sorted set from the lowest score to the highest score. Using ordered collections, you can add, delete and update elements very quickly (O(log(N))). Because the elements are sorted when they are inserted, a range of elements can be quickly obtained by score or position. Access to the middle elements of an ordered set is also very fast. Commonly used commands: zadd/zrange/zrank/zcard/zcount . 4. Redis transaction A
transaction is a series of operations that commands are executed atomically. The commands in the transaction will be executed in order, or all of them will be executed, or all of them will not be executed. Transaction-related commands: multi/exec , use multi to open the transaction, then you can send a series of commands, and finally use exec submits, all commands will be executed, use the discard command to cancel the transaction queue. Redis transactions do not support rollback, because the execution fails only when there is an error in the execution of the command, and rollback does not solve the programming error. Transactions are actually similar to Lua scripts executing redis commands. Multiple commands are grouped together and sent to redis. The scripts are executed faster and easier to write. You can use scripts to replace transactions.
5. Redis persistence The
RDB persistence method can perform snapshot storage and regular backup of your data at specified time intervals. RDB is a very compact file that saves a data set at a certain point in time . RDB is saving RDB The only thing the parent process needs to do when files is to fork a child process. The next work is done by the child process. The parent process does not need to do other IO operations, so the RDB persistence method can maximize the performance of redis. The AOF persistence method records each write operation to the server. When the server restarts, these commands are executed again to restore the original data. The AOF command uses the redis protocol to append and save each write operation to the end of the file. The data set saved in the AOF file is more complete than the data set saved in the RDB file. If the data integrity requirements are particularly high, use AOF. Otherwise, use RDB. You can also enable it at the same time. Set the fsync to disk mode reasonably to take into account performance and security.
6. Distributed lock
Using the redis command setnx can be used as a basis for acquiring locks when sharing resources. When the thread performs related operations, the lock holder del related keys. Basically pay attention to a few points: use setnx to lock, pay attention to setting the timeout; When the lock is not acquired, set the waiting time reasonably; whoever adds the lock in the end will release it. Need to consider the problem of node data synchronization in the cluster, you can directly use the api distributed lock provided by redisson to operate distributed shared resources. 7. Elimination mechanism The behavior that Redis will use when the maxmemory limit is reached is configured by Redis's maxmemory-policy configuration command. noeviction : When the available memory reaches the maximum, return an error directly. allkeys-lru : Try to reclaim the least used key LRU. volatile-lru : Try to reclaim the least used key LRU, but only for keys in the expired collection. allkeys-random : Recycle random keys so that the newly added data has space for storage. volatile-random : Recycling random keys allows space for newly added data to be stored, but only for keys in the expired collection. volatile-ttl : Reclaim the keys in the expired set, and reclaim the keys with a shorter time to live (TTL) first, so that the newly added data has space for storage. The key value of redis expires, and it will be cleared when accessed again to avoid access to the expired key; redis itself also has a scheduled task to clear the expired key; when the memory is maximum, the relevant key has to be cleared.


Guess you like

Origin blog.51cto.com/15060464/2638269