1
\$\begingroup\$

Background

I'm trying to implement a little MMORPG. I'm planning to use Cassandra DB for the database.


Question

I don't really understand which data should be store in DB and which one should be just kept in memory.

For example : the friend list. Friend list will be stored in DB, but what about connected / disconnected friends ? Should I keep in memory all connected account and access it directly ? Or should I store this information in the DB (therefore writing / reading quite often for something that could be kept in the memory) ?

\$\endgroup\$
1
  • 2
    \$\begingroup\$ What benefit is there to keeping this in a database? What do you risk in not keeping this in the database? Why you use a database in the first place? \$\endgroup\$
    – Vaillancourt
    Commented Jan 29, 2019 at 10:26

1 Answer 1

3
\$\begingroup\$

Deciding where to keep your data is a tradeoff between speed and RAM usage. Keeping information in the database saves you money, because your game servers need far less RAM. But keeping it in the database means the data will take longer to access, so you can't do it in real-time. When you need to do something which depends on data from the database, you need to wait until the database returned that information, which can take a few ms.

So when you have data you need really fast access to and which you access very frequently, you should keep it in memory.

The positions of currently playing players, for example. You will need those at least once every update tick, so it would be crazy to read those from the database on demand.

But what about the friend list? Well, that depends on what the "friend list" actually does in your particular game.

  • Inform players about connect/disconnect events of their friends? That happens every few minutes to every few hours for each player, depending on how sociable they are. It's also no problem at all when these messages appear with a latency of a few seconds. So you could retrieve the friend connections from the database whenever someone connects or disconnects and discard the database result immediately after you posted the events.
  • Tell the player where in the world their online friends are currently playing? This means you need to send regular updates to all friends who are also online. Depending on how often you want to send these updates, you might either keep the currently online friends in memory or do it from the database. Keeping the offline friends in memory makes no sense in this particular scenario. But remember to update the online friends list in memory on connect and disconnect events.
  • Do you have social game mechanics which actually use the friends list for mechanical reasons? Like, say, people give 10% of their collected experience to all their friends even if the friends are offline? If you had a mechanic like that then it would make sense to keep the complete friends list in memory. But it would not make sense to update their exp count in the database after every exp event. You could just keep track of the exp share of each offline friend in memory and write them to the database at regular intervals.
\$\endgroup\$
1
  • \$\begingroup\$ Thank you for the detailed answer. This explanation is really good and the given examples help ! \$\endgroup\$
    – Astariul
    Commented Jan 30, 2019 at 0:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .