The developer addressed concurrency issues in a microservice-based payment gateway integration. The system previously struggled with race conditions where multiple requests for the same order resulted in duplicate payment sessions, financial documents, and provider calls.

To manage concurrent creation requests, the team replaced local C# SemaphoreSlim locks with PostgreSQL advisory locks (pg_advisory_xact_lock). This ensures that only one instance of the API can initiate a payment session for a specific order ID at a time, regardless of the number of active service replicas.

For status checks and payment finalization, the system utilizes SELECT FOR UPDATE to lock relevant order and session rows. This prevents multiple concurrent checks from applying the same payment result twice, ensuring that only one financial record is created per order.

To handle cancellations, the system requires atomic validation of cancellation codes before calling the provider's reverse function. This prevents multiple concurrent requests from triggering duplicate reversal operations.

The architecture also incorporates defensive database constraints, including partial unique indexes on payment sessions and accounting entries. These indexes act as a secondary safeguard to prevent duplicate active sessions or financial records even if the primary application logic is bypassed.