Search code examples
laravelamazon-web-servicesautoscalingelastic-load-balancer

Laravel Application & Load balancer


I have hosted my application on AWS cloud and a load balancer is running on top of two instances which is being served by Nginx on top of Php7.0-fpm. Let's say that my application downloads a file and stores it locally, so that the contents can be served to the customers. With an auto scaling group, configured for two instances;

1) If my session begins with instance-1 where my file gets downloaded, and suddenly switches over to instance-2, will I be getting the same content?

Or

2) If a session is created on a single instance, will the same instance be used until I log out of my application?

Any help is much appreciated!!


Solution

  • For a website with more than 1 instance, which is load balanced, it is highly recommended that you store cache and sessions in 1 place and not multiple across them. For this, you can install memcached on all servers and configure them to point them to 1 server to store it all.

    SESSION_DRIVER=memcached
    CACHE_DRIVER= memcached
    MEMCACHED_HOST=127.0.0.1 #on your memcache server, point to localhost
    MEMCACHED_HOST=10.10.1.10 #on other instances, point to memcache server
    MEMCACHED_PORT=11211
    

    For file and image uploads, use S3 from AWS or dedicated storage server with FTP to store so that all the servers can access it directly and the same way. Easiest and most efficient :)

    If you store them locally, your servers won't be synced with the same content, and your users will end up with 404s.