Skip to content

fast-programmer/outboxer

Repository files navigation

Outboxer

Gem Version Ruby

Background

Outboxer is an ActiveRecord implementation of the transactional outbox pattern.

It was built to help Rails teams migrate to eventually consistent event driven architecture quickly, using existing tools and infrastructure.

Installation

1. add the gem to your application's gemfile

gem 'outboxer'

2. install the gem

bundle install

Usage

1. generate the schema

bin/rails g outboxer:schema

2. migrate the schema

bin/rake db:migrate

3. after an event model is created in your application, backlog an outboxer message

class Event < ActiveRecord::Base
  # ...

  after_create do |event|
    Outboxer::Message.backlog(messageable: event)
  end
end

4. generate the message publisher

bin/rails g outboxer:message_publisher

5. update the publish block to perform an event created job async

Outboxer::Publisher.publish do |message|
  case message[:messageable_type]
  when 'Event'
    EventCreatedJob.perform_async({ 'id' => message[:messageable_id] })
  end
end

6. add the event created job

class EventCreatedJob
  include Sidekiq::Job

  def perform(args)
    event = Event.find(args['id'])

    # ...
  end
end

7. run the message publisher

bin/outboxer_message_publisher

8. manage the messages

manage backlogged, queued, publishing and failed messages with the web ui

Screenshot 2024-05-20 at 8 47 57 pm

rails

config/routes.rb
require 'outboxer/web'

Rails.application.routes.draw do
  mount Outboxer::Web, at: '/outboxer'
end

rack

config.ru
require 'outboxer/web'

map '/outboxer' do
  run Outboxer::Web
end

9. monitor the message publisher

understanding how much memory and cpu is required by the message publisher

Screenshot 2024-05-20 at 10 41 57 pm
run bin/outboxer_message_publishermon

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/fast-programmer/outboxer.

License

This gem is available as open source under the terms of the GNU Lesser General Public License v3.0.