Make an automation safe to rerun
Give each event a stable ID and use it to keep a retry from making a second record.
Keep the same event ID across retries
Use an ID from the source event. A request that times out can be sent again with the same ID, including when the first request completed successfully.
Enforce one record in the database
Use a unique key on the event ID. A separate check followed by an insert can race with another worker, so put the uniqueness rule in the database.
CREATE TABLE reservations (event_id TEXT PRIMARY KEY, item TEXT NOT NULL);
INSERT INTO reservations VALUES ('evt-42', 'Room 3') ON CONFLICT(event_id) DO NOTHING;
INSERT INTO reservations VALUES ('evt-42', 'Room 3') ON CONFLICT(event_id) DO NOTHING;
SELECT COUNT(*) FROM reservations WHERE event_id = 'evt-42';
Check the entire retry
Confirm the count is one. Send the same event twice with overlapping runs and check the result again. For a follow-up action such as a notification, record its completion against the same event ID or use the receiving service’s idempotency feature.