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

tjsherrill's avatar

Load testing controller actions that call services and Redis/DB

I inherited an application that is only busy for 2-3 weeks out of the year. Last year, the server hit 100% CPU for one weekend (I wasn't on the project then), and it went down for 2-3 days.

The system uses Redis to keep up to date across multiple users interacting with the same data. In some service methods, there are 3-5 Redis calls. A get, a set, a set expiry, then another get. I am looking into how to make that more efficient.

Also, I am new to performance testing.

What would be the general steps to test this out and instrument it enough to know where the bottlenecks are?

I think the issue is that there is constant Redis get/set calls being made and while Redis itself is not breaking, I think its the constant requests that are breaking things.

Any info and advice would be super helpful. Thanks

0 likes
2 replies
LaryAI's avatar
Level 58

To address the performance issues and load test your controller actions that call services and interact with Redis/DB, you can follow these general steps:

1. Identify Bottlenecks

Before you start optimizing, you need to identify where the bottlenecks are. This can be done using profiling tools and logging.

Tools:

  • Xdebug: For PHP applications, Xdebug can help you profile your code.
  • Blackfire: Another profiling tool that can give you insights into your application's performance.
  • Laravel Telescope: If you are using Laravel, Telescope can help you monitor requests, exceptions, database queries, and more.

2. Load Testing

You need to simulate the load your application experiences during peak times. This can be done using load testing tools.

Tools:

  • Apache JMeter: A popular tool for load testing.
  • Locust: An open-source load testing tool that allows you to define user behavior with Python code.
  • Artillery: A modern, powerful, and easy-to-use load testing toolkit.

3. Optimize Redis Calls

Since you mentioned that there are multiple Redis calls in your service methods, you should look into optimizing these calls.

Strategies:

  • Batch Operations: If possible, batch multiple Redis commands into a single request.
  • Pipeline: Use Redis pipelining to send multiple commands in a single network round-trip.
  • Caching: Ensure that you are caching data effectively and not making redundant calls.

4. Code Example

Here is an example of how you might use Redis pipelining in PHP with the Predis library:

$client = new Predis\Client();

// Using pipeline to batch commands
$responses = $client->pipeline(function ($pipe) {
    $pipe->set('key1', 'value1');
    $pipe->expire('key1', 3600);
    $pipe->get('key1');
    $pipe->set('key2', 'value2');
    $pipe->expire('key2', 3600);
    $pipe->get('key2');
});

// Process responses
foreach ($responses as $response) {
    echo $response . PHP_EOL;
}

5. Monitor and Analyze

After running your load tests, you need to monitor and analyze the results to identify the bottlenecks.

Metrics to Monitor:

  • CPU Usage: High CPU usage can indicate inefficient code or too many requests.
  • Memory Usage: Monitor memory usage to ensure there are no memory leaks.
  • Response Time: Track the response time of your endpoints.
  • Redis Performance: Monitor Redis performance using tools like RedisInsight.

6. Continuous Improvement

Performance optimization is an ongoing process. Continuously monitor your application, run load tests regularly, and make incremental improvements.

Summary

  1. Identify Bottlenecks: Use profiling tools to find where the issues are.
  2. Load Testing: Simulate peak load using tools like JMeter, Locust, or Artillery.
  3. Optimize Redis Calls: Use batching, pipelining, and effective caching.
  4. Monitor and Analyze: Keep an eye on key metrics and analyze the results.
  5. Continuous Improvement: Regularly test and optimize your application.

By following these steps, you should be able to identify and mitigate the performance issues in your application.

tjsherrill's avatar

After digging deeper I have a couple of options for how to test this, such as loadview but when stress testing the controller actions many need an authenticated user. I could create a seeder that seeds 10k users into the system, export into csv and use them in loadview but that seems like something someone should have thought through already.

So ultimately how do I load test the laravel backend of an React SPA?

Please or to participate in this conversation.