21
Applied Redis Barcamp Saigon 2012

Applied Redis

Embed Size (px)

Citation preview

Page 1: Applied Redis

Applied Redis

Barcamp Saigon 2012

Page 2: Applied Redis

About me@hotrannam

KMS Technology

Page 3: Applied Redis

take away

fun

Page 4: Applied Redis

back to school

Page 5: Applied Redis

data structures

Page 6: Applied Redis

big O

Page 7: Applied Redis

do it right( data model & query performance )

Page 8: Applied Redis

redis???

Page 9: Applied Redis

data structureserver

Page 10: Applied Redis

data types

Page 11: Applied Redis

#1 strings#2 hashes#3 lists#4 sets#5 sorted sets

Page 12: Applied Redis

data examples

Keys

page:index.htmllogin_countusers_logged_in_todaylatest_post_idsuser:123:session

users_and_scores

Values

<html><head>[…] string7464{1, 2, 3, 4, 5} set[201, 204, 205] listtime => 10927353 hashusername => joejoe ~ 1348 sorted setfred ~ 938chris ~ 2832

Page 13: Applied Redis

command examples

SET my_key “my value”GET my_keyINCR next_post_idEXPIRE my_key 1234TTL my_keyDEL my_keyEXISTS my_key

HSET product:1 id 1HSET product:1 name “iPad”HSET product:1 available 10

field value

Page 14: Applied Redis

commands have its big O

( mostly )

Page 15: Applied Redis

and beyond

Page 16: Applied Redis

bring to life

Page 17: Applied Redis

#1 cache data

Page 18: Applied Redis

#2 who is online

# add to friends setSADD users:nam:friends duySADD users:nam:friends khoi# add to online setSADD online namSADD online nghiaSADD online khoi# get online friends – {khoi}SINTER users:nam:friends online

Page 19: Applied Redis

#3 leaderboard ( ranking )

# add to leaderboard (sorted set)ZADD leaderboard <score> <player># get top 5ZREVRANGEBYSCORE leaderboard +inf –inf WITHSCORES LIMIT 0 5# get rank of KhoiZREVRANK leaderboard Khoi# get 5 players around KhoiZREVRANGEBYSCORE leaderboard +inf –inf WITHSCORES LIMIT 2 5

max score

min score

countrank

Page 20: Applied Redis

#4 cross-app communication

# web appSUBSCRIBE chatPUBLISH chat “Hello! I’m web app.”

# mobile appSUBSCRIBE chatPUBLISH chat “Hi! I’m mobile app.”

channel

Page 21: Applied Redis

Thank you!