ANDREA LAB / BLOG
How to handle concurrent approvals with PostgreSQL and TypeORM
Two reviewers can act on the same request almost simultaneously. Locking the request lets you record each vote, advance the workflow once, and save the notification in the same transaction.
Imagine a portal that lends audiovisual equipment. Each request needs two approvals before it moves to final review. Ana and Luis click Approve almost simultaneously. If each operation saves its vote and checks the other before either transaction commits, both can conclude that a response is missing. The request stays pending even though both votes are saved.
Start by deciding what has to happen as one operation. Here, recording the vote, checking the other decisions, and advancing the workflow belong together. An implementation using TypeScript, TypeORM, and PostgreSQL can coordinate those steps through the request row.
Lock the request before evaluating the transition
In PostgreSQL, SELECT … FOR UPDATE locks a row until the transaction ends. In TypeORM, inside dataSource.transaction, you can request that lock with lock: { mode: "pessimistic_write" } when loading the request through the transaction manager.
The votes live in separate rows, so locking only the vote row does not serialize operations on the request. If both transactions first acquire the same request lock, Ana can save her decision and commit. Luis waits, takes the lock, and reads the current votes. If the conditions are met, his operation advances the request.
Apply that rule to operations that can cancel or close the same stage. An operation that writes without coordinating with the others can undermine the shared decision. Acquire locks in a consistent order when an operation needs more than one row.
Read the current vote and handle retries
An initial lookup can identify the request from a review link. Before writing, read the vote again after taking the lock. An earlier read can become stale while the transaction waits.
A retry policy can return the original result when the same vote arrives twice. If a different vote arrives for a closed review, the server can return a conflict and ask the client to refresh. Distinguish those cases so a double click does not create a second event and a conflicting decision cannot reopen a stage.
This pseudocode summarizes the order for the example portal:
BEGIN
lock request
read current vote
if it matches a saved vote, return previous result
if the vote or stage is closed, return conflict
save vote
evaluate transition using all votes
save audit event and, if the stage changes, enqueue notification
COMMIT
Save the notification with the state change
Sending email from inside the transaction holds the lock for longer. The provider can also accept the message before the database rolls back the change. Instead, save a row representing the pending notification in the same transaction. A worker picks it up after commit and sends the message.
The queue coordinates the state change and the intent to notify. Delivery retries still need handling. If the worker crashes after the provider accepts the message, it can try again when it restarts. Use a stable notification key and pass it to the provider if it supports deduplication. Record delivery attempts separately from the business event.
Test the ordering that can break the workflow
A sequential test can check the rules for a stage. To test concurrency, run two operations through independent connections and control when they acquire the lock. Verify that both votes are recorded, the stage advances once, and there is one transition notification. Repeat with two requests for the same vote and with a concurrent cancellation.
A lock per request is useful when the rule depends on several rows and the critical section is short. If many operations compete for the same request, measure lock wait time and keep external calls outside the transaction. For the example portal, the practical check is whether the final outcome follows the recorded decisions even when people click at the same time.