💧 Elixir Advanced

What is the Reactor library and event-driven architectures in Elixir?

Answer

Reactor is a dynamic, concurrent workflow engine for Elixir, enabling saga patterns and complex business workflows with automatic compensation. Define a workflow: defmodule CheckoutFlow do; use Reactor; step :charge_card, ChargeCardStep; step :create_order, CreateOrderStep, max_retries: 3; step :send_email, SendEmailStep; end. Steps can be run in parallel, with dependencies, retries, and compensation (undo actions on failure). This goes beyond Broadway's linear pipelines to arbitrary DAG (Directed Acyclic Graph) workflows. Event-driven architecture patterns in Elixir: Phoenix PubSub: in-process or cluster-wide pub-sub (built into Phoenix). EventBus: publish-subscribe with filtering and transformation. CQRS with Commanded: event sourcing framework — all state changes as immutable events, projections rebuild read models from event stream. Commanded + EventStore: full event sourcing stack. Elixir's process model makes event-driven architectures particularly natural — each aggregate root can be a process maintaining its own state.