2014-09-22 | redis - the basics (viennadb)
DESCRIPTION
A basic introduction to Redis, how it compares to Memcached, and use cases for projects.TRANSCRIPT
The Basics
Dominik Gruber, @the_dom ViennaDB – Sep. 22, 2014
Dominik Gruber • @the_domRedis
Agenda• What Is Redis?
• Operations / Data Types
• Transactions
• Scripting
• Use Cases
Dominik Gruber • @the_domRedis
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 Gruber • @the_domRedis
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 Gruber • @the_domRedis
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 Gruber • @the_domRedis
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 Gruber • @the_domRedis
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 Gruber • @the_domRedis
Prominent Adopters• Twitter
• Tumblr
• GitHub
• Stack Overflow
Dominik Gruber • @the_domRedis
Operations / Data Types
Dominik Gruber • @the_domRedis
Basics
• SET key value [EX seconds] [PX milliseconds] [NX|XX]
• GET key
• DEL key
!
• Possible Use Case: Caching
Dominik Gruber • @the_domRedis
Basics
• EXISTS key
• KEYS pattern
• EXPIRE key seconds
• MGET key [key …]
• MSET key value [key value …]
Dominik Gruber • @the_domRedis
Strings
• STRLEN KEY
• APPEND key value
Dominik Gruber • @the_domRedis
Integer
• INCR key / INCRBY key increment
• DECR key / DECRBY key increment
!
• Possible Use Case: Track Ad- or Page-Impressions
Dominik Gruber • @the_domRedis
Hashes• HSET key field value
• HGET key field
• HGETALL key
• HDEL key field [field …]
!
• Possible Use Case: Session Storage
Dominik Gruber • @the_domRedis
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 Gruber • @the_domRedis
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 Gruber • @the_domRedis
Sets: Operations
• SINTER key [key …] / SINTERSTORE destination key [key …]
• SDIFF key [key …] / SDIFFSTORE destination key [key …]
• SUNION key [key …] / SUNIONSTORE destination key [key …]
Dominik Gruber • @the_domRedis
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 Gruber • @the_domRedis
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 Gruber • @the_domRedis
HyperLogLog
• PFADD key element [element …]
• PFCOUNT key [key …]
• PFMERGE destkey sourcekey [sourcekey ...]
!
• Possible Use Case: Track Unique Visitors
Dominik Gruber • @the_domRedis
Pub/Sub
• SUBSCRIBE channel [channel …]
• UNSUBSCRIBE [channel [channel …]]
!
• PUBLISH channel message
Dominik Gruber • @the_domRedis
Transactions
• MULTI
• EXEC
• DISCARD
Dominik Gruber • @the_domRedis
Transactions
WATCH mykey val = GET mykey val = val + 1 MULTI SET mykey $val EXEC !
Dominik Gruber • @the_domRedis
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 Gruber • @the_domRedis
Scripting: ExampleRandomPushScript = <<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 Gruber • @the_domRedis
Use Cases
Dominik Gruber • @the_domRedis
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 Gruber • @the_domRedis
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 Gruber • @the_domRedis
Q & A
Dominik Gruber • @the_domRedis
Source
• http://redis.io
• http://highscalability.com
• http://www.cucumbertown.com/craft/autocomplete-using-redis-nginx-lua/