1

I have a setup where I have a host which routes multiple requests in a load-balanced fashion. My backend uses PHP. Now, I need to use the $_SESSION object for some of my processing.

Will $_SESSION work where I have 3 backend servers which can receive any request at any time?

If not, Can one suggest alternatives to handle such cases?

EDIT: I do understand that we can store sessions in a database and find a way to track it. But, the problem in a realtime load-balanced production scenario is the number of calls that go into a DB. That can be a real bummer for my performance. I'm kind of hoping that, we can handle this at an webserver level.

Not sure, if it is possible, but, if two webservers have some kind of replication mechanism like databases do, it will be brilliant. I dont have to do a thing.

If such a thing does not exist, PHP should be modified to support it. That will actually, make it a seriously robust language.

Umashankar Das
  • 601
  • 4
  • 12
  • 3
    Possible duplicate of [PHP sessions in a load balancing cluster - how?](https://stackoverflow.com/questions/994935/php-sessions-in-a-load-balancing-cluster-how) – Stevie G Jun 07 '17 at 06:34
  • 1
    Look into either memcache (https://stackoverflow.com/questions/13946033/is-it-recommended-to-store-php-sessions-in-memcache) or REDIS. Perhaps have a read of https://blog.newtonhq.com/session-handling-for-1-million-requests-per-hour-68cdece15030. – Nigel Ren Jun 07 '17 at 06:44
  • This is a very good reference @NigelRen . If you could put this as an answer, and make tailor it to the load-balanced scenario. I will accept it as an answer. Thank You. – Umashankar Das Jun 07 '17 at 06:48

3 Answers3

2

My Suggestion is to setup PHP to handle the sessions in the database (this way they can all access the session data independent of which server is requesting it).

A good tutorial for that can be found HERE

Stevie G
  • 5,638
  • 1
  • 10
  • 16
  • Thank you for the suggestion. I understand that when we have consistency issues, it makes sense to store a reference in a storage device aka the database. But, PHP Session provide more flexibility in utilisation, not to mention lesser database calls which can be a bummer in a realtime load-balanced production scenario. Hence, I'm looking at a slightly different webserver level solution. Will update the question with this. Thank you very much for your suggestion. – Umashankar Das Jun 07 '17 at 06:41
1

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.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

You can store sessions in memcache in order to share session among servers.

Please have a look on documentation here

Dileep Kumar
  • 1,077
  • 9
  • 14