Redis — memcached on steroids

91
memcached on steroids

description

Presentation held in the seminar on "NotOnlySQL databases."

Transcript of Redis — memcached on steroids

Page 1: Redis — memcached on steroids

memcached on steroids

Page 2: Redis — memcached on steroids

“blazingly fast” – Kirk Haines, Engineyard

Page 3: Redis — memcached on steroids

Back in the day…

Salvatore Sanfilippo

Page 4: Redis — memcached on steroids

Back in the day…

Salvatore Sanfilippo

Page 5: Redis — memcached on steroids

Back in the day…

LLOOGG

Google Analytics + Realtime

Page 6: Redis — memcached on steroids

Back in the day…

LLOOGG

Google Analytics + Realtime

Page 7: Redis — memcached on steroids

Back in the day…

LLOOGG

Google Analytics + Realtime

Page 8: Redis — memcached on steroids

Back in the day…

LLOOGG

Google Analytics + Realtime

“ Redis is working very well…

We never experienced any stability problem

– Salvatore Sanfilippo

Page 9: Redis — memcached on steroids

Back in the day…

community growth

@simonw Simon Willison

@antirez just posted a redis feature request http://groups.google.com/group/redis-db/.../cd7e7c65dba53e27 - a way of randomly fetching an item from a set without also deleting it 20 Oct 09 via web

Page 10: Redis — memcached on steroids

Back in the day…

community growth

@simonw Simon Willison

@antirez just posted a redis feature request http://groups.google.com/group/redis-db/.../cd7e7c65dba53e27 - a way of randomly fetching an item from a set without also deleting it 20 Oct 09 via web

Page 11: Redis — memcached on steroids

Back in the day…

VMWare acquisition

Page 12: Redis — memcached on steroids

A little server of awesome

http://www.flickr.com/photos/ajc1/4663140532/

man

, th

at’s

to

tally

sto

len

fro

m S

imo

n W

illis

on

Page 13: Redis — memcached on steroids

Redis Manifesto

1. A DSL for Abstract Data Types

2. Memory storage is #1

3. Fundamental data structures for a fundamental API

4. Code is like a poem

5. We’re against complexity

6. Two levels of API

7. We optimize for joy

Page 14: Redis — memcached on steroids

a key/value store?

A DSL For Abstract Data Types

redis> SET key hello OK redis> GET key “hello”

Page 15: Redis — memcached on steroids

a key/value store?

A DSL For Abstract Data Types

redis> SET key hello OK redis> GET key “hello”

request

Page 16: Redis — memcached on steroids

a key/value store?

A DSL For Abstract Data Types

redis> SET key hello OK redis> GET key “hello”

request

awesome, a REPL! redis-cli

Page 17: Redis — memcached on steroids

on par with

A DSL For Abstract Data Types

redis> APPEND key world (integer) 10 redis> SETNX key bye (integer) 1 redis> GET key “helloworld” redis> DEL key (integer) 1

memcache

Page 18: Redis — memcached on steroids

on par with memcache: atomic counters

A DSL For Abstract Data Types

redis> SET participants 10 OK redis> INCR participants (integer) 11

“It gives Memcached a serious run for its money.” – Mathias Meyer, Peritor GmbH

Page 19: Redis — memcached on steroids

on par with memcache: cache invalidation

A DSL For Abstract Data Types

redis> EXPIRE key 60 OK redis> ...

Page 20: Redis — memcached on steroids

on par with memcache: cache invalidation

A DSL For Abstract Data Types

redis> EXPIRE key 60 OK redis> TTL key (integer) 53 redis>

Page 21: Redis — memcached on steroids

on par with memcache: cache invalidation

A DSL For Abstract Data Types

redis> EXPIRE key 60 OK redis> TTL key (integer) 53 redis> GET key “helloworld” redis>

Page 22: Redis — memcached on steroids

on par with memcache: cache invalidation

A DSL For Abstract Data Types

redis> EXPIRE key 60 OK redis> TTL key (integer) 53 redis> GET key “helloworld” redis> ...

Page 23: Redis — memcached on steroids

on par with memcache: cache invalidation

A DSL For Abstract Data Types

redis> EXPIRE key 60 OK redis> TTL key (integer) 53 redis> GET key “helloworld” redis> GET key (nil)

Page 24: Redis — memcached on steroids

“ Memory is the new disk.

–– Jim Gray, Turing Award laureate

Page 25: Redis — memcached on steroids

Memory storage is #1

Redis key space

http://spotfireblog.tibco.com/wp-content/uploads/in-memory-analytics.jpg

Page 26: Redis — memcached on steroids

Memory storage is #1

snapshotting mode

http://www.flickr.com/photos/bionicteaching/3212235059/

Page 27: Redis — memcached on steroids

Memory storage is #1

snapshotting mode

http://www.flickr.com/photos/bionicteaching/3212235059/

every x seconds every y operations on SAVE / BGSAVE

Page 28: Redis — memcached on steroids

Memory storage is #1

snapshotting mode

http://www.flickr.com/photos/bionicteaching/3212235059/

every x seconds every y operations on SAVE / BGSAVE

fork(); // copy on write!

dump(); move();

Page 29: Redis — memcached on steroids

Memory storage is #1

Append-Only File – Write-Ahead Logging

http://www.flickr.com/photos/generationbass/4827013488/

Page 30: Redis — memcached on steroids

Memory storage is #1

Append-Only File – Write-Ahead Logging

http://www.flickr.com/photos/generationbass/4827013488/

fsync() on every command

Page 31: Redis — memcached on steroids

Memory storage is #1

Append-Only File – Write-Ahead Logging

http://www.flickr.com/photos/generationbass/4827013488/

fsync() on every command

fsync() every second

Page 32: Redis — memcached on steroids

Memory storage is #1

Append-Only File – Write-Ahead Logging

http://www.flickr.com/photos/generationbass/4827013488/

fsync() on every command

fsync() every second

fsync() per OS

Page 33: Redis — memcached on steroids

Memory storage is #1

Append-Only File – Write-Ahead Logging

http://www.flickr.com/photos/generationbass/4827013488/

fsync() on every command

fsync() every second

fsync() per OS

BGREWRITEAOF trims AOF

Page 34: Redis — memcached on steroids

Memory storage is #1

meh, persistence

http://www.flickr.com/photos/generationbass/4827013488/

memory overhead

Page 35: Redis — memcached on steroids

Memory storage is #1

memory contention

?

Page 36: Redis — memcached on steroids

Memory storage is #1

memory contention

?

Page 37: Redis — memcached on steroids

Memory storage is #1

memory contention

? Virtual Memory to the rescue

Page 38: Redis — memcached on steroids

Memory storage is #1

memory contention

? Virtual Memory to the rescue

because Salvatore is much brighter than all of Microsoft Research

Page 39: Redis — memcached on steroids

Memory storage is #1

memory contention

? Virtual Memory to the rescue

because Salvatore is much brighter than all of Microsoft Research

…and Redis types do not map to OS pages

Page 40: Redis — memcached on steroids

Memory storage is #1

memory contention

? Virtual Memory to the rescue

because Salvatore is much brighter than all of Microsoft Research

…and Redis types do not map to OS pages

all keys need to stay in memory values must be swapped in their entirety

swapped by age and size

Page 41: Redis — memcached on steroids

Fundamental data structures for a fundamental API

Page 42: Redis — memcached on steroids

lists, we can

redis> LPUSH databases mysql (integer) 1 redis> LPUSH databases mongodb (integer) 2

Fundamental data structures for a fundamental API

Page 43: Redis — memcached on steroids

lists, we can

redis> LPUSH databases mysql (integer) 1 redis> LPUSH databases mongodb (integer) 2

redis> LRANGE databases 0 -1 1) “mongodb” 2) “mysql”

Fundamental data structures for a fundamental API

Page 44: Redis — memcached on steroids

lists, we can

redis> LPUSH databases mysql (integer) 1 redis> LPUSH databases mongodb (integer) 2

redis> LRANGE databases 0 -1 1) “mongodb” 2) “mysql”

Fundamental data structures for a fundamental API

