Cacheable
Responses explicitly labeled
“Data within a response to a request be implicitly or explicitly labeled as cacheable or non-cacheable.” – Roy Fielding
When to use
To optimize network performance.
Why it matters
- Efficiency: Reduces server load. The client (or a proxy) can reuse the last response.
- Latency: No network trip for the client if cached.
Signs of Violation
- Sending
200 OKfor everything withoutCache-Controlheaders. - Re-fetching static data (like list of countries) every time the user navigates.
Explanation
Problem
Fetching the same data over and over wastes bandwidth and CPU.
Solution
Use HTTP Headers: Cache-Control, ETag, Last-Modified.
Real world analogy
Grocery list. If you bought Milk yesterday and the expiration date (Cache Header) says it’s good for a week, you don’t buy Milk again today. You use the cached copy in the fridge.
Pros and Cons
| Pros | Cons |
|---|---|
Comparison
- GraphQL: Often struggles with HTTP caching compared to REST because it uses POST for queries.
Code example
Typescript
Bad (Violation)
Good (Adherence)
PHP
Bad (Violation)
Good (Adherence)