Ruby gem for easily pipelining REDIS commands
Latest commit ecc17e1 Jan 9, 2013 @mrdanadams Update

Ruby gem for easily pipelining REDIS commands

(Inspired by this blog post on 5-10x Speed Ups by Pipeling Multiple REDIS Commands in Ruby by Dan Adams.)

Pipelining in REDIS is a great way to stay performant when executing multiple commands. It should also be easy to use.


Basic usage involves:

  1. Enqueueing a number of REDIS commands inside a pipelined block
  2. Doing something with the results either afterwards or inside blocks specific to each command.

Ex: (a bit contrived...)

# Put a bunch of values in a few different hashes
redis = Redis.connect
redis.hset "h1", "foo", 1
redis.hset "h2", "bar", 2
redis.hest "h3", "baz", 3

# Get the values pipelined and sum them up
values = RedisPipeliner.pipeline redis do |p|
  # This would normally be 3 round-trips
  p << redis.hget("h1", "foo")
  p << redis.hget("h2", "bar")
  p << redis.hget("h3", "baz")
end == 6

You can also pass in a block to be called for each value rather than operating on the values afterwards:

results = []
RedisPipeliner.pipeline redis do |p|
  [%w(h1 foo), %w(h2 bar), %w(h3 baz)].each do |pair|
    p << redis.hget(pair[0], pair[1]) do |value|
      # referencing pair inside the block
      results << pair[1] + value
results.first.should == "foo1"

See the specs for executable examples. alpha