Aishwat Singh
Aishwat Singh

Reputation: 4469

Node.js, store object in memory or database?

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

Upvotes: 3

Views: 4002

Answers (2)

Randy
Randy

Reputation: 9849

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.

Upvotes: 1

Nazar Sakharenko
Nazar Sakharenko

Reputation: 1007

  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.

Upvotes: 5

Related Questions