July 10, 2017

Managing Persistence

We have to deal with persistence - user uploaded files, database, cache, and any data that needs to survive when a server is killed. We'll see how here!

We have a question about what to do with application state:

  1. Session storage
  2. Cache
  3. Database


We'll start by installing some server basics just like we did on the other 3 servers:

sudo apt-get update
sudo apt-get install -y git tmux vim curl wget zip unzip htop

Session Storage

The easiest way to handle session storage is to use cookies to store the session data. This puts the storage in users browser, however if we save a lot of state to sessions, this can break - cookies have data limits.

Common state we save to sessions includes form data during validation.

However I typially set session storage to my cache storage, usually Redis.


We need to use a centralized cache server. We'll use our 4th server for this and install Redis onto it.

sudo apt-get update
sudo apt-get install -y git tmux vim curl wget zip unzip htop

sudo apt-get install -y redis-server

Then we can configure our application servers to use this in the .env file:




For the sake of brevity, we'll put our database on the same server as Redis as well.

sudo apt-get install -y mysql-server

Then set it to listen on the private network:

bind-address =

Note that you can still connect to the local mysql over localhost, but not from within this server. We'll also be able to connect to mysql remotely over the private network.

We need to reload MySQL for these changes to take affect:

sudo service mysql restart

Then we can setup the database and user:

# Create a database
create database myapp charset utf8mb4;

# Create a user that can connect over the private network
create user myuser@'172.31.%' identified by 'secret';

# Grant that user to the new database
grant all privileges on myapp.* to myuser@'172.31.%';

And we can ensure our app can use the remote database by updating the .env file on the application servers:


We should be able to scaffold out our authentication now!

php artisan make:auth
php artisan migrate

And then we should be able to register and log into the server!

All Topics