简体   繁体   English

存储:数据库 vs 内存对象 vs 内存数据库

[英]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:但是,如果出现以下情况,我会提倡使用数据库:

  • 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.但是,对于更复杂的东西(大量数据、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.

相关问题 从内存中ASCII而不是从文件连接反序列化对象 - Unserialize objects from in-memory ASCII instead of from a file connection Java中内存缓存的字符串的唯一表示形式 - Unique representation of String for in-memory cache in Java 如何更新NodeJS中的内存变量? - How to update in-memory variable in NodeJS? 如果名称通过JSON传递,如何访问内存中的变量? - How to access a in-memory variable if its name is passed through JSON? 如何将内存中的JSON字符串读入Spark DataFrame - How to read in-memory JSON string into Spark DataFrame 如何在JsonSystem(Itemscript JSON库)中使用内存数据存储? - How to use in-memory data storing with JsonSystem (Itemscript JSON library)? 优化 json.load() 以减少 Python 中的内存使用和时间 - Optimization of json.load() to reduce in-memory usage and time in Python 性能问题,平面文件与数据库存储 - Performance issue, flat file vs database storage 使用JSON文件的Node.js应用程序是否会在连接之间共享内存? - Will a Node.js app consuming a JSON file share in-memory between connections? 如何将本地 json 文件作为启动器导入 Express 中的内存资源 - How do I import local json file as a starter as my in-memory resource in Express
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM