Redis Flashcards
Memcache
like Hash table
support only simple data type
not support of data persistence
not support master-slave
not support sharding
Redis
support set, list etc, complex data type
support data persistence
support master-slave
support sharding
Why Redis is so fast
support 100,000 QPS
- solely build on memory, not limited by harddisk I/O. one process ,one thread
- data structure is simple, so does the operation, like hashmap, key-value pair O(1)
- single thread,support high concurrency, multi-core => multi instance; do not need lock, less conflicts, no complex locking
- non-blocking IO, I/O多路复用技术(multiplexing)
Redis’s multiplex mechanism
Redis support: epoll/kqueue/evport/select
automatically choose base on use scenario
choose on time complexity O(1)
select as an O(n) method keeps as a backup plan
base on react design pattern to monitor I/O event
Redis‘s data type
- String (support jpg, serialized sequence)
- Hash (both are String)
Create Hashmap : hmset lilei name “lilei” age 36
Get value : hget lilei age // “36”
Set value: hset lilei name “LL”
- List (like stack, LIFO)
lpush mylist aaa
lpush mylist bbb
lpush mylist ccc
lrange mylist 0 10 //print 0_10 elements in mylist
- Other types: Geo HyperLogLog
//result 1. ‘ccc’, 2. ‘bbb’, 3. ‘aaa’
- Set
sadd myset 111
members myset
- Sorted Set
zadd myzset 3 abc
zadd myzset 1 abd
zrangebyscore myzset 0 10 // 1. abd 3. abc
Query for keys with certain prefix in a big set of keys
My Answer: how big is the set?
Use KEYS pattern to query for keys with a certain pattern
“keys k1*” //start with ‘k1’
Interviewer Q: The service is online, Will use KEYS interfere with the service?
My Answer: Yes, it will use a lot of computing resources. Because KEYS will return all the matching keys. if the amount is huge, it will slow down the server.
Use SCAN instead: SCAN cursor [MATCH pattern] [COUNT count]
- iterator based on the cursor (last time result)
- it starts with cursor 0, util it returns another 0 (reach the end), called a full iteration
- does not guarantee the fixed amount of keys a time, around the count parameter; Support fuzzy query
Example:
scan 0 match k1* count 10 // from cursor 0 scan for prefix k1
result:
1) ‘123’
2) 1) ‘k15435’
2) ‘k1644’
3) ‘k14667’
scan 123 match k1* count 10
……
Warning: does not guarantee non-duplication, need to write script to filter out dups.
what are the tasks for distributed lock
- mutex: blocking other
- security: only the lock owner could release the lock
- deadlock resolving
- fault tolerance
How to implement distributed lock using Redis?
Option 1 using SETNX and EXPIRE,not an optimal option which disobeys atomicity
SETNX locknx test, only when the key is existing, it will be created and initialized with value; else, it will decline the update
people use its feature to test whether there are other transactions using this part of data. if there is, wait for its release.
expire locknx 2 //2 seconds it will expire
Option 2 SET key value [EX seconds] [PX milliseconds] [NX|XX]
EX PX: expire time
NX: set the key when it is non-exist
XX: set the key when it exist
return value : OK or nil
example:
set locktarget 12345 ex 10 nx
Follow-up
if many key expires at the same time, it will slow down service, how to solve it?
add random number to expire time when it is created