简体   繁体   中英

Storage: database vs in-memory objects vs in-memory database

I'm doing a project where I have to store data for a NodeJS express server. It's not a LOT of data, but i have to save it somewhere.

I always hear that a database is good for that kinda stuff, but I thought about just saving all the data in objects in NodeJS and back them up as JSON to disk every minute (or 5 minutes). Would that be a good idea?

What im thinking here is that the response time from objects like that are way faster than from a database, and saving them is easy. But then I heared that there are in-memory databases aswell, so my question is:

Are in-memory databases faster than javascript objects? Are JSON-based data backups a good idea in this aspect? Or should I simply go with a normal database because the performance doesn't really matter in this case?

Thanks!

If this is nothing but a school assignment or toy project with very simple models and access patterns, then sure rolling your own data persistence might make sense.

However, I'd advocate for using a database if:

  • you have a lot of objects or different types of objects
  • you need to query or filter objects by various criteria
  • you need more reliable data persistence
  • you need multiple services to access the same data
  • you need access controls
  • you need any other database feature

Since you ask about speed, for trivial stuff, in-memory objects will likely be faster to access. But, for more complicated stuff (lots of data, object relations, pagination, etc.), a database could start being faster.

You mention in-memory databases but those would only be used if you want the database features without the persistence and would be closer to your in-memory objects but without the file writing. So it just depends on if you care about keeping the data or not.

Also if you haven't ever worked with any kind of database, now's a perfect time to learn:).

What I'm thinking here is that the response time from objects like that is way faster than from a database, and saving them is easy.

That's not true. Databases are the persistence storage, there will always be I/O latency. I would recommend using Mysql for sql database and MongoDB or Cassandra for nosql.

An in-memory database is definitely faster but again you need persistence storage for those data. redis is a very popular in-memory database.

MongoDB store data in BSON (a superset of JSON) like formate, so it will be a good choice in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM