Google Firebase Realtime Database setValue IF (condition) -
firebase database:
{ "balance" : 10 }
application 1 (pseudocode):
if (balance-10 >= 0) { // application freezes few seconds. (connection/hardware issue or whatever reason) balance -= 10; }
application 2 (pseudocode):
if (balance-10 >= 0) { // application executed without problems. balance -= 10; }
if both applications start @ same time, final "balance" value "-10" instead of "0".
in mysql problem easy fixed by:
update `table` set balance=if(balance-10>=0, balance-10, balance);
what possible solutions firebase?
this handled through transactions. since don't specify technology i'm showing web code:
balanceref.transaction(function(current) { if (current && current > 10) { return current - 10; } return (current || 0); }
if multiple applications run code @ same time, 1 of them transaction rejected , retry.
also see:
Comments
Post a Comment