Laravel & performance

Laravel Performance Engineering: Queries, Redis Caching & k6 Testing

Query optimization, Redis, profiling, and k6 testing from my Laravel development experience.

Conceptual flow
  1. 01Baseline journey
  2. 02Profile bottleneck
  3. 03Change one cause
  4. 04Repeat workload

Begin with a representative journey

At Prokoders, my Laravel work included query optimization, Redis caching, profiling, targeted refactoring, and PHPUnit and k6 testing. Those tools answer different questions. Profiling shows where time is spent; a load test shows behavior under a defined workload. Start with a journey that matters, such as loading a customer record or searching a catalog, and record the environment and dataset used for the baseline.

My Laravel development experience at Prokoders includes query optimization, Redis caching, profiling, and PHPUnit and k6 testing—the practical foundation for this article.

Understand the work before hiding it

Repeated queries, unnecessary data loading, and expensive transformations are useful investigation targets. Check the query plan and the amount of data returned before adding a cache. A cache introduces its own questions: who owns the key, when does the value expire, and which changes invalidate it? Tenant-specific data also needs appropriately scoped keys. Faster responses are not an improvement if they contain stale or incorrectly shared records.

Compare under the same conditions

After a change, repeat the workload with comparable data, concurrency, and cache conditions. Record latency distribution and error rate, rather than only an average response time. Use functional tests to protect the behavior affected by the optimization. For cached paths, compare cold and warm runs. A useful result explains the measurement conditions and the tradeoff; it does not present a speedup without enough context to reproduce it.

Write a workload you can repeat

Define the endpoint mix, data volume, user concurrency or arrival rate, and test duration before comparing implementations. A tiny dataset may hide an expensive query; an unrealistic mix may overemphasize a rarely used route. Keep the environment, code revision, and cache state with the results. A baseline is useful when another run can reproduce its conditions, not simply when it produces a chart.

Look for repeated database work

Laravel’s Eloquent documentation explains how eager loading can reduce repeated relationship queries. Use query counts and traces to determine whether that pattern is relevant to the measured path. Loading every relationship is not the goal: inspect the fields and rows actually required by the response. Review pagination, filters, and indexes against the query plan, then verify that the returned records and ordering remain correct.

Design a cache as a consistency contract

For each Redis-backed cache, name the source of truth, key scope, expiry, and invalidation trigger. Ask which stale values are tolerable and for how long. Shared data and tenant-specific data need different key boundaries. Also consider what happens when many requests encounter an expired entry together. The performance gain must be assessed alongside correctness and the additional work introduced by misses, refreshes, and invalidation.

Interpret latency together with errors

An average can hide a slow tail, and a fast error response can make a failing system look quick. Inspect latency percentiles, throughput, and failures together. Separate application errors from test-environment limits. If the workload starts queuing, determine whether the limiting resource is the database, a worker pool, CPU, or a downstream dependency before changing configuration. One targeted hypothesis is easier to validate than several simultaneous optimizations.

Keep the optimization honest

Pair the repeat load test with regression checks for the behavior changed by the optimization. For example, compare cached and uncached responses after a record update, and verify tenant separation. Store the before and after measurements with their conditions rather than reporting an unexplained percentage. My CV documents using query optimization, Redis, profiling, PHPUnit, and k6; project-specific benchmark results can be added when their measurement context is available.

Back to all articles →