spakky-outbox¶
spakky-outbox는 Integration Event를 비즈니스 데이터와 같은 transaction에 원자적으로 기록하고 별도 Relay로 전송하기 위한 계약을 제공합니다. 브로커가 수락 가능한 레코드는 확인될 때까지 재전송되지만, 영구적인 레코드 귀속 거부는 retry 소진 후 성공 전달 없이 abandoned 처리될 수 있습니다.
Outbox 패턴 — 원자적 기록과 Relay 상태 계약
플러그인 진입점¶
Plugin initialization entry point.
initialize(app)
¶
Initialize the Outbox plugin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
SpakkyApplication
|
The Spakky application instance. |
required |
Source code in core/spakky-outbox/src/spakky/outbox/main.py
EventBus¶
Outbox Event Bus — sync and async implementations replacing IEventBus/IAsyncEventBus via @Primary.
OutboxEventBus(storage, propagator, synchronization, wakeup, auth_snapshot_headers=None)
¶
Bases: IEventBus
Intercepts integration events and stores them in the Outbox table (sync).
Replaces the default DirectEventBus so that events are persisted atomically within the same database transaction as the business data.
Source code in core/spakky-outbox/src/spakky/outbox/bus/outbox_event_bus.py
__wake_relay_after_commit()
¶
Have the relays send this message as soon as its transaction commits.
Outside a @Transactional call there is no commit to wait for; the
message is then sent by the relay's next poll.
Source code in core/spakky-outbox/src/spakky/outbox/bus/outbox_event_bus.py
AsyncOutboxEventBus(storage, propagator, synchronization, wakeup, auth_snapshot_headers=None)
¶
Bases: IAsyncEventBus
Intercepts integration events and stores them in the Outbox table (async).
Replaces the default AsyncDirectEventBus so that events are persisted atomically within the same database transaction as the business data.
Source code in core/spakky-outbox/src/spakky/outbox/bus/outbox_event_bus.py
__wake_relay_after_commit()
¶
Have the relays send this message as soon as its transaction commits.
Outside a @Transactional call there is no commit to wait for; the
message is then sent by the relay's next poll.
Source code in core/spakky-outbox/src/spakky/outbox/bus/outbox_event_bus.py
포트¶
Outbox storage port.
IOutboxStorage
¶
Bases: ABC
Synchronous outbox message storage abstraction.
save(message)
abstractmethod
¶
fetch_pending(limit, max_retry)
abstractmethod
¶
Claim unpublished messages for this relay instance (with lock).
A partition key must be claimed whole: an implementation may only hand out messages of a key when it also claims that key's oldest message that is neither published nor abandoned. Otherwise two relay instances publish one key in parallel and lose the ordering the key exists to provide. Messages without a partition key carry no such constraint.
Source code in core/spakky-outbox/src/spakky/outbox/ports/storage.py
mark_published(message_id)
abstractmethod
¶
increment_retry(message_id)
abstractmethod
¶
mark_abandoned(message_id)
abstractmethod
¶
Record that the relay gave up on a message after exhausting retries.
The message leaves the pending queue without being published, so a partition key never waits forever on a message that will not be retried again. An implementation must keep the record and the reason readable — the operator has to be able to find what was dropped.
Source code in core/spakky-outbox/src/spakky/outbox/ports/storage.py
release_claim(message)
abstractmethod
¶
Give up this relay's claim on a message it did not publish.
Called when sending the message failed, so the next read can hand it
out again instead of waiting for claim_timeout_seconds to expire.
Only the claim is cleared: the retry count, a published message and an
abandoned message stay as they are.
The claim is released only while it is still the one fetch_pending
handed out, identified by message.claimed_at. A relay that stalled
past claim_timeout_seconds may find the message claimed again by
another instance; clearing that newer claim would let a third read
send it in parallel and break the partition key ordering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
OutboxMessage
|
The message as returned by |
required |
Source code in core/spakky-outbox/src/spakky/outbox/ports/storage.py
purge_published(older_than)
abstractmethod
¶
Delete messages published before older_than and return how many.
Only published messages are removed. A message still awaiting delivery and a message the relay abandoned both stay, whatever their age: the first is still owed to the broker and the second is the operator's record of what was dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
older_than
|
datetime
|
Messages whose |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of deleted messages. |
Source code in core/spakky-outbox/src/spakky/outbox/ports/storage.py
IAsyncOutboxStorage
¶
Bases: ABC
Asynchronous outbox message storage abstraction.
save(message)
abstractmethod
async
¶
fetch_pending(limit, max_retry)
abstractmethod
async
¶
Claim unpublished messages for this relay instance (with lock).
A partition key must be claimed whole: an implementation may only hand out messages of a key when it also claims that key's oldest message that is neither published nor abandoned. Otherwise two relay instances publish one key in parallel and lose the ordering the key exists to provide. Messages without a partition key carry no such constraint.
Source code in core/spakky-outbox/src/spakky/outbox/ports/storage.py
mark_published(message_id)
abstractmethod
async
¶
increment_retry(message_id)
abstractmethod
async
¶
mark_abandoned(message_id)
abstractmethod
async
¶
Record that the relay gave up on a message after exhausting retries.
The message leaves the pending queue without being published, so a partition key never waits forever on a message that will not be retried again. An implementation must keep the record and the reason readable — the operator has to be able to find what was dropped.
Source code in core/spakky-outbox/src/spakky/outbox/ports/storage.py
release_claim(message)
abstractmethod
async
¶
Give up this relay's claim on a message it did not publish.
Called when sending the message failed, so the next read can hand it
out again instead of waiting for claim_timeout_seconds to expire.
Only the claim is cleared: the retry count, a published message and an
abandoned message stay as they are.
The claim is released only while it is still the one fetch_pending
handed out, identified by message.claimed_at. A relay that stalled
past claim_timeout_seconds may find the message claimed again by
another instance; clearing that newer claim would let a third read
send it in parallel and break the partition key ordering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
OutboxMessage
|
The message as returned by |
required |
Source code in core/spakky-outbox/src/spakky/outbox/ports/storage.py
purge_published(older_than)
abstractmethod
async
¶
Delete messages published before older_than and return how many.
Only published messages are removed. A message still awaiting delivery and a message the relay abandoned both stay, whatever their age: the first is still owed to the broker and the second is the operator's record of what was dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
older_than
|
datetime
|
Messages whose |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of deleted messages. |
Source code in core/spakky-outbox/src/spakky/outbox/ports/storage.py
Relay¶
Outbox Relay Background Services (sync and async).
OutboxRelayBackgroundService(storage, transport, config, wakeup)
¶
Bases: AbstractBackgroundService
Relays pending Outbox messages to the transport (sync).
The relay reads the outbox as soon as a transaction that stored messages
commits, and every polling_interval_seconds to recover what an earlier
read could not send.
Initialize with storage, transport, config, and wakeup dependencies.
Source code in core/spakky-outbox/src/spakky/outbox/relay/relay.py
initialize()
¶
dispose()
¶
stop()
¶
Stop the relay without waiting for the current polling interval.
wake()
¶
run()
¶
Relay pending messages on every wake-up and poll until stopped.
A failing read is logged and the loop goes on, so one database error does not stop delivery until the service restarts.
Source code in core/spakky-outbox/src/spakky/outbox/relay/relay.py
__register_refusal(message, halted_partition_keys, settled_ids)
¶
Spend one retry on a refused message, or abandon it when none is left.
A message whose budget is spent will never be fetched again, so it is abandoned rather than left behind: that releases its partition key, which would otherwise wait forever on a message nobody will retry, and keeps the record findable. A message that still has budget holds its key back until it is delivered, so nothing of that key overtakes it.
Source code in core/spakky-outbox/src/spakky/outbox/relay/relay.py
__publish_one_at_a_time(messages, halted_partition_keys, settled_ids)
¶
Replay a batch whose flush failed, confirming one message at a time.
A batch flush reports that the broker refused something but not what, so neither the retry budget nor the partition key hold-back can be aimed without replaying. Confirming one message per flush puts the broker's verdict on the message that earned it: the refused message spends its budget and holds back the rest of its key, while its neighbours publish. Only a refusal counts — a transport that cannot reach the broker at all stops the replay untouched, because an outage is no message's fault. The replay can re-deliver a record the failed flush had already accepted — delivery stays at-least-once, which consumers already assume.
Source code in core/spakky-outbox/src/spakky/outbox/relay/relay.py
__release_unsettled(messages, settled_ids)
¶
Release the claims of the messages this read did not settle.
A message that was neither published nor abandoned — refused with budget left, held back behind its partition key, or caught in a transport failure — goes back to the queue right away, so the next read sends it again instead of waiting for the claim to time out.
Source code in core/spakky-outbox/src/spakky/outbox/relay/relay.py
AsyncOutboxRelayBackgroundService(storage, transport, config, wakeup)
¶
Bases: AbstractAsyncBackgroundService
Relays pending Outbox messages to the transport (async).
The relay reads the outbox as soon as a transaction that stored messages
commits, and every polling_interval_seconds to recover what an earlier
read could not send.
Initialize with async storage, transport, config, and wakeup dependencies.
Source code in core/spakky-outbox/src/spakky/outbox/relay/relay.py
initialize_async()
async
¶
Start listening for commits that stored outbox messages.
Source code in core/spakky-outbox/src/spakky/outbox/relay/relay.py
dispose_async()
async
¶
stop_async()
async
¶
Stop the relay without waiting for the current polling interval.
wake()
¶
Read the outbox now instead of at the next poll.
Safe to call from any thread: a commit in synchronous code running on a worker thread wakes the relay on its own event loop. Before the relay starts there is no loop to wake, and its first read picks the message up.
Source code in core/spakky-outbox/src/spakky/outbox/relay/relay.py
run_async()
async
¶
Relay pending messages on every wake-up and poll until stopped.
A failing read is logged and the loop goes on, so one database error does not stop delivery until the service restarts.
Source code in core/spakky-outbox/src/spakky/outbox/relay/relay.py
__register_refusal(message, halted_partition_keys, settled_ids)
async
¶
Spend one retry on a refused message, or abandon it when none is left.
A message whose budget is spent will never be fetched again, so it is abandoned rather than left behind: that releases its partition key, which would otherwise wait forever on a message nobody will retry, and keeps the record findable. A message that still has budget holds its key back until it is delivered, so nothing of that key overtakes it.
Source code in core/spakky-outbox/src/spakky/outbox/relay/relay.py
__publish_one_at_a_time(messages, halted_partition_keys, settled_ids)
async
¶
Replay a batch whose flush failed, confirming one message at a time.
A batch flush reports that the broker refused something but not what, so neither the retry budget nor the partition key hold-back can be aimed without replaying. Confirming one message per flush puts the broker's verdict on the message that earned it: the refused message spends its budget and holds back the rest of its key, while its neighbours publish. Only a refusal counts — a transport that cannot reach the broker at all stops the replay untouched, because an outage is no message's fault. The replay can re-deliver a record the failed flush had already accepted — delivery stays at-least-once, which consumers already assume.
Source code in core/spakky-outbox/src/spakky/outbox/relay/relay.py
__release_unsettled(messages, settled_ids)
async
¶
Release the claims of the messages this read did not settle.
A message that was neither published nor abandoned — refused with budget left, held back behind its partition key, or caught in a transport failure — goes back to the queue right away, so the next read sends it again instead of waiting for the claim to time out.
Source code in core/spakky-outbox/src/spakky/outbox/relay/relay.py
공통¶
Outbox configuration.
OutboxConfig()
¶
Bases: BaseSettings
Outbox plugin configuration loaded from environment variables.
Load outbox configuration from environment variables.
Source code in core/spakky-outbox/src/spakky/outbox/common/config.py
Outbox message model.
OutboxMessage(id, event_name, payload, headers, created_at, published_at=None, retry_count=0, claimed_at=None, partition_key=None, abandoned_at=None)
dataclass
¶
Persistence-agnostic Outbox message model.
에러¶
Outbox error classes.