Solved MongoDB - Using a server side cache or querying everytime?

Discussion in 'Spigot Plugin Development' started by Maxx_Qc, Jan 19, 2020.

  1. Hi guys,
    I'm kinda new to MongoDB and I would like to know if I do need to keep the player's data cached into my plugin memory or if it is useless and I can just always querry directly from MongoDB.

    Thanks in advance :)
  2. Depends on how can the data in the DB change without you noticing it.

    No need to query anything as long as player doesn't log out
  3. Basically anything in the DB is gonna be changed within the plugin.
    But that's my question, should I load everything when a player logs in and store it or should I query directly from the DB when I need something about that particular player?
  4. Storing everything when a player logs in seems like an obvious solution, but you need to make sure that there is no other factors that can influence e. g. your player's balance from outside of the server (like donations) - in that case you'll need something like an event system.

    If you keep things simple ("Please reconnect to see the transactions" or smthing like that) - then storing data on server should work for you.
  5. But if something goes wrong and the server crash, no data from the online players is gonna be saved :(
  6. I would go for
    Caching everything about the player in the server and when there is an update with it, to sync it with the database.
    So that you won't lose your data on a crash. Also, you can use CAS (Compare and Swap) for when syncing with the database, so if something changed the value from the database, you gonna work according to the new in the database

    So basically on getters, cached value
    and on setters sync with db, and work with data in real-time.
    • Agree Agree x 1
  7. Hey OP!

    You've marked this as solved, but I'll give my input either way to see if it helps you!

    You can create a runnable that loops through all the players and sync their data every X amount of minutes. So in case you do encounter a crash, you only lose X amount of minutes worth of data.

    This is in no means a perfect solution, but it is certainly better than querying from Mongo every time, and hitting Mongo every time a change is made.
    • Like Like x 1