Page 1 of 1

Why use a database

Posted: 22 August 2024, 23:45
by Swinkels
Sorry for the naïeve question, but what's the point in storing small amounts of data in a database when you can store them in php memory instead? It seems like php memory is way faster than db queries.

Is it true that the server thread might die and another thread needs to be able to recover the state from the db?

Currently, I maintain a cached version of a db table in memory, and make insert/delete/update queries when information changes. To access information, I use the cache as long as it is defined. If not, I select all from the database to restore the cache.

Is this the proper way of doing it? How long does a cached object exist? The entire game? Just a single action? Do multiple independent server threads manage the php based on availability?

Re: Why use a database

Posted: 22 August 2024, 23:56
by RicardoRix
Nothing persists in php memory between calls, which is why the whole game state and data is stored in a db.

Normally you just query and update the db as and when required.

Re: Why use a database

Posted: 23 August 2024, 08:01
by imralav
I don't think we have the same instance shared between the calls. I believe each call is handled separately, with clear memory state, so it is always loading stuff from database anyway.

Re: Why use a database

Posted: 23 August 2024, 09:25
by Swinkels
Thanks! I already suspected that, but I just wanted to verify it.

Re: Why use a database

Posted: 24 August 2024, 20:26
by imralav
Considering the above, I like the approach to load all necessary data upfront in a single call, modify it if necessary. And only persist it at the end of the processing, instead of hitting the DB multiple times on each request. You probably already have something similar.