Bulk reply

Page 45: Redis — memcached on steroids

lists, we can

redis> LPUSH databases mysql (integer) 1 redis> LPUSH databases mongodb (integer) 2

redis> LRANGE databases 0 -1 1) “mongodb” 2) “mysql”

Fundamental data structures for a fundamental API

Bulk reply

implemented as Linked Lists

Page 46: Redis — memcached on steroids

lists, we can

redis> LPUSH databases mysql (integer) 1 redis> LPUSH databases mongodb (integer) 2

redis> LRANGE databases 0 -1 1) “mongodb” 2) “mysql”

Fundamental data structures for a fundamental API

Bulk reply

implemented as Linked Lists

PUSH POP

O(1)

Page 47: Redis — memcached on steroids

a data structure server

hash tables

sets

sorted sets

Fundamental data structures for a fundamental API

lists

Page 48: Redis — memcached on steroids

a data structure server

hash tables

sets

sorted sets

Fundamental data structures for a fundamental API

lists

Page 49: Redis — memcached on steroids

a data structure server

hash tables

sets

sorted sets

Fundamental data structures for a fundamental API

lists

Page 50: Redis — memcached on steroids

RENAME key newkey RENAMENX key newkey RPOP key RPOPLPUSH source destination RPUSH key value RPUSHX key value SADD key member SCARD key SDIFF key [key ...] SDIFFSTORE destination key [key ...] SELECT index SET key value SETBIT key offset value SETEX key seconds value SETNX key value SETRANGE key offset value SINTER key [key ...] SINTERSTORE destination key [key ...] SISMEMBER key member SMEMBERS key SMOVE source destination member SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] SPOP key SRANDMEMBER key SREM key member STRLEN key SUNION key [key ...] SUNIONSTORE destination key [key ...] TTL key TYPE key ZADD key score member ZCARD key ZCOUNT key min max ZINCRBY key increment member ZINTERSTORE destination numkeys key [key ...] ZRANGE key start stop [WITHSCORES] ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] ZRANK key member ZREM key member ZREMRANGEBYRANK key start stop ZREMRANGEBYSCORE key min max ZREVRANGE key start stop [WITHSCORES] ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count] ZREVRANK key member ZSCORE key member ZUNIONSTORE destination numkeys key [key ...]

APPEND key value BLPOP key [key ...] BRPOP key [key ...] timeout BRPOPLPUSH source destination timeout DECR key DECRBY key decrement DEL key [key ...] EXISTS key EXPIRE key seconds EXPIREAT key timestamp FLUSHALL R FLUSHDB GET key GETBIT key offset GETRANGE key start end GETSET key value HDEL key field HEXISTS key field HGET key field HGETALL key HINCRBY key field increment HKEYS key HLEN key HMGET key field [field ...] HMSET key field value [field value ...] HSET key field value HSETNX key field value HVALS key INCR key INCRBY key increment KEYS pattern LINDEX key index LINSERT key BEFORE|AFTER pivot value LLEN key LPOP key LPUSH key value LPUSHX key value LRANGE key start stop LREM key count value LSET key index value LTRIM key start stop MGET key [key ...] MOVE key db MSET key value [key value ...] MSETNX key value [key value ...] PERSIST key RANDOMKEY

a data processing server

Fundamental data structures for a fundamental API

Page 51: Redis — memcached on steroids

RENAME key newkey RENAMENX key newkey RPOP key RPOPLPUSH source destination RPUSH key value RPUSHX key value SADD key member SCARD key SDIFF key [key ...] SDIFFSTORE destination key [key ...] SELECT index SET key value SETBIT key offset value SETEX key seconds value SETNX key value SETRANGE key offset value SINTER key [key ...] SINTERSTORE destination key [key ...] SISMEMBER key member SMEMBERS key SMOVE source destination member SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] SPOP key SRANDMEMBER key SREM key member STRLEN key SUNION key [key ...] SUNIONSTORE destination key [key ...] TTL key TYPE key ZADD key score member ZCARD key ZCOUNT key min max ZINCRBY key increment member ZINTERSTORE destination numkeys key [key ...] ZRANGE key start stop [WITHSCORES] ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] ZRANK key member ZREM key member ZREMRANGEBYRANK key start stop ZREMRANGEBYSCORE key min max ZREVRANGE key start stop [WITHSCORES] ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count] ZREVRANK key member ZSCORE key member ZUNIONSTORE destination numkeys key [key ...]

