Preventing Duplicate Side Effects in Event-Driven Systems
Описание и применение
В системах, основанных на событиях, повторные доставки могут вызывать дублирование внешних действий. Решение – использовать ключи идемпотентности на уровне бизнес‑операций, уникальные ограничения в базе, явные состояния и классификацию повторных попыток.
Задача
Event-driven systems can deliver the same event multiple times, leading to duplicate side effects when external actions are performed without proper idempotency.
Результат
By shifting from event-level to business-level idempotency and adding durable state, retries became safe, eliminating duplicate side effects and improving system reliability at scale.
Рабочий процесс
- Receive external event via webhook
- Validate and persist event with stable identity
- Enqueue for processing
- Create or claim business action with unique key (account_id+automation_id+source_event_id+action_type)
- Insert action record with unique constraint
- Perform idempotent external API call using same key
- Update durable state to COMPLETED
- Log metrics and trace identifiers
Доказательства
The real problem appears after the action has been claimed. Consider: Create action → Call external API → External API succeeds → Worker crashes → Completion state is never written.
If the external API supports idempotency keys, use them. The same logical operation should always carry the same key.
- Дата записи: 2026-09-13T10:31:58.667Z