Redis
Redis
Maarten Smeets
09-04-2018
INTRODUCTION REDIS
INTRODUCING REDIS DATATYPES PUBLISH SUBSCRIBE
PERSISTENCE TRANSACTIONS
1 2 3
What is Redis Why use Redis Who uses Redis
INTRODUCING REDIS
• Redis
First release 2009
An open-source in-memory database project implementing
a distributed, in-memory key-value store with optional
durability.
INTRODUCING REDIS
• Features like
• Transactions
• Pub/Sub
• Scalability / availability options
• Time to live for entries Mostly single threaded!
Modules can be multi threaded
TYPICAL USES OF REDIS
Persistent storage
WHO USES REDIS
1 2 3
Strings and expiry Lists, Sets, Hashes Sorted sets
DATATYPES
• Expiry can be set when a variable is created and expiry can be removed
• Maps between string fields and values 127.0.0.1:6379> HMSET user:1000 username antirez
password P1pp0 age 34
OK
127.0.0.1:6379> HGETALL user:1000
• Ideal for storing objects 1) “username”
2) “antirez”
3) “password”
4) “P1pp0”
5) “age”
6) “34”
127.0.0.1:6379> HSET user:1000 password 12345
(integer) 0
127.0.0.1:6379> HGET user:1000 password
“12345”
127.0.0.1:6379> HKEYS user:1000
1) “username”
2) “password”
3) “age”
SORTED SETS
1 2
Redis Database File (RDB) Append Only File (OAF)
PERSISTENCE
https://www.slideshare.net/eugef/redis-persistence-in-practice-1
PUBLISH SUBSCRIBE
1 2
How to use it Things to mind
PUBLISH SUBSCRIBE
HOW DOES IT WORK Channel can contain
glob patterns like news.*
• SUBSCRIBE [channel]
• UNSUBSCRIBE [channel]
1 2
TRANSACTIONS There is no ZPOP but
it can be implemented like below
WATCH zset
• Not like relational database transactions! element = ZRANGE zset 0 0
MULTI
ZREM zset element
• Start a transaction: MULTI EXEC
Commands after MULTI are queued
Redis LUA scripts are also
• Execute the queued commands: EXEC executed in a transaction
All or none of the commands are executed
However, if one fails, the others are still executed
1 2
ReJSON RediSearch
REDIS AS A JSON STORE
JSON support can be implemented by adding the ReJSON module from Redis Labs
https://redislabs.com/blog/redis-as-a-json-store/
REDISEARCH - REDIS POWERED SEARCH ENGINE
Auto completion
127.0.0.1:6379> FT.SUGADD autocomplete “hello world” 100
OK
127.0.0.1:6379> FT.SUGGET autocomplete “he”
1) "hello world“
Full text search
127.0.0.1:6379> FT.ADD myIdx doc1 1.0 FIELDS title “hello” body “bla” url “http://redis.io”
OK
127.0.0.1:6379> FT.SEARCH myIdx “hello world” LIMIT 0 10
1) (integer) 1
2) “doc1”
3) 1) “title”
2) “hello”
3) “body”
4) “bla”
5) "url”
6) “http://redis.io”
http://redisearch.io/Quick_Start/
docker run -p 6379:6379 redislabs/redisearch:latest
CLIENTS
1 2
Node.js Spring Boot
CLIENTS
NODE.JS
Subscribe to a channel
CLIENTS
SPRING BOOT
• This requires more code than for example listening to a Kafka topic
https://spring.io/guides/gs/messaging-redis/
CLIENTS
SPRING BOOT
• Snapshotting is blocking
• Investigate persistence requirements and consider rolling BGSAVE, OAF
instead of snapshotting
http://tech.trivago.com/2017/01/25/learn-redis-the-hard-way-in-production/