Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling laravel app horizontally?

As my laravel app will be deployed to heroku, I am wondering how to avoid session affinity so that any node can handle the user request.

As I know, the server who served the authentication for the first time, will store auth-token in a session to identify the user later, but, what about adding new nodes to scale the app, would the user has to be served using the same server that has the auth-token stored? how to avoid such scenario in laravel?

like image 425
simo Avatar asked Oct 29 '25 22:10

simo


1 Answers

If you want to scale horizontally, you first need to make your web app stateless, which means that you need to store user session & auth info centrally somewhere else instead of storing locally on each server. Redis servers would be the best choice as mentioned by @Amir Bar, since it's a data-structure server (which was used commonly for caching), all data stored on Redis is stored in common data structures (list, hashtable...) on RAM, thus its latency would be exceptionally low.

Once your web app is stateless, just use a load balancer to distribute the load, and then add as much web server nodes as needed behind the load balancer. That would be enough.

Your next challenge after scaling web server would be the database server scalability. You can add as much web server nodes as you want behind the load balancer. But scaling database is another beast. If you're using NoSQL, then congrats! Since NoSQL database is very easy to scale, the horizontally scaling feature is built-in in almost every NoSQL database. Scaling relational database would be harder than scaling NoSQL database. If you're scaling for high-read system, Master-slave replication model would be appropriate and easy. But if you're scaling for a both high-read and high-write system. Hope you will have fun time with your solution research. The solution would be based on your current design.

Anyway, when you reach the database read/write bottlenecks, try to optimize your queries and database access first, N+1 problem is a very common issue that will greatly slow down your database access.

like image 100
Dat Hoang Avatar answered Oct 31 '25 13:10

Dat Hoang