Blog

What is RES - Rails Event Store?

Jonathan Canaveral
November 21, 2023

The Rails Event Store (RES) serves as a versatile tool applicable in diverse scenarios, enhancing the architecture and maintainability of Ruby on Rails applications. Below are a few illustrative use cases demonstrating how the Rails Event Store can be employed:

Decoupled Systems and Microservices:

Within a microservices architecture, effective communication between services is crucial, necessitating a decoupled approach. The Rails Event Store (RES) becomes instrumental by enabling the publication of events when significant actions occur in one service. Subsequently, other services can subscribe to these events, facilitating responsive reactions without tight coupling.

Event Sourcing:

RES is frequently employed in tandem with event sourcing, a methodology wherein the state of an application is shaped by a sequence of events. These events are stored and can be replayed, providing a means to reconstruct the state of an entity.

CQRS (Command Query Responsibility Segregation):

In a CQRS architecture, commands that modify state are separated from queries that retrieve state. RES can be used to publish commands as events, which are then handled by command handlers.

Logging and Auditing:

Utilizing RES allows you to record crucial events within your application, creating a traceable trail of actions for auditing purposes.

Real-time Updates:

Leverage RES to broadcast real-time events to connected clients through technologies such as Action Cable or equivalents. For instance, you could inform clients about a new message being posted in a chat application.

Let’s get down into the code

Installation


# Gemfile
gem 'rails_event_store'
rails generate rails_event_store:install
rails db:migrate

Creating and publish your first event class


rails generate event_store:generate TaskCreated

This will generate a file in app/events named task_created.rb. Open it and define the event: In your controller or service where you want to publish the event, you can use the Rails Event Store to publish the TaskCreated event:


class TasksController < ApplicationController
  def create
    @task = Task.new(task_params)

    if @task.save
      event_store.publish(TaskCreated.new(data: { task_id: @task.id }))
      redirect_to tasks_path, notice: 'Task was successfully created.'
    else
      render :new
    end
  end

  private

  def task_params
    params.require(:task).permit(:name, :completed)
  end

  def event_store
    Rails.configuration.event_store
  end
end

Subscribing to your events

In your application configuration (e.g., config/application.rb), configure the Rails Event Store to use the subscriber:


# config/application.rb
config.to_prepare do
  Rails.configuration.event_store.tap do |store|
    store.subscribe(TaskCreatedSubscriber.new, to: [TaskCreated])
  end
end

Conclusion

In summary, adopting the Rails Event Store (RES) can enhance Ruby on Rails applications based on specific needs.

Event-driven systems, particularly beneficial in microservices, offer scalability by enabling asynchronous reactions and workload distribution.

Using events for auditing creates a traceable history for debugging and auditing purposes.

However, RES introduces a new paradigm, potentially requiring documentation and training for developers unfamiliar with event-driven architectures.

For simpler applications, consider the complexity RES may add, ensuring it aligns with project needs.

When integrating RES into an existing codebase, anticipate necessary refactoring for alignment with the event-driven paradigm.