Obix
Implementation of the outbox pattern backed by PostgreSQL and sqlx.
Features
- Transactional outbox pattern for reliable event publishing
- Persistent events stored in PostgreSQL with sequential ordering
- Ephemeral events for transient state updates
- Real-time event delivery via PostgreSQL NOTIFY/LISTEN
- Event caching for efficient replay and new listener catchup
- Automatic backfill from database for events not in cache
- Large payload handling with automatic database fallback
Usage
Add this to your Cargo.toml:
[]
= "0.1"
Basic Example
use ;
use ;
use StreamExt;
// Define your event types
async
Setup
The outbox pattern requires two PostgreSQL tables (persistent_outbox_events and ephemeral_outbox_events). You must apply the migration to create these tables before using the library.
Option 1: Copy the migration file
Copy the migration file into your project's migrations directory:
Then run your migrations as usual with sqlx:
migrate!.run.await?;
Option 2: Use a custom table prefix
If you need to avoid table name conflicts or want to namespace your outbox tables, you can define custom tables with a prefix:
;
// Initialize with custom tables
let outbox = init.await?;
When using a custom prefix, you'll need to create a modified migration with your prefix. For example, with prefix myapp, the tables would be named myapp_persistent_outbox_events and myapp_ephemeral_outbox_events.
You can copy the default migration and add your prefix to all table names, sequence names, and channel names in the SQL.
Event Types
Persistent Events: Stored in the database with sequential ordering, guaranteed delivery, and replay capability. Use for critical business events that must be processed reliably.
Ephemeral Events: Persisted to the database to enable replication across multiple runtime instances, but only the latest event per event type is kept. Later events of the same type replace earlier ones via database UPSERT. Use for current state updates like online status, real-time metrics, or any state that only needs the most recent value.
Listening to Events
// Listen to persistent events from the beginning
let mut listener = outbox.listen_persisted;
// Listen to persistent events from a specific sequence
let mut listener = outbox.listen_persisted;
// Listen to new persistent events only
let mut listener = outbox.listen_persisted;
// Listen to ephemeral events
let mut listener = outbox.listen_ephemeral;
// Listen to all events (persistent + ephemeral)
let mut listener = outbox.listen_all;
License
Licensed under the Apache License, Version 2.0.