Nowy standard. Twoje centrum tworzenia inteligentnego, szybkiego i zrównoważonego oprogramowania AI
Spis treści
Rails Event Store (RES) is an open-source Ruby library for publishing, storing and reading domain events in a Rails (or plain Ruby) application.
It gives you an append-only event log in your existing database, a pub-sub bus to react to those events synchronously or in background jobs, and the building blocks for audit logs, read models and event sourcing.
The current major version is RES 3.x (3.2.0 was released in September 2026).
Executive Summary
Rails Event Store lets a Rails team record every business fact as an immutable event and react to it, without adopting a separate database or framework.
You get an audit trail that doubles as a debugging timeline, plus a clean way to move side effects out of models and controllers.
The cost is a new mental model for the team and eventual consistency wherever you go async. Start with pub-sub and an audit log, and reach for full event sourcing only in the parts of the domain that need it.
What is an event store?
An event store is a proper name for a..., well, storage of events. Events are facts from the past. That makes the storage a natural fit for append-only mode: there are only two operations available, read and add. It might feel like a weird constraint, but it lets both operations be optimized to be as fast as possible. At the same time, that simplicity is a foundation for building really sophisticated solutions.
In other words, you can only read the history of events and add new ones. You can't modify existing events, delete them or change their order.
When adding new events, you can also assign them to a stream. A stream limits future reads to just part of the history; for example, a stream can contain all events related to one order.
Where does Rails Event Store come from?
Event Store is also the name of a specific database designed by Greg Young, a pioneer of CQRS and event sourcing. That database, EventStoreDB, is now called KurrentDB and developed by Kurrent. In the beginning, Rails Event Store was just a Ruby implementation of EventStore based on ActiveRecord, trying to mimic the same API and functionality. Over time, it evolved to take advantage of Ruby and support the Rails ecosystem better.
Today, Rails Event Store is really Ruby Event Store plus tooling, but the Rails prefix sells better. You can still use it outside a Rails project without any problems.
The Rails Event Store ecosystem consists of a few gems:
rails_event_store: Ruby Event Store plus Rails-specific helpers (ActiveJob integration, request metadata, the browser)ruby_event_store: the event store implementation itselfruby_event_store-active_record: the ActiveRecord repository that persists events in your SQL databaseruby_event_store-rspec: RSpec matchersruby_event_store-browser: a web UI for reading events and streamsaggregate_root: an event-sourced implementation of the Aggregate Root pattern
I won't repeat the installation docs here (spoiler alert: it's one bin/rails app:template command). Instead, I'd like to focus on what RES can do for you.
Why would a Rails team adopt an event store?
Most Rails apps start by putting side effects in callbacks and service objects: send the email, update the counter, notify the CRM. As the app grows, those side effects become tangled, hard to test and impossible to trace after the fact. An event store turns each business fact ("order placed", "payment failed") into a record you can subscribe to and replay later. For teams in regulated domains like FinTech or HealthTech, that same record doubles as an audit trail you'd otherwise have to build separately.
What can you use Rails Event Store for?
How do you set up publish-subscribe with Rails Event Store?
Publish-subscribe with Rails Event Store is trivial to set up. All you need to do is subscribe a handler to specific event types:
event_store.subscribe(Actor.new, to: [DomainEvents::Foo, DomainEvents::Bar])and implement the handler:
class Actor
# called after an event is published
def call(event)
end
endFinally, you can start firing your events:
event_store.publish(
DomainEvents::Foo.new(data: { key1: "value1", key2: "value2" })
)Sometimes you want a temporary subscription, just for a single call of an action that emits an event. In that case, use a temporary subscription:
event_store.within do
run_the_operation
end.subscribe(to: [DomainEvents::Foo]) do |event|
render_foo
end.subscribe(to: [DomainEvents::Bar]) do |event|
render_bar
end.call # don't forget the final `call`!How does Rails Event Store give you an audit log?
An audit log comes for free if you implement event sourcing or introduce changes to the system via events. For a full audit log, it's worth also recording events that don't change anything, like errors and failed authentication or authorization attempts.
In Rails, RES automatically adds the request ID and remote IP to every published event's metadata. Anything else, like the current user, takes a few lines:
def publish_event(event, metadata = {})
event_store.with_metadata({ user_id: current_user.id }.merge(metadata)) do
event_store.publish(event)
end
endWhen debugging, such a log can save your day and cut the time needed to find the cause of a problem. For more ideas on debugging with an event store, watch Thomas Pierrain's talk from DDD Europe 2018.
What are projections in Rails Event Store?
A projection derives current state from a stream of events. It's a fold (reduce/inject in Ruby) applied to events from history. Since RES 3.0, you build one with init and on, then pass the events to read to call:
balance = RubyEventStore::Projection
.init({ total: 0 })
.on(MoneyDeposited) { |state, event| { total: state[:total] + event.data[:amount] } }
.on(MoneyWithdrawn) { |state, event| { total: state[:total] - event.data[:amount] } }
balance.call(event_store.read.stream("Customer$1"))You can filter events by stream, event type or position. As a result, you can:
build a highly optimized read model or cache
generate a report for a specific point in time, e.g. the state of inventory at the end of a month
recreate the state of an entity at the time a client reported a bug
rebuild the correct state of an entity after fixing a bug in its logic
How does Rails Event Store support event sourcing?
Event sourcing is the next step after projections. Every change is applied to an entity projected from its history of events, and generates another event or events. In other words:
read a stream of events
reduce them to rebuild the current state
call the logic
save the new events to the stream
Rails Event Store implements this with the AggregateRoot gem:
class Order
include AggregateRoot
class AlreadySubmitted < StandardError; end
def initialize
@state = :new
end
def submit
raise AlreadySubmitted if @state == :submitted
apply OrderSubmitted.new(data: { delivery_date: Time.now + 24.hours })
end
on OrderSubmitted do |event|
@state = :submitted
end
end
repository = AggregateRoot::Repository.new(Rails.configuration.event_store)
repository.with_aggregate(Order.new, "Order$123") { |order| order.submit }What is a process manager, and when do you need one?
A process manager is the reverse of an aggregate: an aggregate receives a command and emits an event, while a process manager receives events and emits a command.
A process manager follows a process in the application that stretches over time or requires several actions, each with its own reaction. Classic examples are orders (cart, confirmation, payment, shipping) and bank transactions, but you can easily imagine many others: searching for a flight connection, renting a car, handling a complaint, planning a party.
How do you run Rails Event Store handlers asynchronously?
By default, synchronous handlers run right after events are saved, inside the same database transaction. That's fine as long as not much is happening. When you need better performance, move the heavy part to a background job. The default RailsEventStore::Client already schedules ActiveJob handlers after the transaction commits, so all you need is a job:
class SendOrderEmail < ApplicationJob
prepend RailsEventStore::AsyncHandler
def perform(event)
OrderMailer.notify_customer(event.data.fetch(:customer_email)).deliver_now!
end
end
event_store.subscribe(SendOrderEmail, to: [OrderPlaced])Async comes at the cost of eventual consistency, but it lets the request finish faster and respond to the client while the heavy work happens later (sometimes just milliseconds later), like sending an email from a background job instead of the controller action. If losing a job between your database and Redis isn't acceptable, RES also supports a transactional outbox.
How do you inspect events with the Rails Event Store browser?
Rails Event Store ships with a UI that helps with debugging and can also serve as a handy way of showing users a log of specific events. It lets you read an event or a stream and jump to its correlated and caused events, so you can answer "what happened?" out of the box. Mount it under authentication in production; it exposes your raw event data.
What are the trade-offs of using Rails Event Store?
An event store isn't free. Events are immutable, so changing an event's shape later means versioning or upcasting old events rather than running a quick migration. Async handlers bring eventual consistency, which your UI and your team have to plan for. The event tables only grow. And developers used to CRUD need time to think in events, which slows the first few features.
If your app is mostly forms over a database with few side effects, RES (and event sourcing especially) is likely overkill. It pays off where history matters: payments, orders, compliance-heavy workflows and processes that span many steps.
Are there alternatives to Rails Event Store?
Rails Event Store is mainly a library for storing events and leaves many decisions to the developer. If you prefer a more opinionated solution that guides you in building an event-based architecture, check out the Sequent project.
Key Takeaways
Rails Event Store is an append-only event log, pub-sub bus and event sourcing toolkit that runs on your existing SQL database.
Pub-sub and audit logs are the cheapest wins; full event sourcing belongs only where history matters.
In RES 3.x, async handlers need only an ActiveJob class with
RailsEventStore::AsyncHandler; the default client handles the rest.Immutable events make schema changes harder: plan for versioning and upcasting from day one.
For CRUD-heavy apps with few side effects, an event store adds more complexity than value.
Is Rails Event Store worth it for your project?
Rails Event Store works best when you don't adopt all of it at once. Introduce it gradually, starting with pub-sub to pull side effects out of callbacks and an audit log you can query, and keep event sourcing for the parts of the domain where history is the product. If you're weighing an event-driven redesign of an existing Rails app, our Ruby on Rails team can help you decide where it's worth it and where it isn't.
Rails Event Store FAQ



)

