NodeJs服务器上存储数据靠谱吗?

Is storing data on the NodeJs server reliable?

我正在学习如何使用 socket.io 和 nodejs。在 this answer 中,他们解释了如何在 nodejs 中将在线用户存储在数组中。这是在不将它们存储在数据库中的情况下完成的。这有多可靠?

  1. 存储在服务器中的数据可靠吗?数据始终保持原样吗?
  2. 是否建议将数据存储在服务器中?我在想一个有百万用户的场景。
  3. 是否总是有一个服务器实例 运行 即使应用程序是从不同的位置提供服务的?如果不是,将数据存储在服务器中会导致不同服务器实例之间的不一致吗?

恭喜你到目前为止的学习!我希望你玩得开心。

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

不,将数据存储在服务器上通常不够可靠,除非您对服务器进行整体管理。使用托管服务时,永远不要将数据存储在服务器上,因为它很容易被管理服务器的一方擦除。

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

完全不可取,您需要某种数据库。

  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?

这种工作方式通常是服务器始终 运行,并在本地存储一些有关其配置的基本信息 - 在扩展时,托管服务能够自动增加处理能力,并处理 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) 然后将它们加载到内存中以使用 'functions' 操作数据。

此外,要了解有关您的 'data inconsistency' 问题的更多信息,请查看 并发性 因为它与数据库有关,以及 数据竞争条件 .

希望对您有所帮助!