fosdem how you can benefit from redis, javier ramirez @ teowaki

48
javier ramirez @supercoco9 How you can benefit from using Redis

Upload: javier-ramirez

Post on 06-May-2015

2.634 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Fosdem how you can benefit from redis, javier ramirez @ teowaki

javier ramirez@supercoco9

How you can benefit from using

Redis

Page 2: Fosdem how you can benefit from redis, javier ramirez @ teowaki
Page 3: Fosdem how you can benefit from redis, javier ramirez @ teowaki
Page 4: Fosdem how you can benefit from redis, javier ramirez @ teowaki
Page 5: Fosdem how you can benefit from redis, javier ramirez @ teowaki
Page 6: Fosdem how you can benefit from redis, javier ramirez @ teowaki
Page 7: Fosdem how you can benefit from redis, javier ramirez @ teowaki

I was Lois Lane

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 8: Fosdem how you can benefit from redis, javier ramirez @ teowaki

In the beginner's mind there are many possibilities, in the expert's mind there are few.

Shunryu Suzuki

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 9: Fosdem how you can benefit from redis, javier ramirez @ teowaki

myth: the bottleneck

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 10: Fosdem how you can benefit from redis, javier ramirez @ teowaki

redis-benchmark -r 1000000 -n 2000000 -t get,set,lpush,lpop,mset -P 16 -q

On my laptop:

SET: 513610 requests per secondGET: 641436 requests per secondLPUSH: 845665 requests per secondLPOP: 783392 requests per secondMSET (10 keys): 89988 requests per second

On a small digital ocean server ($5/month)

SET: 227816 requests per secondGET: 228258 requests per secondLPUSH: 251098 requests per secondLPOP: 251572 requests per secondMSET (10 keys): 43918 requests per second

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 11: Fosdem how you can benefit from redis, javier ramirez @ teowaki

myth: Redis is just like memcached

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 12: Fosdem how you can benefit from redis, javier ramirez @ teowaki

myth: Redis is for queues

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 13: Fosdem how you can benefit from redis, javier ramirez @ teowaki

open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.

http://redis.io

started in 2009 by Salvatore Sanfilippo @antirez

104 contributors at https://github.com/antirez/redis

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 14: Fosdem how you can benefit from redis, javier ramirez @ teowaki

The Redis Manifesto1.A DSL for Abstract Data Types

2.Memory storage is #1

3.Fundamental data structures for a fundamental API

4.Two levels of API

5.Code is like a poem; it's not just something we write to reach some practical result

6.We're against complexity

7.We optimize for joy

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 15: Fosdem how you can benefit from redis, javier ramirez @ teowaki

Redis makes you think

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 16: Fosdem how you can benefit from redis, javier ramirez @ teowaki

Redis data typesStrings

Hashes

Lists

Sets

Sorted Sets

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 17: Fosdem how you can benefit from redis, javier ramirez @ teowaki

basic commands

Setting/getting/deleting/expiring

Increment/decrement

Pushing/popping

Checking membership, size...

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 18: Fosdem how you can benefit from redis, javier ramirez @ teowaki

much cooler commandsSetting only if exists

Blocking pop

(Blocking) pop from one list, push to another

Get/set string ranges (and bit operations)

Set intersections/diffs (and store)

Pub/subjavier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 19: Fosdem how you can benefit from redis, javier ramirez @ teowaki

A chat in 6 lines of code

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

r=Redis.new driver: :hiredisr.subscribe “chatrooms:42” do |on|

on.message do |topic, msg|puts “#{topic}: #{msg}”

endend

r.publish “chatrooms:42”, “Hello Fosdem”

Page 20: Fosdem how you can benefit from redis, javier ramirez @ teowaki

atomicity

single threaded, so no concurrency problems

transactions and lua scripts to run multiple operations atomically

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 21: Fosdem how you can benefit from redis, javier ramirez @ teowaki

Scripting with luaYou can use Lua for scripting Redis when you need to atomically execute a sequence of commands in which the output of a command is used as input for another

It reduces the need to use complex lock mechanisms and simplifies dependencies between clients

You can even extend the functionality of Redis by using Lua scripts

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 22: Fosdem how you can benefit from redis, javier ramirez @ teowaki

Redis keeps everything in memory all the time

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 23: Fosdem how you can benefit from redis, javier ramirez @ teowaki

Persistance: RDB Compact binary formatSaves snapshots every few minutesGood for backups and synchronizing

If Redis crashes, a few minutes worth of data will be lost

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 24: Fosdem how you can benefit from redis, javier ramirez @ teowaki

Durability: AOF

Log text formatConfigurable durabilityLarge file, can slow down startup

If Redis crashes, typically one second of data could be lost

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 25: Fosdem how you can benefit from redis, javier ramirez @ teowaki

replication & scaling out

one master, several read-only slaves

sharding

Twemproxy & redis clusterjavier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 26: Fosdem how you can benefit from redis, javier ramirez @ teowaki

what's being used for

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 27: Fosdem how you can benefit from redis, javier ramirez @ teowaki

twitter

Every time line (800 tweets per user) is on redis

5000 writes per second avg300K reads per second

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 28: Fosdem how you can benefit from redis, javier ramirez @ teowaki

twitter

user info from gizmoduck(memcached)

user id tweet id metadata

write API (from browser or client app)

rpushx to Redis

tweet info from tweetypie (memcached + mysql)

your twitter timeline

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

fanout (flockDB)one per follower

Page 29: Fosdem how you can benefit from redis, javier ramirez @ teowaki

