pogo
pogo

Reputation: 2307

Updating nginx cache in the background

I have a nginx server that is acting as a proxy to a 3rd party API, requests to the API can be a bit slow sometimes and pages that rely on the slower API calls can take a long time to load when the items in the cache have expired.

Is there anyway to have something similar to the following happen:

The crucial thing is when nginx retrieves the fresh content it does so after serving the stale content and so the user making the request doesn't have to wait for it to complete.

Is that possible?

Upvotes: 2

Views: 4258

Answers (1)

Anton
Anton

Reputation: 441

I think the closest possible behaviour will be the next:

  1. If a cache item is stalled then the first request will wait for response from upstream (API) server.
  2. In the meantime, other requests for the same cache item will receive the stalled result.

This behaviour can be enabled by proxy_cache_use_stale updating.

Please see proxy_cache_use_stale documentation for more. You also may be interested in proxy_cache_lock

Source: NGINX caching webinar and notes

Actually, the behaviour that is mentioned in the question is supported by NGINX. Sorry for the incorrect answer. Please see: https://stackoverflow.com/a/40134179/1079966

Upvotes: 2

Related Questions