Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A quick introduction to Redis

A quick introduction to Redis

A short talk given at the Memphis Super User group meeting.

Brad Montgomery

June 20, 2013
Tweet

More Decks by Brad Montgomery

Other Decks in Technology

Transcript

  1. 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
  2. Redis? Redis ("REmote DIctionary Server"), is an in-memory Key-Value Database.

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

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

    Also frequently called a "data structure server". Thursday, June 20, 13
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. Lists ‣Store & Manipulate a list (or array) ‣Maintains Order

    ‣Fast index-based operations Thursday, June 20, 13
  17. 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
  18. 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
  19. Sets ‣Stores unique values ‣No Order ‣Set-based Operations ‣Great for

    “tagging” or tracking properties Thursday, June 20, 13
  20. 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
  21. 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
  22. Sorted Sets ‣Just like Sets, but each item gets a

    Score ‣Creates an Order or Ranking ‣Leaderboards! Thursday, June 20, 13
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. Thanks! Learn more here: ‣Official Docs: redis.io ‣Try it Online:

    try.redis-db.com ‣The Little Book of Redis Thursday, June 20, 13