Slide 1

Slide 1 text

Hello Redis! a quick introduction Thursday, June 20, 13

Slide 2

Slide 2 text

Who is this guy? ‣Brad Montgomery @bkmontgomery on twitter ‣Organizer of MEMpy; www.mempy.org ‣Cofounder/Developer at Work for Pie; www.workforpie.com Thursday, June 20, 13

Slide 3

Slide 3 text

Redis? Redis ("REmote DIctionary Server"), is an in-memory Key-Value Database. Also frequently called a "data structure server". Thursday, June 20, 13

Slide 4

Slide 4 text

Redis? Redis ("REmote DIctionary Server"), is an in-memory Key-Value Database. Also frequently called a "data structure server". Thursday, June 20, 13

Slide 5

Slide 5 text

Redis? Redis ("REmote DIctionary Server"), is an in-memory Key-Value Database. Also frequently called a "data structure server". Thursday, June 20, 13

Slide 6

Slide 6 text

Key Points: ‣Very Fast (writes & reads) ‣It's Durable (it can be). ‣In-Memory. Your data should fit in RAM. ‣Values are associated with unique Keys ‣Data Types: Strings, Lists, Sets, Sorted Sets, Hashes ‣Every command is Atomic ‣Keys can be segmented. Thursday, June 20, 13

Slide 7

Slide 7 text

Can I use? Many “supported” language libraries: C, C#, Clojure, Dart, Erlang, Go, Haskell, Java, Lua, Node.js, Perl, PHP, Python, Ruby, Scala (many more “unsupported” libraries) See http://redis.io/clients for more info. Thursday, June 20, 13

Slide 8

Slide 8 text

Durability Wait... Isn’t Redis an “in-memory” data store? ‣ RDB: Write Snapshots to disk; Based on number of changes in a keys in a time period; ‣ AOF: (append-only file) writes data to disk ‣ None: ONLY stores data in memory. ‣ Both: If you really care about your data, use AOF, with periodic snapshots. Thursday, June 20, 13

Slide 9

Slide 9 text

On Keys ‣Redis is lumped into the NoSQL category... ‣You query data via a Key ‣You don’t query the values ‣Keys can be anything (including binary data) ‣Smaller keys are good. ‣Define a schema: object-type:id:field Thursday, June 20, 13

Slide 10

Slide 10 text

On Values Values are associated with a unique key and are stored as adata type: ‣Strings ‣Lists ‣Sets ‣Sorted Sets ‣Hashes Thursday, June 20, 13

Slide 11

Slide 11 text

Strings ‣Most basic data type: ‣Can store up to 512Mb ‣Examples: Serialized data such as JSON or binary data such as a JPEG image. ‣Redis doesn't care what you store. Thursday, June 20, 13

Slide 12

Slide 12 text

Strings Set some values: SET users:brad "{email: [email protected], password: SOMEHASH}" SET users:brad:avatar "\xf8\xf8\xf8\xf0\xf0\xf0\xf8\xf8\xf8\xeb..." Get some values: GET users:brad GET users:brad:avatar Thursday, June 20, 13

Slide 13

Slide 13 text

Strings Set some values: SET users:brad "{email: [email protected], password: SOMEHASH}" SET users:brad:avatar "\xf8\xf8\xf8\xf0\xf0\xf0\xf8\xf8\xf8\xeb..." Get some values: GET users:brad GET users:brad:avatar Key! Thursday, June 20, 13

Slide 14

Slide 14 text

Strings Set some values: SET users:brad "{email: [email protected], password: SOMEHASH}" SET users:brad:avatar "\xf8\xf8\xf8\xf0\xf0\xf0\xf8\xf8\xf8\xeb..." Get some values: GET users:brad GET users:brad:avatar Value! Thursday, June 20, 13

Slide 15

Slide 15 text

Strings Incrementing: SET users:count 0 INCR users:count # users:count -> 1 INCRBY users:count 5 # users:count -> 6 Additional Utilities: STRLEN users:brad # returns 53 APPEND users:brad " moar data" # Careful! GET users:brad # Returns: "{email: [email protected], password: SOMEHASH} moar data" Thursday, June 20, 13

Slide 16

Slide 16 text

Strings Incrementing: SET users:count 0 INCR users:count # users:count -> 1 INCRBY users:count 5 # users:count -> 6 Additional Utilities: STRLEN users:brad # returns 53 APPEND users:brad " moar data" # Careful! GET users:brad # Returns: "{email: [email protected], password: SOMEHASH} moar data" Redis doesn’t care what you do to the data! Thursday, June 20, 13

Slide 17

Slide 17 text

Lists ‣Store & Manipulate a list (or array) ‣Maintains Order ‣Fast index-based operations Thursday, June 20, 13

Slide 18

Slide 18 text

Lists Adding to a list: LPUSH users sally LPUSH users jane LPUSH users bill Do an index-based lookup: LINDEX users 0 # Returns "bill" LINDEX users 2 # Returns "sally" Thursday, June 20, 13

Slide 19

Slide 19 text

Lists Access a range of items: LRANGE users 0 3 # returns "bill", "jane", "sally" Utilities: LLEN users # returns 3 LSET users 0 sallie # changes "sally" to "sallie" POP items (removes first item pushed): LPOP users # returns "bill" Thursday, June 20, 13

Slide 20

Slide 20 text

