Node.js, store object in memory or database?

谁说我不能喝 提交于 2019-12-10 11:10:03

问题


Am developing a node js application, which reads a json list from a centralised db

List Object is around 1.2mb(if kept in txt file)

Requirement is like, data is to be refreshed every 24 hours, so i kept a cron job for it

Now after fetching data i keep it into a db(couchbase) which is locally running on my server

Data access is very frequent, i get around 1 or 2 req per sec and nearly all req need that Object

Is it good to keep that Object as a in memory object in node js or to keep it in local db ?

What are the advantages and disadvantages of both ?

Object only read for all requests , only written once by cron job

it's a high end system, i7 quad core, 16gb ram


回答1:


  1. It depends from your hardware
  2. If this object is immutable, per requests, it's better to keep it in memory. If no - depends.
  3. In any case workflow open connection to db - fetch data - return result - free data will consume more resources than caching in memory.

For example, in our project we processing high definition images, and keep all objects in memory - 3-7mb in raw format. Tests shows that this is much efficient than usage of any caching systems, such as redis or couch base.




回答2:


I would keep the most recent version as memory object, and store it as well. That way you have a backup if anything crashes. If you edit the file however, I would only keep it as database object.

Accessing the DB for that object every 2 seconds would probably work fine, but 1.2MB of memory is not that much and if you can keep that contained, your server won't likely run into problems.

The DB is a little slow compared to memory, but has the advantage to (most likely) be thread-safe. If you would edit the document, you could run into thread problems with a memory object.

You know the application and the requirements, you should be able to tell if you would need a thread-safe database, or if you need to safe your memory on the server. If you don't know, we need to see the actual code and use-cases to tell you what you could do best.



来源:https://stackoverflow.com/questions/38762017/node-js-store-object-in-memory-or-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!