Successfully reported this slideshow.
Your SlideShare is downloading. ×

2014-09-22 | Redis - The Basics (ViennaDB)

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad

Check these out next

1 of 30 Ad

More Related Content

Viewers also liked (13)

Recently uploaded (20)

Advertisement

2014-09-22 | Redis - The Basics (ViennaDB)

  1. 1. The Basics Dominik Gruber, @the_dom ViennaDB – Sep. 22, 2014
  2. 2. Agenda • What Is Redis? • Operations / Data Types • Transactions • Scripting • Use Cases Dominik Redis Gruber • @the_dom
  3. 3. Dominik Gruber • Work at KURIER.at online network • Many services, among them events.at–which makes use of Redis • Studied “Software Engineering & Internet Computing” at Vienna University of Technology Dominik Redis Gruber • @the_dom
  4. 4. Fun Facts • Redis = REmote DIctionary Server • Initial release in 2009 • Most recent version: 2.8.17 • Written in C • Libraries for 30+ programming languages • Good documentation Dominik Redis Gruber • @the_dom
  5. 5. Redis “Redis is an open source, BSD licensed, advanced key-value cache and store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, sorted sets, bitmaps and hyperloglogs.” Dominik Redis Gruber • @the_dom
  6. 6. Redis • Redis is an in-memory but persistent on disk database • 1 Million small Key -> String value pairs use ~ 100 MB of memory • Single threaded – but CPU should not be the bottleneck • Average Linux system can deliver even 500k requests per second • Limit is likely the available memory in your system • max. 232 keys Dominik Redis Gruber • @the_dom
  7. 7. Differences to Memcached • Memcached is a “distributed memory object caching system” • Redis persists data to disk eventually • Memcached is an LRU cache • Redis has different data types and more features • Memcached is multithreaded • Similar speed Dominik Redis Gruber • @the_dom
  8. 8. Prominent Adopters • Twitter • Pinterest • Tumblr • GitHub • Stack Overflow Dominik Redis Gruber • @the_dom
  9. 9. Operations / Data Types Dominik Redis Gruber • @the_dom
  10. 10. Basics • SET key value [EX seconds] [PX milliseconds] [NX|XX] • GET key • DEL key ! • Possible Use Case: Caching Dominik Redis Gruber • @the_dom
  11. 11. Basics • EXISTS key • KEYS pattern • EXPIRE key seconds • MGET key [key …] • MSET key value [key value …] Dominik Redis Gruber • @the_dom
  12. 12. Strings • STRLEN KEY • APPEND key value Dominik Redis Gruber • @the_dom
  13. 13. Integer • INCR key / INCRBY key increment • DECR key / DECRBY key increment ! • Possible Use Case: Track Ad- or Page-Impressions Dominik Redis Gruber • @the_dom
  14. 14. Hashes • HSET key field value • HGET key field • HGETALL key • HDEL key field [field …] ! • Possible Use Case: Session Storage Dominik Redis Gruber • @the_dom
  15. 15. Lists • LSET key index value • LPUSH key value [value …] / RPUSH key value [value …] • LPOP key / RPOP key • LRANGE key start stopl • LREM key count value • Possible Use Cases: Task queue, Last modified items (with paging) Dominik Redis Gruber • @the_dom
  16. 16. Sets • SADD key member [member …] • SMEMBERS key / SRANDMEMBER key [count] • SSCAN key cursor [MATCH pattern] [COUNT count] • SISMEMBER key member • SPOP key • SREM key member [member …] • Possible Use Case: Index items by category Dominik Redis Gruber • @the_dom
  17. 17. Sets: Operations • SINTER key [key …] / SINTERSTORE destination key [key …] • SDIFF key [key …] / SDIFFSTORE destination key [key …] • SUNION key [key …] / SUNIONSTORE destination key [key …] Dominik Redis Gruber • @the_dom
  18. 18. Sorted Sets • ZADD key score member [score member …] • ZSCORE key member • ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count] • ZREM key member [member ...] • ZREMRANGEBYLEX key min max • ZINCRBY key increment member • Possible Use Case: Scoreboards, Track time-based events Dominik Redis Gruber • @the_dom
  19. 19. HyperLogLog “Hyperloglog is an approximate technique for computing the number of distinct entries in a set (cardinality). It does this while using a small amount of memory. For instance, to achieve 99% accuracy, it needs only 16 KB. “ – Wikipedia Dominik Redis Gruber • @the_dom
  20. 20. HyperLogLog • PFADD key element [element …] • PFCOUNT key [key …] • PFMERGE destkey sourcekey [sourcekey ...] ! • Possible Use Case: Track Unique Visitors Dominik Redis Gruber • @the_dom
  21. 21. Pub/Sub • SUBSCRIBE channel [channel …] • UNSUBSCRIBE [channel [channel …]] ! • PUBLISH channel message Dominik Redis Gruber • @the_dom
  22. 22. Transactions • MULTI • EXEC • DISCARD Dominik Redis Gruber • @the_dom
  23. 23. Transactions WATCH mykey val = GET mykey val = val + 1 MULTI SET mykey $val EXEC ! Dominik Redis Gruber • @the_dom
  24. 24. Scripting • Execute Lua scripts on the server side • EVAL script numkeys key [key ...] arg [arg …] ! • SCRIPT LOAD script • EVALSHA sha1 numkeys key [key ...] arg [arg ...] Dominik Redis Gruber • @the_dom
  25. 25. Scripting: Example RandomPushScript = <<EOF local i = tonumber(ARGV[1]) local res math.randomseed(tonumber(ARGV[2])) while (i > 0) do res = redis.call('lpush',KEYS[1],math.random()) i = i-1 end return res EOF ! r.del(:mylist) puts r.eval(RandomPushScript,1,:mylist,10,rand(2**32)) Dominik Redis Gruber • @the_dom
  26. 26. Use Cases Dominik Redis Gruber • @the_dom
  27. 27. Resque • “Resque is a Redis-backed Ruby library for creating background jobs, placing them on multiple queues, and processing them later.” • Comes with a web interface • Developed by GitHub • https://github.com/resque/resque • https://github.com/blog/542-introducing-resque Dominik Redis Gruber • @the_dom
  28. 28. Typeahead Completion • ZRANGEBYLEX key min max [LIMIT offset count] • ZRANGEBYLEX uses plain binary comparison ! • ZADD autocomplete 0 apricot 0 apple 0 banana 0 brocolli 0 brinjal • ZRANGEBYLEX autocomplete [br [br(0xff) ! • ZADD 0 mango:{"name":"Mango Smoothie Recipe"} 0 smoothie:{"name":"Mango Smoothie Recipe"} 0 recipe:{"name":"Mango Smoothie Recipe"} Dominik Redis Gruber • @the_dom
  29. 29. Q & A Dominik Redis Gruber • @the_dom
  30. 30. Source • http://redis.io • http://highscalability.com • http://www.cucumbertown.com/craft/ autocomplete-using-redis-nginx-lua/ Dominik Redis Gruber • @the_dom

×