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

mikevrind's avatar

Memory usage

I recently built a webshop with L5 and last weekend the (Apache) webserver ran out of memory because of a spike in the number of visitors (from 9 to 200 visitors in just half an hour).

The hosting company suggested to increase the RAM on the server with 2G (why wouldn't they advice to spend more money? :) ) But I'm just curious about your opinions about the memory usage. Is it out of control (and I should be ashamed as a webdeveloper) or is 4G on a webserver just to low?

For example, a page that shows an overview of 12 products, pagination and a list of custom filters (which can me managed via a CMS) uses 90 queries and 24,75MB of memory (based on Barry's debugbar) and takes 653MS to load.

And a random product pages uses 50 queries, 23.75MB of memory and takes 417MS to load.

Maybe a lot of queries, but is this memory usage just to high and should I invest some time to optimize?

0 likes
9 replies
toniperic's avatar

What kind of hosting are you having? If it's a shared hosting, it might not even be your fault.

4 gigs of RAM should be more than enough for 12 products and orders, but still it depends on how you might have coded everything. Use Clockwork or similar tool to find out what's taking so long. Perhaps there are some slow queries.

1 like
bashy's avatar

Apache ran out of memory or PHP? Have you got any logs to show this increase or maybe they showed you a graph?

1 like
mikevrind's avatar

It's a VPS (currently hosting about 25 websites). I'm not familiar with Clockwork (Can't even find it online :)). And there are 12 products per page, in total there are 324 products at this moment.

And according to the company hosting (and managing) the server, Apache used up all the RAM on the server. So maybe saying that Apache ran out of memory wasn't the appropriate description of what happened.

I just did some query optimization and the product overview page went from 90 to 19queries and the product page went from 41 to 23. Apparently the default parameter on the Cache::get() method is always fired (which contained the method for quering the database). But this doesn't really lower the memory usage (about 1 MB) and offcourse the debugbar itself uses some memory. I could apply some more caching to other queries, there are just 2 queries slower then 1ms.

What more could I do to lower the memory usage?

toniperic's avatar

Well if you're hosting 25 websites, couldn't some of the other 24 websites be the reason why 4 gigs of RAM are insufficient?

1 like
mikevrind's avatar

It could be. But the downtime of the server matches with the time of the spike in the number of visitors of this webshop. And before the launch of this shop (earlier last week) these problems never occurred.

bashy's avatar

Apache is known to use a lot of RAM when the requests build up, Nginx could be a better solution since it manages workers

1 like
sitesense's avatar

Is Apache using mod_php?

php-fpm (fastcgi) can drastically reduce the memory usage of each Apache process.

As bashy says though +1 for Nginx

1 like
ddutra's avatar

I had to deal with spikes in traffic a couple years ago. One of my websites was featured on brazilian's sunday night biggest tv show.

Thats when I dumped apache for good and installed nginx and php5-fpm.

Also something I strongly recommend if you really really need to squeeze out of this server, but you will have to make some planning and learn a new configuration language, is to put a cache server in front of nginx. That would be Varnish. For common websites (Wordpress, Joomlas) its easy, for your online store you will have to really put some effort on the cache rules to make the most out of it, but if you cache 70% of your hits on memory and serve them with Varnish, that means 70% less hits on the backend. As long as the user dont put anything on the Basket you are fine (Varnish != cookies). Or you if really like the ideias you can go the ESI route and cache only parts of your online store. That woudnt be that hard using Laravel blade system.

1 like
mikevrind's avatar

I'm afraid we can't just swap Apache for Nginx on this server. DirectAdmin is running on this server, which also installed Apache and PHP. I don't know if one can just replace Apache with Nginx without negatively effecting all other websites that are currently running. But I also agree with @bashy. On some other servers we are running Nginx because of performance reasons. These servers are used for other clients / projects. The difference in memory usage (among other) is just amazing! Apache is using mod_php5, but isn't using php-fpm.

I will try to add some more caching to the webshop. There are still some queries that are executed (unnecessary) on each page. Thanks for the tips everybody!

Please or to participate in this conversation.