2. Agenda
• What Is Redis?
• Operations / Data Types
• Transactions
• Scripting
• Use Cases
Dominik Redis Gruber • @the_dom
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. 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. 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. 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. 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
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. 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
23. Transactions
WATCH mykey
val = GET mykey
val = val + 1
MULTI
SET mykey $val
EXEC
!
Dominik Redis Gruber • @the_dom
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. 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
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