TaskRabbit is Hiring!

We’re a tight-knit team that’s passionate about building a solution that helps people by maximizing their time, talent and skills. We are actively hiring for our Engineering and Design teams. Click To Learn more

Brian Leonard

Queue Bus

@ 02 Apr 2015

architecture resque sidekiq queue


At TaskRabbit, we have been using resque-bus for about two years. It has continued to provide value by linking components via a loosely coupled publish/subscription model. We have seen 10x the number of events going through it, but have not yet hit any scaling issues. The benefit of using the tools we already have continues to be a huge win.

But other teams are using other tools like Sidekiq. We’re also interested in trying it out, but resque-bus (unsurprisingly) was tied closely to Resque. We’ve changed that by creating queue-bus and using an adapter pattern. There are now adapters in resque-bus and sidekiq-bus, as well as a compatible version in node-queue-bus.

The code interactions are basically the same but can work across systems. You can even have one app using Resque and one using Sidekiq.

Pick your adapter

By requiring one of the adapters, it automatically gets set up to be the one for the app.

require 'sidekiq-bus' # (or 'resque-bus')

Application A publishes an event

Something happens in your application and you want to let the world know. In this case, you publish an event.

# business logic
QueueBus.publish("user_created", id: 42, first_name: "John", last_name: "Smith")

# or do it later
QueueBus.publish_at(1.hour.from_now, "user_created", id: 42, first_name: "John", last_name: "Smith")

Application B subscribes to events

If the same or different application is interested when an event happens, it subscribes to it by name.

# initializer
QueueBus.dispatch("app_b") do
  subscribe "user_created" do |attributes|
    # business logic
    NameCount.find_or_create_by_name(attributes["last_name"]).increment!
  end
end

Upgrading

The formats changed a little bit with the move. The last version that used the old format (0.3.7) also can adapt to the new format. This is important because you’ll have things in the queue during the transition.

Steps:

  • Upgrade everyone to 0.3.7
  • Deploy all the things
  • Upgrade everyone to the newest version

More to come

If people continue to like this approach and gem, we have lots of approaches and tools built on top of it that we’d be excited to make available. Let us know on Github that you like it by watching, starring, or creating issues with questions, etc.

Now that we have the adapter pattern, also let us know if you are interested in making one for your background system of choice. A special thanks goes out to Jonathan Greenberg and the team at purpose.com who did just that to get this whole thing going.

Comments

Coments Loading...