Search code examples
rubyrediseventmachinegoliath

Suggested Redis driver for use within Goliath?


There seem to be several options for establishing Redis connections for use within EventMachine, and I'm having a hard time understanding the core differences between them.

My goal is to implement Redis within Goliath

The way I establish my connection now is through em-synchrony:

require 'em-synchrony'
require 'em-synchrony/em-redis'

config['redis'] = EventMachine::Synchrony::ConnectionPool.new(:size => 20) do 
  EventMachine::Protocols::Redis.connect(:host => 'localhost', :port => 6379)
end 

What is the difference between the above, and using something like em-hiredis?

If I'm using Redis for sets and basic key:value storage, is em-redis the best solution for my scenario?


Solution

  • what em-synchrony does is patch the em-redis gem to allow using it with fibers which effectively allows it to run in goliath.

    Here is a project using Goliath + Redis which can guide you on how to make all this works: https://github.com/igrigorik/mneme

    Example with em-hiredis, what goliath do is wrap your request in a fiber so a way to test it is:

    require 'rubygems'
    require 'bundler/setup'
    
    require 'em-hiredis'
    require 'em-synchrony'
    
    EM::run do
      Fiber.new do
        ## this is what you can use in goliath
        redis = EM::Hiredis.connect
        p EM::Synchrony.sync redis.keys('*')
        ## end of goliath block
      end.resume
    
    end
    

    and the Gemfile I used:

    source :rubygems
    
    gem 'em-hiredis'
    gem 'em-synchrony'
    

    If you run this example you will get the list of defined keys in your redis database printed on screen. Without the EM::Synchrony.sync call you would get a deferrable but here the fiber is suspended until the calls return and you get the result.