Suggested Redis driver for use within Goliath?

1.3k views Asked by At

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?

2

There are 2 answers

1
Schmurfy On BEST ANSWER

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.

0
jfrprr On

We use em-hiredis very successfully inside Goliath. Here's a sample of how we coded publishing:

config/example_api.rb

# These give us direct access to the redis connection from within the API
config['redisUri'] = 'redis://localhost:6379/0'
config['redisPub'] ||= EM::Hiredis.connect('')

example_api.rb

class ExampleApi < Goliath::API

  use Goliath::Rack::Params             # parse & merge query and body parameters
  use Goliath::Rack::Formatters::JSON   # JSON output formatter
  use Goliath::Rack::Render             # auto-negotiate response format

  def response(env)
    env.logger.debug "\n\n\nENV: #{env['PATH_INFO']}"
    env.logger.debug "REQUEST: Received"
    env.logger.debug "POST Action received: #{env.params} "

    #processing of requests from browser goes here

    resp =
      case env.params["action"]
      when 'SOME_ACTION'        then process_action(env)
      when 'ANOTHER_ACTION'     then process_another_action(env)
      else
        # skip
      end

    env.logger.debug "REQUEST: About to respond with: #{resp}"

    [200, {'Content-Type' => 'application/json', 'Access-Control-Allow-Origin' => "*"}, resp]
  end

  # process an action
  def process_action(env)
    # extract message data
    data = Hash.new
    data["user_id"], data["object_id"] = env.params['user_id'], env.params['object_id']

        publishData = { "action"   => 'SOME_ACTION_RECEIVED',
                        "data" => data }

        redisPub.publish("Channel_1", Yajl::Encoder.encode(publishData))

      end 
    end 
    return data
  end

  # process anothr action
  def process_another_action(env)
    # extract message data
    data = Hash.new
    data["user_id"], data["widget_id"] = env.params['user_id'], env.params['widget_id']

        publishData = { "action"   => 'SOME_OTHER_ACTION_RECEIVED',
                        "data" => data }
        redisPub.publish("Channel_1", Yajl::Encoder.encode(publishData))

      end 
    end 
    return data
  end
end

Handling subscriptions are left as an exercise for the reader.