APPEND key value BLPOP key [key ...] BRPOP key [key ...] timeout BRPOPLPUSH source destination timeout DECR key DECRBY key decrement DEL key [key ...] EXISTS key EXPIRE key seconds EXPIREAT key timestamp FLUSHALL R FLUSHDB GET key GETBIT key offset GETRANGE key start end GETSET key value HDEL key field HEXISTS key field HGET key field HGETALL key HINCRBY key field increment HKEYS key HLEN key HMGET key field [field ...] HMSET key field value [field value ...] HSET key field value HSETNX key field value HVALS key INCR key INCRBY key increment KEYS pattern LINDEX key index LINSERT key BEFORE|AFTER pivot value LLEN key LPOP key LPUSH key value LPUSHX key value LRANGE key start stop LREM key count value LSET key index value LTRIM key start stop MGET key [key ...] MOVE key db MSET key value [key value ...] MSETNX key value [key value ...] PERSIST key RANDOMKEY

a data processing server

Fundamental data structures for a fundamental API

every operation is atomic

Page 52: Redis — memcached on steroids

RENAME key newkey RENAMENX key newkey RPOP key RPOPLPUSH source destination RPUSH key value RPUSHX key value SADD key member SCARD key SDIFF key [key ...] SDIFFSTORE destination key [key ...] SELECT index SET key value SETBIT key offset value SETEX key seconds value SETNX key value SETRANGE key offset value SINTER key [key ...] SINTERSTORE destination key [key ...] SISMEMBER key member SMEMBERS key SMOVE source destination member SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] SPOP key SRANDMEMBER key SREM key member STRLEN key SUNION key [key ...] SUNIONSTORE destination key [key ...] TTL key TYPE key ZADD key score member ZCARD key ZCOUNT key min max ZINCRBY key increment member ZINTERSTORE destination numkeys key [key ...] ZRANGE key start stop [WITHSCORES] ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] ZRANK key member ZREM key member ZREMRANGEBYRANK key start stop ZREMRANGEBYSCORE key min max ZREVRANGE key start stop [WITHSCORES] ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count] ZREVRANK key member ZSCORE key member ZUNIONSTORE destination numkeys key [key ...]

APPEND key value BLPOP key [key ...] BRPOP key [key ...] timeout BRPOPLPUSH source destination timeout DECR key DECRBY key decrement DEL key [key ...] EXISTS key EXPIRE key seconds EXPIREAT key timestamp FLUSHALL R FLUSHDB GET key GETBIT key offset GETRANGE key start end GETSET key value HDEL key field HEXISTS key field HGET key field HGETALL key HINCRBY key field increment HKEYS key HLEN key HMGET key field [field ...] HMSET key field value [field value ...] HSET key field value HSETNX key field value HVALS key INCR key INCRBY key increment KEYS pattern LINDEX key index LINSERT key BEFORE|AFTER pivot value LLEN key LPOP key LPUSH key value LPUSHX key value LRANGE key start stop LREM key count value LSET key index value LTRIM key start stop MGET key [key ...] MOVE key db MSET key value [key value ...] MSETNX key value [key value ...] PERSIST key RANDOMKEY

a data processing server

Fundamental data structures for a fundamental API

every operation is atomic

Mind blown.

Page 53: Redis — memcached on steroids

CAP & ACID promises

consistency

partition tolerance

availability durability

atomicity

isolation

consistency

Fundamental data structures for a fundamental API

“kicks ACID out the door” –Mathias Meyer, Chief Cloud Officer

Page 54: Redis — memcached on steroids

CAP & ACID promises

consistency

partition tolerance

availability durability

atomicity

isolation

consistency

Fundamental data structures for a fundamental API

“kicks ACID out the door” –Mathias Meyer, Chief Cloud Officer

