Smart sessions

    We solve performance problems.

    Initial data.
    We broadcast football matches over the Internet. Visitors: on ordinary days 5,000 - 10,000, on match days 100,000 - 150,000.

    In the data center
    • 5 web servers with Apache and PCP posted outside through a hardware load balancer
    • 2 Memkesh pools: for sessions and data from web services

    Problem
    With a large influx of visitors, the local network is overloaded due to the large number of calls to the memkesh. Aggravating factors : 100Mbit network, both pools on the same servers.

    Solutions
    • Network expansion up to 1Gb after the end of a series of matches
    • Increasing cache storage time (as a temporary measure)
    • Using smart session technology

    Today we’ll talk about the technology of smart sessions, which we used to reduce the load.

    What is the essence of technology

    1. We do not create a session if it is not really needed.
    2. We do not update the session in the repository if it has not changed.

    In fact, we have a paid site, even if we have 150,000 unique per day, there are really few people willing to pay. Visitors are distributed approximately as follows:
    • 70% - Gawkers. They opened the main page, realized that not what was needed, closed. That is, SEO worked well, but we are raking.
    • 28% - Fans. There is no money, but use our website to follow the information during the match (goals, substitutions). Usually they are not registered, because information is available as well.
    • 2% - Really watch matches by registering and paying.


    Implementation

    Since we use memkesh, we already have a Session object, which overloads the standard functionality of sessions and allows us to store the latter in the memkesh. Additionally, we do the following:
    • We put the global session_start () in if so that the session is not created when not asked.
      if(!empty($_COOKIE[ini_get('session.name')]))
      session_start();
    • In the place where the user is logged in, add the start of the session, because here it is already necessary:
      if(empty($_COOKIE[ini_get('session.name')]))
      session_start();
    • And finally, in our object, create an additional static variable, where we store the contents of the session when reading, and when writing, do the following check at the very beginning:
      if(self::$data !== null && self::$data == $currentData)
      return strlen($currentData);


    Voila, partially the load on the network is reduced, on the memekes too. Then they switched the caching of some of the blocks to local web servers and were able to safely wait for the decline of visitors, after which they increased the network bandwidth.

    Comments are welcome.

    UPD : The essence of the post is not to show how cool I am, but that there are trifles in PCP that are so mundane that we stop paying attention to them, and sometimes they are not too thought out and affect performance.

    Also popular now: