Large Scale Laravel 5.2 – Problems, Tip and Tricks

Table of Content

When we develope PHP project, we care about logic, functional and the site features. But when we go production, especially with many users, the story will be very diffirent.

Today, i will tell about problems when you make a website with Laravel 5.2, which have requirement must support about 30K CCU (website must run ok even have about 30K users are accessing on it same time).

Example case.

Let’s start with a very simple website build in Laravel project : Players of one GMO game can go to website to login then choose their correct server to submit, and we check if they not get gift yet, we give them a gift on game.

  • We have an index page which have details about gift, with a link call “Get Gift”.
  • When user click to a link, we ask them login and redirect them to a form.
  • This form allow user to choose their server which they current play on and submit the form.
  • Once they submit, we check in our database, if user not get gift yet, we call Game API to give gift to user and then insert in our database.

Local Code before going production.

With the requirements and designs above, let create some simple code on Laravel 5.2

As you see above, i wil setup a new fresh Laravel 5.2 project on local environment, and using IDE Helper and Form/Html packages.

And install Redis Client composer require predis/predis.

If every is success we will have a website run on local : http://local.example-gift.com.

app/Http/routes.php :

resources/views/index.blade.php :

app/Http/Controllers/GiftsController.php :

app/Gift.php :

resources/views/gift.blade.php :

Migration script for Model Gift :

Ok so now we have a working project which meet the logic requirements.

We test in on our Local environment and see it working very good.

Now the time we deploy it on production.

What happened in production environment?

Normal design for an production application

When setup the environment in production, we normally using Mysql Replication for Database Scale and Round Robin Load Balancer for Web.

I wil setup the example of production using Docker on my local computer.

Setup Production Environment on Local using Docker.

First we build an Docker image on our Local Computer by create Dockerfile below :

In the file above, we need nginx-vhost.conf :

and supervisor.conf :

Ok now we build a Docker image on Local :

When command success, we will see image there :

Run an test container to see everything working well :

Browser to http://localhost:8000/ to see if this working.

Create Docker composer file docker-compose.yml :

As we see above, it contains a Load Balancer using Tutum HAProxy, 2 Web using Nginx + PHP FPM , Mysql Master and Slave for Replication and Redis.

Why we need Redis?

We using Redis for Laravel Session Driver, because when using Round-Robin Load Balancer, user may loss their session if they come to wrong server. To avoid that we using Laravel Session Redis.

Run docker-compose to create container :

Now we check if our Mysql Master Server and Slave work well or not :

It seems ok now, we have database example_gift in both servers.

Database design

The normal way to setup MySQL on production is Mysql Replication – which using 2 server :

  • Master server can read and write database to.
  • Slave server which get sync database from Master and using to read only.

Because of that, we need change abit on our code as :

.env :

we change config/database.php follow the construction on Laravel Database Read/Write Configuration

to

Ok now we can login to web1 in our “Docker Production” to run migrations :

Now go to browser http://localhost:8000 every will work wel as normal.

Next step we will make a huge records on database and using problem to load huge amount of users to our website.

To be continue…

Leave a Reply

Your email address will not be published. Required fields are marked *