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

Processes & Threads - Resque vs. Sidekiq

Processes & Threads - Resque vs. Sidekiq

From RailsConf 2015

Avatar for James Dabbs

James Dabbs

April 21, 2015
Tweet

More Decks by James Dabbs

Other Decks in Technology

Transcript

  1. Read, read, read. Read everything – trash, classics, good and

    bad, and see how they do it. — William Faulkner, on writing
  2. Resque Developed at Github, ca. 2009 » Let Redis handle

    the hard queue problems » Let Resque focus on reliability, visibility, stats and responsiveness
  3. Forking def log(str); puts "#{Process.pid}: #{str}"; end a = 1

    if pid = fork log "Waiting on child" Process.wait pid log "Done with fork. a=#{a}" else log "Child doing work" sleep 1 a += 1 log "a=#{a}" exit end
  4. Forking Produces (modulo different pids) 57388: Waiting on child 57416:

    Child doing work 57416: a=2 57388: Done with fork. a=1
  5. Resque, distilled start # Initialize, register signal handlers, &c. loop

    do if job = redis_job_queue.pop child = fork do job.process end Process.wait child else sleep polling_frequency end end shutdown # Unregister signal handlers, &c.
  6. Forking » mitigates the risk of leaking memory » avoids

    the cost of booting Rails for each job » allows using signals to e.g. shut down hung jobs
  7. Sidekiq By @mperham, ca. 2012 » "What if one process

    could do the work of 20 Resque processes?" » Performance, simplicity, support » Batteries included (failure retry, scheduled jobs, middleware)
  8. Race Conditions @wallet = 100 Thread.new do tmp = @wallet

    sleep rand(0..5) @wallet = tmp + 10 end Thread.new do sleep rand(0..5) tmp = @wallet @wallet = tmp - 10 end
  9. class Wallet include Celluloid attr_reader :amount def initialize(amt); @amount =

    amt; end def adjust(Δ) ; @amount += Δ ; end end @wallet = Wallet.new 100 [10, -10].each { |Δ| wallet.async.adjust(Δ) } @wallet.amount # => 100
  10. Takeaways If you're: » memory-constrained - use Sidekiq » not

    thread-safe - use Resque » both - ¯\_()_/¯
  11. Takeaways Considerations: » How important is job isolation? » What's

    your bottleneck? RAM? I/O? » What about the GIL? » How important is support?
  12. Takeaways Don't be afraid to dive in to code »

    pry - ls, @, wtf?!?, &c. » pry-stack_explorer, pry-byebug, pry-rescue, &c. » bundle show, bundle open » ack --rb
  13. Further Reading » These slides - jamesdabbs/railsconf-2015 » T. Ball

    - Unicorn Unix Magic Tricks » J. Storimer - Understanding the GIL » J. Lane - Writing thread-safe Rails