Scrivio Interview library
DebuggingIntermediate6 min read · updated 13 November 2026

A public catalogue API returns JSON or XML by the Accept header and localised text by Accept-Language, with Cache-Control: public, max-age=300. Behind a new shared cache, some clients get XML when they asked for JSON, and French users sometimes see German text.

What is missing from the responses, how do you fix it, and what does the fix cost in cache efficiency?

The responses are missing a Vary header. The shared cache keys them by URL only, so whichever variant, JSON or XML, German or French, is stored first is served to everyone. We fix it by sending Vary: Accept, Accept-Language, but because raw header values are so diverse that this splits the cache into many entries, we normalise those headers to a few supported values at the gateway or move the variant into the URL, then purge the bad entries and add a test.

Why this question is asked

Content negotiation, choosing the format and language of a response from the request's headers, works perfectly until a shared cache is placed in front of it. Then users start receiving each other's variants, which looks like a random bug but is completely predictable once we know how caches build their keys. Interviewers ask this because it tests whether we understand the cache key, the Vary header, and the cost of fixing it naively. In this answer, we will recap how a shared cache keys responses, reproduce the mix-up, add Vary, measure what it costs in hit ratio, reduce that cost with normalisation or URL variants, cover the other Vary values we should send, and finish with cleanup and testing.

1A Quick Recap: How a Shared Cache Builds Its Key

A shared cache, such as a CDN or a gateway cache, stores responses under a cache key. Unless told otherwise, the key is the request method and the URL. Request headers are not part of it, because most responses do not depend on them and including them would split the cache for no reason.

Content negotiation breaks that assumption. Our catalogue API reads the Accept header, for example application/json or application/xml, to choose the format, and Accept-Language, for example fr-FR,fr;q=0.9, to choose the language. So one URL now has several correct answers, and the URL alone is not enough to tell them apart.

2Reproducing the Mix-Up

A French user's mobile app requests /products/1 with Accept: application/json and Accept-Language: fr. The cache misses, the origin returns French JSON with Cache-Control: public, max-age=300, and the cache stores it under the key GET /products/1. Within the next five minutes a German partner system requests the same URL asking for XML in German. The cache finds a fresh entry under the same key and returns French JSON.

Step throughOne key, two different requests
French appPartner (XML, de)Shared cacheOrigin
GET /products/1 Accept: json, lang fr
miss: forward
French JSON, public, max-age=300
store under GET /products/1
GET /products/1 Accept: xml, lang de
hit: French JSON

6 messages in order. Play them, or step through them one at a time.

Without Vary, the cache cannot know the response depended on the request headers.

So the symptom looks random, because the first request after each expiry decides what everyone gets for the next five minutes. The responses are missing the one header that would tell the cache which request headers matter.

3The Fix: Vary: Accept, Accept-Language

The Vary response header lists the request headers that influenced the response. A cache that sees it stores the values of those request headers alongside the entry and only reuses the entry for a request with matching values. In Spring we can add it per response.

Java
@GetMapping(value = "/products/{id}", produces = {"application/json", "application/xml"})
ResponseEntity<Product> product(@PathVariable long id, Locale locale) {
return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(Duration.ofMinutes(5)).cachePublic())
.varyBy("Accept", "Accept-Language") // Vary: Accept, Accept-Language
.body(catalogue.find(id, locale));
}

Now the French JSON entry is stored together with Accept: application/json and Accept-Language: fr, and the partner's request with different values misses and gets its own entry. Correctness is restored.

4What Vary Costs in Cache Efficiency

