In Node.js, why do I prefer to store key values ​​by application variable?

I am developing a real-time application with Socket.IO support in Node.JS that will be used by several hundred users at any given time, and I need to save some basic information about each connected client (and delete this data when disconnecting the client).

I read that using a key value store (like Redis) is the preferred choice for storing such data.

Why is data stored in a regular variable in the application (an object, for example var connectedClientsData = {}), compared to storing data in a key value store, for example Redis?

Is it only support for scaling (for example, several NodeJS-based application servers can connect to one central keystore), or are there even more serious disadvantages?

+5
source share
3 answers

There are several problems in the game:

1) Yes, scaling. Not only for several servers, but also for several processors through something like a "cluster" (which you can find on npm).

2) V8 has memory limitations currently based on its browser-based legacy. They can be adjusted to some extent and removed more thoroughly in the next version of V8, but this still needs to be considered.

, , , node ( ), redis, .

+4
  • . , . , redis , .

  • - , , . , / , KV, .

  • - KV , . .

  • KV - KV , (, ), , .

+2

, , .

, , . . Redis, , , .

, . , . , - :

var storage = new Storage();
storage.registerClientDisconnection("client_id_1");

Storage.registerClientDisconnection()

Storage.proptotype.registerClientDisconnection = function(clientId) {
    this.info[clientId].connected = false;
};

Storage.proptotype.registerClientDisconnection = function(clientId) {
    var client = redis.createClient();
    client.set("clientId:"+clientId+":connected", false);
};

To summarize: I would recommend using some key store, but I'm sure you can use some object store too. However, if you decide to use an in-memory object, simply draw this implementation detail so that you can easily migrate the key store implementation in the future. The above solution seems to be more important than your decision to implement.

0
source

All Articles