KEMBAR78
Introduction to Redis | PPTX
INTRODUCTION REDIS
Maarten Smeets
09-04-2018
INTRODUCTION REDIS
INTRODUCING REDIS DATATYPES PUBLISH SUBSCRIBE
PERSISTENCE TRANSACTIONS
REJSON AND REDISEARCH CLIENTS
1 2 3
What is Redis Why use Redis Who uses Redis
INTRODUCTION 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
• Redis is an open source, in-memory data structure store
Can be used as Database, Cache, Message broker
• NoSQL Key/Value store
• Supports multiple data structures
• 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
• Cache database query results
• https://redislabs.com/ebook/part-1-getting-started/chapter-2-anatomy-of-a-redis-web-application/2-4-
database-row-caching/
• Cache entire webpages (e.g. Wordpress)
• https://wordpress.org/plugins/redis-cache/
• Use for HTTP session store
• https://docs.spring.io/spring-session/docs/current/reference/html5/
• Cache logins / cookies
• https://redislabs.com/ebook/part-1-getting-started/chapter-2-anatomy-of-a-redis-web-application/2-1-
login-and-cookie-caching/
WHY REDIS?
• Very flexible
• Very fast
• Caching & Disk persistence
• No schemas, column names
• Rich Datatype Support
HOW TO USE REDIS
EXAMPLE: CACHE DB RESULTS
Clients Back-end / server side
Persistent storage
Cache
WHO USES REDIS
Azure Redis Cache
WHO USES REDIS
IN THE NETHERLANDS
1 2 3
Strings and expiry Lists, Sets, Hashes Sorted sets
DATATYPES
DATATYPES
• Strings
• Lists
• Sets
• Sorted sets
• Hashes
• Bitmaps
• Hyperlogs
• Geospatial
indexes
Datatypes cannot be nested!
E.g. no lists of hashes
You can name your variables like:
person:1
person:2
KEYS person:* gives all person variables
The KEYS command is blocking.
Better to keep a set or list of person keys
STRINGS
• Max size 512Mb
• Byte save. Can store binaries
• Use cases:
• Store JPEG’s
• Store serialized objects
• Example usage
127.0.0.1:6379> SET greeting
‘Hello’
OK
127.0.0.1:6379> GET greeting
“Hello”
127.0.0.1:6379> DEL greeting
(integer) 1
127.0.0.1:6379> EXISTS greeting
(integer) 0
EXPIRY
• Expiry can be set for existing variables
127.0.0.1:6379> SET greeting “Hello”
OK
127.0.0.1:6379> EXPIRE greeting 10
(integer) 1
127.0.0.1:6379> TTL greeting
(integer) 8
127.0.0.1:6379> GET greeting
“Hello”
127.0.0.1:6379> GET greeting
(nil)
EXPIRY
• Expiry can be set when a variable is created and expiry can be removed
127.0.0.1:6379> SETEX greeting 10 “Hello”
OK
127.0.0.1:6379> TTL greeting
(integer) 8
127.0.0.1:6379> PERSIST greeting
OK
127.0.0.1:6379> TTL greeting
(integer) -1
LISTS
• A list of Strings
• Max size 4294967295
• Can for example be used for
• timelines of social networks
• Speed
• Actions at the start or end of the list are very fast.
• Actions in the middle are a little less fast
127.0.0.1:6379> LPUSH people “Maarten”
(integer) 1
127.0.0.1:6379> RPUSH people “John”
(integer) 2
127.0.0.1:6379> LRANGE people 0 -1
1) “Maarten”
2) “John”
127.0.0.1:6379> LLEN people
(integer) 2
127.0.0.1:6379> LPOP people
“Maarten”
127.0.0.1:6379> RPOP people
“John”
SETS
• Unordered collection of Strings
• Does not allow repeated elements
• Useful for tracking unique items
• Allows extracting random members
Using SPOP, SRANDMEMBER
• Useful for intersects and diffs
127.0.0.1:6379> SADD cars “Honda”
(integer) 1
127.0.0.1:6379> SADD cars “Ford”
(integer) 1
127.0.0.1:6379> SADD cars “Honda”
(integer) 0
127.0.0.1:6379> SISMEMBER cars “Honda”
(integer) 1
127.0.0.1:6379> SISMEMBER cars “BMW”
(integer) 0
127.0.0.1:6379> SMEMBERS cars
1) “Ford”
2) “Honda”
127.0.0.1:6379> SMOVE cars mycars “Honda”
(integer) 1
127.0.0.1:6379> SDIFF cars mycars
1) “Ford”
HASHES
• Maps between string fields and values
• Ideal for storing objects
127.0.0.1:6379> HMSET user:1000 username antirez
password P1pp0 age 34
OK
127.0.0.1:6379> HGETALL user:1000
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
• Non repeating collections of Strings
• Every member has a score.
• Members are unique. Scores are not
• Ordering is from small to large score
• Ordering for items with the same score is
alphabetic
• Useful for leader boards or autocomplete
127.0.0.1:6379> ZADD myzset 1 "one"
(integer) 1
127.0.0.1:6379> ZADD myzset 1 "one"
(integer) 0
127.0.0.1:6379> ZADD myzset 1 "uno"
(integer) 1
127.0.0.1:6379> ZADD myzset 2 "two" 3 "three"
(integer) 2
127.0.0.1:6379> ZRANGE myzset 0 -1 WITHSCORES
1) "one"
2) "1"
3) "uno"
4) "1"
5) "two"
6) "2"
7) "three"
8) "3"
127.0.0.1:6379> ZRANGE myzset 0 0
1) "one"
1 2
PERSISTENCE
Redis Database File (RDB) Append Only File (OAF)
PERSISTENCE
RDB (Redis Database File) AOF (Append Only File)
Provides point in time snapshots Logs every write
Creates complete snapshot at specified interval
Replays at server startup.
If log gets big, optimization takes place
File is in binary format File is easily readable
On crash minutes of data can be lost Minimal chance of data loss
Small files, fast (mostly) Big files, ‘slow’
https://www.slideshare.net/eugef/redis-persistence-in-practice-1
1 2
PUBLISH SUBSCRIBE
How to use it Things to mind
PUBLISH SUBSCRIBE
HOW DOES IT WORK
• SUBSCRIBE [channel]
• UNSUBSCRIBE [channel]
• PUBLISH [channel] [message]
Channel can contain
glob patterns like news.*
PUBLISH SUBSCRIBE
THINGS TO MIND
• A missed message has been missed permanently.
No retry
• There is no history of messages
Like a JMS topic without durable subscribers
1 2
TRANSACTIONS
TRANSACTIONS
• Not like relational database transactions!
• Start a transaction: MULTI
Commands after MULTI are queued
• Execute the queued commands: EXEC
All or none of the commands are executed
However, if one fails, the others are still executed
• Make EXEC conditional: WATCH
EXEC will only execute if watched variables are unchanged
WATCH zset
element = ZRANGE zset 0 0
MULTI
ZREM zset element
EXEC
There is no ZPOP but
it can be implemented like below
Redis LUA scripts are also
executed in a transaction
1 2
JSON AND SEARCH
ReJSON RediSearch
REDIS AS A JSON STORE
JSON support can be implemented by adding the ReJSON module from Redis Labs
127.0.0.1:6379> JSON.SET scalar . '"Hello JSON!"'
OK
127.0.0.1:6379> JSON.SET object . '{"foo": "bar", "ans": 42}'
OK
127.0.0.1:6379> JSON.GET object
"{"foo":"bar","ans":42}"
127.0.0.1:6379> JSON.GET object .ans
"42"
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
1 2
CLIENTS
Node.js Spring Boot
CLIENTS
NODE.JS
Create a client and connect
Create an event handler
Subscribe to a channel
CLIENTS
SPRING BOOT
• There is no annotation available to
• Create a container
• Register a listener to the container
• Register a receiver to the listener
• This requires more code than for example listening to a Kafka topic
https://spring.io/guides/gs/messaging-redis/
CLIENTS
SPRING BOOT
Wait for a single message
CLIENTS
SPRING BOOT
SOME THINGS TO MIND IN PRODUCTION
• Redis is single threaded
• Want to use more CPU’s? Use more Redis instances!
• Requests are blocking. Be careful with for example KEYS commands. Mind
the time complexity of operations!
• Mind the number of connections!
• Implement a proxy, for example twemproxy (manages persistent connections)
• 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/
Introduction to Redis

