0

I know this is not ideal but I just started experimenting with online stuff and I don't know how to search for possible solutions or if this problem has a name. please excuse my behavior.

The Problem: I want to have a database with item that can be changed by more than 1 user. Lets say player 1 loads the DB and see that the item says "A". Player 1 changes "A" into "B". But before he can upload it to DB Player 2 loads the DB. Player 2 loaded "A" and wants to change it to "C". So the DB changes from A to B and then to C.

My project is a webpage. Im planning to have less than 10 players and about 1000 items. I know basics of PHP and MySQL. I think reloading the DB frequently can solve my problem but I'm afraid the load will not be good for the server.

Im asking for solution, possible tools designed for this application or simply tell me what to google to learn more. Thank you for your time.

1 Answers1

0

The problem you are encountering is called "update conflict." Reading from the database frequently can reduce how often it occurs, but it can't eliminate it. In fact, it is impossible to completely eliminate the problem.

There are two strategies for dealing with it: Optimistic and Pessimistic locking

Optimistic locking

Optimistic locking ensures that you detect the problem. You version your resource and increment the version number each time it is written. Every write to the database needs to use the current version number or the write is rejected. Clients need to have a strategy for when their write to the resource is rejected.

Pessimistic locking

With pessimistic locking, you allow somebody to lock the resource, read the resource, write the resource back, then release the lock. The lock ensures that only one person can write to the resource, but others may have to wait on the lock which can slow performance.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109