“that’s like saying why aren’t filesystems ACID”

Page 55: Redis — memcached on steroids

Code is like a poem

C 23.000 lines of code

Page 56: Redis — memcached on steroids

We’re against complexity

Page 57: Redis — memcached on steroids

Two levels of APIs

transactions

redis> MULTI OK redis> GET key QUEUED redis> DEL key QUEUED redis> EXEC 1) “helloworld” 2) (integer) 1

Page 58: Redis — memcached on steroids

Two levels of APIs

transactions

redis> MULTI OK redis> GET key QUEUED redis> DEL key QUEUED redis> EXEC 1) “helloworld” 2) (integer) 1

EXEC DISCARD

Page 59: Redis — memcached on steroids

Two levels of APIs

transactions

redis> MULTI OK redis> GET key QUEUED redis> DEL key QUEUED redis> EXEC 1) “helloworld” 2) (integer) 1

atomic

Page 60: Redis — memcached on steroids

Two levels of APIs

transactions

redis> MULTI OK redis> GET key QUEUED redis> DEL key QUEUED redis> EXEC 1) “helloworld” 2) (integer) 1

atomic

there’s always a but:

server crash = partial execution

Page 61: Redis — memcached on steroids

Two levels of APIs

transactions

redis> MULTI OK redis> GET key QUEUED redis> DEL key QUEUED redis> EXEC 1) “helloworld” 2) (integer) 1

atomic

there’s always a but:

server crash = partial execution

there’s always a but but:

detected on startup => exit

Page 62: Redis — memcached on steroids

Two levels of APIs

transactions

redis> MULTI OK redis> GET key QUEUED redis> DEL key QUEUED redis> EXEC 1) “helloworld” 2) (integer) 1

atomic

there’s always a but:

server crash = partial execution

there’s always a but but:

detected on startup => exit

there’s always a but but but:

redis-check-aof repairs it

Page 63: Redis — memcached on steroids

Two levels of APIs

optimistic locking

redis> WATCH key OK redis> MULTI OK

Page 64: Redis — memcached on steroids

Two levels of APIs

optimistic locking

redis> WATCH key OK redis> MULTI OK

SET key ciao <redis OK

Page 65: Redis — memcached on steroids

Two levels of APIs

optimistic locking

redis> WATCH key OK redis> MULTI OK

SET key ciao <redis OK

redis> SET key bye QUEUED redis> EXEC (nil)

Page 66: Redis — memcached on steroids

Two levels of APIs

optimistic locking

redis> WATCH key OK redis> MULTI OK

SET key ciao <redis OK

redis> SET key bye QUEUED redis> EXEC (nil) GET key <redis

“ciao”

Page 67: Redis — memcached on steroids

We optimize for joy

Pub/Sub

redis> SUBSCRIBE chat Reading messages… (press Ctrl-C to quit) 1) “subscribe” 2) “chat” 3) (integer) 1

Page 68: Redis — memcached on steroids

We optimize for joy

Pub/Sub

redis> SUBSCRIBE chat Reading messages… (press Ctrl-C to quit) 1) “subscribe” 2) “chat” 3) (integer) 1

type

Page 69: Redis — memcached on steroids

We optimize for joy

Pub/Sub

redis> SUBSCRIBE chat Reading messages… (press Ctrl-C to quit) 1) “subscribe” 2) “chat” 3) (integer) 1

channel

Page 70: Redis — memcached on steroids

We optimize for joy

Pub/Sub

redis> SUBSCRIBE chat Reading messages… (press Ctrl-C to quit) 1) “subscribe” 2) “chat” 3) (integer) 1 message

Page 71: Redis — memcached on steroids

We optimize for joy

Pub/Sub

redis> SUBSCRIBE chat Reading messages… (press Ctrl-C to quit) 1) “subscribe” 2) “chat” 3) (integer) 1

PUBLISH chat “asl?” <redis (integer) 1

Page 72: Redis — memcached on steroids

We optimize for joy

Pub/Sub

redis> SUBSCRIBE chat Reading messages… (press Ctrl-C to quit) 1) “subscribe” 2) “chat” 3) (integer) 1

