KodingKaro

Laravel Development

Your Laravel API Sends the Same Data Again and Again. Fix It With HTTP Caching

Is your Laravel API repeatedly sending the same JSON data to clients? Learn how to use Cache-Control, ETag, conditional requests, and HTTP 304 responses to reduce unnecessary data transfer and improve API performance.

8 min read
Your Laravel API Sends the Same Data Again and Again. Fix It With HTTP Caching
Your Laravel API Sends the Same Data Again and Again. Fix It With HTTP Caching
Laravel API Performance
Your Laravel API Sends the Same Data Again and Again. Fix It With HTTP Caching

Your API might be working perfectly, but that doesn't mean it's efficient. Learn how HTTP caching can prevent unnecessary JSON responses and reduce repeated data transfers.

Laravel Development • 8–10 min read • 27 November 2026
Laravel API response caching with ETag headers and a 304 Not Modified response
HTTP caching helps clients reuse unchanged API responses.

The Problem: Your API Keeps Sending the Same Data

Imagine you are building a website that displays a list of plants, products, articles, or categories. Your frontend requests this data from a Laravel API whenever a user opens a page.

The first request returns a JSON response containing 100 records. A few seconds later, another request asks for exactly the same data. Nothing has changed in your database, but your server sends the complete response again.

This might not seem like a problem when you have a few visitors. But as traffic increases, repeated responses can increase bandwidth usage, server workload, and unnecessary network transfers.

A simple example

First request 200 OK · Full JSON
Second request 200 OK · Same JSON
Third request 200 OK · Same JSON

Three requests, but the underlying data has not changed.

Instead of repeatedly transferring the same content, you can let HTTP caching mechanisms determine whether the client actually needs a new response.

What Is HTTP Caching?

HTTP caching allows browsers and intermediary caches to store responses and reuse them according to the caching rules provided by the server.

For example, if an API response is allowed to remain fresh for 60 seconds, a compatible cache can reuse that response during that period without contacting your Laravel application.

When the cached response becomes stale, the client can ask the server whether the content has changed. This is where ETag and conditional requests become useful.

Important distinction

Laravel's application cache and HTTP response caching are different things. Laravel's Cache facade can reduce database queries inside your application. HTTP caching can reduce repeated response transfers between your server and clients. You can use both together.

Understand Three Important HTTP Headers

1. Cache-Control

Cache-Control tells browsers and caches how a response may be stored and reused.

Cache-Control: public, max-age=60

In this example, the response can be stored by shared caches and remain fresh for 60 seconds. Only use public caching when the response is genuinely safe to share between users.

2. ETag

ETag is an identifier representing a particular version of a resource. Your server can generate it from a hash of the response content or another reliable version identifier.

ETag: "a82f91c4"

When the resource changes, its ETag should change too. This lets the server determine whether the client's cached representation is still valid.

3. If-None-Match

When a cached response becomes stale, the client can send its previously received ETag back to the server using the If-None-Match request header.

If-None-Match: "a82f91c4"

If the resource has not changed, the server can respond with 304 Not Modified. The client can reuse its cached response instead of downloading the full JSON payload again.

What Does 304 Not Modified Actually Do?

A 304 response tells the client that its previously cached representation is still valid. It does not contain the original JSON response body.

200 OK

Resource has changed or no valid cached copy exists.

{ "name": "Example Plant", "status": "active" }

Full response body is sent.

304 Not Modified

The cached representation is still valid.

No response body

The client reuses its cached body.

A 304 response still requires a network request and server-side validation. Its advantage is that it avoids retransmitting the full representation when nothing has changed.

Implement ETag Caching in Laravel

Let's implement this in a Laravel API endpoint. Assume you have a Plant model and want to return plant details to your frontend.

Step 1: Create an API route

// routes/api.php

use App\Http\Controllers\Api\PlantController;
use Illuminate\Support\Facades\Route;

Route::get('/plants/{id}', [PlantController::class, 'show']);

Step 2: Add conditional response handling

Laravel's response object is based on Symfony's HTTP response components. You can use response validators to handle conditional requests.

<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Plant;
use Illuminate\Http\Request;

class PlantController extends Controller
{
    public function show(Request $request, int $id)
    {
        $plant = Plant::findOrFail($id);

        $data = [
            'id' => $plant->id,
            'name' => $plant->name,
            'updated_at' => $plant->updated_at,
        ];

        $response = response()->json($data);

        // Generate an identifier for this response version.
        $response->setEtag(
            hash('sha256', $response->getContent())
        );

        // Include the resource's last modification time.
        if ($plant->updated_at) {
            $response->setLastModified($plant->updated_at);
        }

        // Use public caching only for genuinely public data.
        $response->setPublic();
        $response->setMaxAge(60);

        // Return 304 when the client's cached representation is valid.
        if ($response->isNotModified($request)) {
            return $response;
        }

        return $response;
    }
}

Production note

This example is intended for public, identical responses. Do not apply public caching to authenticated or user-specific API data. For those endpoints, use an appropriate private caching policy or Cache-Control: no-store when the data must not be stored. Also ensure that every field affecting the response is represented in the validator.

Step 3: Test Your API With cURL

You can test the endpoint directly from your terminal. First, make a normal request:

curl -i http://127.0.0.1:8000/api/plants/1

Check the response headers for an ETag. Your response should also include the configured Cache-Control policy.

Copy the ETag value and send another request using If-None-Match:

curl -i \
  -H 'If-None-Match: "YOUR_ETAG_VALUE"' \
  http://127.0.0.1:8000/api/plants/1

If the resource is unchanged and the validator matches, you should receive a 304 Not Modified response without a JSON body.

If you update the plant record and the generated ETag changes, the next conditional request should return the new representation with a 200 OK response.

Choosing the Right Cache Policy

Not every API response should be cached in the same way. Choose the policy based on the type of information your API returns.

Data type Suggested approach
Public categories Short public cache
Public plant details ETag + revalidation
Frequently updated data No-cache + validators
Personal account data Private or no-store
Sensitive information No-store where appropriate

Remember: no-cache does not mean that a response cannot be stored. It means a stored response must be revalidated before reuse. No-store is the directive used when caches should not store the response.

Common Mistakes to Avoid

1. Caching private data publicly

Never use public caching for responses containing private account information or user-specific records.

2. Using the same ETag after data changes

Your validator must change whenever the representation changes. Otherwise, clients could reuse outdated content.

3. Assuming 304 means no server work

The server still needs to validate the request. A 304 reduces response transfer, but does not automatically eliminate database queries or application processing.

4. Forgetting related data

If an API response contains related records, changes to those records must also invalidate or update the response validator.

HTTP Caching Is Not a Replacement for Laravel Cache

Both approaches solve different problems. Laravel's Cache facade can store expensive query results, while HTTP caching helps clients avoid downloading unchanged responses.

For example, you could cache a frequently used database query in Redis and also return an ETag with the API response. The first technique reduces backend work; the second reduces unnecessary response transfers.

The best performance strategy is to measure your actual bottleneck and use the right caching layer for it.

Final Thoughts

An API returning the correct data is only part of building a reliable application. It should also avoid unnecessary work and unnecessary network transfers.

HTTP caching, ETag, and conditional requests give Laravel developers a standard way to handle unchanged responses. They are especially useful for public data that many users request repeatedly.

Start with one read-only endpoint, test its behavior, and measure the results before applying the same strategy across your API. Small improvements at the API level can make a meaningful difference as your application grows.

Further Reading

Published on KodingKaro — Practical tutorials for developers who want to build better web applications.