Jim Cheung

Redis

(notes from Redis in Action, the book is pretty old and does not cover some latest updates like redis 3.0 comes with cluster support)

Data Types

five different data structure types:

Commands for Data Types

STRING

> set hello world
> get hello
"hello"
> del hello
> get hello
(nil)

LIST

> rpush list-key item
> rpush list-key item2
> rpush list-key item
> lrange list-key 0 -1
1) "item"
2) "item2"
3) "item"
> lindex list-key 1
"item2"
> lpop list-key
"item"
> lrange list-key 0 -1
1) "item2"
2) "item"

SET

combine multiple SETs:

> sadd set-key item
(integer) 1

> sadd set-key item2
> sadd set-key item3
> sadd set-key item
(integer) 0
> smembers set-key
1) "item"
2) "item2"
3) "item3"
> sismember set-key item4
(integer) 0
> sismember set-key item
(integer) 1
> srem set-key item2
(integer) 1
> srem set-key item2
(integer) 0
> smembers set-key
1) "item"
2) "item3"

HASH

> hset hash-key sub-key1 value1
(integer) 1
> hset hash-key sub-key2 value2
(integer) 1
> hset hash-key sub-key1 value1
(integer) 0
> hgetall hash-key
1) "sub-key1"
2) "value1"
3) "sub-key2"
4) "value2"
> hdel hash-key sub-key2
(integer) 1
> hdel hash-key sub-key2
(integer) 0
> hget hash-key sub-key1
"value1"
> hgetall hash-key
1) "sub-key1"
2) "value1"

ZSET

> zadd zset-key 728 member1
(integer) 1
> zadd zset-key 982 member0
(integer) 1
> zadd zset-key 982 member0
(integer) 0
> zrange zset-key 0 -1 withscores
1) "member1"
2) "728"
3) "member0"
4) "982"
> zrangebyscore zset-key 0 800 withscores
1) "member1"
2) "728"
> zrem zset-key member1
(integer) 1
> zrem zset-key member1
(integer) 0
> zrange zset-key 0 -1 withscores
1) "member0"
2) "982"

Publish/Subscribe

Sort

Expiring keys

Persistence

there are two different ways of persisting data to disk. One is a method called snapshotting that takes the data as it exists at one moment in time and writes it to disk. The other method is called AOF, or append-only file, and it works by copying incoming write commands to disk as they happen. These methods can be used together, separately, or not at all in some circumstances.

snapshotting options:

save 60 1000
stop-writes-on-bgsave-error no
rdbcompression yes
dbfilename dump.rdb

append-only options:

appendonly no
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

shared options:

dir ./

Replication

What happens when a slave connects to a master:

StepMaster operationsSlave operations
1(waiting for a command)(Re-)connects to the master; issues the SYNC command
2Starts BGSAVE operation; keeps a backlog of all write commands sent after BGSAVEServes old data (if any), or returns errors to commands (depending on configuration)
3Finishes BGSAVE; starts sending the snapshot to the slave; continues holding a backlog of write commandsDiscards all old data (if any); starts loading the dump as it’s received
4Finishes sending the snapshot to the slave; starts sending the write command backlog to the slaveFinishes parsing the dump; starts responding to commands normally again
5Finishes sending the backlog; starts live streaming of write commands as they happenFinishes executing backlog of write commands from the master; continues executing commands as they happen

Handling system failures

$ redis-check-aof
Usage: redis-check-aof [--fix] <file.aof>
$ redis-check-dump
Usage: redis-check-dump <dump.rdb>

Redis transactions

We begin our transaction by calling the special command MULTI, passing our series of commands, followed by EXEC.

In the process of listing, we’ll use a Redis operation called WATCH, which we combine with MULTI and EXEC, and sometimes UNWATCH or DISCARD

Redis doesn’t lock data during WATCH. Instead, Redis will notify clients if someone else modified the data first, which is called optimistic locking