-- +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;