WalEx

Hex.pm Documentation

Simple and reliable Postgres Change Data Capture (CDC) in Elixir.

Walex mascot

WalEx listens to the write-ahead log of a PostgreSQL database through logical replication and calls your Elixir functions when rows are inserted, updated or deleted. You declare handlers with a small DSL. Why capture changes?

Credit

This library borrows liberally from realtime from Supabase, which in turn draws heavily on cainophile. The delivery-guarantee work (exact acknowledgements, backoff, slot handling) borrows ideas from Sequin.

Installation

If available in Hex, add walex to your dependencies in mix.exs, then run mix deps.get:

def deps do
  [
    {:walex, "~> 4.9.0"}
  ]
end

Postgres Setup

Enable logical replication (needs a restart), create a publication, and set the replica identity of each table:

ALTER SYSTEM SET wal_level = 'logical';
CREATE PUBLICATION events FOR ALL TABLES;
ALTER TABLE todo REPLICA IDENTITY FULL;

Specific tables, row filters, AWS RDS and hosted Postgres are covered in Postgres Setup.

Quick Start

  1. Configure WalEx:

    # config.exs
    config :my_app, WalEx,
      hostname: "localhost",
      username: "postgres",
      password: "postgres",
      port: "5432",
      database: "postgres",
      publication: "events",
      subscriptions: ["user", "todo"],
      name: MyApp

    Pass url: "postgres://user:password@host:port/database" instead of the separate connection options if you prefer.

  2. Start it under your supervisor:

    children = [
      {WalEx.Supervisor, Application.get_env(:my_app, WalEx)}
    ]
    
    Supervisor.start_link(children, strategy: :one_for_one, name: MyApp.Supervisor)
  3. Handle events. For the subscription "user" in an app named MyApp, WalEx looks for the module MyApp.Events.User:

    defmodule MyApp.Events.User do
      use WalEx.Event, name: MyApp
    
      on_insert(:user, fn users ->
        IO.inspect(on_insert: users)
      end)
    
      on_update(:user, fn users ->
        IO.inspect(on_update: users)
      end)
    end

    Each handler receives a list of %WalEx.Event{} structs.

Main options

Option Default Meaning
name required Instance name. Also used to find event modules.
url none Database URL, instead of hostname, username, password, port and database.
publication required Publication name, or a list of names.
subscriptions required Tables to handle, or [:all_tables].
durable_slot false Keep the replication slot across disconnects. Otherwise it is temporary.
ack_after_processing false Acknowledge WAL only after handlers finish (at-least-once). Needs durable_slot: true and PostgreSQL 13+.

All options, including TLS, slot names and back-pressure, are in Configuration.

Documentation

Alternatives

If you are not an Elixir user, or don't need to subscribe to WAL events directly in Elixir, look at Sequin. It streams Postgres changes to destinations such as Kafka and S3.

Development

You need a local Postgres instance.

MIX_ENV=test mix walex.setup
MIX_ENV=test mix test

Set PGUSER, PGPASSWORD, PGHOST or PGPORT to use a customized Postgres server. They take precedence in tests and in mix walex.setup.

License

MIT. See LICENSE.