Resque Bus Presentation

View the Presentation

Or view the slide sheet below

Resque Bus

Brian Leonard

TaskRabbit
02/17/2014

Redis

  • Key/Value Store
  • Atomic Operations

Redis Logo

Resque

Resque UI

Resque Pattern

1
2
3
4
5
6
7
class Rating < ActiveRecord::Base
  after_commit :enqueue_worker

  def enqueue_worker
    Resque.enqueue(CalculateAverageWorker, self.id)
  end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CalculateAverageWorker
  @queue = :default

  def self.perform(rating_id)
    rating = Rating.find(rating_id)
    total  = Rating.where(rabbit_id: rating.rabbit_id).count
    sum    = Rating.where(rabbit_id: rating.rabbit_id').sum(:value)

    profile = RabbitProfile.find_by(user_id: rating.rabbit_id)
    profile.ratings_total  = total
    profile.rating_average = sum.to_f / (5*total.to_f)
    profile.save!
  end
end

Resque 1

Resque 2

Resque 3

Resque 4

New Profile App

Bus 1

Resque Bus Pattern

1
2
3
4
5
6
7
class Rating < ActiveRecord::Base
  after_commit :enqueue_worker

  def enqueue_worker
    ResqueBus.publish("rating_created", self.attributes)
  end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
# initializer
ResqueBus.dispatch("profile") do
  subscribe "rating_created" do |attributes|
    rating = Rating.find(rating_id)
    total  = Rating.where(rabbit_id: rating.rabbit_id).count
    sum    = Rating.where(rabbit_id: rating.rabbit_id').sum(:value)

    profile = RabbitProfile.find_by(user_id: rating.rabbit_id)
    profile.ratings_total  = total
    profile.rating_average = sum.to_f / (5*total.to_f)
    profile.save!
  end
end

Bus 2

Bus 3

Bus 4

Bus 5

Bus 6

Bus 7

Bus 8

Bus 9

It’s Just Resque

Resque UI

Use Cases

Publishing all all data changes, especially state changes.

  • Rails app communication
  • Bus Apps
  • Loggers

Rails app communication

  • Own codebase and storage mechanisms
  • +1: Local Simplicity
  • +1: Local Specialization
  • -1: Global Complexity

Bus Apps

  • No UI, only listens to bus
  • +1: Local Simplicity
  • +1: Local Specialization
  • +1: Memory Profile

Examples

  • External Analytics
  • Communications
  • Fraud

Switchboard

  • Centralized sending email, text messages, push notifications, etc.
  • No shared knowledge or credentials
  • Asynchronous API for all apps to use and just focus on the content.

Loggers

  • Subscribes to everything that’s published
  • Writes to Elastic Search

Enabling…

  • Tracing events through system
  • Simple centralized logging
  • Realtime business dashboards and metrics

Best Practices

  • Always Be Publishing
  • Make many small bus apps
  • Start small

Thanks!

Questions?

Comments

Copyright © 2017 Brian Leonard