Redis memcached pdf

Preview:

DESCRIPTION

SF MySQL meetup June 2012

Citation preview

CachingMemcached vs. Redis

San Francisco MySQL Meetup

Ryan LoweErin O’Neill

1

Databases

WE LOVE THEM...

Except when we don’t

2

When Databases Rule

• Many access patterns on the same set of data

• Transactions (both monetary and isolated units of work)

• Don’t know what the end state access patterns will be

• Always :)

3

When Databases Suck

• Lots of concurrent users

• ORMs

• Big Data Sets

• Small Pockets of VERY hot data

4

How Caching Works

• External vs. built-in caching

• MySQL Query Cache

• InnoDB Buffer Pool

• Rails SOMETHING

5

Caching Architecture

Becomes

6

Why Caching Works

7

8

9

There are only two hard problems in Computer Science: cache invalidation, naming things, and off-by-one errors.

-- Martin Fowler

10

Problems with Caching

• Cache Misses

• Thundering Herd

• Stale Data

• Warm-Up Times

• Overly-Aggressive Caching

• Poor Cache Design

11

Cache Misses

Cache Hit: 1 OperationCache Miss: 3 Operations

12

Thundering Herd

• Key TOP_10_VIDEOS expires @ 9:00

• Generating the K/V takes three seconds

• Page gets 100 req/s = 100*3 = 300 threads!

13

Stale Data

• Must maintain consistency between the database and the cache from within the application

• Extremely difficult to validate correctness

14

Cache Warm-Up

• All attempts to read from the cache are CACHE MISSES, which require three operations.

• This can result in a significant degradation of response time.

• Usually accompanied by a Thundering Herd

15

Use Cases

• Sessions

• Popular Items

• Full Page Cache

• Profile Information

• User Preferences

• Tag Clouds

• Auto-suggest lists

• Relationships

• User Information

• Online Users

• Statistics

16

Memcached

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

17

Redis

Redis is an open source, 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.

18

In-Memory Means We’re Bound By RAM

19

Consistent Hashing

• Each Key deterministically goes to a particular server. Think (KEY % SERVERS)

20

Memcached

• Dead Simple & Battle Tested

• Fast

• Non-Blocking get()/set()

• Multi-Threaded

• Consistent Hashing

21

Memcached Example

employee_id = 1234

employee_json = {

name => ‘Ryan Lowe’,

title => ‘Production Engineer’ }

set(employee_id, employee_json)

get(employee_id) [Returns employee_json]

22

But I don’t want all the data

• What if I just want the name?

