Skip to content

redis rb on Phusion Passenger

didip edited this page Sep 12, 2011 · 12 revisions

Problem:

Because of the fork() nature of Passenger, you might accidentally share the exact-same Redis connection between Passenger workers. That's not good. User X on worker A might accidentally do unexpected things on User Y's stuff on worker B. For more details, visit 12.3.1. Example 1: Memcached connection sharing (harmful)

Solution:

You need to re-establish Redis connection when a new Passenger worker is created. See code example below: if defined?(PhusionPassenger) PhusionPassenger.on_event(:starting_worker_process) do |forked| # We're in smart spawning mode. if forked # Re-establish redis connection require 'redis' redis_config = YAML.load_file("#{Rails.root.to_s}/config/redis.yml")[Rails.env]

      # The important two lines
      $redis.client.disconnect
      $redis = Redis.new(:host => redis_config["host"], :port => redis_config["port"])
    end
  end
end
Clone this wiki locally