But there is a catch. Caches compare the header values as they arrive, and real clients send a huge variety. Browsers send Accept-Language strings such as fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7, and different browsers, versions and user settings produce thousands of distinct strings. Accept varies too: one client sends application/json, another application/json, text/plain, */*. Each distinct combination becomes its own cache entry for the same content.

Entries per URL for the same few real variants
Cache key includesDistinct entries per URLEffect
URL only1Wrong variants served
URL + raw Accept + raw Accept-Languagehundreds to thousandsCorrect, but hit ratio collapses
URL + normalised format + normalised locale2 formats x 6 locales = 12Correct and efficient
Variant in the URL (/fr/products/1?format=json)12, as separate URLsCorrect, simplest for every cache

If the API supports two formats and six locales, there are only 12 real variants, yet raw values can produce hundreds of entries per product. Each of them is a miss the first time, so the hit ratio drops and origin load rises.

5Reducing the Cost: Normalise or Move the Variant Into the URL

The first remedy is normalisation. At the gateway or the CDN edge, before the cache lookup, we map Accept to one of json or xml and Accept-Language to one of the six supported locales, falling back to a default. The origin then receives clean values and its Vary matches them, so at most 12 entries exist per URL. Many CDNs let us go further and put the normalised values into an explicit cache key, which is more predictable than relying on Vary, because some CDNs support Vary only partly, for example honouring only Accept-Encoding or treating any other Vary value as uncacheable. So we check our CDN's documentation and prefer an explicit key where Vary is weak.

The second remedy is to move the variant into the URL: /fr/products/1 or /products/1?format=json. Then every cache keys correctly by default, links can be shared, and nothing depends on header handling. For a public catalogue API, this is often the cleanest long-term answer, with header negotiation kept for compatibility and redirected or normalised to the URL form.

6Other Vary Values, Cleanup and a Test

Two more headers often need Vary. If the response's CORS headers depend on the request's Origin, for example Access-Control-Allow-Origin echoes the caller's origin, we must send Vary: Origin, or one site's CORS answer is served to another and browsers block requests. If the origin compresses responses itself, it should send Vary: Accept-Encoding so a client that cannot decode gzip never receives a gzipped body.

After deploying the fix, the wrong entries are still in the cache, so we purge the affected URLs. Finally we add an automated test that requests the same URL through the cache with different Accept and Accept-Language values, in alternating order, and asserts that each response matches what was asked for. That turns this class of mistake into a failing build.

7Follow-up questions

Why can Vary: User-Agent ruin a hit ratio?

Because there are tens of thousands of distinct User-Agent strings, changing with every browser and operating system release, so almost every request has a value the cache has not seen and almost every request misses. If we really serve different content to device classes, we normalise the User-Agent to a few classes such as mobile and desktop at the edge and key on that class instead.

Why not just mark these responses private to avoid the problem?

That would stop the mix-up, but it throws away the shared cache entirely for a public catalogue, which is exactly the content that benefits most from it. The data is not personal, it only has a few variants, so the right fix is to key correctly, not to stop caching.

Does Vary affect browser caches too?

Yes. A browser also stores the request header values and only reuses an entry when they match, but since one browser usually sends the same Accept-Language every time, the cost there is small. The fragmentation problem is mainly a shared cache problem.

8Conclusion

In this answer, we saw that a shared cache keys responses by URL, so when the API varied by Accept and Accept-Language without saying so, the first stored variant was served to everyone. Sending Vary: Accept, Accept-Language makes it correct, but raw header values split each URL into many entries, so we normalise them at the gateway or edge into a small set, use an explicit CDN cache key where Vary support is partial, or move the variant into the URL. We also send Vary: Origin for origin-dependent CORS and Vary: Accept-Encoding for origin compression, purge the bad entries and add a test that varies the headers through the cache.

What to say in the interviewAbout 80 seconds aloud

Open with

The responses depend on Accept and Accept-Language, but they do not say so, so the shared cache keys them by URL and serves whichever variant it stored first to everybody.

Then, if they want more

The fix is to send Vary: Accept, Accept-Language, which tells the cache to store those request header values with the entry and only reuse it when they match. In Spring that is varyBy on the ResponseEntity.

The cost is fragmentation: raw Accept-Language strings come in thousands of forms, so one product could end up with hundreds of entries for what are really maybe twelve variants, and the hit ratio drops.

So I would normalise at the gateway or edge, mapping Accept to json or xml and the language to our supported locales, and ideally put those normalised values into an explicit CDN cache key, because some CDNs only partly support Vary. For a public API, moving the variant into the URL, like /fr/products/1 or ?format=json, is often the cleanest.

I would also check for Vary: Origin if CORS headers echo the origin, and Vary: Accept-Encoding if the origin compresses. Then I purge the affected URLs and add a test that hits one URL through the cache with different headers and checks each answer.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is missing from the responses, how do you fix it, and what does the fix cost in...": https://scrivio-interview-library.netlify.app/architecture/caching-strategies/6f8c63c288/