2.17. Redis

Redis is an in memory datastore with optional snapshot persistence and replication.

Interactive Experiments

Use the redis-cli command to interact with a local redis server.

> redis-cli
127.0.0.1:6379> help
...

2.17.1. Data Model

Redis server provides access to multiple databases identified by sequential numbers starting from zero. Each database is a key value store whose keys are binary safe strings and values can be one of strings, lists, sets, sorted sets, maps, streams, and hyper log log estimators.

Redis Data Model

Databases.  A server can host multiple databases

  • Identified by sequential numbers starting from zero

  • Each database is a key value store

  • Keys are binary safe strings

  • Keys can have expiration time

  • Values are typed

Value Types.  Binary safe string

  • Can also be interpreted as an integer or a float or a bitmap

List

  • Ordered list of binary safe strings

  • Atomic element removal from both ends

Set

  • Set and sorted set of binary safe strings

  • Sorted set keeps float score with each element

Hash

  • A key value map of binary safe strings

Stream

  • A stream of key value maps of binary safe strings

  • Individual consumers query entries by timestamp

  • Consumer groups can cooperate in processing

Hyper Log Log estimator

  • Opaque type for estimating set cardinality

2.17.2. Database

A database can have an eviction policy, default is no eviction. Alternatives include random and LRU and TTL based policies working across either all keys or keys with expiration set.

The database content snapshot can be periodically written to disk, or an operation log can be written to disk and periodically compacted, or both.

Database Selection Commands

SELECT

select database to use

SWAPDB

swap databases

DBSIZE

get number of keys in database

MOVE

move existing key to another database

FLUSHALL

delete all keys from all databases

FLUSHDB

delete all keys from current database

General Data Access Commands

SCAN

iterate over existign keys

KEYS

list keys matching glob

RANDOMKEY

get random existing key (but not value)

EXISTS

test existence of a key

COPY

copy existing value to another key

RENAME

move existing value to another key

RENAMENX

... if target does not exist

DEL

delete a key

UNLINK

... with asynchronous memory reclaim

EXPIRE

set relative key expiration time

PEXPIRE

... in milliseconds

EXPIREAT

set absolute key expiration time

PEXPIREAT

... in milliseconds

TTL

get key expiration time

PTTL

... in milliseconds

PERSIST

remove key expiration time

DUMP

serialize value with checksum

RESTORE

... and restore serialized value

TOUCH

set last access time for eviction policies

TYPE

get key type (string, list, hash, zset, set, stream)

String Type Access Commands

GET

get the value associated with a key

GETRANGE

... or only some characters

GETDEL

... and delete the key

GETEX

... and set key expiration time

SET

set the value associated with a key

SETRANGE

... or only some characters

SETNX

... if target does not exist

SETEX

... and set the key expiration time

PSETEX

... in milliseconds

GETSET

... and return the previous value

MGET

get values for multiple keys

MSET

set values for multiple keys

MSETNX

... if targets do not exist

APPEND

append new value to the existing value

STRLEN

get the length of the existing value

GETBIT

get single bit

SETBIT

set single bit

BITCOUNT

count bits that are set

BITPOS

find first bit that is set or reset

BITOP

perform logical operation between multiple keys

BITFIELD

perform get or set or inc on subset of bits

INCR

interpret string as integer and increment

INCRBY

... by arbitrary value

DECR

interpret string as integer and decrement

DECRBY

... by arbitrary value

INCRBYFLOAT

interpret string as float and increment or decrement

Experiments With Strings

> redis-cli
127.0.0.1:6379> help @string
...
127.0.0.1:6379> SET somekey somevalue
OK
127.0.0.1:6379> GET somekey
"somevalue"
127.0.0.1:6379> GETRANGE somekey 3 6
"eval"
127.0.0.1:6379> GETSET somekey anothervalue
"somevalue"
127.0.0.1:6379> GETBIT somekey 15
(integer) 0
127.0.0.1:6379> SETBIT somekey 15 1
(integer) 0
127.0.0.1:6379> GET somekey
"aoothervalue"
127.0.0.1:6379> INCR somekey
(error) ERR value is not an integer or out of range
127.0.0.1:6379> SET somekey 123
OK
127.0.0.1:6379> INCR somekey
(integer) 124
127.0.0.1:6379> GET somekey
"124"
127.0.0.1:6379> SET somekey ephemeral PX 1
OK
127.0.0.1:6379> GET somekey
(nil)
...

