How to deal with concurrent updates in databases?

后端 未结 9 1732
名媛妹妹
名媛妹妹 2020-11-27 12:24

What\'s the common way to deal with concurrent updates in an SQL database ?

Consider a simple SQL schema(constraints and defaults not shown..) like

c         


        
9条回答
  •  悲&欢浪女
    2020-11-27 13:27

    There are is one critical point in your case when you decrease user`s current credit field by a requested amount and if it decreased successfully you do other operations and problem is in theory there can be many parallel requests for decrease operation when for example user has 1 credits on balance and with 5 parallel 1 credit charge requests he can purchase 5 things if request will be sent exactly on the same time and you end up with -4 credits on user`s balance.

    To avoid this you should decrease current credits value with requested amount (in our example 1 credit) and also check in where if current value minus requested amount is more or equal to zero:

    UPDATE credits SET creds = creds-1 WHERE creds-1>=0 and userid = 1

    This will guaranty that user will never purchase many things under few credits if he will dos your system.

    After this query you should run ROW_COUNT() which tells if current user credit met criteria and row was updated:

    UPDATE credits SET creds = creds-1 WHERE creds-1>=0 and userid = 1
    IF (ROW_COUNT()>0) THEN 
       --IF WE ARE HERE MEANS USER HAD SURELY ENOUGH CREDITS TO PURCHASE THINGS    
    END IF;
    

    Similar thing in a PHP can be done like:

    mysqli_query ("UPDATE credits SET creds = creds-$amount WHERE creds-$amount>=0 and userid = $user");
    if (mysqli_affected_rows())
    {
       \\do good things here
    }
    

    Here we used nor SELECT ... FOR UPDATE neither TRANSACTION but if you put this code inside transaction just make sure that transaction level always provides most recent data from row (including ones other transactions already committed). You also can user ROLLBACK if ROW_COUNT()=0

    Downside of WHERE credit-$amount>=0 without row locking are:

    After update you surely know one thing that user had enough amount on credit balance even if he tries yo hack credits with many requests but you dont know other things like what was credit before charge(update) and what was credit after charge(update).

    Caution:

    Do not use this strategy inside transaction level which does not provide most recent row data.

    Do not use this strategy if you want to know what was value before and after update.

    Just try to rely on fact that credit was successfully charged without going below zero.

提交回复
热议问题