60
redis love at 8tracks @nettofarah

a 8tracks ama o Redis

Embed Size (px)

DESCRIPTION

Na 8tracks a gente acredita na ferramenta certa pra resolver os problemas que a gente encontra pela frente! Passamos por vários desafios, mas encontramos uma ferramenta excelente pra nos ajudar a servir mais de 100 mil request por minuto e tornar nossos problemas de persistência bem mais simples. Redis salvou a nossa pele mais de uma vez!

Citation preview

Page 1: a 8tracks ama o Redis

redis love at 8tracks

@nettofarah

Page 2: a 8tracks ama o Redis

@nettofarah

8tracks.com/nettofarah [email protected]

full stack developer na 8tracks.com

Page 3: a 8tracks ama o Redis

8tracks

Page 4: a 8tracks ama o Redis

8tracks

Page 5: a 8tracks ama o Redis
Page 6: a 8tracks ama o Redis
Page 7: a 8tracks ama o Redis

stats

• users: 13.3mi!

• public playlists: 1.2mi (4,2 mi total)!

• uploaded tracks: 31mi

Page 8: a 8tracks ama o Redis

not a sysadmin talk

Page 9: a 8tracks ama o Redis

getting sudo

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

Page 10: a 8tracks ama o Redis

a melhor ferramenta para o problema

http://peopletakingpictureswithipads.tumblr.com/

Page 11: a 8tracks ama o Redis

melhor ferramenta

http://peopletakingpictureswithipads.tumblr.com/

Page 12: a 8tracks ama o Redis

para o problema!

http://peopletakingpictureswithipads.tumblr.com/

Page 13: a 8tracks ama o Redis

esforço humano esforço da máquina

Page 14: a 8tracks ama o Redis

F.C.P.P. = dev <3 + machine <3

Page 15: a 8tracks ama o Redis
Page 16: a 8tracks ama o Redis

key-value store

• open source

• in memory (most of the times)

• data-structure server

Page 17: a 8tracks ama o Redis

data types• strings

• lists

• sets

• sorted sets

• hashes

Page 18: a 8tracks ama o Redis

just like that data structures class

Page 19: a 8tracks ama o Redis

except it is COOL!

Page 20: a 8tracks ama o Redis

you can turn this

Page 21: a 8tracks ama o Redis

into this

next next next next

head

tail

Page 22: a 8tracks ama o Redis

or this

next next next

Page 23: a 8tracks ama o 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 24: a 8tracks ama o Redis

why?

• sometimes basic data structures is all you need

• some problems are hard to fit in a relational way

Page 25: a 8tracks ama o 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 26: a 8tracks ama o Redis

awesome documentation

Page 27: a 8tracks ama o Redis
Page 28: a 8tracks ama o Redis

sidekiq

Page 29: a 8tracks ama o Redis
Page 30: a 8tracks ama o Redis

what’s indie?tag

artistplaylist

Page 31: a 8tracks ama o 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 32: a 8tracks ama o 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 33: a 8tracks ama o 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 34: a 8tracks ama o Redis

not a fair comparison

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

Page 35: a 8tracks ama o Redis

sometimes basic data structures!

is all you need

Page 36: a 8tracks ama o Redis

sorted sets

• score

• add, remove, or update log(n)

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

Page 37: a 8tracks ama o Redis

breaking it down

indie =>

probably irrelevant

relevant

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

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

Page 38: a 8tracks ama o Redis

our collection

The indie summer Indiana Industrial

Page 39: a 8tracks ama o Redis

one zset per n-gram

ind [ ][ ][ ]

indi

indie

Page 40: a 8tracks ama o Redis

which is the same asZADD ind, <score>,

ZADD indi, <score>,

ZADD indie, <score>,

Page 41: a 8tracks ama o Redis

searching

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

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

Page 42: a 8tracks ama o Redis

tag browsing

Page 43: a 8tracks ama o Redis

how?

Page 44: a 8tracks ama o Redis

tags mixes

taggings

Page 45: a 8tracks ama o 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 46: a 8tracks ama o 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 47: a 8tracks ama o 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 48: a 8tracks ama o Redis
Page 49: a 8tracks ama o Redis

ZINTERSTORE

http://redis.io/commands/zinterstore

Page 50: a 8tracks ama o 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 51: a 8tracks ama o Redis

chill

Page 52: a 8tracks ama o Redis

acoustic

Page 53: a 8tracks ama o Redis

chill acoustic

+

Page 54: a 8tracks ama o Redis

chill acoustic

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

Page 55: a 8tracks ama o 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 56: a 8tracks ama o 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 57: a 8tracks ama o Redis
Page 58: a 8tracks ama o Redis

cool stuff worth checking out

• pipelining

• lua scripting

• replication + persistence

Page 59: a 8tracks ama o Redis

lessons

• it consumes a LOT of memory (duuuh)

• sentinel for failure recovery

• DO NOT USE “keys” in production

• duplication is tricky

Page 60: a 8tracks ama o Redis

gracias!

thank you!

obrigado!