• 64 Bytes for the object vs. 10 for just the name :-(

• 6x network traffic

• More work for the application

• Fatter applications

23

Redis

• Advanced Data Types

• Replication

• Persistence

• Usually Fast

• Very Cool Atomic Operations

24

Redis: The Bad

• Single-Threaded

• Limited client support for consistent hashing

• Significant overhead for persistence (do be discussed later)

• Not widely deployed (compared to Memcached)

25

Redis: Datatypes

• Strings (just like Memcached)

• Lists

• Sets

• Sorted Sets

• Hashes

26

Redis: Lists

• Stored in sorted order

• Can push/pop

• Fast head/tail access

• Index access (yay)

27

Redis: Lists

r.lpush(‘employees’, ‘Ryan Lowe’)

r.lpush(‘employees’, ‘Dave Apgar’)

r.lrange(‘employees’, 0, -1)

(‘Dave Apgar’, ‘Ryan Lowe’)

r.rpush(‘employees’, ‘Evan Miller’)

r.lrange(‘employees’, 0, -1)

(‘Dave Apgar’, ‘Ryan Lowe’, ‘Evan Miller’)

28

Redis: Sets

• Un-ordered collections of strings

• Unique (no repeated members)

• diff, intersect, merge

29

Redis: Sets

sadd(‘employees’, ‘Ryan Lowe’)

sadd(‘former_employees’, ‘Bryan Lowe’)

sdiff(‘former_employees’, ‘employees’)

(‘Ryan Lowe’,‘Bryan Lowe’)

30

Redis: Sorted Sets

• Same as Sets but ordered by a score

31

Redis: Hashesr.hset(‘employees’, ‘count’, 1234)

r.hset(‘employees’,‘females’, 1000)

r.hset(‘employees’,‘males’, 234)

hget(‘employees’,‘count’)

“1234”

hgetall(‘employees’)

{ ‘count’ => 1234,

‘females’ => 1000,

‘males’ => 234 }

32

Memcached vs. RedisMemcached Redis

(multi)get ✓ ✓(multi)set ✓ ✓incr/decr ✓ ✓

delete ✓ ✓Expiration ✓ ✓

prepend/append ✓Range Queries ✓

Data Types! ✓Persistence (sorta) ✓

Multi-Threaded ✓Replication (sorta) ✓

33

Instrumentation

• Redis: info

• Memcached: stats

• Both give system information, connections, hits, misses, etc.

• Graphite most of the metrics!!!

34

Benchmarks

35

About the Benchmarks

• 1 Hour

• Redis 2.6 & Memcached 1.4.5

• 64,000,000 Keys"KEY_#{i.to_s}"

• 51-Character Values(0...50).map{ ('a'..'z').to_a[rand(26)] }.join

36

Redis Benchmarks

37

Redis Set (1 Server)

0

1500

3000

4500

6000

7500

9000

10500

12000

1 Client 2 Clients 4 Clients 8 Clients12 Clients 16 Clients 24 Clients 32 Clients

38

Redis Set (1 Server)

0

1500

3000

4500

6000

7500

9000

10500

12000

1 Client 2 Clients 4 Clients 8 Clients12 Clients 16 Clients 24 Clients 32 Clients

WTF?!

39

Redis Set (1 Server)

0

2000

4000

6000

8000

10000

1 Client 2 Clients 4 Clients 8 Clients12 Clients 16 Clients 24 Clients

40

Redis Set (2 Servers)

0

4000

8000

12000

16000

1 Client 2 Clients 4 Clients 8 Clients12 Clients 16 Clients 24 Clients 32 Clients

41

Redis Set (2 Servers)

0

4000

8000

12000

16000

1 Client 2 Clients 4 Clients 8 Clients12 Clients 16 Clients 24 Clients 32 Clients

WTF?!

42

Redis Set (4 Servers)

0

4000

8000

12000

16000

1 Client 2 Clients 4 Clients 8 Clients12 Clients 16 Clients 24 Clients 32 Clients

43

Redis Set (8 Servers)

0

4000

8000

12000

16000

1 Client 2 Clients 4 Clients 8 Clients12 Clients 16 Clients 24 Clients 32 Clients

44

Hash Ring Balance (%)

0

50

100

Redis Memcached

Server 1 Server 2

45

Hash Ring Balance (%)

0

25

50

Redis Memcached

Server 1 Server 2 Server 3 Server 4

46

Hash Ring Balance (%)

0

12.5

25

Redis Memcached

Server 1 Server 2 Server 3 Server 4 Server 5Server 6 Server 7 Server 8

47

Redis Get (1 Server)

0

25000

50000

75000

100000

125000

1 Client 2 Clients 4 Clients 8 Clients12 Clients 16 Clients 24 Clients 32 Clients

48

Redis Get (2 Servers)

0

25000

50000

75000

100000

125000

1 Client 2 Clients 4 Clients 8 Clients12 Clients 16 Clients 24 Clients 32 Clients

49

Redis Get (4 Servers)

0

25000

50000

75000

100000

125000

1 Client 2 Clients 4 Clients 8 Clients12 Clients 16 Clients 24 Clients 32 Clients

50

Redis Get (8 Servers)

0

25000

50000

75000

100000

125000

1 Client 2 Clients 4 Clients 8 Clients12 Clients 16 Clients 24 Clients 32 Clients

51

The Cost of Persistence

4000

6000

8000

10000

12000

Redis No Persistence Redis BGSAVE (FIO) Redis AOF

52

Redis & MemcachedBenchmarks

53

Set Operations (1 Server, 24 Clients)

9300

9475

9650

9825

10000

Memcached Redis

54

Get Operations(1 Server, 24 Clients)

80000

92500

105000

117500

130000

Memcached Redis

55

Set Operations (2 Servers, 24 Clients)

8000

9250

10500

11750

13000

Memcached Redis

56

Get Operations (2 Servers, 24 Clients)

8000

41000

74000

107000

140000

Memcached Redis

57

Set Operations (4 Servers, 24 Clients)

8000

9250

10500

11750

13000

Memcached Redis

58

Get Operations (4 Servers, 24 Clients)

8000

41000

74000

107000

140000

Memcached Redis

59

Set Operations (8 Servers, 24 Clients)

8000

9000

10000

11000

12000

Memcached Redis

60

Get Operations (8 Servers, 24 Clients)

8000

43500

79000

114500

150000

Memcached Redis

61

Conclusions

• Redis inconsistent under heavy load

• We need more benchmarks!

• (Redis) Datatype-specific

• Big Memory

• (Redis) Big Keys

62

Questions?

63