0

I am currently developing an online real-time game which uses a global dictionary to store all the game rooms. If a user try to enter a game, the script checks the dictionary looking for an empty room. If no empty room is found, a new room object is added to the dictionary so other logged users can enter a game room.

The problem is that using a global dictionary for such task is not a good idea as pointed in these questions: Are global variables thread safe in flask? How do I share data between requests? and Preserving global state in a flask application

In the answers, it was recommended storing requests shared data in databases or memcached.. In case I wanted to do it using the database way, should I store the entire dict in the database every time it was requested? Is there a better and secure way to do this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
37dev
  • 179
  • 1
  • 3
  • 13

1 Answers1

1

should I store the entire dict in the database every time it was requested

If you use a database (like SQLite), the entire dict should already be in the database. You can then query the database whenever you need information about the game rooms. Do not keep the entire dict with shared data in memory, move all the shared data into the database, remove the dict with shared data from memory, query the database whenever you need shared data and update the database when shared data is changed.

I suggest you try it out. I think you will find the database fast (and secure) enough.

Note that a database also has ACID properties which you can use and rely on. The value of these ACID properties might not be clear at the moment, but that can change the more you use a database.

vanOekel
  • 6,358
  • 1
  • 21
  • 56