[英]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.我正在做一个项目,我必须为 NodeJS 快速服务器存储数据。 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).我总是听说数据库很适合这种东西,但我想只是将所有数据保存在 NodeJS 中的对象中,然后每分钟(或 5 分钟)将它们作为 JSON 备份到磁盘。 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?内存数据库比 javascript 对象快吗? Are JSON-based data backups a good idea in this aspect?在这方面,基于 JSON 的数据备份是个好主意吗? Or should I simply go with a normal database because the performance doesn't really matter in this case?或者我应该简单地使用普通数据库的 go 因为在这种情况下性能并不重要?
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:但是,如果出现以下情况,我会提倡使用数据库:
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.但是,对于更复杂的东西(大量数据、object 关系、分页等),数据库可能会开始变得更快。
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/O 延迟。 I would recommend using Mysql
for sql database and MongoDB
or Cassandra
for nosql. 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. redis
是一个非常流行的内存数据库。
MongoDB
store data in BSON (a superset of JSON) like formate, so it will be a good choice in your case. MongoDB
像甲酸盐一样以 BSON(JSON 的超集)存储数据,因此在您的情况下它将是一个不错的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.