feat(events): add idempotent Kafka event consumer runtime

This commit is contained in:
shira committed 2026-10-06 03:59:57 +03:00
1 parent 565ef8d4f5
commit 3adb0ef9e4
47 files changed
+5018 -105

No files matched your search

@@ -0,0 +1,95 @@
-- +goose Up
-- Idempotent event consumers. The event bus delivers at least once, so a
-- consumer records every event it applied in the same transaction as the
-- effect. A repeated or concurrent delivery of the same event to the same
-- consumer finds the marker and skips the effect.
-- There is no index on processed_at yet: every applied event would pay for
-- it, and nothing reads by time until marker retention is implemented.
CREATE TABLE consumer_processed_events (
-- Stable consumer name from the code; also the consumer group suffix.
consumer_name text NOT NULL,
event_id uuid NOT NULL,
processed_at timestamptz NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT consumer_processed_events_pkey PRIMARY KEY (
consumer_name, event_id
),
CONSTRAINT consumer_processed_events_consumer_name_format CHECK (
char_length(consumer_name) <= 64
AND consumer_name ~ '^[a-z][a-z0-9_]*$'
)
);
-- Records a consumer could not apply: a value that is not a valid event
-- envelope, an event the handler rejected, or one whose retries ran out. The
-- consumer commits the offset after writing the row, so one bad record does
-- not stop its partition. The record value is not stored: a valid event is
-- still in outbox_events under its event_id, and an invalid record can be
-- read from Kafka by topic, partition, and offset while the topic retains
-- it. value_sha256 identifies that exact value without keeping a copy of
-- data that may later be personal.
CREATE TABLE consumer_dead_letters (
id bigint GENERATED ALWAYS AS IDENTITY,
consumer_name text NOT NULL,
topic text NOT NULL,
kafka_partition integer NOT NULL,
kafka_offset bigint NOT NULL,
-- From the envelope, or from the event_id header of an invalid record;
-- NULL when neither holds a UUID.
event_id uuid,
-- NULL when the record is not a valid envelope.
event_type text,
error_class text NOT NULL,
-- Short sanitized cause; never the record value.
reason text NOT NULL,
-- Handler attempts made; 0 for a record that was never handed to it.
attempts integer NOT NULL,
value_size integer NOT NULL,
value_sha256 bytea NOT NULL,
dead_lettered_at timestamptz NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT consumer_dead_letters_pkey PRIMARY KEY (id),
-- A record delivered again, after a crash before the offset commit or a
-- rewind of the group, updates its row instead of adding one. The value
-- digest is part of the key: a topic that was deleted and created again
-- reuses offsets, and a different value at a reused position is another
-- record that keeps its own row.
CONSTRAINT consumer_dead_letters_record_key UNIQUE (
consumer_name, topic, kafka_partition, kafka_offset, value_sha256
),
CONSTRAINT consumer_dead_letters_consumer_name_format CHECK (
char_length(consumer_name) <= 64
AND consumer_name ~ '^[a-z][a-z0-9_]*$'
),
CONSTRAINT consumer_dead_letters_topic_length CHECK (
char_length(topic) BETWEEN 1 AND 249
),
CONSTRAINT consumer_dead_letters_position_not_negative CHECK (
kafka_partition >= 0 AND kafka_offset >= 0
),
CONSTRAINT consumer_dead_letters_event_type_length CHECK (
char_length(event_type) <= 128
),
CONSTRAINT consumer_dead_letters_error_class_known CHECK (
error_class IN (
'invalid_record',
'handler_rejected',
'handler_panic',
'retries_exhausted'
)
),
CONSTRAINT consumer_dead_letters_reason_length CHECK (
char_length(reason) BETWEEN 1 AND 512
),
CONSTRAINT consumer_dead_letters_attempts_not_negative CHECK (
attempts >= 0
),
CONSTRAINT consumer_dead_letters_value_size_not_negative CHECK (
value_size >= 0
),
CONSTRAINT consumer_dead_letters_value_sha256_size CHECK (
octet_length(value_sha256) = 32
)
);
-- +goose Down
DROP TABLE consumer_dead_letters;
DROP TABLE consumer_processed_events;
+63
View File
@@ -0,0 +1,63 @@
-- Records that a consumer applies an event, in the transaction of the effect.
-- It returns 0 rows when the marker exists. While another transaction holds
-- an uncommitted marker for the same key, the insert waits for it: after a
-- commit it returns 0, after a rollback it inserts. So a concurrent duplicate
-- never runs the effect at the same time as the first delivery.
-- name: InsertProcessedEventMarker :execrows
INSERT INTO consumer_processed_events (consumer_name, event_id)
VALUES (sqlc.arg(consumer_name), sqlc.arg(event_id))
ON CONFLICT (consumer_name, event_id) DO NOTHING;
-- Stores a record that a consumer could not apply. A second delivery of the
-- same record (same position and value) refreshes the outcome of its row:
-- after a rewind the handler may fail differently. A different value at a
-- reused position, after the topic was recreated, gets its own row.
-- name: InsertConsumerDeadLetter :exec
INSERT INTO consumer_dead_letters (
consumer_name,
topic,
kafka_partition,
kafka_offset,
event_id,
event_type,
error_class,
reason,
attempts,
value_size,
value_sha256
) VALUES (
sqlc.arg(consumer_name),
sqlc.arg(topic),
sqlc.arg(kafka_partition),
sqlc.arg(kafka_offset),
sqlc.narg(event_id),
sqlc.narg(event_type),
sqlc.arg(error_class),
sqlc.arg(reason),
sqlc.arg(attempts),
sqlc.arg(value_size),
sqlc.arg(value_sha256)
)
ON CONFLICT (
consumer_name, topic, kafka_partition, kafka_offset, value_sha256
) DO UPDATE SET
event_id = excluded.event_id,
event_type = excluded.event_type,
error_class = excluded.error_class,
reason = excluded.reason,
attempts = excluded.attempts,
dead_lettered_at = clock_timestamp();
-- Bounds a consumer transaction on the server, local to the transaction.
-- statement_timeout stops a slow statement; idle_in_transaction_session_timeout
-- ends the session of a process that froze or lost the network inside the
-- handler, which releases the processed-event marker that a concurrent
-- duplicate waits for.
-- name: SetConsumerTransactionTimeouts :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
);