Bennett Bennett - 2 months ago 18
Node.js Question

When to save user data to disk?

I've been getting into web applications and node.js lately, and it's obvious that you should write user data to the disk, but when should I? It would be a bit overkill and very resource intensive to write to the disk every time data is updated, so when should you?


Simple, Safe Method: Memory cache and disk saving

I'd recommend creating a temporary memory (RAM) cache to store user data. (For data of active users)

This temporary disk cache should be read and written to as though it were the normal on-disk user save data. Just ensure that every once in a while this cached data is written to disk, and you're good to go.

Warnings / Tips

  • Do not cache all of your data. (If you expect to have larger data than RAM on your machine.) Instead, only cache user data of logged in / active users for speed and efficiency.
  • Ensure that your autosave delay is short enough to avoid any risk in the case of a crash. Crashes happen. Be prepared.