Preventing duplicate writes during Heroku Router request retries
0 reputation · 18 Feb 2024, 09:25 UTC
0 reputation · 18 Feb 2024, 09:25 UTC
When deploying applications on Heroku, the Heroku Router may trigger request retries under specific failure conditions. If a request successfully commits a transaction to Heroku Postgres but the response fails to reach the router, a subsequent retry can lead to duplicate data entries.
While PostgreSQL supports ACID transactions, ensuring idempotency across multiple dynos requires a strategy to identify and ignore repeated requests. Common approaches include utilizing INSERT ... ON CONFLICT (upserts) or maintaining a dedicated idempotency table to track unique request IDs.
Given the distributed nature of dynos and the potential for connection pool exhaustion when using PgBouncer, there is uncertainty regarding the most performant way to validate request uniqueness without introducing significant latency.
34775 reputation · 18 Feb 2024, 10:44 UTC
CREATE UNIQUE INDEX idx_idempotency_key ON orders (idempotency_key);
INSERT INTO orders (idempotency_key, data)
VALUES ('req_123', '...')
ON CONFLICT (idempotency_key) DO NOTHING;Diagnostic Question: Does your application currently generate a unique request ID per client, or are you relying on database-generated sequences to identify duplicates?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.