user1768685
user1768685

Reputation:

Where to store consistent JSON, Redis or global variable?

It's been a while that i am using node for my applications and i was wondering where a global or local variable is stored? (in RAM or CPU cache maybe. guessing RAM. right?) and is it a good idea to store some JSON's that are most of the times static as a global variable and access them right away.

would it be faster than Reading from in-memory database like Redis?

For example let's see i am talking about something like website categories list which is a JSON with some nodes in it.

Most of the times this JSON is constant and even if it gets changed i can refresh the variable with new value because one server app handles all requests.

And when node app starts i can have this initializer function that reads the JSON from in-disk database.

Currently i am using Redis for this situation and when app starts i'm reading this JSON from mySQL and keep it in redis for faster request handling.

But i'm wondering is it a good practice to keep JSON as a global variable and how would it be compared against having it in Redis performance wise?

P.S: I know redis has consistency and keeps value in disk too but i am reading them from mySQL because redis is a caching mechanism for a small part of schema and using initializer gives me a manual sync if needed.

Thanks

Upvotes: 2

Views: 2357

Answers (2)

Dhaval Chaudhary
Dhaval Chaudhary

Reputation: 5815

I would prefer Redis. Because even if you restart node application data will be there and putting global variables in memory has one disadvantage that at run time if you want them to be changed you are just left with choice of restarting whole application.

Plus while running application you should always query Redis to get data whenever you want.So in future if you want these values to be dynamic it will directly reflect by just changing it in Redis.

Upvotes: 1

Abhyudit Jain
Abhyudit Jain

Reputation: 3748

You can keep it anywhere you want. You can store them as files and require them while starting your app. I'd prefer this if they do not change.

If you update them, then you can use any database or caching mechanism and read them. It's up to you.

Yes, the variables are stored in memory. They won't persist if the app crashes. So a persistent storage is recommended.

Upvotes: 0

Related Questions