PHP and Concurrency PHP and Concurrency database database

PHP and Concurrency


Keep the logic at the DB, those semantics have mostly been solved for you.

update teamsset score = score + 1where team_id = 1

.. or whatever score and whatever team number.


IF your real world problem is bigger than this simple example, you should use a transaction in conjunction with lock tables to make sure users don't overwrite each other.


Major databases are designed to handle such situations.

When using MySQL, the InnoDB storage engine has the row-locking feature, which locks the row that is being written to. So, any update request that is for the same row will wait until the previous operation on the row completes.

If you use MyISAM storage engine, that locks the whole table when a row is being updated. That creates a choke on the server when multiple players simultaneously push update requests. So it is all about what database and particularly what storage engine you are using for the purpose.