ping
ping

Reputation: 61

Http cache by vary on a custom header

I'm researching http cache by using Vary header, my website has to serve a response content base on a user preferred currency, they can choose displayed currency from the currency selection on my site and I'm using session to store their chosen currency instead of using query parameter in the url.

I want to cache responses base on currencies, I created the custom header for every response x-currency: currency_value and set the vary header vary: x-currency, to tell the http cache to create a different cached content by currency.

but I don't understand at the point, when a user send a request to the http cache, that request header is not included the custom header x-currency: current_value, so how the http cache knows a preferred currency to serve the correct response to user ?

I also think about vary on cookie, but it seems to serve a cached response to unique user not for the group of users that have the same preferred currency.

please show me what I misunderstood or any alternative solutions to achieve that !

Upvotes: 1

Views: 887

Answers (1)

Joe
Joe

Reputation: 31087

You have misunderstood what header the Vary header should indicate. Vary:

describes what parts of a request message, aside from the method, Host header field, and request target, might influence the origin server's process for selecting and representing this response.

In this case, you say you're:

using session to store their chosen currency

That means that the response you're selecting is influenced by the Cookie header that the user's session was sent in. You should therefore send:

Vary: cookie

in the response.

As a point of design, you mention that you've chosen session storage:

instead of using query parameter in the url.

and also mention:

I also think about vary on cookie, but it seems to serve a cached response to unique user not for the group of users that have the same preferred currency.

You're right. Using a query parameter would be significantly more cacheable, as requests for the same currency could be reused. Vary: cookie is essentially the same as Cache-control: private, as no two users will have the same session cookie.

Upvotes: 1

Related Questions