Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lat4732's avatar
Level 12

Dealing with database cluster

Hey, everyone!

I have another request from my client, which this time reads ... He wants us to optimize the database in some way. A very sharp increase in database requests is expected and such optimization is necessary. The client is aware of the manageable databases (clusters), but we do not know exactly how things work, in combination with Laravel. The question is this - when we create a connection with the cluster in Laravel, should the cluster itself be configured by digitalocean or it is at Laravel level? In general, what is the best way to build a database? It must be on an external server - that's clear. But how to setup this cluster to work the best way with Laravel?

0 likes
8 replies
Snapey's avatar

if you are already with DO then go with their managed database so that you can easily scale it vertically

scaling horizontally is complicated as you have to start to split read and writes to different nodes

1 like
lat4732's avatar
Level 12

@Snapey Any other suggestions? What is actually vertically/horizontally scaling?

Snapey's avatar

vertical scaling is adding more grunt to your server(s), ie, more ram and more processors

horizontal scaling is adding more servers.

Far simpler to just get a bigger box until that either becomes economically disadvantageous or because you want to horizontally scale to improve resilience.

https://www.digitalocean.com/products/managed-databases

1 like
lat4732's avatar
Level 12

@Snapey So what you're suggesting? To start with vertical scaling until that becomes economically disadvantageous and then scale horizontally?

Snapey's avatar

@Laralex Depends where you are starting from. If your database is currently 1cpu and 2GB ram then you have a lot of room for vertically scaling.

If your database server is already 4cpu and 16GB ram then horizontal would be more appropriate

lat4732's avatar
Level 12

@Snapey Okay, but I have another question. Do I need to setup something on the code when changing to cluster? Is it that simple as just pay the cluster and connect Laravel with it through the .env file? Because currently we are on a simple PostgreSQL database hosted on the same server as the app. And if I need any preparation for migrating to the cluster, I should do it from now on. Also.. When the time comes to decide how to scale the server, if we decide to scale it horizontally, how does this happen? How does Laravel communicate with both servers? These 2 servers actually record the same information in real time, right?

The specific and most important question:

Do I need to prepare something for the cluster migration? Or I can simply do

...
DB_CONNECTION=pgsql
DB_HOST=MANAGED_PGSQL_HOST
DB_PORT=MANAGED_PGSQL_PORT
DB_DATABASE=MANAGED_PGSQL_DB
DB_USERNAME=MANAGED_PGSQL_USER
DB_PASSWORD=MANAGED_PGSQL_PASSWORD
...
Snapey's avatar
Snapey
Best Answer
Level 122

@Laralex No, both servers don't record the same data. Its important to have one as the 'master' and then replication will push changes to the others (note master and slave are no longer politically correct). Reads happen from the other members of the cluster.

You have a lot of way to go before you need to worry about horizontal scaling. Note that doubling the number of servers will not give 2x the performance

This article is mysql specific but you will get the idea about splitting read and writes. But I would not go this route until you are spending $1000 per month on your database server https://devdojo.com/bobbyiliev/scaling-laravel-app-with-multiple-databases

1 like
lat4732's avatar
Level 12

@Snapey I learned a lot. If I have more questions I will reply again. Thanks!

Please or to participate in this conversation.