Redis Flashcards

1
Q

Memcache

A

like Hash table

support only simple data type

not support of data persistence

not support master-slave

not support sharding

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Redis

A

support set, list etc, complex data type

support data persistence

support master-slave

support sharding

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why Redis is so fast

A

support 100,000 QPS

  1. solely build on memory, not limited by harddisk I/O. one process ,one thread
  2. data structure is simple, so does the operation, like hashmap, key-value pair O(1)
  3. single thread,support high concurrency, multi-core => multi instance; do not need lock, less conflicts, no complex locking
  4. non-blocking IO, I/O多路复用技术(multiplexing)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Redis’s multiplex mechanism

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Redis‘s data type

A
  • 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Query for keys with certain prefix in a big set of keys

A

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]

  1. iterator based on the cursor (last time result)
  2. it starts with cursor 0, util it returns another 0 (reach the end), called a full iteration
  3. 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what are the tasks for distributed lock

A
  1. mutex: blocking other
  2. security: only the lock owner could release the lock
  3. deadlock resolving
  4. fault tolerance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to implement distributed lock using Redis?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly