How to protect a in used function from calling by another sessions


You may concerning about how to protect a record from database or cache to not get attack from a lot of sessions at the same time.

Example : Your code logic is to check if user A has enough money you will increase quantity of Apple belong to user A

Example above is user buying apple scenario. So see the process below if we not yet protect and after protected.

Not yet implement lock wait

When 10 sessions come at the same time your server still do the same thing to check on each session. If 1 Apple is 1$ and user has only 1$ so this case the result is user will get 10 apples and paid only 1$. That's very dangerous thing in your system.

Implement lock wait

When 10 sessions come at the same time server will allow only first session to finish its job first then allow next session it's like peoples stand and waiting to pay at the mart.

How to Implement

I will provide PHP code for example. You can apply your own logic I perhaps not yet clear for all but it's just an idea that may helpful. 

Post a Comment

* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

Random Posts