feat(outbox): relay committed outbox events to Kafka
This commit is contained in:
1 parent
78f1651f6b
commit
6c3b34f73e
42 files changed
+3731
-45
No files matched your search
+32
-3
@@ -51,21 +51,50 @@ The named `kafka_data` volume mounts at `/var/lib/kafka/data` and stores topic l
|
||||
|
||||
Changing the value after the volume is formatted stops Kafka from starting with that volume.
|
||||
|
||||
Automatic topic creation is disabled: writing to a missing topic fails instead of creating it. Create each topic explicitly; provisioning topics for product events belongs to later work. Kafka CLI tools inherit the broker's `KAFKA_HEAP_OPTS`, so give them a small heap when running them in the container. For example, a manual technical check with the default `app` project:
|
||||
Automatic topic creation is disabled: writing to a missing topic fails instead of creating it. The backend creates its events topic itself (see [Outbox relay](#outbox-relay)); create any other topic explicitly. Kafka CLI tools inherit the broker's `KAFKA_HEAP_OPTS`, so give them a small heap when running them in the container. For example, a manual technical check with the default `app` project:
|
||||
|
||||
docker compose --env-file .env -p app -f deploy/docker/docker-compose.yml \
|
||||
exec -e KAFKA_HEAP_OPTS=-Xmx128m kafka /opt/kafka/bin/kafka-topics.sh \
|
||||
--bootstrap-server localhost:9092 --create --topic lincodex.technical.probe \
|
||||
--partitions 1 --replication-factor 1
|
||||
|
||||
The Compose health check runs `kafka-broker-api-versions.sh` against `localhost:9092`, so Kafka is healthy only after the broker answers API requests. `make -C deploy smoke` waits for that health check together with the other core services; it does not publish or read events.
|
||||
The Compose health check runs `kafka-broker-api-versions.sh` against `localhost:9092`, so Kafka is healthy only after the broker answers API requests. `make -C deploy smoke` waits for that health check together with the other core services; it does not check the relay or read events.
|
||||
|
||||
The backend receives `KAFKA_BROKERS`, a comma-separated list of `host:port` bootstrap addresses. Compose supplies `kafka:9092`; a directly launched Go process defaults to `localhost:9092`. An explicitly empty value, an empty list entry, a missing or zero port, or a URL scheme fails configuration. The backend has no Kafka client yet: it does not wait for Kafka at startup, and `/readyz` does not check Kafka. The advertised address `kafka:9092` resolves only inside the Compose network, so direct execution needs a separately reachable broker. Redis stays a cache and is not used as a queue.
|
||||
The backend receives `KAFKA_BROKERS`, a comma-separated list of `host:port` bootstrap addresses. Compose supplies `kafka:9092`; a directly launched Go process defaults to `localhost:9092`. An explicitly empty value, an empty list entry, a missing or zero port, or a URL scheme fails configuration. The outbox relay connects to these brokers in the background: the backend does not wait for Kafka at startup, and `/readyz` does not check Kafka. The advertised address `kafka:9092` resolves only inside the Compose network, so direct execution needs a separately reachable broker. Redis stays a cache and is not used as a queue.
|
||||
|
||||
`PASSWORD_HASH_CONCURRENCY` sets the Argon2id memory budget in derivations of 64 MiB and four lanes each (from 1 to 4). Compose passes the value from `.env`, default 1, because the backend container has 1 CPU and 768 MB: a second derivation would only add 64 MiB while sharing the CPU. A directly launched backend without the variable uses the smaller of 2 and GOMAXPROCS. Further registrations and logins wait for memory until their request context ends. Compose also sets `GOMEMLIMIT=600MiB`, about 78% of the container limit, so the Go collector reclaims finished derivations before the container runs out of memory; change it together with the memory limit. The value is read and validated at startup; registration and login will use it once they are implemented. See [ARCHITECTURE.md](ARCHITECTURE.md#password-hashing).
|
||||
|
||||
down stops containers and keeps named volumes. Do not delete database or file volumes during an ordinary stop.
|
||||
|
||||
### Outbox relay
|
||||
|
||||
The backend runs the outbox relay in its own process next to the HTTP servers. The relay publishes committed outbox events to Kafka; its design is in [ARCHITECTURE.md](ARCHITECTURE.md#outbox-relay). Compose passes none of the settings below, so the stack uses the defaults; to change one, add it to the backend `environment` in `deploy/docker/docker-compose.yml`. A directly launched backend reads them from its environment. An invalid value stops startup.
|
||||
|
||||
| Variable | Default | Meaning |
|
||||
| --- | --- | --- |
|
||||
| `RELAY_ENABLED` | `true` | `true` or `false`. With `false`, or without `DATABASE_URL`, the relay does not run and events stay in the outbox. |
|
||||
| `RELAY_BATCH_SIZE` | `100` | Events one pass locks and publishes, from 1 to 1000. |
|
||||
| `RELAY_POLL_INTERVAL` | `1s` | Wait after a pass that found no pending event, from 10ms to 5m. |
|
||||
| `RELAY_PRODUCE_TIMEOUT` | `10s` | Longest wait for the broker acknowledgements of one pass and for topic creation, from 1s to 2m. The pass keeps its transaction open for that long at most. |
|
||||
| `RELAY_STATEMENT_TIMEOUT` | `5s` | Server-side `statement_timeout` of each SQL statement of a pass, from 100ms to 1m. The pass transaction also gets `idle_in_transaction_session_timeout` of `RELAY_PRODUCE_TIMEOUT` plus this value, so PostgreSQL ends the transaction of a frozen or disconnected backend and releases its locks. |
|
||||
| `RELAY_BACKOFF_INITIAL` | `500ms` | Wait after the first failed pass; it doubles with each consecutive failure. |
|
||||
| `RELAY_BACKOFF_MAX` | `30s` | Longest wait between retries, from `RELAY_BACKOFF_INITIAL` to 10m. |
|
||||
| `KAFKA_EVENTS_TOPIC` | `lincodex.domain.events` | Topic for all domain events: up to 249 letters, digits, dots, underscores, or hyphens. |
|
||||
| `KAFKA_EVENTS_TOPIC_PARTITIONS` | `3` | Partitions of a topic the relay creates, from 1 to 1000. |
|
||||
| `KAFKA_EVENTS_TOPIC_REPLICATION_FACTOR` | `-1` | Replication factor of a topic the relay creates: -1 takes the broker's `default.replication.factor`, otherwise from 1 to the broker count. |
|
||||
| `KAFKA_EVENTS_TOPIC_MIN_INSYNC_REPLICAS` | unset | `min.insync.replicas` of a topic the relay creates, at least 1 and not above an explicit replication factor. Unset leaves the broker default. |
|
||||
| `KAFKA_PROVISION_TOPICS` | `true` | With `false` the relay never creates the topic, and every publish fails until the topic exists. |
|
||||
|
||||
`SHUTDOWN_TIMEOUT` also bounds the relay: a pass in progress at shutdown has that long to finish, otherwise it rolls back and its events stay pending. Compose gives the backend `stop_grace_period: 20s`, more than the default 10s `SHUTDOWN_TIMEOUT`, so Docker does not kill the process while a pass commits; raise it together with `SHUTDOWN_TIMEOUT`.
|
||||
|
||||
The single-node stand creates the topic with the broker defaults of the Compose Kafka: replication factor 1 and `min.insync.replicas` 1. That is acceptable only for this stand: one broker cannot replicate, and losing its volume loses the events. A production cluster needs at least three brokers, a replication factor of at least 3, and `min.insync.replicas` 2, either as broker defaults or through `KAFKA_EVENTS_TOPIC_REPLICATION_FACTOR=3` and `KAFKA_EVENTS_TOPIC_MIN_INSYNC_REPLICAS=2`. The producer always uses acks from all in-sync replicas, so with these values an acknowledged event survives the loss of one broker, and writes stop rather than land on a single replica when two of three brokers are down.
|
||||
|
||||
The relay creates a missing topic and never changes an existing one; when the existing topic has another partition count or replication factor than configured, the relay logs one warning. Adding partitions to the topic later moves aggregate keys to other partitions, so per-aggregate order is not guaranteed across that change.
|
||||
|
||||
When Kafka is unavailable, the backend still starts and serves the API. The relay logs warnings such as `outbox relay could not prepare the event bus` or `outbox relay pass failed` with the retry delay and keeps retrying; events stay in `outbox_events` until Kafka answers. In Compose the backend does not wait for the Kafka health check, so these warnings are expected during the first seconds after `make -C deploy up`. Relay logs carry the `outbox_relay` component and contain event IDs, counts, and broker errors, never payloads.
|
||||
|
||||
The internal /metrics endpoint shows the backlog: `lincodex_outbox_relay_pending_events` and `lincodex_outbox_relay_oldest_pending_age_seconds` are refreshed at most every 15 seconds, and `lincodex_outbox_relay_events_total` counts the `published` and `failed` outcomes. A growing age with a flat `published` count means the relay is stuck; the relay warnings name the event IDs, and [ARCHITECTURE.md](ARCHITECTURE.md#outbox-relay) describes the causes. Every backend instance reports the same backlog, so aggregate it with `max`. Published rows stay in `outbox_events`; their retention is not implemented yet.
|
||||
|
||||
## Mail
|
||||
|
||||
The backend sends mail through the `MailSender` port. `EMAIL_PROVIDER` selects exactly one transport: `resend` (the Resend HTTP API, the primary provider for the working stand), `smtp` (a separate SMTP relay), or `disabled`. It has no default, and there is no fallback: when the selected transport cannot start, the backend stops instead of switching to another one. Settings of transports that are not selected are not read.
|
||||
|
||||
Reference in new issue
Block a user