Building A Scalable Laravel Application
Tags: laravel, howto
Published: 2023-05-31 05:21:30

---
title: Building A Scalable Laravel Application
description: Looking forward to scaling your laravel application. This is just the perfect blog to get you started on that journey.
edition: 31 May 2023
created: 20230531
published: true
tags: Laravel, howTo
cover_image: https://cleavr.io/images/multi-server-architecture/banner.png
---

## Introduction

In scenarios where independent developers are working on an initial project that begins small and gradually scales over time, it is common practice to deploy applications on a single server. This means that all components of the application,such as the main application, database, queue, and others, are hosted on a single server.

Deploying on a single server is cost-effective for smaller projects and indie hackers who may have budget constraints as it eliminates the need for additional infrastructure, making it a cost-efficient solution during initial stages. Additionally, deploying on a single server simplifies the development and debugging process since having all components within a single environment makes it easier to monitor and troubleshoot issues during.

However, as the user base grows, this approach can impose a heavy load on the server, leading to application slowdowns, increased risks like single point of failure, maintenance and upgrade challenges, and potential security vulnerabilities.

To address these challenges, developers recognize the need for a multi-server architecture. By distributing the application across multiple servers, the workload can be balanced, ensuring better performance and availability.

Overall, the multi-server architecture provides scalability, performance optimization,fault isolation, flexibility, security, and easier maintenance. It is a proven approach for building complex applications that need to handle high loads, store large amounts of data, and provide a reliable user experience.

By the end of this blog, you will have a solid understanding of how to deploy a multi-server architecture in [Cleavr](https://cleavr.io). So, let's dive in and explore multi-server architecture deployment in Cleavr.

For demonstration purposes, I will be using [Feedmas](https://feedmas.com), a platform we built at LittleBets to manage our customer support requests. We wanted Feedmas to have a dedicated database and queue server so we could provide
a scalable and reliable solution. To accomplish this, we deployed Feedmas using Cleavr to setup and manage the multi-server architecture.

Feedmas consists of a main application server, a [PostgreSQL](https://www.postgresql.org/) database server, and a [Redis](https://redis.io) queue server. The main application server will host the primary application logic, while the PostgreSQL database server will be responsible for storing and managing the application's data. The Redis queue server will be used for task management, message queues, and other related functions.

Here is a summary of what I will be covering in this blog:

- [Introduction](#introduction)
- [Provisioning servers](#provisioning-servers)
- [Create a site](#create-a-site)
- [Link Servers](#link-servers)
- [Sync Environment Variable](#sync-environment-variable)
- [Add deployment hooks and deploy](#add-deployment-hooks-and-deploy)

## Provisioning servers

First, I will provision three [servers](https://docs.cleavr.io/provision),

1. App Server ( Rabin Feedmas )
2. Queue Server (Rabin Feedmas Queue)
3. Database Server (Rabin Feedmas Database)

![Servers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9569s96wf0skb4e18cus.png)

## Create a site

Once the servers are provisioned, I will head back to the **App Server** and create a [Laravel site](https://docs.cleavr.io/guides/laravel/). For this, I will go to the **Servers** page and click on the **Feedmas** server and then click on the **Add site** button.

![Site](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zfdsi3dd9svd6wqo7uyl.png)

## Link Servers

To organize & maintain all three servers, I will go to **Deployments > Networks** and click on **Link Servers**. Once the servers are linked we can organize the respective hooks for each server and we don't have to worry about triggering individual deployments in each server. Also, it allows us to sync environment variables between multiple servers.

![Link Servers Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c2c7v62h8hq8mwy5a1sv.png)

![Link Servers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xape8d8dfewzcf3o5zip.png)

![Servers Network](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q5hqhpxkoquqbse20cwq.png)

## Sync Environment Variable

It might be the case that multiple servers may share the same environment variable. So, to link those environment variables, Cleavr allows syncing environment variables between multiple servers. In our case, **App** server and **Queue** server share the same environment variable so I will sync them.

To sync them, I will go to **Deployments > Environments** and after updating the environment file, I will click on the **Sync** button.

![Sync environment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3yrfpnctu87f9kqfa42.png)

Now, environment variables for both **Feedmas** and **Queue** Server are synced.

## Add deployment hooks and deploy

Deployments hooks are located in **Deployments > Deployment hooks**. Make sure your deployment hooks are linked to the correct servers. For example, to update the **Copy Project** hook, click on the ellipsis icon and click on the **Edit hook**.

![Copy hook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oyyzkk4lpl132jcgjlpx.png)

I am going to copy projects on two servers, **App** server and **Queue** server.

Also, you can add your own custom hook. For example, in our application we need to restart the queue in our Queue server. To do so, I will go to **Deployments > Deployment hooks** and click on **Add Hook**.

![Add hook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gyxpvc89zxruppkb0ydn.png)

You can also select the servers where you want the hook to be triggered. Similarly, you can edit other hooks accordingly. Make sure your hooks are placed in the correct order and assigned correctly to the right servers.

![Deployment hooks](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7dhbxna6q4lkpi9c1xi6.png)

Ok,so now deployment hooks are all in place. It’s finally time to deploy the application. To do so, I will go **Deployment > Recent Deployments** and click on **Deploy**.

![Final Site](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w6jzi4tgebtqvgbf9k0k.png)

Hurrah! Now, our Feedmas app with multiple servers is finally deployed. If you have a multi server architecture or you are trying to scale your application,Cleavr makes it a breeze in managing these servers.

- [Rabin Dhamala](https://github.com/rabinkmc)