4 ms·
My use case: I run a couple of stateful multiplayer games where I have a Node.js server writing to MongoDB on each player update (e.g. "update player.x to 123")
by Kiro 10y ago
My use case: I run a couple of stateful multiplayer games where I have a Node.js server writing to MongoDB on each player update (e.g. "update player.x to 123"). I only read from the DB when a player logs in and is added to the game or when the server restarts (items etc). As long as a player is online the state is just kept in in memory (a big array of all the players) but is written to the DB every time it's updated.
This means the game could theoretically work without a DB at all, the time it takes to write to the DB etc doesn't matter as long as it happens in the correct order. Read speed is also not really relevant since it happens so seldom.
The MongoDB document is the same as the player object in Node.js.
I've been thinking of migrating to RethinkDB but I've also been looking at PostgreSQL. Would the JSON support cover this sufficiently and would it make sense? I don't need any schemas or anything like that. I just want to be able to add and update JSON objects.
- raverbashing 10y agoYou could move to redis
- Kiro 10y agoThanks. Can I update a specific property without overwriting the whole JSON blob? Or how do I do it effectively?
- gamesbrainiac 10y agoYes, you can namespace properly, and have that effect.
- siddboots 10y agoThe biggest benefit of using an RDBMS is that it forces you to organise your data in a generalised way, and make it more available for queries that haven't been written yet. In your particular use case that doesn't sound very important, as you are basically just using it to store the game state. Postgres's JSON types are really powerful, but using a document store is probably all you need, so I wouldn't recommend switching unless you've identified the tangible benefit. One scenario that I can imagine is if you want to generate statistics across your game data. If you find yourself pulling lots of JSON from the database, and then doing loops, aggregations, or manual joins, within your application code, then switching out to a RDBMS could be a good idea. In that case, you could use postgres to store JSON much as you are doing with Mongo, but then have some in-database ETL to transform that data into a relational schema, which you could query in more powerful ways.
- marknadal 10y agoIf you are already using Node.JS and need to merge/update JSON objects rather than them being overwritten, check out these couple of games made with a database designed for what you are talking about: https://github.com/PsychoLlama/Trace https://github.com/PsychoLlama/Trace https://github.com/PsychoLlama/connect-four https://github.com/PsychoLlama/connect-four
- Kiro 10y agoThanks! How does it work? Is it just storing and updating a JSON file without any real DB? My game is running on Heroku (connecting to mLab) so I don't think that would work.
- marknadal 10y agoOh, that is great! We even have an one click instant deploy Heroku gaming server https://github.com/gundb/gaming https://github.com/gundb/gaming ! However, you are right, that will just run "in-memory" (and I think that URL is using an old version of the database). But! No fear, there are adapters that connect to Amazon S3 (or others, you can connect to other storage engines, like Level - which in turn can connect to pretty much anything else). The S3 route is awesome because it means... 1. Multiple Heroku instances will still have persistent data across many reboots. 2. You don't have to worry about any Heroku plugin configuration / buildpack stuff. 3. S3 is ridiculously cheap, and you can even set it to auto-delete stuff after a month (in case you don't want to pay for long term storage). Plus, there is no vendor lock in, so you can easily switch away later.