Designing Retry-Safe Customer Intake with PostgreSQL
Disclosure: This draft was generated with AI assistance for GILNEX editorial review. The SQL is an illustrative example, not a verified production implementation. References checked November 27, 2026. A customer taps Submit, the network times out, and the browser retries. Did the first request fail, or did the response disappear after the server committed? A reliable intake system must handle…
When a customer clicks Submit, the network connection may time out, causing the browser to automatically retry the request. This creates an important question: did the original request fail, or did the server's response vanish after it was committed? A dependable system for handling customer intake must manage both scenarios without generating duplicate appointments, notifications, or CRM records.
To accomplish this, a unique "retry key" is assigned to each intended operation. This key is generated a single time when the submission occurs and then reused after a network timeout. The key is specific to the authenticated tenant and the type of operation being performed. The server determines the tenant; it never relies on a tenant identifier supplied by the browser, which could be tampered with.
The system also saves a simplified version of the client's request, known as a "fingerprint." If the identical key is received with different contents, it is considered a conflict rather than an accepted change. However, a different key does not automatically imply the same customer; identifying duplicate customers at the business level is a separate issue.
To enforce this, PostgreSQL is used to create a table called "intake_requests". This table has four columns: tenant_id, request_key, payload_hash, and response_id. The tenant_id and request_key columns are declared as the primary key to ensure that each unique combination can only exist once.
When a new request is made, an SQL insert operation is performed within a transaction. If the server attempts to insert a row using the ON CONFLICT (tenant_id, request_key) DO NOTHING syntax, it will return the response_id if the insert was successful. If no row is returned, it means the transaction did not own the initial operation.
In this case, the server retrieves the committed record using a subsequent READ COMMITTED statement. It then compares the payload_hash of the retrieved row to the one in the new request. If they match, the existing response identifier is returned; if not, a conflict is raised.
Sending notifications before a transaction is committed could lead to a situation where a customer receives a confirmation for a record that was never saved. To prevent this, a transactional outbox is used. This outbox contains the intent of the operation alongside the lead. A separate dispatcher then claims these records, delivers them, and records the delivery result.
However, it's important to note that even with these safeguards, delivery is not guaranteed to be exactly once. If a worker crashes after an external provider has accepted a message but before the receipt is stored, the result can be ambiguous. In such cases, the system should use provider-supported idempotency where possible, reconcile against provider receipts, and avoid blind retries when the outcome is uncertain.
While a request key helps prevent two different customers from trying to reserve the same appointment, it does not stop two customers from attempting to book the same time slot in a multi-capacity resource. Additional measures, such as unique constraints or atomic capacity reservations, are needed to enforce capacity limits.
Testing is crucial to ensure the system behaves as expected. Simulations should be run to test different scenarios, including concurrent requests with the same key and payload, requests with different payloads, lost HTTP responses after commit, crashes before commit, crashes after external delivery, and cross-tenant access. Logs should also be reviewed to ensure no sensitive customer data is disclosed.
Ultimately, the aim is not to guarantee perfect delivery. Instead, the goal is to make every ambiguous state visible, bounded, and recoverable. This ensures that the system is reliable enough to handle the complexities of real-world customer intake.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to