1

I am learning how to use socket.io and nodejs. In this answer they explain how to store users who are online in an array in nodejs. This is done without storing them in the database. How reliable is this?

  1. Is data stored in the server reliable does the data always stay the way it is intended?
  2. Is it advisable to even store data in the server? I am thinking of a scenario where there are millions of users.
  3. Is it that there is always one instance of the server running even when the app is served from different locations? If not, will storing data in the server bring up inconsistencies between the different server instances?
YulePale
  • 6,688
  • 16
  • 46
  • 95
  • 1
    I think the answer is intended to be used as an *example* that just shows how to do the connection thing. It doesn't recommend storing stuff inside an array always - it's simply that adding a database would overcomplicate the example for little to no benefit. – VLAZ Aug 15 '19 at 11:42
  • @VLAZ Just to be clear, in production one should store the data in a database? – YulePale Aug 15 '19 at 11:46
  • 2
    All variables in your program including arrays, objects and strings are stored in RAM. When your program exits the data is not saved anywhere (not to mention when you turn your computer off). You need to store long-term data on disk either via some database server or to a file or a library that treats a file on your disk as a database (for example SQLite) – slebetman Aug 15 '19 at 11:49
  • 1
    Yes, in short "do not store data in memory". Although from there this can grow and be distorted - databases are definitely an option but you could just write the data to a file and still have a persistent copy of it when the machine shuts down. Also, you could actually have a system that *does* story data in memory for speed of access. However, those systems almost always also save a copy of the data into some persistent storage (e.g., database). Still, memory *alone* is not good enough. You already touched upon why but the simplest reason is turning the server off and on again wipes the data. – VLAZ Aug 15 '19 at 11:52

1 Answers1

3

Congrats on your learning so far! I hope you're having fun with it.

  1. Is data stored in the server reliable does the data always stay the way it is intended?

No, storing data on the server is generally not reliable enough, unless you manage your server in its entirety. With managed services, storing data on the server should never be done because it could easily be wiped by the party managing your server.

  1. Is it advisable to even store data in the server? I am thinking of a scenario where there are millions of users.

It is not advisable at all, you need a DB of some sort.

  1. Is it that there is always one instance of the server running even when the app is served from different locations? If not, will storing data in the server bring up inconsistencies between the different server instances?

The way this works typically is that the server is always running, and has some basics information regarding its configuration stored locally - when scaling, hosted services are able to increase the processing capacity automatically, and handle load balancing in the background. Whenever the server is retrieving data for you, it requests it from the database, and then it's loaded into RAM (memory). In the example of the user, you would store the user data in a table or document (relational databases vs document oriented database) and then load them into memory to manipulate the data using 'functions'.

Additionally, to learn more about your 'data inconsistency' concern, look up concurrency as it pertains to databases, and data race conditions.

Hope that helps!