feat(outbox): relay committed outbox events to Kafka

This commit is contained in:
shira committed 2026-10-06 03:01:53 +03:00
1 parent 78f1651f6b
commit 6c3b34f73e
42 files changed
+3731 -45

No files matched your search

@@ -0,0 +1,13 @@
-- +goose Up
-- Supports the relay check that an outbox row is the earliest unpublished
-- event of its aggregate. The relay publishes only that row for each
-- aggregate per pass, so the check runs once for every candidate row. Without
-- this index the lookup reads every published event of the aggregate through
-- the unique key. The index covers only unpublished rows and shrinks as
-- events are published.
CREATE INDEX outbox_events_unpublished_aggregate_idx
ON outbox_events (aggregate_type, aggregate_id, aggregate_version, position)
WHERE published_at IS NULL;
-- +goose Down
DROP INDEX outbox_events_unpublished_aggregate_idx;
+79
View File
@@ -16,3 +16,82 @@ INSERT INTO outbox_events (
sqlc.arg(aggregate_version),
sqlc.arg(payload)
);
-- Locks the next relay batch. For each aggregate only its earliest unpublished
-- event (lowest aggregate_version, then position) qualifies, so a relay never
-- holds a later version while an earlier one is still pending, whether that
-- earlier row is locked by another relay or was just rolled back. Rows locked
-- by another relay are skipped without waiting. The statement snapshot can
-- only see a later version after the earlier version's transaction committed,
-- because aggregate writers check the expected version, so scanning without a
-- position watermark cannot overtake an earlier version.
-- name: LockPendingOutboxEvents :many
SELECT
pending.event_id,
pending.event_type,
pending.schema_version,
pending.aggregate_type,
pending.aggregate_id,
pending.aggregate_version,
pending.payload,
pending.occurred_at
FROM outbox_events AS pending
WHERE
pending.published_at IS NULL
AND NOT EXISTS (
SELECT 1
FROM outbox_events AS earlier
WHERE
earlier.aggregate_type = pending.aggregate_type
AND earlier.aggregate_id = pending.aggregate_id
AND earlier.published_at IS NULL
AND (earlier.aggregate_version, earlier.position)
< (pending.aggregate_version, pending.position)
)
ORDER BY pending.position
LIMIT sqlc.arg(batch_limit)
FOR UPDATE OF pending SKIP LOCKED;
-- Marks the events that the broker acknowledged. The relay passes only IDs
-- whose rows its own transaction locked, so the statement never waits and the
-- count equals the number of distinct IDs.
-- name: MarkOutboxEventsPublished :execrows
UPDATE outbox_events
SET published_at = clock_timestamp()
WHERE
event_id = ANY(sqlc.arg(event_ids)::uuid [])
AND published_at IS NULL;
-- Reads the relay backlog for metrics without locking. Both subqueries use
-- the partial index on unpublished positions, so the cost follows the backlog
-- size, not the table size. The age comes from the database clock, the same
-- clock that set occurred_at.
-- name: GetOutboxBacklog :one
SELECT
(
SELECT count(*)
FROM outbox_events AS pending
WHERE pending.published_at IS NULL
)::bigint AS pending_events,
coalesce((
SELECT extract(EPOCH FROM clock_timestamp() - oldest.occurred_at)
FROM outbox_events AS oldest
WHERE oldest.published_at IS NULL
ORDER BY oldest.position
LIMIT 1
), 0)::float8 AS oldest_pending_age_seconds;
-- Bounds the claim transaction on the server. statement_timeout stops a
-- statement that runs too long; idle_in_transaction_session_timeout ends the
-- session, and with it the transaction and its row locks, when the client
-- stays silent longer than a publish may take, for example after the process
-- froze or lost the network without closing the connection. Both settings
-- are local to the transaction.
-- name: SetRelayTransactionTimeouts :exec
SELECT
set_config('statement_timeout', sqlc.arg(statement_timeout_ms)::text, true),
set_config(
'idle_in_transaction_session_timeout',
sqlc.arg(idle_timeout_ms)::text,
true
);