redis

66
redis love at 8tracks @nettofarah

Upload: ivayr-farah-netto

Post on 15-Jan-2015

475 views

Category:

Software


2 download

DESCRIPTION

At 8tracks.com we believe on finding the best tool for the job. In the quest of finding a great persistence solution to power our "Explore" system one of our engineers stumbled upon redis.io. A few years later, we run 25 different Redis instances powering several features for our 10 million users. From Tag Browsing, to Autocomplete Search, persisting Player sessions and our News Feed. Our 4 engineers team eats the redis documentation for breakfast. Come join me in some redis love while I tell you why we're such big fans of zsets, set intersections and some other nice features that help us power 12.5 million hours of streaming per month.

TRANSCRIPT

Page 1: Redis

redis love at 8tracks

@nettofarah

Page 2: Redis

@nettofarah

8tracks.com/nettofarah [email protected]

full stack developer at 8tracks.com

Page 3: Redis

8tracks

Page 4: Redis

8tracks

Page 5: Redis
Page 6: Redis
Page 7: Redis
Page 8: Redis
Page 9: Redis

stats

• users: 11,351,961!

• public playlists: 1,545,586 (3,5 mi total)!

• uploaded tracks: 28,185,644

Page 10: Redis

not a sysadmin talk

Page 11: Redis

getting sudo

http://devopsreactions.tumblr.com/post/83704083404/getting-sudo

Page 12: Redis

the right tool for the job

http://peopletakingpictureswithipads.tumblr.com/

Page 13: Redis

RIGHT TOOL

http://peopletakingpictureswithipads.tumblr.com/

Page 14: Redis

FOR THE JOB!

http://peopletakingpictureswithipads.tumblr.com/

Page 15: Redis

engineering effort machine effort

Page 16: Redis

engineering effort machine effort

Page 17: Redis

engineering effort machine effort

Page 18: Redis

engineering effort machine effort

Page 19: Redis

When development tries to blame operations for the outage

http://devopsreactions.tumblr.com/post/84309007419/when-development-tries-to-blame-operations-for-the

Page 20: Redis

R.T.F.J. = engineer <3 + machine <3

Page 21: Redis
Page 22: Redis

key-value store

• open source

• in memory (most of the times)

• data-structure server

Page 23: Redis

data types• strings

• lists

• sets

• sorted sets

• hashes

Page 24: Redis

just like that data structures class

Page 25: Redis

except it is COOL!

Page 26: Redis

you can turn this

Page 27: Redis

into this

next next next next

head

tail

Page 28: Redis

or this

next next next

Page 29: Redis

things to keep in mind

• all in memory (but not restricted to)

• no joins or iterating over data (but not restricted to)

• you should be able to rebuild data

Page 30: Redis

why?

• sometimes basic data structures is all you need

• some problems are hard to fit in a relational way

Page 31: Redis

redis @ 8tracks = <3sidekiq

feature toggles

caching

player (moved to couchdb, then to mysql)

autocomplete (moved to SOLR, then to ElasticSearch)

tag browsing!

news feed

listening sessions

rate limiter

Page 32: Redis

awesome documentation

Page 33: Redis
Page 34: Redis

sidekiq

Page 35: Redis
Page 36: Redis

what’s indie?tag

artistplaylist

Page 37: Redis

1 SELECT name !! ! FROM tags !! ! WHERE name !! ! LIKE 'ind%' LIMIT 10;!!2 SELECT name !! ! FROM mixes !! ! WHERE name !! ! LIKE 'ind%' LIMIT 10;!!3 SELECT login !! ! FROM users !! ! WHERE login !! ! LIKE 'ind%' LIMIT 10;!!4 SELECT name !! FROM artists !! WHERE name !! LIKE 'ind%' LIMIT 10;!

Page 38: Redis

you could do

1 (SELECT name FROM tags WHERE name LIKE 'ind%' LIMIT 10)!2 UNION ALL!3 (SELECT name FROM mixes WHERE name LIKE 'ind%' LIMIT 10);!

Page 39: Redis

the problems

• cross-type search

• I don’t want 10 of each, I want the 10 most relevant ones

• can’t select different column counts

Page 40: Redis

not a fair comparison

people don’t use sql databases for search. they use SOLR (or elasticsearch)

Page 41: Redis

sometimes basic data structures!

is all you need

Page 42: Redis

sorted sets

• score

• add, remove, or update log(n)

• elements in order, fast existence test, fast access to elements in the middle

Page 43: Redis

breaking it down

indie =>

probably irrelevant

relevant

i !! ! ! ! ! ! ! in !!

! ! ! ! ! ! ! ind!! ! ! ! ! ! ! indi!! ! ! ! ! ! ! indie

Page 44: Redis

our collection

The indie summer Indiana Industrial

Page 45: Redis

one zset per n-gram

ind [ ][ ][ ]

indi

indie

Page 46: Redis

which is the same asZADD ind, <score>,

ZADD indi, <score>,

ZADD indie, <score>,

Page 47: Redis

searching

1 ind = $r.zevrange('ind', 0, 10)!!

2 indie = $r.zevrange('indie', 0, 10)!

Page 48: Redis

tag browsing

Page 49: Redis

how?

Page 50: Redis

tags mixes

taggings

Page 51: Redis

a single tag1 SELECT m.* FROM mixes m!2 INNER JOIN taggings tg!3 ON tg.mix_id = m.id!4 WHERE tg.tag_id = 28!5 LIMIT 10;!

chill

Page 52: Redis

two tags1 SELECT m.* FROM mixes m!2 INNER JOIN taggings tg!3 ON tg.mix_id = m.id!4 INNER JOIN taggings tg2!5 ON tg2.mix_id = m.id!6 WHERE tg.tag_id = 28!7 AND tg2.tag_id = 12!8 LIMIT 10;! chill

acoustic

Page 53: Redis

adding sort1 SELECT m.* FROM mixes m!2 INNER JOIN taggings tg!3 ON tg.mix_id = m.id!4 INNER JOIN taggings tg2!5 ON tg2.mix_id = m.id!6 WHERE tg.tag_id = 28!7 AND tg2.tag_id = 12!8 ORDER BY m.first_published_at!9 LIMIT 10;!

Page 54: Redis
Page 55: Redis

ZINTERSTORE

http://redis.io/commands/zinterstore

Page 56: Redis

zinterstore

Time complexity: O(N*K)+O(M*log(M))

intersects two sets, then store them in a new different set

we’ll get to this

Page 57: Redis

chill

Page 58: Redis

acoustic

Page 59: Redis

chill acoustic

+

Page 60: Redis

chill acoustic

1 sets = ['chill', 'acoustic']!2 cache_key = 'chill:acoustic:cache'!3 $r.zinterstore(cache_key, sets, :weights => [1, 1])!

Page 61: Redis

chill

chill acoustic

popular

1 sets = ['popular', 'chill', 'acoustic']!2 cache_key = 'popular:chill:acoustic:cache'!3 $r.zinterstore(cache_key, sets, :weights => [1, 0, 0])!

Page 62: Redis

back to the BIG O thing

O(N*K)+O(M*log(M))

- N: smallest input - K: number of sorted sets - M: number of elements of the resulting set

Page 63: Redis
Page 64: Redis

cool stuff worth checking out

• pipelining

• lua scripting

• replication + persistence

Page 65: Redis

lessons

• it consumes a LOT of memory (duuuh)

• sentinel for failure recovery

• DO NOT USE “keys” in production

• duplication is tricky

Page 66: Redis

gracias!

thank you!

obrigado!