This document contains slides from a presentation on Redis, an in-memory data structure store. The presentation introduces Redis, describes its key-value store model and various data types including strings, hashes, lists, sets and sorted sets. It provides examples of how to use Redis commands to store, retrieve and manipulate data. The presentation also covers Redis features like atomic operations, expiration of keys, pipelining, sorting with SORT, publish/subscribe and master-slave replication. It suggests use cases for Redis like caching data, storing sessions, logging and high score tables.
2. About Me
Belgian living in Zürich, Switzerland
Weby stuff for 10 years
http://seld.be
Symfony2, Composer and other OSS contributions
http://github.com/Seldaek
Working at Nelmio
http://nelm.io
Symfony2 & frontend performance consulting
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
3. Agenda
Intro
Features
Use Cases
Using it with PHP
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
4. Redis
Wut?
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
5. Store
You put data in it
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
6. Key-Value Store
Like NoSQL?
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
7. In-Memory Key-Value Store
Like Memcached?
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
8. In-Memory Key-Value Store
Memory is fast, but ephemeral
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
9. Persist to Disk
Fast and lasting
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
10. Created by @antirez
Sponsored by VMWare
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
11. So how does it work?
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
12. Simple Text Protocol
Human Readable!
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
13. Example - Client Library
1 SET key value
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
14. Example - Client Library
1 SET key value
2 > OK
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
15. Example - Client Library
1 SET key value
2 > OK
3 GET key
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
16. Example - Client Library
1 SET key value
2 > OK
3 GET key
4 > "value"
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
17. Example - Low Level
1 *3
2 $3
3 SET
4 $3
5 key
6 $5
7 value
1 +OK
1 *2
2 $3
3 GET
4 $3
5 key
1 $5
2 value
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
20. Strings
1 SET name Bob
2 SET age 20
3 MGET name age
4 > Bob
5 > 20
1 GETSET name Alice
2 > Bob
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
21. Strings
1 SETEX age 3 20
2 GET age
3 > 20
4 // .. 3 seconds later ..
5 GET age
6 > null
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
23. Hashes
01 HMSET user name Alice email
alice@example.org
02 HGET user email
03 > alice@example.org
04 HKEYS user
05 > name
06 > email
07 HGETALL user
08 > name
09 > Alice
10 > email
11 > alice@example.org
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
24. Lists
01 RPUSH admins Alice
02 > 1
03 RPUSH admins Bob
04 > 2
05 LINDEX admins 0
06 > Alice
07 LLEN admins
08 > 2
09 RPOP admins
10 > Bob
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
32. Atomic Operations
1 SETNX name Alice
2 GET name
3 > Alice
4 SETNX name Bob
5 GET name
6 > Alice
1 INCR foo
2 GET foo
3 > 1
4 INCRBY foo 3
5 GET foo
6 > 4
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
33. EXPIRE / EXPIREAT / PERSIST
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
41. Lua Scripting: EVAL / EVALSHA
This is great.
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
42. EVAL
1 EVAL <body> <num_keys_in_args> [<arg1> <arg2> ... <arg_N>]
1 GET A
2 EVAL "return redis.call('get', KEYS[1])" 1 A
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
43. EVAL
Example: Atomic Conditional Decrement, Client-Side
1 WATCH foo
2 $val = GET foo
3 $newVal = max(0, $val - 1); // decrement if foo > 0 client-side
4 MULTI
5 SET foo $newVal
6 EXEC
This may return -ERR, or will block.
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
44. EVAL
Example: Atomic Conditional Decrement, Server-Side
01 EVAL "local value = tonumber(redis.call('get', KEYS[1]))
02 if value == nil
03 then
04 return {err="Value at key is not integer"}
05 end
06 if value > tonumber(ARGV[1])
07 then
08 value = value - 1
09 redis.call('set', KEYS[1], value)
10 end
11 return value" 1 foo 0
This is instant and can not fail.
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
45. Redis Cluster
Almost as delayed as
Duke Nukem Forever
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
46. Speed
Redis is FAST.
CPU is unlikely to be the
bottleneck.
What do you do when
you run out of RAM?
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
47. Wait for Redis Cluster?
If you can. Cluster looks great.
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
48. Use sharding, most client
libraries can do it.
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
49. Use Edis, a protocol compatible
storage-oriented server.
http://inaka.github.com/edis/index.html
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
50. Use it without persistence,
store to disk/DB
and load hot data in memory.
A good example in these slides from Wooga
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
51. Ok, great. But..
What is it good for?
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
52. WEB SCALE
THE CLOUD
NOSQL
ELASTIC
HORIZONTAL SCALING
VERTICAL TOO
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
53. WEB SCALE
THE CLOUD
NOSQL
ELASTIC
HORIZONTAL SCALING
VERTICAL TOO
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
55. Get real
"If you do actually have to scale,
then your database isn't going to
magically do it for you."
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
56. Really.
What is it good for?
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
57. Storing Sessions
Caching Data
Other typical Memcached uses
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
58. Caching: Newspaper Site
Expensive reads
Fast-changing pages
Dynamic tracking of what's read for users
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
59. Caching: Newspaper Site
Cache generic results:
1 SETEX <pageid>:content 600 <data>
Store user read-state in sets
1 SADD <pageid>:views <userid>
Combine and render for each user
1 GET <pageid>:content
2 SISMEMBER <pageid>:views <userid>
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
60. Logging, games and other
write-heavy usages
1 LPUSH logs "Log message"
2 // keep the last 1000 entries
3 LTRIM logs 0 999
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be
61. Highscore tables
1 ZADD scores 4290 <playerid>
2 ZADD scores 390 <playerid2>
3 // ...
4 ZREVRANK scores <playerid> // 0
5 ZREVRANGE scores 0 10 WITHSCORES
Note: players will only be listed once since it is a set.
Jordi Boggiano Company nelm.io
Twitter @seldaek Blog seld.be