Most of the bandwidth your Laravel API sends out is data the client already has. A mobile app polls an endpoint every minute, a dashboard refetches a list that has not changed since Tuesday, and each time your server serializes the same JSON and ships it again. ETags and conditional requests fix this: the server stamps a response with a fingerprint, and on the next request the client asks, "has this changed?" If it has not, the server answers 304 Not Modified with an empty body. No JSON, no serialization, no bandwidth.
How ETags and 304 responses work
An ETag is a short string that identifies one version of a resource. The first time a client requests GET /api/claims, the server returns the JSON plus an ETag header, for example ETag: "a3f91c". The client stores the response and the tag. On the next request it sends If-None-Match: "a3f91c". The server compares the tag with the current version of the resource. If they match, it returns 304 Not Modified and no body. The client reuses what it already has. If they do not match, the server returns a normal 200 with the new payload and a new ETag.
The important detail: a 304 only saves bandwidth if you check the tag before doing the expensive work. If you run the query, build the JSON, and only then compare ETags, you saved the transfer but not the server time. The real win is a cheap check up front, usually a timestamp or a hash of something small.
Generating a stable ETag
An ETag must change whenever the response would change, and stay the same whenever it would not. For a single record, the updated_at timestamp is the obvious source. For a list, use the newest updated_at in the result set plus the total count, so an added or removed row changes the tag even if no row was edited.
// A stable fingerprint for a list response.
$latest = Claim::where('organization_id', $orgId)
->max('updated_at');
$count = Claim::where('organization_id', $orgId)->count();
$etag = md5($orgId . '|' . $count . '|' . $latest);
Two warnings. First, never build an ETag from data the client cannot see, and never include per-user data in a shared tag. If the response differs per user, the ETag must differ per user too, or the check must run after authentication. Second, use a strong ETag (no W/ prefix) when the bytes must match exactly. Weak ETags are fine for "semantically equivalent" checks, but for JSON APIs a strong tag is simpler and honest.
Checking If-None-Match before you query
Laravel responses are Symfony responses underneath, so the framework already has the helpers you need. setEtag() attaches the tag, and isNotModified() compares it against the incoming If-None-Match header and turns the response into a 304 when it matches.
public function index(Request $request)
{
$orgId = $request->user()->organization_id;
$latest = Claim::where('organization_id', $orgId)
->max('updated_at');
$count = Claim::where('organization_id', $orgId)->count();
$etag = md5($orgId . '|' . $count . '|' . $latest);
$response = response()->json([]); // placeholder, replaced below
$response->setEtag($etag);
if ($response->isNotModified($request)) {
return $response; // 304, empty body, no further queries
}
$claims = Claim::where('organization_id', $orgId)
->latest()->paginate(25);
return response()->json($claims)->setEtag($etag);
}
The two cheap aggregate queries (max and count) run on indexed columns and cost far less than fetching and serializing 25 rows. When nothing changed, the request ends there. When something changed, you pay the full cost, but only then. Make sure the updated_at column is indexed on the tables you check this way, or the "cheap" check becomes a full table scan.
A reusable middleware for GET endpoints
Repeating the same ETag logic in every controller gets old. A middleware can handle the common case: hash the response body, compare it with If-None-Match, and return a 304. It does not skip the controller work, so it saves bandwidth rather than server time, but it is a good default for endpoints where the payload is the expensive part.
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class ETag
{
public function handle(Request $request, Closure $next)
{
$response = $next($request);
if (! $request->isMethod('get') || ! $response->isSuccessful()) {
return $response;
}
$etag = md5($response->getContent());
$response->setEtag($etag);
if ($response->isNotModified($request)) {
return $response;
}
return $response;
}
}
Register it on the routes that serve cacheable GET responses, not globally. POST, PUT, and DELETE responses should never get an ETag from this middleware, and error responses should not either. The isSuccessful() guard handles the error case; the method guard handles the rest.
When ETags backfire
ETags are not free, and there are places they do more harm than good. Do not ETag responses that change on every request, such as a feed with a "seconds ago" timestamp baked into the JSON. The tag will never match, and you paid for a hash on every response. Do not ETag a response behind a shared cache unless you also set Cache-Control: private for user-specific data, or one user's ETag can leak another user's data through a 304. And do not assume every client sends If-None-Match back. Browsers do it reliably for static assets. API clients, mobile apps, and curl scripts only do it if someone wrote the code. A 304 is a contract: both sides have to play their part. If your main client is your own app, add the header handling there too, or the server-side work buys nothing.
The takeaway
ETags turn repeated identical responses into a cheap question and a one-line answer. The pattern is small: build the tag from something cheap like a max timestamp and a count, check If-None-Match before the heavy query, and return 304 when nothing changed. Applied to the handful of endpoints your clients poll most, that is a real cut in bandwidth and database load, with no change to the API contract. If you already rate limit your API with Redis, conditional requests are the natural next step: rate limiting caps abusive traffic, and ETags cut the cost of the legitimate traffic that remains.