Urgent.News

What's breaking now, across thousands of outlets.

Tech

How to prevent spending from the same balance twice?

Two requests came from the same customer at the same time. Both passed our balance check. One went to the bank and returned with insufficient balance. The customer saw an error message on the screen and complained. We only learned about the issue from that complaint. There was no obvious error in the code. We were reading the balance, checking if it was sufficient, and then deducting it. This is correct for a single request. The problem is with two requests for the same account…

Translated from Portuguese Read in Portuguese

A bug was discovered in a banking system that occurred when two withdrawal requests were made simultaneously from the same account. The issue arose because the system checked the account balance and then updated it in two separate steps, allowing one request to pass the balance check while another request was still in progress, resulting in an incorrect balance.

To fix this, the developers implemented a row lock using UPDLOCK in SQL Server, which prevents other requests from accessing the account until the transaction is complete. Alternatively, they suggested using a single UPDATE statement with a conditional check to update the balance, eliminating the need for a separate read operation.

Written by urgent.news from Dev.to's report — not a translation of it. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Daffi: A High-Performance Brokerless RPC Framework for Python

What if you could build distributed Python applications without introducing another message broker — and still get impressive performance?

  • Daffi is a brokerless RPC framework for Python.
  • It simplifies distributed Python app development.
  • Supports multi-communication patterns and encryption.

More from Wednesday 18 November →