Introduction to Redis

  • 1.
  • 2.
    INTRODUCTION REDIS INTRODUCING REDISDATATYPES PUBLISH SUBSCRIBE PERSISTENCE TRANSACTIONS REJSON AND REDISEARCH CLIENTS
  • 3.
    1 2 3 Whatis Redis Why use Redis Who uses Redis INTRODUCTION REDIS
  • 4.
    INTRODUCING REDIS • Redis Firstrelease 2009 An open-source in-memory database project implementing a distributed, in-memory key-value store with optional durability.
  • 5.
    INTRODUCING REDIS • Redisis an open source, in-memory data structure store Can be used as Database, Cache, Message broker • NoSQL Key/Value store • Supports multiple data structures • Features like • Transactions • Pub/Sub • Scalability / availability options • Time to live for entries Mostly single threaded! Modules can be multi threaded
  • 6.
    TYPICAL USES OFREDIS • Cache database query results • https://redislabs.com/ebook/part-1-getting-started/chapter-2-anatomy-of-a-redis-web-application/2-4- database-row-caching/ • Cache entire webpages (e.g. Wordpress) • https://wordpress.org/plugins/redis-cache/ • Use for HTTP session store • https://docs.spring.io/spring-session/docs/current/reference/html5/ • Cache logins / cookies • https://redislabs.com/ebook/part-1-getting-started/chapter-2-anatomy-of-a-redis-web-application/2-1- login-and-cookie-caching/
  • 7.
    WHY REDIS? • Veryflexible • Very fast • Caching & Disk persistence • No schemas, column names • Rich Datatype Support
  • 8.
    HOW TO USEREDIS EXAMPLE: CACHE DB RESULTS Clients Back-end / server side Persistent storage Cache
  • 9.
  • 10.
    WHO USES REDIS INTHE NETHERLANDS
  • 11.
    1 2 3 Stringsand expiry Lists, Sets, Hashes Sorted sets DATATYPES
  • 12.
    DATATYPES • Strings • Lists •Sets • Sorted sets • Hashes • Bitmaps • Hyperlogs • Geospatial indexes Datatypes cannot be nested! E.g. no lists of hashes You can name your variables like: person:1 person:2 KEYS person:* gives all person variables The KEYS command is blocking. Better to keep a set or list of person keys
  • 13.
    STRINGS • Max size512Mb • Byte save. Can store binaries • Use cases: • Store JPEG’s • Store serialized objects • Example usage 127.0.0.1:6379> SET greeting ‘Hello’ OK 127.0.0.1:6379> GET greeting “Hello” 127.0.0.1:6379> DEL greeting (integer) 1 127.0.0.1:6379> EXISTS greeting (integer) 0
  • 14.
    EXPIRY • Expiry canbe set for existing variables 127.0.0.1:6379> SET greeting “Hello” OK 127.0.0.1:6379> EXPIRE greeting 10 (integer) 1 127.0.0.1:6379> TTL greeting (integer) 8 127.0.0.1:6379> GET greeting “Hello” 127.0.0.1:6379> GET greeting (nil)
  • 15.
    EXPIRY • Expiry canbe set when a variable is created and expiry can be removed 127.0.0.1:6379> SETEX greeting 10 “Hello” OK 127.0.0.1:6379> TTL greeting (integer) 8 127.0.0.1:6379> PERSIST greeting OK 127.0.0.1:6379> TTL greeting (integer) -1
  • 16.
    LISTS • A listof Strings • Max size 4294967295 • Can for example be used for • timelines of social networks • Speed • Actions at the start or end of the list are very fast. • Actions in the middle are a little less fast 127.0.0.1:6379> LPUSH people “Maarten” (integer) 1 127.0.0.1:6379> RPUSH people “John” (integer) 2 127.0.0.1:6379> LRANGE people 0 -1 1) “Maarten” 2) “John” 127.0.0.1:6379> LLEN people (integer) 2 127.0.0.1:6379> LPOP people “Maarten” 127.0.0.1:6379> RPOP people “John”
  • 17.
    SETS • Unordered collectionof Strings • Does not allow repeated elements • Useful for tracking unique items • Allows extracting random members Using SPOP, SRANDMEMBER • Useful for intersects and diffs 127.0.0.1:6379> SADD cars “Honda” (integer) 1 127.0.0.1:6379> SADD cars “Ford” (integer) 1 127.0.0.1:6379> SADD cars “Honda” (integer) 0 127.0.0.1:6379> SISMEMBER cars “Honda” (integer) 1 127.0.0.1:6379> SISMEMBER cars “BMW” (integer) 0 127.0.0.1:6379> SMEMBERS cars 1) “Ford” 2) “Honda” 127.0.0.1:6379> SMOVE cars mycars “Honda” (integer) 1 127.0.0.1:6379> SDIFF cars mycars 1) “Ford”
  • 18.
    HASHES • Maps betweenstring fields and values • Ideal for storing objects 127.0.0.1:6379> HMSET user:1000 username antirez password P1pp0 age 34 OK 127.0.0.1:6379> HGETALL user:1000 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”
  • 19.
    SORTED SETS • Nonrepeating collections of Strings • Every member has a score. • Members are unique. Scores are not • Ordering is from small to large score • Ordering for items with the same score is alphabetic • Useful for leader boards or autocomplete 127.0.0.1:6379> ZADD myzset 1 "one" (integer) 1 127.0.0.1:6379> ZADD myzset 1 "one" (integer) 0 127.0.0.1:6379> ZADD myzset 1 "uno" (integer) 1 127.0.0.1:6379> ZADD myzset 2 "two" 3 "three" (integer) 2 127.0.0.1:6379> ZRANGE myzset 0 -1 WITHSCORES 1) "one" 2) "1" 3) "uno" 4) "1" 5) "two" 6) "2" 7) "three" 8) "3" 127.0.0.1:6379> ZRANGE myzset 0 0 1) "one"
  • 20.
    1 2 PERSISTENCE Redis DatabaseFile (RDB) Append Only File (OAF)
  • 21.
    PERSISTENCE RDB (Redis DatabaseFile) AOF (Append Only File) Provides point in time snapshots Logs every write Creates complete snapshot at specified interval Replays at server startup. If log gets big, optimization takes place File is in binary format File is easily readable On crash minutes of data can be lost Minimal chance of data loss Small files, fast (mostly) Big files, ‘slow’ https://www.slideshare.net/eugef/redis-persistence-in-practice-1
  • 22.
    1 2 PUBLISH SUBSCRIBE Howto use it Things to mind
  • 23.
    PUBLISH SUBSCRIBE HOW DOESIT WORK • SUBSCRIBE [channel] • UNSUBSCRIBE [channel] • PUBLISH [channel] [message] Channel can contain glob patterns like news.*
  • 24.
    PUBLISH SUBSCRIBE THINGS TOMIND • A missed message has been missed permanently. No retry • There is no history of messages Like a JMS topic without durable subscribers
  • 25.
  • 26.
    TRANSACTIONS • Not likerelational database transactions! • Start a transaction: MULTI Commands after MULTI are queued • Execute the queued commands: EXEC All or none of the commands are executed However, if one fails, the others are still executed • Make EXEC conditional: WATCH EXEC will only execute if watched variables are unchanged WATCH zset element = ZRANGE zset 0 0 MULTI ZREM zset element EXEC There is no ZPOP but it can be implemented like below Redis LUA scripts are also executed in a transaction
  • 27.
    1 2 JSON ANDSEARCH ReJSON RediSearch
  • 28.
    REDIS AS AJSON STORE JSON support can be implemented by adding the ReJSON module from Redis Labs 127.0.0.1:6379> JSON.SET scalar . '"Hello JSON!"' OK 127.0.0.1:6379> JSON.SET object . '{"foo": "bar", "ans": 42}' OK 127.0.0.1:6379> JSON.GET object "{"foo":"bar","ans":42}" 127.0.0.1:6379> JSON.GET object .ans "42" https://redislabs.com/blog/redis-as-a-json-store/
  • 29.
    REDISEARCH - REDISPOWERED 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
  • 30.
  • 31.
    CLIENTS NODE.JS Create a clientand connect Create an event handler Subscribe to a channel
  • 32.
    CLIENTS SPRING BOOT • Thereis no annotation available to • Create a container • Register a listener to the container • Register a receiver to the listener • This requires more code than for example listening to a Kafka topic https://spring.io/guides/gs/messaging-redis/
  • 33.
  • 34.
  • 35.
    SOME THINGS TOMIND IN PRODUCTION • Redis is single threaded • Want to use more CPU’s? Use more Redis instances! • Requests are blocking. Be careful with for example KEYS commands. Mind the time complexity of operations! • Mind the number of connections! • Implement a proxy, for example twemproxy (manages persistent connections) • 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/

Editor's Notes

  • #8 Caching like memcached, persistence like MongoDB