I am currently making a little browser game, even though it is not multiplayer i still need prevent cheating so the client can’t hold any meaningful data, and so far it has been going fine, but I am currently looking at optimizing bandwidth and server load which will increase heavily due to this authoritative style, and this is where I have conflicting ideas.
Now this could be quite simple, but I lack experience with any kind of server interaction.
When a user wishes to purchase an item the client sends their name and item they wish to purchase to my PHP handler, now PHP has to query for the items attributes, such as price, now currently these “attributes” are just stored in PHP files (for development and testing), but I want to change this, and I’m undecided whether to store all these item attributes in a database, as this adds more pressure on my database, to continuing storing all these items in my PHP or even store them in TXT files on my server for PHP to access. All items are split into three tiers to minimize load.
A database ultimately stores the data in files on disk but unlike a text file, the whole system is heavily optimised for fast operation and is also highly robust against inconsistent or damaged data. If there are only a few items which have simple data and are unlikely to change very often then it might be easiest to store them in the PHP file. This will be quite efficient if you are using a bytecode caching system rather than loading and compiling the PHP file. Otherwise, a database should be much better.
Use a database. I’ve been running browser-games for 10+ years. Very, very few things are even worth optimizing since usually the database will be much faster than anything you can roll yourself. As andeeee said, use eAccelerator or Zend Compiler or something like it to speed up the PHP part, since that will much more likely be a bottleneck.
The performance impact will be minimal, if you query the prices from database. Actually its much faster, because reading from textfiles manually and with selfwritten functions is usually very slow. Since the prices if your items won’t change often, you can also cache the Query (i.e. the Query to get all items form the database + prices) by just adding “SQL_CACHE” in front of the select query, like “SELECT SQL_CACHE itemName, price FROM prices”
With modern databases you can do 1000s of queries on a small database. Even doing queries on a database with 10.000 rows barely causes any cpu usage. Got a DB which stores 780.000 rows and doing 5-10 selects on it per requests is barely noticeable.