Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Redis is the answer, what's the question - Tech Nottingham

355 views

Published on

Published in: Software
  • Login to see the comments

  • Be the first to like this

Redis is the answer, what's the question - Tech Nottingham

  1. 1. Garry Shutler | @gshutler
  2. 2. Garry Shutler | @gshutler
  3. 3. @gshutler What is it? @gshutler
  4. 4. @gshutler What is it good for? @gshutler
  5. 5. The Redis protocol Simple to implement Fast to parse Human readable @gshutler
  6. 6. @gshutler The Redis protocol COMMAND KEY ARGS... @gshutler
  7. 7. @gshutler The Redis protocol redis.command(key, *args) @gshutler
  8. 8. @gshutler The Redis protocol SET mykey 3 @gshutler
  9. 9. @gshutler The Redis protocol redis.set("mykey", 3) @gshutler
  10. 10. @gshutler The Redis protocol SET mykey 3 EX 3600 @gshutler
  11. 11. @gshutler The Redis protocol redis.set("mykey", 3, { "ex": 3600 }) @gshutler
  12. 12. @gshutler Cache @gshutler
  13. 13. Cache function cached(key, fn) { var value = redis.get(key); if (!value) { value = fn(); redis.set(key, value); } return value; } @gshutler
  14. 14. Cache function expensiveThing() { return cached("expensive", function () { // do something that's expensive return expensiveValue; }); } @gshutler
  15. 15. Cache expensiveThing(); // miss expensiveThing(); // hit expensiveThing(); // hit @gshutler
  16. 16. Cache function expensiveThing(param) { var key = "expensive:" + param; return cached(key, function () { sleep(param); return param; }); } @gshutler
  17. 17. Cache expensiveThing(1); // miss expensiveThing(1); // hit expensiveThing(2); // miss expensiveThing(2); // hit expensiveThing(1); // hit @gshutler
  18. 18. Cache function cached(key, expiry, fn) { var value = redis.get(key); if (value) { redis.expire(key, expiry); } else { value = fn(); redis.set(key, value, { "ex": expiry }); } return value; } @gshutler
  19. 19. Cache function expensiveThing(param) { var key = "expensive:" + param; return cached(key, 3600, function () { sleep(param); return param; }); } @gshutler
  20. 20. or vs Relative In 5 minutes Absolute At 9am @gshutler Expiration
  21. 21. @gshutler Expiration my_report:last_hour @gshutler
  22. 22. @gshutler Expiration my_report:2015051118 @gshutler
  23. 23. @gshutler Data types @gshutler
  24. 24. @gshutler String @gshutler
  25. 25. @gshutler List @gshutler
  26. 26. @gshutler Set @gshutler
  27. 27. @gshutler Conditional GET @gshutler
  28. 28. @gshutler Conditional GET var etagKey = "etag:" + url; if (storedEtag = redis.get(etagKey)) { headers["If-None-Match"] = storedEtag; } var response = http.get(url, headers); var etag = response.headers["ETag"]; redis.set(etagKey, etag, { "ex": 300 }); @gshutler
  29. 29. @gshutler Touch values @gshutler
  30. 30. @gshutler Touch values SET {KEY} 1 EX {TIME} EXISTS {KEY} @gshutler
  31. 31. @gshutler Ephemeral lookups @gshutler
  32. 32. @gshutler Ephemeral lookups Forgotten password IDs OAuth access tokens Voucher codes @gshutler
  33. 33. @gshutler Time-series counter @gshutler
  34. 34. @gshutler Time-series counter INCR clicks:201505111830 @gshutler
  35. 35. @gshutler Time-series counter INCR clicks:201505111830 INCR clicks:2015051118 INCR clicks:20150511 @gshutler
  36. 36. @gshutler Time-series counter INCR clicks:201505111830 EXPIRE clicks:201505111830 86400 INCR clicks:2015051118 EXPIRE clicks:2015051118 2419200 INCR clicks:20150511 @gshutler
  37. 37. @gshutler Time-series counter > MGET clicks:201505111826 clicks:201505111827 clicks:201505111828 clicks:201505111829 clicks:201505111830 1)"5" 2)(nil) 3)"2" 4)"10" 5)"1" @gshutler
  38. 38. @gshutler Unique visitors @gshutler
  39. 39. @gshutler Unique visitors SADD {KEY} {VALUE} @gshutler
  40. 40. @gshutler Unique visitors SADD visitors:2015051118 1 SADD visitors:2015051118 2 SADD visitors:2015051118 2 @gshutler
  41. 41. @gshutler Unique visitors SCARD {KEY} @gshutler
  42. 42. @gshutler Unique visitors SCARD visitors:2015051118 @gshutler
  43. 43. @gshutler Queuing @gshutler
  44. 44. @gshutler Queuing RPUSH {KEY} {VALUE} @gshutler
  45. 45. @gshutler Queuing RPUSH queue 1 RPUSH queue 2 @gshutler
  46. 46. @gshutler Queuing ["1", "2"] @gshutler
  47. 47. @gshutler Queuing LPOP {KEY} @gshutler
  48. 48. @gshutler Queuing LPOP queue @gshutler
  49. 49. @gshutler Queuing "1" @gshutler
  50. 50. @gshutler Queuing ["2"] @gshutler
  51. 51. @gshutler Queuing LPUSH queue 3 @gshutler
  52. 52. @gshutler Queuing ["3", "2"] @gshutler
  53. 53. @gshutler Queuing @gshutler RPUSH queue 4 RPUSH queue 5 queue LPOP queue LPOP queue LPOP queue
  54. 54. @gshutler Queuing BLPOP {KEY}... {TIMEOUT} @gshutler
  55. 55. @gshutler Queuing Encoded values Reply queues @gshutler
  56. 56. @gshutler Have a go @gshutler
  57. 57. @gshutler Have a go Cached Fibonacci Distributed processing @gshutler
  58. 58. Garry Shutler | @gshutler

×