Hash Type Access Commands

HSCAN

iterate over existing fields

HKEYS

list existing fields

HVALS

get existing values

HRANDFIELD

get random existing field (and value)

HEXISTS

test existence of a field

HLEN

get the number of existing fields

HDEL

delete a field

HGET

get the value of a field

HSET

set the value of a field

HSETNX

... if target does not exist

HGETALL

get all fields and values

HMGET

get multiple fields

HMSET

set multiple fields

HSTRLEN

get the length of a field value

HINCRBY

interpret field as integer and increment or decrement

HINCRBYFLOAT

interpret field as float and increment or decrement

Experiments With Hashes

> redis-cli
127.0.0.1:6379> help @hash
...
127.0.0.1:6379> HSET somekey somefield somevalue
(integer) 1
127.0.0.1:6379> HSET somekey anotherfield anothervalue
(integer) 1
127.0.0.1:6379> GET somekey
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> HGET somekey somefield
"somevalue"
127.0.0.1:6379> HGETALL somekey
1) "somefield"
2) "somevalue"
3) "anotherfield"
4) "anothervalue"
127.0.0.1:6379> HRANDFIELD somekey 1 WITHVALUES
1) "somefield"
2) "somevalue"
...

2.17.3. Publish Subscribe

Publish Subscribe Commands

SUBSCRIBE

subscribe to messages from given channels

PSUBSCRIBE

... with channels given by glob

UNSUBSCRIBE

unsubscribe from given channels

PUNSUBSCRIBE

... with channels given by glob

PUBLISH

publish a message on a given channel

PUBSUB CHANNELS

list channels with subscribers

  • subscription restricts connection commands to pubsub

  • publishing does not require prior channel connection

  • pattern subscription therefore matches continuously

  • key update notifications available on channels derived from key names

Experiments With Publish Subscribe

> redis-cli
127.0.0.1:6379> help @pubsub
...
127.0.0.1:6379> SUBSCRIBE somechannel
Reading messages... (press Ctrl-C to quit)
...
> redis-cli
127.0.0.1:6379> PUBSUB CHANNELS
1) "somechannel"
127.0.0.1:6379> PUBLISH somechannel somemessage
(integer) 1
...

2.17.4. Transactional Command Execution

Transactional Command Execution

MULTI

begin a transaction

EXEC

commit a transaction

DISCARD

abort a transaction

WATCH

changes to watched keys label transaction fail only

UNWATCH

discard watched keys

  • results of all operations are collected during the transaction

  • individual operation failures do not terminate the transaction

Experiments With Transactions

> redis-cli
127.0.0.1:6379> help @transactions
...
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379(TX)> SET somekey somevalue
QUEUED
127.0.0.1:6379(TX)> SET anotherkey anothervalue
QUEUED
127.0.0.1:6379(TX)> EXEC
1) OK
2) OK
127.0.0.1:6379> WATCH somekey
OK
127.0.0.1:6379> GET somekey
"somevalue"
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379(TX)> SET anotherkey somevalue
QUEUED
127.0.0.1:6379(TX)> EXEC
(nil)
127.0.0.1:6379> GET somekey
"interference"
...

2.17.5. Scripting

Scripting

EVAL

execute LUA code with arguments

EVALSHA

... or execute LUA code from cache

SCRIPT LOAD

explicitly load LUA code to cache

SCRIPT EXISTS

check presence of LUA code in cache

SCRIPT FLUSH

drop LUA code cache content

  • script execution is atomic

  • long running read only scripts can be terminated after timeout

  • long running read write scripts cannot be terminated other than by shutdown

Experiments With Scripting

> redis-cli
127.0.0.1:6379> help @scripting
...
127.0.0.1:6379> SET somekey 123
OK
127.0.0.1:6379> SET anotherkey 456
OK
127.0.0.1:6379> EVAL "return tonumber (redis.call ('GET', KEYS [1])) + tonumber (redis.call ('GET', KEYS [2]))" 2 somekey anotherkey
(integer) 579
...

2.17.6. References

  1. The redis Website. http://www.redis.io