Redis memcached pdf

8,524 views

Published on

SF MySQL meetup June 2012

Published in: Technology

Redis memcached pdf

  1. 1. CachingMemcached vs. Redis San Francisco MySQL Meetup Ryan Lowe Erin O’Neill 1
  2. 2. Databases WE LOVE THEM ...Except when we don’t 2
  3. 3. 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
  4. 4. When Databases Suck• Lots of concurrent users• ORMs• Big Data Sets• Small Pockets of VERY hot data 4
  5. 5. How Caching Works• External vs. built-in caching• MySQL Query Cache• InnoDB Buffer Pool• Rails SOMETHING 5
  6. 6. Caching Architecture Becomes 6
  7. 7. Why Caching Works 7
  8. 8. 8
  9. 9. 9
  10. 10. There are only two hardproblems in ComputerScience: cache invalidation,naming things, and off-by-one errors. -- Martin Fowler 10
  11. 11. Problems with Caching• Cache Misses• Thundering Herd• Stale Data• Warm-Up Times• Overly-Aggressive Caching• Poor Cache Design 11
  12. 12. Cache Misses Cache Hit: 1 Operation Cache Miss: 3 Operations 12
  13. 13. 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
  14. 14. Stale Data• Must maintain consistency between the database and the cache from within the application• Extremely difficult to validate correctness 14
  15. 15. 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
  16. 16. Use Cases• Sessions • Tag Clouds• Popular Items • Auto-suggest lists• Full Page Cache • Relationships• Profile • User Information Information • Online Users• User Preferences • Statistics 16
  17. 17. MemcachedMemcached is an in-memory key-value store for small chunks ofarbitrary data (strings, objects) fromresults of database calls, API calls,or page rendering. 17
  18. 18. RedisRedis is an open source, advancedkey-value store. It is often referredto as a “data structure server” sincekeys can contain strings, hashes,lists, sets and sorted sets. 18
  19. 19. In-Memory MeansWe’re Bound By RAM 19
  20. 20. Consistent Hashing• Each Key deterministically goes to a particular server. Think (KEY % SERVERS) 20
  21. 21. Memcached• Dead Simple & Battle Tested• Fast• Non-Blocking get()/set()• Multi-Threaded• Consistent Hashing 21
  22. 22. Memcached Exampleemployee_id = 1234employee_json = { name => ‘Ryan Lowe’, title => ‘Production Engineer’ }set(employee_id, employee_json)get(employee_id) [Returns employee_json] 22
  23. 23. 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
  24. 24. Redis• Advanced Data Types• Replication• Persistence• Usually Fast• Very Cool Atomic Operations 24
  25. 25. 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
  26. 26. Redis: Datatypes• Strings (just like Memcached)• Lists• Sets• Sorted Sets• Hashes 26
  27. 27. Redis: Lists• Stored in sorted order• Can push/pop• Fast head/tail access• Index access (yay) 27
  28. 28. Redis: Listsr.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
  29. 29. Redis: Sets• Un-ordered collections of strings• Unique (no repeated members)• diff, intersect, merge 29
  30. 30. Redis: Setssadd(‘employees’, ‘Ryan Lowe’)sadd(‘former_employees’, ‘Bryan Lowe’)sdiff(‘former_employees’, ‘employees’) (‘Ryan Lowe’,‘Bryan Lowe’) 30
  31. 31. Redis: Sorted Sets• Same as Sets but ordered by a score 31
  32. 32. 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
  33. 33. Memcached vs. Redis Memcached Redis (multi)get ✓ ✓ (multi)set ✓ ✓ incr/decr ✓ ✓ delete ✓ ✓ Expiration ✓ ✓prepend/append ✓Range Queries ✓ Data Types! ✓ Persistence (sorta) ✓Multi-Threaded ✓ Replication (sorta) ✓ 33
  34. 34. Instrumentation• Redis: info• Memcached: stats• Both give system information, connections, hits, misses, etc.• Graphite most of the metrics!!! 34
  35. 35. Benchmarks 35
  36. 36. 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
  37. 37. Redis Benchmarks 37
  38. 38. Redis Set (1 Server) 1 Client 2 Clients 4 Clients 8 Clients 12 Clients 16 Clients 24 Clients 32 Clients1200010500 9000 7500 6000 4500 3000 1500 0 38
  39. 39. Redis Set (1 Server) 1 Client 2 Clients 4 Clients 8 Clients 12 Clients 16 Clients 24 Clients 32 Clients12000 WTF?!10500 9000 7500 6000 4500 3000 1500 0 39
  40. 40. Redis Set (1 Server) 1 Client 2 Clients 4 Clients 8 Clients 12 Clients 16 Clients 24 Clients10000 8000 6000 4000 2000 0 40
  41. 41. Redis Set (2 Servers) 1 Client 2 Clients 4 Clients 8 Clients 12 Clients 16 Clients 24 Clients 32 Clients1600012000 8000 4000 0 41
  42. 42. Redis Set (2 Servers) 1 Client 2 Clients 4 Clients 8 Clients 12 Clients 16 Clients 24 Clients 32 Clients1600012000 8000 WTF?! 4000 0 42
  43. 43. Redis Set (4 Servers) 1 Client 2 Clients 4 Clients 8 Clients 12 Clients 16 Clients 24 Clients 32 Clients1600012000 8000 4000 0 43
  44. 44. Redis Set (8 Servers) 1 Client 2 Clients 4 Clients 8 Clients 12 Clients 16 Clients 24 Clients 32 Clients1600012000 8000 4000 0 44
  45. 45. Hash Ring Balance (%) Server 1 Server 2100 50 0 Redis Memcached 45
  46. 46. Hash Ring Balance (%) Server 1 Server 2 Server 3 Server 45025 0 Redis Memcached 46
  47. 47. Hash Ring Balance (%) Server 1 Server 2 Server 3 Server 4 Server 5 Server 6 Server 7 Server 8 2512.5 0 Redis Memcached 47
  48. 48. Redis Get (1 Server) 1 Client 2 Clients 4 Clients 8 Clients 12 Clients 16 Clients 24 Clients 32 Clients125000100000 75000 50000 25000 0 48
  49. 49. Redis Get (2 Servers) 1 Client 2 Clients 4 Clients 8 Clients 12 Clients 16 Clients 24 Clients 32 Clients125000100000 75000 50000 25000 0 49
  50. 50. Redis Get (4 Servers) 1 Client 2 Clients 4 Clients 8 Clients 12 Clients 16 Clients 24 Clients 32 Clients125000100000 75000 50000 25000 0 50
  51. 51. Redis Get (8 Servers) 1 Client 2 Clients 4 Clients 8 Clients 12 Clients 16 Clients 24 Clients 32 Clients125000100000 75000 50000 25000 0 51
  52. 52. The Cost of Persistence Redis No Persistence Redis BGSAVE (FIO) Redis AOF1200010000 8000 6000 4000 52
  53. 53. Redis & Memcached Benchmarks 53
  54. 54. Set Operations (1 Server, 24 Clients) Memcached Redis10000 9825 9650 9475 9300 54
  55. 55. Get Operations (1 Server, 24 Clients) Memcached Redis130000117500105000 92500 80000 55
  56. 56. Set Operations (2 Servers, 24 Clients) Memcached Redis130001175010500 9250 8000 56
  57. 57. Get Operations (2 Servers, 24 Clients) Memcached Redis140000107000 74000 41000 8000 57
  58. 58. Set Operations (4 Servers, 24 Clients) Memcached Redis130001175010500 9250 8000 58
  59. 59. Get Operations (4 Servers, 24 Clients) Memcached Redis140000107000 74000 41000 8000 59
  60. 60. Set Operations (8 Servers, 24 Clients) Memcached Redis120001100010000 9000 8000 60
  61. 61. Get Operations (8 Servers, 24 Clients) Memcached Redis150000114500 79000 43500 8000 61
  62. 62. Conclusions• Redis inconsistent under heavy load• We need more benchmarks! • (Redis) Datatype-specific • Big Memory • (Redis) Big Keys 62
  63. 63. Questions? 63

×