the beauty of simplicity: mastering database design with redis
DESCRIPTION
To many on the outside, Redis looks like your average key-value store. To those in the know, Redis is a powerful data structure store that’s capable of doing much of what a full-featured database can handle.In this talk, Ryan will give a quick overview of Redis’ API and the datatypes it allows you to store and query. He’ll then take you through the process of designing a complex schema with Redis and demonstrate how using simple data structures can help you in making better decisions for your applications.TRANSCRIPT
The Beauty of Simplicity
Mastering Database Design with
What is this talk about?
1 2 3
1 2 3Basic Redis API
1 2 3Simple Use Cases
1 2 3Learning from Redis
SET the-answer 42OK
GET the-answer“42”
The End
42 24
ListsRPUSH primes 2(integer) 1RPUSH primes 3(integer) 2RPUSH primes 5(integer) 3
Lists
LRANGE primes 0 -11) “2”2) “3”3) “5”
SetsSADD words foo(integer) 1SADD words bar(integer) 1SADD words foo(integer) 0
Sets
SMEMBERS words1) “foo”2) “bar”
Sorted SetsZINCRBY words 1 foo“1”ZINCRBY words 1 bar“1”ZINCRBY words 1 foo“2”
Sorted Sets
ZREVRANGE words 0 -1 WITHSCORES1) “foo”2) “2”3) “bar”4) “1”
Hashes
HSET person first_name Ryan(integer) 1HSET person last_name Briones(integer) 1
Hashes
HGETALL person1) “first_name”2) “Ryan”3) “last_name”4) “Briones”
QueuesRPUSH queue foo(integer) 1RPUSH queue bar(integer) 2
LPOP queue“1”
Set IntersectionSADD fb_friends 1234567(integer) 1SADD fb_friends 1234568(integer) 1SADD purchasers 1234567(integer) 1
SINTER fb_friends purchasers1) 1234567
Chat ServerZADD room 1307420357571.96 {:msg => “Hello Mike”}
(integer) 1ZADD room 1307420462678.8{:msg => “Hello Joe”}
(integer) 1ZADD room 1307420496256.58{:msg => “Hello World”}
(integer) 1
Chat Server
ZREVRANGE room 0 301) {...}2) {...}
ZRANGEBYSCORE room1307420357571.96 +inf
1) {...}
Application Data in Redis
Data in RDBMS
id first_name last_name login1 Ryan Briones ryanbriones
... ... ... ...
Data in Redis
INCR users:uids“1”
id first_name last_name login1 Ryan Briones ryanbriones
... ... ... ...
Data in RDBMS
INCR users:uids“1”
SET users:1:first_name RyanOK
SET users:1:last_name BrionesOK
SET users:1:login ryanbrionesOK
Data in Redis
id first_name last_name login1 Ryan Briones ryanbriones
... ... ... ...
Data in RDBMS
KEYS users:1:*1) users:1:first_name2) users:1:last_name3) users:1:login
GET users:1:first_name“Ryan”
GET users:1:last_name“Briones”
GET users:1:login“ryanbriones”
Data in Redis
SELECT * FROM users;
Data in RDBMS
SADD users:all 1(integer) 1
SMEMBERS users:all1) 12) ...
Data in Redis
users:uids 1users:1:first_name Ryanusers:1:last_name Briones
users:1:login ryanbrionesusers:all {1}
Data in Redis
SET users:login:ryanbriones:uid 1OK
GET users:login:ryanbriones:uid“1”
Data in Redis
SELECT * FROM users ORDER BY last_name DESC;
Data in RDBMS
users:uids 1users:1:first_name Ryanusers:1:last_name Briones
users:1:login ryanbrionesusers:all {1}
users:login:ryanbriones:uid 1
Data in Redis
SET users:last_name:Briones:uid 100OK
SADD users_last_names BrionesOK
SET users:last_name:Andrews:uid 200OK
SADD users_last_names AndrewsOK
SORT users_last_names ALPHA DESC BY * GET users:last_name:*:uid
1) 1002) 200
Data in Redis1
2
Data in RedisSORT users_last_names ALPHA DESC BY * GET users:last_name:*:uidSTORE users_uid_by_last_name_desc
(integer) 2
TYPE users_uid_by_last_name_desclist
LRANGE users_uid_by_last_name_desc 0 -11) 2002) 100
EXPIRE users_uid_by_last_name_desc 86400(integer) 1
Data in Redis
Data in RDBMS
id first_name last_name login1 Ryan Briones ryanbriones
... ... ... ...
id title body permalink user_id10 Redis Rocks ... redis-rocks 1
... ... ... ... ...
SET posts:10:title “Redis Rocks”OKSET posts:10:body “Lorem ipsum..”OKSET posts:10:permalink “redis-rocks”OK
SET posts:10:user_id 1OKSET posts:permalink:redis-rocks:id 10OKSADD posts:all 10OK
Data in Redis
SET posts:10:title “Redis Rocks”OKSET posts:10:body “Lorem ipsum..”OKSET posts:10:permalink “redis-rocks”OK
SET posts:10:user_id 1OKSET posts:permalink:redis-rocks:id 10OKSADD posts:all 10OK
LPUSH users:1:posts 10OK
Data in Redis
SET posts:10:title “Redis Rocks”OKSET posts:10:body “Lorem ipsum..”OKSET posts:10:permalink “redis-rocks”OK
SET posts:10:user_id 1OKSET posts:permalink:redis-rocks:id 10OKSADD posts:all 10OK
ZADD users:1:posts 1307463108973.28 10OK
Data in Redis
Data in Redis
SORT user:1:posts DESCGET posts:*:titleGET posts:*:permalink
1) “Redis Rocks”2) redis-rocks
AOF Disk Persistence
Lua Scripting
Pub-Sub
Simple Transactions
Cluster/DR
Replication
Sharding
Ryan Briones
work for//obtiva
@ryanbriones