The question is only about the web server. Regarding decentralization, replication, I have some idea of ​​reducing the load.

Situation: A user comes to the web server (many users), does something ... and gradually the load rises.

From the user comes a request to the server, but since the server has reached the maximum load (at which it will be distributed), this request will be redirected to another machine (another server). BUT after redirection of the salary to another computer, the client (user) will receive another session identifier.

Session on the first server will become irrelevant ( ? )

How is this problem solved?

    2 answers 2

    If your workload is so high that it requires several application servers to serve visitors, then storing the sessions regularly on disk is extremely unreasonable - you will jerk your hard drive too often - it is better to place the sessions in RAM. Usually, in this case, they resort to storing them in some NoSQL-storage, for example, memcached, for which it is nice to allocate a separate server (but first you can place it on one of the current servers).

    To transfer sessions from disk to memory, it is enough to install the memcached server, the PHP extension and designate the memcached server as session processing in php.ini in the [Session] section

    [Session] ... session.save_handler = 'memcached' session.save_path = 'mem00.domain.com:11211' 

    If one memcached does not cope, then you can enter several nodes, the extension will automatically calculate on which of the nodes is located in the current user session

     [Session] ... session.save_handler = 'memcached' session.save_path = 'mem00.domain.com:11211, mem01.domain.com:11211' 
    • I do not even have the site code added. I ask for the future to more or less do the right thing. In order to have fewer problems in the future. - root_x Povierennyy
    • here is ru.stackoverflow.com/questions/541023/… - root_x Povierennyy

    You have two options - either put the storage session in a separate layer (a good option, described in the next answer), or put a smart balancer (eg haproxy) at the entrance and send users to a specific server according to the value of a cookie (cookie itself does not say which server the user will land on, it simply participates in the mechanism for calculating the final server) so that the session is stored there. The second option is very cheap, but at the same time very unstable (any change in the cluster will cause the entire system to "go"), so it can be used only for the period when the server no longer holds and you need to add a second one, and Session transfer not yet. Well, the option remains with vertical scaling, which you probably enough for the next projects.

    In an amicable way, the PHP session mechanism in loaded projects should not be used.

    • "In an amicable way, the PHP session mechanism itself in loaded projects should not be used." - intrigue :). I will look, I will read, I will try to understand. - root_x Povierennyy