PHP Sessions in a Load Balancing Cluster - How

PHP sessions in a load balancing cluster - how?

You could set PHP to handle the sessions in the database, so all your servers share same session information as all servers use the same database for that.

A good tutorial for that can be found here.

Handling $_SESSION object in a load-balanced setup, PHP

Look into either memcache (Is it recommended to store PHP Sessions in MemCache?) or REDIS (https://joshtronic.com/2013/06/20/redis-as-a-php-session-handler/).

There is a good tutorial on setting up memcache on Ubuntu at https://www.globo.tech/learning-center/php-memcached-instances-ubuntu-16/. Which also covers using haproxy as a load balancer (although you may already a solution).

Perhaps have a read of https://blog.newtonhq.com/session-handling-for-1-million-requests-per-hour-68cdece15030.

How To maintain Session over Load Balancer?

i can think of these two methods for this purpose.

  1. Use a clustered web application server where the session are available for all the servers
  2. Use IP level information to maintain affinity between a user and a server

What is the best way to handle sessions for a PHP site on multiple hosts?

Database, or Database+Memcache. Generally speaking sessions should not be written to very often. Start with a database solution that only writes to the db when the session data has changed. Memcache should be added later as a performance enhancement. A db solution will be very fast because you are only ever looking up primary keys. Make sure the db has row locking, not table locking (myISAM). MemCache only is a bad idea... If it overflows, crashes, or is restarted, the users will be logged out.

Are there problems using PHP sessions in a server cluster?

Yes this is possible, you need to store your sessions in a central location like a database though. This is pretty simple and just requires you to make some changes to session_set_save_handler - there's a good example of the process you need to follow here



Related Topics



Leave a reply



Submit