Sets ‣Stores unique values ‣No Order ‣Set-based Operations ‣Great for “tagging” or tracking properties Thursday, June 20, 13

Slide 21

Slide 21 text

Sets Adding a set: SADD blog:10 redis nosql SADD blog:11 couchdb nosql Updating a Set: SADD blog:11 json # would now include "couchdb", "nosql", "json" Remove an item from a set: SREM blog:11 json # removes the "json" value from the set tags for a blog entry. Thursday, June 20, 13

Slide 22

Slide 22 text

Sets View the members of a set: SMEMBERS blog:10 # returns "nosql", "redis" Does an set contain a given value?: SISMEMBER blog:10 python # returns 0 or 1 Set-based operations: SUNION blog:10 blog:11 # returns "redis", "couchdb", "nosql" SINTER blog:10 blog:11 # returns "nosql" SDIFF blog:10 blog:11 # returns "redis" (in blog:10 but not blog:11) Thursday, June 20, 13

Slide 23

Slide 23 text

Sorted Sets ‣Just like Sets, but each item gets a Score ‣Creates an Order or Ranking ‣Leaderboards! Thursday, June 20, 13

Slide 24

Slide 24 text

Sorted Sets Add some users to the leaderboard: ZADD leaders 30 sally ZADD leaders 50 julie ZADD leaders 10 bill Find a rank (default ordering is low to high): ZRANK leaders bill # returns 0 Find a rank (sorted high to low): ZREVRANK leaders bill # returns 2 Thursday, June 20, 13

Slide 25

Slide 25 text

Sorted Sets How many users have a score between 20 and 50 (inclusive): ZCOUNT leaders 20 50 # returns 2 Find a user's score: ZSCORE leaders bill # returns 10 Thursday, June 20, 13

Slide 26

Slide 26 text

Hashes ‣Similar to strings, but with fields ‣Gives you well-defined objects ‣Gives you Granular access ‣Think JSON or Python dictionaries Thursday, June 20, 13

Slide 27

Slide 27 text

Hashes Given the JSON object for a user: { id: 1234, username: johndoe, email: [email protected], fullname: 'John Doe' } Store in a Redis HASH with: HSET users:1234 username johndoe HSET users:1234 email [email protected] HSET users:1234 fullname "John Doe" Thursday, June 20, 13

Slide 28

Slide 28 text

Hashes Get individual field values: HGET users:1234 username # returns "johndoe" Get Multiple field values: HMGET users:1234 username email # returns "johndoe", "[email protected]" Get all keys/values for a hashed item: HGETALL users:1234 # returns: "username", "johndoe", # "email", "[email protected]", # "fullname", "John Doe" Thursday, June 20, 13

Slide 29

Slide 29 text

Hashes Additional Utilities: HKEYS users:1234 # returns "username", "email", "fullname" HLEN users:1234 # returns 3 HEXISTS users:1234 password # returns 0 (false) HDEL users:1234 fullname # removes the "fullname" field & value Thursday, June 20, 13

Slide 30

Slide 30 text

How is Redis Used? ‣High-frequency writes/reads (Metrics) ‣As a cache or storage for ephemeral data ‣Task Queue ‣Pub/Sup Thursday, June 20, 13

Slide 31

Slide 31 text

Metrics Python Code: metric('github-api') Redis Keys are stored as: m:github-api:2013-01-31 -> 100 ; daily usage m:github-api:w:2013-05 -> 1000 ; weekly usage m:github-api:m:2013-01 -> 10,000 ; monthly usage m:github-api:y:2013 -> 100,000 ; yearly usage ‣django-redis-metrics ‣Easy, Arbitrary metrics for Django apps ‣Backed by Redis Thursday, June 20, 13

Slide 32

Slide 32 text

Metrics daily queries to the github Thursday, June 20, 13

Slide 33

Slide 33 text

Cache/Ephemeral Store ‣User Session Keys ‣“flash messages” ‣cache for common database query results Redis Key expiration EXPIRE messages:brad 30 # expires the "messages:brad" key in 30s Thursday, June 20, 13

Slide 34

Slide 34 text

Task Queue ‣Simple, [asynchronous] Task Queue ‣We use python-rq (e.g. hit 3rd-party REST api’s during an http request). Thursday, June 20, 13

Slide 35

Slide 35 text

Pub/Sup ‣Implements the Publish-Subscribe Pattern ‣Subscribe to a channel ‣Publish a message on a Channel ‣Subscribers get the message. Subscribe to a Channel called messages: SUBSCRIBE messages Publish a message: PUBLISH messages "Hello World!" Thursday, June 20, 13

Slide 36

Slide 36 text

Pub/Sup ‣Implements the Publish-Subscribe Pattern ‣Subscribe to a channel ‣Publish a message on a Channel ‣Subscribers get the message. Subscribe to a Channel called messages: SUBSCRIBE messages Publish a message: PUBLISH messages "Hello World!" Try in multiple terminals. Thursday, June 20, 13

Slide 37

Slide 37 text

There’s More! Thursday, June 20, 13

Slide 38

Slide 38 text

There’s More! but we’re out of time! Thursday, June 20, 13

Slide 39

Slide 39 text

Thanks! Learn more here: ‣Official Docs: redis.io ‣Try it Online: try.redis-db.com ‣The Little Book of Redis Thursday, June 20, 13