PUBLISH chat “asl?” <redis (integer) 1

1) “message” 2) “chat” 3) “asl?” ^C

Page 73: Redis — memcached on steroids

We optimize for joy

Pub/Sub

redis> SUBSCRIBE chat Reading messages… (press Ctrl-C to quit) 1) “subscribe” 2) “chat” 3) (integer) 1

PUBLISH chat “asl?” <redis (integer) 1

1) “message” 2) “chat” 3) “asl?” ^C

in other news:

PSUBSCRIBE UNSUBSCRIBE

Page 74: Redis — memcached on steroids

“ Redis is more than just a key-value store, it’s a lifestyle.

–– Mathias Meyer

Page 75: Redis — memcached on steroids

Benchmarks

redis-benchmark

$ redis-benchmark

====== SET ======

100000 requests completed in 0.88 seconds 50 parallel clients 3 bytes payload

====== GET ======

100000 requests completed in 1.23 seconds

Linux 2.6, Xeon X3320 2.5GHz, loopback

Page 76: Redis — memcached on steroids

Benchmarks

Colin Howe

3.7x writes

2.1x reads

speed improvement for over MySQL

Page 77: Redis — memcached on steroids

Replication

master-slave scenario

http://www.flickr.com/photos/joiearai/4393860397/

Page 78: Redis — memcached on steroids

Replication

master-slave scenario

http://www.flickr.com/photos/joiearai/4393860397/

Page 79: Redis — memcached on steroids

Replication

master-slave scenario

http://www.flickr.com/photos/joiearai/4393860397/

Page 80: Redis — memcached on steroids

Replication

master-slave scenario

http://www.flickr.com/photos/joiearai/4393860397/

SLAVEOF host port

Page 81: Redis — memcached on steroids

Replication

master-slave scenario

http://www.flickr.com/photos/joiearai/4393860397/

SLAVEOF host port

save + re-issuing

Page 82: Redis — memcached on steroids

Who’s using it

Github

Resque message queue service

Configuration Management

Routing Information

Page 83: Redis — memcached on steroids

Who’s using it

Digg

Digg Streaming API

personalized news data

real time view

click counts

Page 84: Redis — memcached on steroids

Who’s using it

wooga

Monster World

Happy Hospital

inventory system

user system

Page 85: Redis — memcached on steroids

Who’s using it

Peter Noordhuis

# Author: Peter Noordhuis # Description: Simple demo to showcase Redis PubSub with EventMachine

<170 lines of Ruby code> <220 lines of HTML+JS>

Page 86: Redis — memcached on steroids

Who’s using it

WildlifeNearYou

Vanity

API rate limiting Crowdsourcing taxonomies

A/B testing

The Guardian Crowdsourced analysis of MP expenses

BNP membership list analysis

Hurl HTTP request testing

Page 87: Redis — memcached on steroids

Who’s using it

WildlifeNearYou

Vanity

API rate limiting Crowdsourcing taxonomies

A/B testing

The Guardian Crowdsourced analysis of MP expenses

BNP membership list analysis

Hurl HTTP request testing

general blogosphere hype storm http://redis.io/topics/using-redis lists about 30 others

Page 88: Redis — memcached on steroids

“ I think the… problem is… believing that there can be “one true datastore”.

Different technologies excel at different things.

–– Weixi Yen

Page 89: Redis — memcached on steroids

Check out…

Simon Willison’s tutorial

http://simonwillison.net/static/2010/redis-tutorial

Page 90: Redis — memcached on steroids

Check out…

http://blog.mjrusso.com/2010/10/17/redis-from-the-ground-up

Redis, from the Ground Up

http://try.redis-db.com/

Try Redis

coming August 2011, Salvatore & Pieter, published by O’Reilly

Redis: The Definitive Guide Data modeling, caching, and messaging

also check my Redis bookmarks: http://del.icio.us/lehmannro/redis

http://rediscookbook.org/

The Redis Cookbook

Page 91: Redis — memcached on steroids

man, that was a hell of a ride.

Thanks. Questions?

Now go use Redis.io!