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

Redis 101

Redis 101

The aim of this talk is to give a brief introduction to Redis, and then show a few practical examples of what we can actually do with it.

First presented at the Great Western Rails meetup in Bristol, hosted by Cookpad to foster the Ruby community in the south west!

Lewis Buckley

May 15, 2018
Tweet

More Decks by Lewis Buckley

Other Decks in Programming

Transcript

  1. 101

  2. What’s Redis? key / string key / hash key /

    list key / set key / sorted set
  3. key / string What can we do? • Store any

    data we like (up to 512MB)
  4. key / string Example redis.set(key, value) redis.set "meetup:5", "Great Western

    Rails - May 18" => "OK" redis.get "meetup:5" => "Great Western Rails - May 18"
  5. key / string Use case: Analytics redis.set request.fullpath, 0 =>

    "OK" 10_400.times { redis.incr request.fullpath } redis.get request.fullpath => "10400"
  6. key / string Rails class ApplicationController before_action :count_visit private def

    count_visit redis.incr(request.fullpath) end end class ProductsController < ApplicationController def show @hits = redis.get(request.fullpath) end end
  7. key / hash What can we do? • Store a

    hash • Basically unlimited • Get specific fields • Get all fields • Increment value key: { "field1"=>"value", "field2"=>"value" }
  8. key / hash Use case: Comments with voting redis.mapped_hmset "comment:1",

    { body: "Wow. Great post. ", upvotes: 1, downvotes: 0 } => "OK"
  9. key / hash Use case: Comments with voting redis.mapped_hmset "comment:1",

    { body: "Wow. Great post. ", upvotes: 1, downvotes: 0 } => "OK" redis.hincrby "comment:1", "upvotes", 1 => 2
  10. key / hash Use case: Comments with voting redis.mapped_hmset "comment:1",

    { body: "Wow. Great post. ", upvotes: 1, downvotes: 0 } => "OK" redis.hincrby "comment:1", "upvotes", 1 => 2 upvotes = redis.hget("comment:1", "upvotes").to_i downvotes = redis.hget("comment:1", "downvotes").to_i score = upvotes - downvotes => 1
  11. key / list What can we do? • Store a

    list (Array) • Order is maintained • Maximum length 4294967295 • Add to beginning or end, trim, get range of values
  12. key / list Use case: Social media feed Jane follows

    Tweet.where(author: current_user.following).order(created_at: :desc)
  13. key / list Example tweet = Tweet.create(author: current_user, body: "Hello,

    world") tweet.author.followers.each do |follower| redis.lpush "user:#{follower.id}:feed", "tweet:#{tweet.id}" end
  14. key / list Example: Most recent tweets # LRANGE key,

    start, stop redis.lrange "user:2:feed", 0, 9
  15. key / set What can we do? • Store a

    unique list of members • Order not guaranteed • Check difference and intersection • Check if set contains member
  16. key / set Example redis.sadd "user:2:following", ["user:5", "user:1", "user:10"] =>

    3 redis.sadd "users:2:following", "user:10" => false redis.smembers "user:2:following" => ["user:10", "user:5", "user:1"]
  17. key / set Example # Is user 2 following user

    10? redis.sismember "user:2:following", "user:10" => true # Is user 2 following user 11? redis.sismember "user:2:following", "user:11" => false
  18. key / set Example redis.sadd "user:10:following", ["user:4", "user:19", "user:1", "user:12"]

    redis.sadd "user:2:following", ["user:5", "user:1", "user:10"] # Who are both users following? redis.sinter "user:2:following", "user:10:following" => ["user:1"]
  19. key / sorted set What can we do? • Similar

    to a set • Score for each item • Sorted! • Fetch by score
  20. key / sorted set Example redis.zadd "eurovision", 500, "Australia" redis.zadd

    "eurovision", 529, "Israel" redis.zadd "eurovision", 170, "Moldova" redis.zadd "eurovision", 0, "United Kingdom"
  21. key / sorted set Example redis.zadd "eurovision", 500, "Australia" redis.zadd

    "eurovision", 529, "Israel" redis.zadd "eurovision", 170, "Moldova" redis.zadd "eurovision", 0, "United Kingdom" redis.zrevrange "eurovision", 0, -1, with_scores: true => [["Israel", 529.0], ["Australia", 500.0], ["Moldova", 170.0], ["United Kingdom", 0.0]]
  22. Combine key / string key / hash key / list

    key / set key / sorted set