Transient errors (not marked permanent) result in:
- Negative acknowledgements with delay (`NakWithDelay`) according to `nats.consumer_rules.backoff`.
- A retry streak counter inside the consumer:
- Each transient error increases `consecutiveProcessErrors`.
- After 10 or more consecutive errors, the consumer applies an additional **sleep**:
-`backoff = min(consecutive_errors * 100ms, 5s)`.
- A warning log with the sleep duration and error count is emitted.
This combination provides **backpressure** when downstream systems (especially the DB) are in trouble, slowing down consumption instead of aggressively retrying.
### Persistence and Idempotency
The primary persistence path is `Repository.InsertOne` into `aviation.telegrams`. To avoid applying the same business event multiple times, a **minimal idempotency check** is implemented:
- If both `message_id` and `date_time` are non-empty:
-`InsertOne` first calls `messageExists(message_id, date_time)`.
- If a row already exists, the insert is **skipped** and an informational log is written.
- Otherwise, the insert proceeds.
This makes repeated delivery of the same telegram (same `message_id`/`date_time`) safe from a business perspective, even if JetStream redelivers messages or upstream replays.
For higher guarantees in production environments, you may:
- Add a unique index on `(message_id, date_time)` at the DB level, and treat any conflict as a duplicate.
- Extend the idempotency key with additional fields (e.g. originator, category) if required by the business model.
### DB Degradation and Backpressure
Database write failures in `Repository.InsertOne` and related methods are treated as **transient** by default:
- Errors propagate back to the NATS consumer.
- The consumer issues a NAK (with delay) and increases the transient error counter.
- When errors persist, the added sleep in the consumer reduces message throughput and gives the DB time to recover.
DB health also feeds into readiness:
- The monitoring server hits `pgxpool.Pool.Ping` on `/readyz` and `/healthz`.
- If the DB is not reachable, the endpoints return `503`, signalling to orchestrators that this instance should be drained from traffic.
Together, this yields:
- **Backpressure** via reduced consumption rate and NATS-level backoff.
- **Degradation signalling** via health probes for external systems to act upon.