2014-09-22 | redis - the basics (viennadb)

30
The Basics Dominik Gruber, @the_dom ViennaDB – Sep. 22, 2014

Upload: dominik-gruber

Post on 27-Jun-2015

639 views

Category:

Technology


0 download

DESCRIPTION

A basic introduction to Redis, how it compares to Memcached, and use cases for projects.

TRANSCRIPT

Page 1: 2014-09-22 | Redis - The Basics (ViennaDB)

The Basics

Dominik Gruber, @the_dom ViennaDB – Sep. 22, 2014

Page 2: 2014-09-22 | Redis - The Basics (ViennaDB)

Dominik Gruber • @the_domRedis

Agenda• What Is Redis?

• Operations / Data Types

• Transactions

• Scripting

• Use Cases

Page 3: 2014-09-22 | Redis - The Basics (ViennaDB)

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

Page 4: 2014-09-22 | Redis - The Basics (ViennaDB)

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

Page 5: 2014-09-22 | Redis - The Basics (ViennaDB)

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.”

Page 6: 2014-09-22 | Redis - The Basics (ViennaDB)

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

Page 7: 2014-09-22 | Redis - The Basics (ViennaDB)

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

Page 8: 2014-09-22 | Redis - The Basics (ViennaDB)

Dominik Gruber • @the_domRedis

Prominent Adopters• Twitter

• Pinterest

• Tumblr

• GitHub

• Stack Overflow

Page 9: 2014-09-22 | Redis - The Basics (ViennaDB)

Dominik Gruber • @the_domRedis

Operations / Data Types

Page 10: 2014-09-22 | Redis - The Basics (ViennaDB)

Dominik Gruber • @the_domRedis

Basics

• SET key value [EX seconds] [PX milliseconds] [NX|XX]

• GET key

• DEL key

!

• Possible Use Case: Caching

Page 11: 2014-09-22 | Redis - The Basics (ViennaDB)

Dominik Gruber • @the_domRedis

Basics

• EXISTS key

• KEYS pattern

• EXPIRE key seconds

• MGET key [key …]

• MSET key value [key value …]

Page 12: 2014-09-22 | Redis - The Basics (ViennaDB)

Dominik Gruber • @the_domRedis

Strings

• STRLEN KEY

• APPEND key value

Page 13: 2014-09-22 | Redis - The Basics (ViennaDB)

Dominik Gruber • @the_domRedis

Integer

• INCR key / INCRBY key increment

• DECR key / DECRBY key increment

!

• Possible Use Case: Track Ad- or Page-Impressions

Page 14: 2014-09-22 | Redis - The Basics (ViennaDB)

Dominik Gruber • @the_domRedis

Hashes• HSET key field value

• HGET key field

• HGETALL key

• HDEL key field [field …]

!

• Possible Use Case: Session Storage

Page 15: 2014-09-22 | Redis - The Basics (ViennaDB)

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)

Page 16: 2014-09-22 | Redis - The Basics (ViennaDB)

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

Page 17: 2014-09-22 | Redis - The Basics (ViennaDB)

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 …]

Page 18: 2014-09-22 | Redis - The Basics (ViennaDB)

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

Page 19: 2014-09-22 | Redis - The Basics (ViennaDB)

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

Page 20: 2014-09-22 | Redis - The Basics (ViennaDB)

Dominik Gruber • @the_domRedis

HyperLogLog

• PFADD key element [element …]

• PFCOUNT key [key …]

• PFMERGE destkey sourcekey [sourcekey ...]

!

• Possible Use Case: Track Unique Visitors

Page 21: 2014-09-22 | Redis - The Basics (ViennaDB)

Dominik Gruber • @the_domRedis

Pub/Sub

• SUBSCRIBE channel [channel …]

• UNSUBSCRIBE [channel [channel …]]

!

• PUBLISH channel message

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

Dominik Gruber • @the_domRedis

Transactions

• MULTI

• EXEC

• DISCARD

Page 23: 2014-09-22 | Redis - The Basics (ViennaDB)

Dominik Gruber • @the_domRedis

Transactions

WATCH mykey val = GET mykey val = val + 1 MULTI SET mykey $val EXEC !

Page 24: 2014-09-22 | Redis - The Basics (ViennaDB)

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 ...]

Page 25: 2014-09-22 | Redis - The Basics (ViennaDB)

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))

Page 26: 2014-09-22 | Redis - The Basics (ViennaDB)

Dominik Gruber • @the_domRedis

Use Cases

Page 27: 2014-09-22 | Redis - The Basics (ViennaDB)

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

Page 28: 2014-09-22 | Redis - The Basics (ViennaDB)

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"}

Page 29: 2014-09-22 | Redis - The Basics (ViennaDB)

Dominik Gruber • @the_domRedis

Q & A

Page 30: 2014-09-22 | Redis - The Basics (ViennaDB)

Dominik Gruber • @the_domRedis

Source

• http://redis.io

• http://highscalability.com

• http://www.cucumbertown.com/craft/autocomplete-using-redis-nginx-lua/