Outbox Core
The core logic and trait definitions for Oxide Outbox, a high-performance implementation of the Transactional Outbox pattern for Rust.
outbox-core provides the architectural backbone for reliable message delivery, allowing you to decouple database transactions from asynchronous event publishing with full type safety.
Key Features
- Trait-First Architecture: Completely decoupled from specific storage or message brokers. Switch between Postgres, MySQL, Kafka, or RabbitMQ by implementing core traits.
- Type-Safe Generic Payloads: No more
serde_json::Valueoverhead. Define your events using your own domain types:Event<OrderCreated>,Event<UserSignedUp>, etc. - Async Native: Built from the ground up on
tokiofor maximum concurrency. - Flexible Idempotency: Built-in support for multiple deduplication strategies (UUID v7, Provided tokens, or Custom logic).
- Extensible Storage & Transport: Interfaces designed to be implemented by specialized crates (like
outbox-postgres).
Core Concepts
The OutboxManager<S, P, PT>
The central engine that orchestrates event discovery and dispatching. It is generic over your Payload Type (PT), ensuring that every event handled by the manager respects your domain's type constraints.
S(Storage): ImplementsOutboxStorage<PT>. Responsible for DB operations (e.g., PostgreSQL, MySQL).P(Publisher): ImplementsTransport<PT>. Responsible for sending events to brokers (e.g., Kafka, NATS).PT(Payload Type): Your domain event type. Must implementDebug + Clone + Serialize.
Type-Safe Events
As of v0.3.0, events use a Payload<PT> wrapper. This ensures zero unnecessary JSON roundtrips and provides compile-time safety from the moment you record an event until it is sent to the transport layer.
Quick Start
Here is a complete, working example using outbox-core alongside outbox-postgres and a custom Tokio-channel based transport.
1. Define your Domain Event & Transport
First, define your event payload and implement the Transport trait to tell the outbox how to publish messages.
use *;
use ;
// 1. Define your strongly-typed event
// 2. Implement Transport for your chosen broker (e.g., Tokio MPSC, Kafka, etc.)
;
;
2. Wire up the Manager and Service
Set up your database pool, configure the outbox, and spawn the background manager task.
use ;
use PgPool;
use Arc;
use Duration;
use watch;
use ;
async
Core Traits
To extend outbox-core, you can implement these primary traits:
| Trait | Responsibility |
|---|---|
| OutboxStorage | Handles saving, locking, and deleting events in your DB (Postgres, Mongo, etc). |
| Transport | Defines how the event is published to the outside world (Kafka, RabbitMQ, HTTP). |
| IdempotencyProvider | Checks if a request has already been processed to prevent duplicates. |