跳轉到

Redis

Use Cases

img

Cheat Sheet

redis-cli

redis-cli -h example.com -p 6379

keys *
flushall

string

set name jojo
setex name 10 jojo
get name
ttl name
expire name
del name

list

lpush fruits apple
lrange fruits <start> <stop>
rpush fruits banana
rrange fruits <start> <stop>
lpop fruits
rpop fruits

hash

hset person name jojo
hget person name
hgetall person

set

sadd hobbies "running"
smembers hobbies
scard hobbies
srem hobbies "running"

sorted set

zadd ranking 1 evan
zadd ranking 2 bob
zrange ranking <start> <stop>
zincrby ranking 10 bob
zrem ranking bob

bitmap

setbit users 1 1
getbit users 1
bitcount users 1 5

hyperloglog

pfadd hll1 redis
pfcount hll1
pfadd hll2 mysql
pfmerge hll3 hll1 hll2

geospatial

geoadd Sicily 13.361389 38.115556 Palermo 15.087269 37.502669 Catania
geopos Sicily Palermo Catania
geodist Sicily Palermo Catania
georadius Sicily 15 37 100 km

Transaction & Locking

Optimistic Locking

watch, multi, exec

Pessimistic Locking

lua script

Data Persistence & Durability

img img img img

Scaling

img img img

Q&A

Question

Why Redis Choose Single-threading?

The performance bottleneck lies in network latency rather than speed of command execution because of pure memory operations. Therefore, multi-threading will not bring significant improvement in performance.

Reference