World Of Warcraft

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Blizzard is quite secretive about it

Known to have at least 8 servers with 64Gb each to serve user avatars

Known to have seen 1.000.000 users concurrently in the Asian version of the game

Page 30: Fosdem how you can benefit from redis, javier ramirez @ teowaki

pinterest object graph

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

per userRedis SortedSet, with timestamp as the score to

store the users followed explicitlystore the users followed implicitlystore the user’s explicit followersstore the user’s implicit followersstore boards followed explicitly

Redis set to store boards unfollowed explicitly

per boardRedis Hash to store a board’s explicit followersRedis Set to store a board’s explicit unfollowers

Page 31: Fosdem how you can benefit from redis, javier ramirez @ teowaki

viacomObject dependency graph. Cache on steroids

Redis as a queue for background jobs

Activity tracking and view counts buffering before saving to mysql

Lua script working on a slave to recalculate ranking and popularity of contents, then send the data to master. The new process takes 1/60th less than the old one in mysql

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 32: Fosdem how you can benefit from redis, javier ramirez @ teowaki

stack overflowThree level cache:

local cache (no persistence)sessions, and pending view count updates

site cache hot question id lists, user acceptance rates...

global cache (separate Redis DB)Inboxes, API usage quotas...

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 33: Fosdem how you can benefit from redis, javier ramirez @ teowaki

youporn

Most data is found in hashes with ordered sets used to know what data to show.

zInterStore on: videos:filters:released, Videos:filters:orientation:straight,Videos:filters:categories:{category_id}, Videos:ordering:rating

Then perform a zRange to get the pages we want and get the list of video_ids back.

Then start a pipeline and get all the videos from hashes.

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 34: Fosdem how you can benefit from redis, javier ramirez @ teowaki

githubGit as a service internally. Redis is used for storing routing info matching user repositories to server names

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

hipchatRedis for caching. Information like which users are in which rooms, presence information, who is online, etc, so it doesn’t matter which XMPP server you connect to.

Page 35: Fosdem how you can benefit from redis, javier ramirez @ teowaki

when things go wrong

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 36: Fosdem how you can benefit from redis, javier ramirez @ teowaki

the instagram case

moving from redis to cassandra: 75% savings on servers

lesson learnt: know when redis is not appropriate

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 37: Fosdem how you can benefit from redis, javier ramirez @ teowaki

the twilio case

credit card hell

lesson learnt: know what you are doing. Don't change config on the fly

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 38: Fosdem how you can benefit from redis, javier ramirez @ teowaki

Numbers can be scary

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Twitter: 2TB RAM for Redis on its own servers

Blizzard: At least 8 servers with 64Gb each

Pinteres:t 110 Redis instances

Instagram: 1.2 TB of data

Page 39: Fosdem how you can benefit from redis, javier ramirez @ teowaki

openredis

redis as a service on AWS

serving over 1000 customers with a single machine

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 40: Fosdem how you can benefit from redis, javier ramirez @ teowaki

how teowaki is using redis

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 41: Fosdem how you can benefit from redis, javier ramirez @ teowaki

Abusing sidekiq/resque

keep track of every activity in the system, even if you don't need them all right now:- every page view- every API request- every time a record is created/updated/deleted

benefits:- highly decoupled system- easier to divide into services- you can add behaviour without changing your app

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 42: Fosdem how you can benefit from redis, javier ramirez @ teowaki

intermediate cache* As a very fast lightweight storage for analytics data before sending them to our google bigquery based solution

* As a cache for attributes frequently looked up in join tables(names, nicknames, guids, delegated or included model names...)

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 43: Fosdem how you can benefit from redis, javier ramirez @ teowaki

Some of our uses of LuaExpiring attributes inside a Redis hash

Inserting notifications into a list only if there are notpending notifications from the same user for the same scope

Paginating a list by an attribute

Manipulating JSON directly at the Redis layer

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 44: Fosdem how you can benefit from redis, javier ramirez @ teowaki

counters

Atomic counters can be safely invoked concurrently from anywhere, so you can implement “like” features, global sequences or usage monitoring systems in highly concurrent applications for free.

You can share your counters with any other internal application and still be sure they won't collide.

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 45: Fosdem how you can benefit from redis, javier ramirez @ teowaki

Temporary dataRedis allows us to self expire keys after a time has passed. You can use this mechanism for a simple cache

If you operate on a key with expiration time, you can change its value and still keep the expiration going. By combining a decrementing counter with an expiration time, implementing usage quotas is trivial

Also, you can inspect which keys you have in your server efficiently using SCAN commands

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 46: Fosdem how you can benefit from redis, javier ramirez @ teowaki

nginx + lua + redisapache + mruby + redis

Multiple levels of cache by using Redis on the webserver/middleware layer

http://wiki.nginx.org/HttpRedis

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 47: Fosdem how you can benefit from redis, javier ramirez @ teowaki

summarizing* Redis is more powerful than it seems

* Very fast, easy to use, simple, good documentation

* In-memory data structures, distributed, shared and persisted

* Good as data store, intermediate data store, cache or queue

* Lots of use cases, both in huge and smaller systems

You should probably use it a lot more

javier ramirez @supercoco9 https://teowaki.com FOSDEM 2014

Page 48: Fosdem how you can benefit from redis, javier ramirez @ teowaki

Find related links at

https://teowaki.com/teams/javier-community/link-categories/redis

Dank u.Merci.

If you enjoyed this talk, please sign up for https://teowaki.com

Javier Ramírez@supercoco9

FOSDEM 2014