Should I use a queuing system to process payments?

I use Slim in combination with Stripe PHP Library to process payments in my application.

Everything is fine, but until recently, I found an alarming error in my system, which, in my opinion, could be a much more serious problem than I probably think. In my logic, at three separate control points of the payment process, I check the inventory in my (MySQL) database to ensure that the user does not buy more products than is available.

However, when several users make a request approximately 500 ms apart, the payment system seems to process these requests immediately, which leads to many problems, ranging from incorrect and unbalanced inventory to false confirmation of successful payments.

With some due diligence, I narrowed down the solution to two options (although I can sell myself short):

1) Use the Queuing System , which, in my opinion, will queue these requests and process them one at a time, creating a sort first in the first place, first and foremost.

2) Attach some middleware to each request, which will act as a queue, and try to process each request synchronously (although this might look like what I already have)

Now, with that said, any suggestions / opinions on these parameters? and obviously don’t hesitate to completely abandon my ideology and point me in a different direction.

+4
source share
5 answers

Using OPERATIONS would be useful for this, as it seems that you are faced with what is called a race condition.

Disabled from https://www.w3resource.com/mysql/mysql-transaction.php and I quote:

- , SQL. - , . , , , .

SQL. , , COMMIT, ROLLBACK , DDL ( (DDL) CREATE, ALTER, RENAME, DROP TRUNCATE ).

.

:

LOCK TABLES      
  tbl_name [[AS] alias] lock_type      
  [, tbl_name [[AS] alias] lock_type] ...    

lock_type:      
  READ [LOCAL]    
 | [LOW_PRIORITY] WRITE    

UNLOCK TABLES

- MySQL:

" ", https://www.informit.com/articles/article.aspx?p=29312

, , . , , . - , .

+1

, , , , - , ( )...

, , . - .

, , () , , . (, ?).

+1

, , . , , , , - , , , , , , .

"event-listener", jQuery Javascript PHP, , , - , , .

0

, . . :

  • " ", .
  • :
    • = > .
    • = > (, 5 ).

, 5 ( ), , - .

0

, . , Black Friday Cyber ​​Monday .

  • , . , Redis . , , Redis .
  • , .
  • When the user completes the payment, subtracts the purchased quantity from the inventory in the database transaction and uses the lock. if the transaction does not succeed either refund the payment, or you want to place an order for off-road goods or not.

Hope this helps you.

0
source

Source: https://habr.com/ru/post/1691643/


All Articles