Scaling Laravel with Serverless Redis

Created May 9, 2022


Laravel is a popular PHP framework for building scalable, high-performance web applications.

In this article, we will learn how to use serverless Redis to scale Laravel applications by storing the Laravel session and cache data in a serverless Redis instance.


Before you get started, you'll need to have the following:

Architecture Overview

Rather than running Laravel on a single server, let's consider the following scenario:


Scaling Laravel with serverless Redis

What is Serverless Redis?

Serverless Redis is a fully managed database as a service product where the pricing is based on per command, so you are only charged what you actually use.

That way you don't have to over provision your servers, and you can scale your application as needed.

Why Serverless Redis?

By default, Laravel would store the user sessions in files on the web server's disk. That way if the load balancer forwards the user request to a different server, the user session would be lost.

This is why it is important to have a centralized place to store the user sessions and application cache, so that they can be shared between requests and across multiple servers, and not be lost each time the load balancer forwards the request to a different server.

Of course, you can also use your database to store the user sessions and cache data, but for better performance, it is recommended to use Redis for better performance. If you want to learn more about the performance benefits of the different options, check out this great article here: Which is the best Laravel cache driver for performance?.

Horizontal Scaling vs. Vertical Scaling

Just a few words about the difference between horizontal and vertical scaling:

Here is a simple example of horizontal scaling vs. vertical scaling:

Horizontal vs vertical scaling

When horizontally scaling an application, it is important to handle your user sessions and cache data in a scalable way.

Creating a serverless Redis cluster

With Upstash, you can create a serverless Redis cluster in 30 seconds by following these steps:

That's it! You now have a serverless Redis cluster ready to use.

Make sure to note down the endpoint of your Redis cluster along with the password and the port.

Configuring Laravel with Serverless Redis

Now that you have a serverless Redis cluster, you can configure Laravel to use it just as you would any other Redis instance.

Install the Predis package

In the past, you would use the PHP Redis extension to connect to your Redis cluster. However, now you can use the Predis package instead.

To install the Predis package, run the following command:

composer require predis/predis

Next, head over to your Laravel project's .env file and update the following lines:


While changing the Redis details, make sure to also change the cache driver and the session driver to redis:


Finally, clear your config cache by running the following command:

php artisan config:clear

That way your Laravel application will use the serverless Redis cluster to store its cache and session data.


Using Laravel with Serverless Redis is a great way to scale your application. Even if you are running Laravel on a Kubernetes cluster, you can still use a serverless Redis cluster to store your user sessions and cache data in a scalable way.

For more information on Upstash, check their documentation.

For more information on how to scale your Laravel application check out the following article: