Summary of redis interview questions

Reprinted from:
http://blog.csdn.net/zdp072/article/details/50991116
http://www.100mian.com/mianshi/dba/37381.html

1. What are the benefits of using redis?

(1) Fast speed, because the data is stored in memory, similar to HashMap, the advantage of HashMap is that the time complexity of search and operation is O(1)
(2) Support rich data types, support string, list, set, sorted set , hash
(3) supports transactions, operations are atomic, the so-called atomicity is that all changes to the data are either executed or not executed at all
(4) Rich features: can be used for caching, messages, set expiration time by key, It will be automatically deleted after expiration

2. What are the advantages of redis over memcached?

(1) All values ​​of memcached are simple strings, and Redis , as its replacement, supports richer data types
(2) Redis is much faster than memcached
(3) Redis can persist its data

3. What are the differences between Memcache and Redis?

1) The storage method
Memecache stores all the data in the memory, and it will hang up after power failure, and the data cannot exceed the memory size.
Part of Redis is stored on the hard disk, which ensures the persistence of data.
2), data support type
Memcache is relatively simple to support data types.
Redis has complex data types.
3), the use of the underlying model is different The underlying implementation between
them and the application protocol for communication with the client are different.
Redis directly builds the VM mechanism by itself, because if the general system calls system functions, it will waste a certain amount of time to move and request.

4. Redis common performance problems and solutions:

(1) It is best for the Master not to do any persistent work, such as RDB memory snapshots and AOF log files
(2) If the data is more important, a Slave enables AOF backup data, and the policy is set to synchronize once per second
(3) For the master-slave The speed of replication and the stability of the connection, Master and Slave are best in the same local area network
(4) Try to avoid adding slave libraries to the main library under great pressure
(5) Master-slave replication Do not use a graph structure, use one-way The linked list structure is more stable, that is: Master <- Slave1 <- Slave2 <- Slave3...
This structure is convenient to solve the single point of failure problem and realize the replacement of Master by Slave. If the Master hangs, you can immediately enable Slave1 to be the Master, and the others remain unchanged.

5. There are 2000w data in mySQL, and only 20w data in redis. How to ensure that the data in redis is hot data

Related knowledge: When the size of the redis memory data set rises to a certain size, the data elimination strategy will be implemented . Redis provides 6 data elimination strategies:
volatile-lru: select the least recently used data from the data set (server.db[i].expires) that has set the expiration time to eliminate
volatile-ttl: from the data set that has set the expiration time (server.db[i].expires) to select the data to be expired to eliminate
volatile-random: to arbitrarily select data from the data set (server.db[i].expires) that has set the expiration time to eliminate
allkeys-lru: from the data Pick the least recently used data from the set (server.db[i].dict) Elimination
allkeys-random: Select data arbitrarily from the dataset (server.db[i].dict) Eliminate
no-enviction: prohibit eviction data

6. Please use Redis and any language to implement a malicious login protection code. Each user ID can only log in at most 5 times within 1 hour. The specific login function or function can be used as an empty function, and there is no need to write it out in detail.

Implemented with a list: Each element in the list represents the login time. As long as the last fifth login time is within 1 hour of the current time, the login is prohibited. The code written in Python is as follows:

#!/usr/bin/env python3
import redis  
import sys  
import time

r = redis.StrictRedis(host=’127.0.0.1′, port=6379, db=0)  
try:  
    id = sys.argv[1]
except:  
    print(‘input argument error’)
    sys.exit(0)

if r.llen(id) >= 5 and time.time() – float(r.lindex(id, 4)) <= 3600:  
    print(“you are forbidden logining”)
else:  
    print(‘you are allowed to login’)
    r.lpush(id, time.time())
    # login_func()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326232894&siteId=291194637