July 23rd, 2012, 11:56 AM
Database locking/transactions techniques for browsergame PHP/MySQL
I'm wondering what's the proper way to handle concurrent actions in a browsergame, that access the same resource (e.g. one player's warehouse), combined with the "on view" method instead of cronjob.
Basically that means, instead of running some script that processes the action queues each second, the calculations are made when that resource (warehouse) is accessed the next time (e.g. one attacks and take resources, or the player itself spends some of them).
Problem obviously occurs when two players access the same resource at the same time, and I'm not sure if there's a good-proven standard way to implement this...
Some guesses of me would be:
1. Using MySQL transactions (one transactions includes: read from actions table, execute them, delete executed actions)
2. Using MySQL table lock for actions table
3. Locking with PHP flock()
4. Using semaphore with PHP sem_acquire()
July 25th, 2012, 07:27 PM
Use database transactions, possibly with an explicit lock if you need to run some code at the application layer between a read and a write to the database.