Answer the question
In order to leave comments, you need to log in
How to have a single array of user sessions when parallelizing a nodejs application across different servers?
Previously, when there were few requests to the application, everything worked on one server, and user sessions were simply stored in environment variables. But now the load has quintupled and the question of scaling has arisen. What method will be the fastest in terms of access from servers to a single user data?
Answer the question
In order to leave comments, you need to log in
The best thing to do is to keep sessions and shared state (like the game world model or the state machine used to manage the industrial automation object, etc.) in RAM. Sessions are only in one process, and the shared state is in each process. In order for the client to always connect to the process in which its session lies, the connection must be signed with a special cookie with the process number, this cookie can be set during authentication and after that, the network router or reverse proxy will transmit all requests marked with this cookie, in the right process. But processes die periodically, so sessions need to be saved in the database, but it's better to do it in lazy mode, i.e. not during an HTTP request from the client, but to put a periodic event and reset it from memory to the database, and have a recovery mechanism from the database. If the user's process dies, then at the first next request, the state will be restored and then it will be taken from memory again. You can do a read-ahead read, if the process dies, then store in the database for each process an array of users whose state it processed, and immediately after restarting, the process will restore all sessions from the database and will wait for "its" users. As for the state common to many users, it can be synchronized between processes using a simple API that sends a log of state changes from the process in which they occurred to all others using IPC, HTTP or ZeroMQ. An example of such an API is here: You can do a read-ahead read, if the process dies, then store in the database for each process an array of users whose state it processed, and immediately after restarting, the process will restore all sessions from the database and will wait for "its" users. As for the state common to many users, it can be synchronized between processes using a simple API that sends a log of state changes from the process in which they occurred to all others using IPC, HTTP or ZeroMQ. An example of such an API is here: You can do a read-ahead read, if the process dies, then store in the database for each process an array of users whose state it processed, and immediately after restarting, the process will restore all sessions from the database and will wait for "its" users. As for the state common to many users, it can be synchronized between processes using a simple API that sends a log of state changes from the process in which they occurred to all others using IPC, HTTP or ZeroMQ. An example of such an API is here: then it can be synchronized between processes using a simple API that sends a log of state changes from the process in which they occurred to all others using IPC, HTTP or ZeroMQ. An example of such an API is here: then it can be synchronized between processes using a simple API that sends a log of state changes from the process in which they occurred to all others using IPC, HTTP or ZeroMQ. An example of such an API is here:https://github.com/tshemsedinov/impress/blob/maste...In short, it has the functions inc(path, value, delay); dec(path, value, delay); set(path, value, delay); get(path); delete(path, delay); etc. here path is the path to the data in the state object, for example inc('myCar.speed', 15, "2s") - increase the speed of the car in the model by 15 with guaranteed synchronization between processes within 2 seconds (this is in order to reduce number of transfers of changes, they will stick together within 2 seconds). All inc/dec changes are additive in any order, so they can be sent as you like, and in another process you can subscribe to the change subscribe('myCar.speed', function(path, value, remote) { ... }); where path is 'myCar.speed', value is the new value, remote is true if the change is from another process. I think that you will find more suitable implementations, but the example should be clear.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question