redis presentation
TRANSCRIPT
![Page 1: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/1.jpg)
P E T E R C O O P E R
A whirlwind tour of the next big thing in NoSQL data storage
Redis 101
h t t p : / / t w i t t e r . c o m / p e t e r c
h t t p : / / c o d e r . i o /
![Page 2: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/2.jpg)
No overbearing detail.
Whirlwind tour?
A quick whizz-through.
Enough to get you excited(if Redis is for you.)
Official docs, etc, are an awesome way to continue.
![Page 3: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/3.jpg)
Redis is...an “advanced key-value
store”
NoSQL
b yS A L V AT O R E S A N F I L I P P O
( @ a n t i r e z )
![Page 4: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/4.jpg)
An informal, loosely-defined term for non-relational,
structured data storage systemsLike MongoDB, memcached, CouchDB, and Redis
See http://en.wikipedia.org/wiki/Structured_storage for comparisons
NoSQL?
![Page 5: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/5.jpg)
The canonically simple examplea networked “hash in the sky” behind a simple protocol
memcached
page:index.html
user:123:session
login_count
user:100:last_login_time
<html><head>[...]
xDrSdEwd4dSlZkEkj+
“7464”
“102736485756”
Keys Values
Everything’s a string (or a “blob”)Commands just set or get data (mostly)
![Page 6: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/6.jpg)
Take memcached’s simplicity,Add more data types,
Add persistence,Add more commands,
.. and more™
Redis
![Page 7: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/7.jpg)
StringsListsSets
Sorted/Scored SetsHashes
all accessed by a string “key”
Redis Data Types
![Page 8: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/8.jpg)
Redis Data Examples
page:index.html
user:123:session
login_count
users_logged_in_today
<html><head>[...]
time => 10927353username => joe
7464
{ 1, 2, 3, 4, 5 }
Keys Values
Set
String
latest_post_ids [201, 204, 209,..] List
Hash
users_and_scores joe ~ 1.3483bert ~ 93.4fred ~ 283.22chris ~ 23774.17
Sorted(scored)Set
![Page 9: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/9.jpg)
Strings
SET mystring “hello world”./redis-cli
Redis commandline client app
GET mystring returns
“hello world”
./redis-cli
Key Value
![Page 10: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/10.jpg)
Strings
GETSETMGETSETNXSETEXMSETMSETNX
INCRINCRBYDECRDECRBYAPPENDSUBSTR
Works on strings that appear to be integers. Magic!
http://code.google.com/p/redis/wiki/CommandReference
![Page 11: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/11.jpg)
When caching, you don’t want things to live forever.
Any item in Redis can be made to expire after or at a certain time.
Expiration
EXPIRE your_key 1234TTL your_key == 1234
seconds
![Page 12: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/12.jpg)
Deleting KeysYou can also delete data at will.
DEL your_key
EXISTS your_key == 0 (false)
![Page 13: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/13.jpg)
Lists
fedcba
RPUSH
LPOP
LPUSH
RPOPRPUSH my_q fe.g.
![Page 14: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/14.jpg)
Lists
fedcba }LRANGE 2 3
LLEN == 6
LINDEX 5
X
LREM 1 b
![Page 15: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/15.jpg)
Queues
fedcba
RPUSH
LPOP RPUSH my_q abcRPUSH my_q defLPOP my_q == “abc”LPOP my_q == “def”LPOP my_q == (nil)
Or BLPOP to block (wait) until something can be popped
NOT A NATIVE TYPEStill just a list!
![Page 16: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/16.jpg)
Sets
contains:aba
contains:ase
abacus cabal baba hello teabag base cabaret database
vase vaseline baseline uncaseunbased phase database tease
SADD contains:ase suitcase
SREM contains:aba hello
SMOVE contains:aba contains:ase base
![Page 17: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/17.jpg)
Sets
contains:aba
contains:ase
abacus cabal baba teabag cabaret database
vase vaseline baseline unbased phase database suitcase
SCARD contains:aba == 6
SISMEMBER contains:aba chips == 0 (meaning false)
SRANDMEMBER contains:aba == “teabag”
SMEMBERS contains:ase == vase, vaseline, baseline, unbased, phase, database, suitcase
![Page 18: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/18.jpg)
Setscontains:aba
contains:ase
abacus cabal baba teabag cabaret
vase vaseline baseline unbased phase suitcase
SINTER contains:aba contains:ase == database
database
This is only a simple example. SINTER can take any number of arguments!SUNION is another command that will join sets together.
![Page 19: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/19.jpg)
Setscontains:aba
contains:ase
abacus cabal baba teabag cabaret
vase vaseline baseline unbased phase suitcase
SINTERSTORE resultset contains:aba contains:ase
database
SUNIONSTORE does the same for set unions.
database
resultset
![Page 20: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/20.jpg)
Sorry - no time!
Basically, like normal sets but each element can have a “rank” or “score” and
be returned or sorted by it.
Sorted Sets?
![Page 21: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/21.jpg)
Hashes
created_at 102374657product_id 1name Twinkiesavailable 10
HGET product:1 name == Twinkies
HLEN product:1 == 4
HKEYS product:1 == created_at, product_id, name, available
HGETALL product:1 == created_at => 102374657 product_id => 1 [.. etc ..]
HSET product:1 created_at 102374657
HSET product:1 product_id 1
HSET product:1 name “Twinkies”
HSET product:1 available 10
product:1
HVALS HEXISTS HINCRBY HMGET HMSETAlso...
![Page 22: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/22.jpg)
It’s basically a hash
Session StorageSession 8d3e4created_at: 102374657
user_id: 1
HSET session:8d3e4 created_at 102374657HSET session:8d3e4 user_id 1
HMSET session:8d3e4 created_at 102374657 user_id 1
OR
EXPIRE session:8d3e4 86400
Then let Redis automatically expire it in 24 hours!
![Page 23: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/23.jpg)
Redis Social Network
Usershave names, can follow others, and be
followed
Postsare things like messages, photos, etc.
![Page 24: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/24.jpg)
Userid: 1
name: joe
Userid: 2
name: fred
PostPostPost
PostPostPost
has many..
has many..
![Page 25: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/25.jpg)
Userid: 1
name: joe
Userid: 2
name: fred
PostPostPost
PostPostPost
user:1:name joeusername:joe 1
post:1:content hello world
So we can do a twoway reference
post:1:user 1 Ditto
![Page 26: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/26.jpg)
Building unique key names with colons like
user:1:nameis just a
conventionAny string will dooooo.....
![Page 27: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/27.jpg)
Userid: 1
name: joe
Userid: 2
name: fred
PostPostPost
PostPostPost
set user:1:name joeset username:joe 1
set post:1:content “hello world”set post:1:user 1
Remember, SET and GET are used for string values
![Page 28: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/28.jpg)
Userid: 1
name: joe
Userid: 2
name: fred
PostPostPost
PostPostPost
user:1:posts [3, 2, 1] List
![Page 29: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/29.jpg)
Userid: 1
name: joe
Userid: 2
name: fred
PostPostPost
PostPostPost
user:1:posts [3, 2, 1]
lpush user:1:posts 1lpush user:1:posts 2lpush user:1:posts 3
LPUSH and RPUSH add items to the start or end of a list
![Page 30: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/30.jpg)
Userid: 1
name: joe
user:1:follows {2, 3, 4}
sadd user:1:follows 2sadd user:1:follows 3sadd user:1:follows 4
SADD and SREM add or remove elements to/from a set
Userid: 2
name: fred
Userid: 3
name: bill
Userid: 4
name: jane
Set
Order notimportant
![Page 31: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/31.jpg)
user:1:followed_by {3}
sadd user:1:followed_by 3
You might want to track the relationship in the opposite direction too. Just create
another set!
Userid: 1
name: joe
Userid: 2
name: fred
Userid: 3
name: bill
Userid: 4
name: jane
![Page 32: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/32.jpg)
A Simple Social Network
user:1:nameuser:2:nameusername:joeusername:freduser:1:followsuser:2:followsuser:1:followed_byuser:2:followed_bypost:1:contentpost:1:userpost:2:contentpost:2:useruser:1:postsuser:2:posts
joefred12{2,3,4}{1}{2}{1}“Hello world”2“Blah blah”1[2,3,4][1,5,6]
Keys Values
Set
List
Simplified from the earlier graphs due to lack of space :-)
![Page 33: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/33.jpg)
Unique IDsINCR next_post_id
returns
If next_post_id doesn’t exist or doesn’t contain a number, it’ll be set at 0, incremented, and 1 will be returned.
1 post:1:etc
INCR next_post_idINCR increments the element by 1 andreturns the new value. Great for unique IDs!
returns 2or next_user_id!
![Page 34: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/34.jpg)
INCR next_user_idSET user:[uid]:name [username]SET username:[username] [id]
Creating a new user
INCR next_post_idSET post:[pid]:content [content]
SET post:[pid]:user [pid]LPUSH user:[uid]:posts [pid]
LPUSH posts:global [pid]
Creating a new post
returns [uid]
returns [pid]
![Page 35: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/35.jpg)
I haven’t covered them all though..
On to softer issues.
Enough commands!
SORT SUBSCRIBEZCARD PUBLISH
MONITOR
SLAVEOFSAVERENAME
SELECT
![Page 36: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/36.jpg)
Redis is single threadedNo locking necessary
In other words, commands like INCR won’t tread on each other’s toes coming
from multiple clients simultaneously!
Atomicity
![Page 37: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/37.jpg)
BSD licensed (free, open)
Sponsored by VMware
Written in ANSI C
Good community (list, IRC & wiki)
Works on all POSIX-compliant UNIXes
An unofficial Windows/Cygwin build is available
Redis Factoids
![Page 38: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/38.jpg)
Download a tarball or clone the git repo
Run make
redis-server and redis-cli are ready to roll
(You can make a config file later, if you want.)
Installation
http://code.google.com/p/redis/
![Page 39: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/39.jpg)
Depends a lot on configuration and operation complexity.
Common range from 5000 to 120,000 rps for basic ops
GET/SET/LPUSH/LPOP, etc.(ultra low end to high end hardware)
Performance
![Page 40: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/40.jpg)
redis-benchmark tool on a CentOS virtual machine on a 2009 iMac
Performance
GET: 28011 rps
SET: 36101 rps
INCR: 36496 rps
LPUSH: 38759 rps
LPOP: 38610 rps
And that’s with 1024 byte payloads!
} average~36000
![Page 41: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/41.jpg)
Dump data to disk after certain conditions are met. Or
manually.
An append only log file(which can be optimized/rebuilt automatically)
Persistence
AND/OR
SAVE and BGSAVE commands
but you need to set this up in a config file
![Page 42: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/42.jpg)
Ruby, Python, PHP, Erlang,
Tcl, Perl, Lua, Java, Scala,
Clojure, C#, C/C++,
JavaScript/Node.js, Haskell,
IO, Go
Language Support
i.e. anything actually worth using
![Page 43: Redis Presentation](https://reader034.vdocuments.site/reader034/viewer/2022042422/5527b1fc550346b9358b48c0/html5/thumbnails/43.jpg)
Google “Redis”the official site is great
http://coder.io/tag/redisfor news and articles
P.S. I’m writing a Redis book a little like this presentation. E-mail [email protected] to be put on an announce list!
Missed a lot, so where next!?