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

shawnveltman's avatar

Help with database server connection speed

Hey all,

I feel like I'm probably missing something very stupid here, and hoping somebody can help!

I recently set up a load balancer, separate DB server, separate cache server (ie the amazing Chris Fideo old course https://courses.serversforhackers.com/scaling-laravel because I'm always at least 4 years behind the cool stuff!)

Everything's working beautifully EXCEPT that I've got a 4 second TTFB on the database connection. Last time I faced something like this with a client using RDS, it was because it was configured to use public IP vs. the private one, but I did all this in forge, gave the app servers access to the DB server, and used the private IP in my DB connections variable in the .env file.

It's connecting to the database, it's pulling data, and the DB is on a 16GB server with 13GB innodb_buffer_pool_size... On a monolith server, everything's loading in about 200ms, whereas on this one it's taking 4s to load.

Has anybody faced this recently, and is there something drop-dead stupid that I may have forgotten to do?

Thanks!

0 likes
3 replies
Tray2's avatar

There is always a bit of a slowness when running the database on it's own server, however it should not be that big.

Have you tried fetching just a single record with a primary key?

Something like this

User::find(1);

Is that just as slow or is it as fast as you expect?

If that is still slow, I suggest checking that all of your servers are in the same area, so you don't have the web server in Asia and the database in Europe and the load balancer in the US.

If it's fast then I suggest using a tool like Debugbar or Clockwork to see if there is something with your queries.

n+1 issues, high memory usage or slow execution time due to bad or missing indexes.

1 like
shawnveltman's avatar

@Tray2 - thanks for the quick response, and for reminding me that any time I"m assuming, I'm probably wrong... :)

Same TTFB issue without any DB queries - so the issue isn't (or isn't necessarily) the connection to the database, but rather the load balancer itself.

Now to try to track down what's going wrong there... :)

ETA: It definitely seems to be a laravel specific / laravel loading issue.

I removed the load balancer, and connected to the server directly.

When I load the robots.txt file (ie skipping Laravel), it's got a 150ms time. When I try to load a route that just returns "Hello" (it touching Laravel), it's up to 3 seconds.

This is pretty worrisome! No idea what's causing such a delay.

shawnveltman's avatar

Tracked it down to the weirdest thing - we've been using the Blade UI Kit - and some of their packages slow the system WAY the hell down.

Specifically I had 2 from the "codeat3" provider ("codeat3/blade-phosphor-icons": "^1.4", "codeat3/blade-google-material-design-icons": "^1.8") - removing those took the site from a 3-4 second TTFB to sub 300ms.

It also halved my test run speed for the 1200 tests in the project (from 6 mins to 3).

Insanity.

Please or to participate in this conversation.