Return stale version from cache on recreate

Not applicable

I've created a cache policy in front of my proxy. I set TimeoutInSec and everything is work. I'm just wondering if is possible to setup a "return stale while recreating" behaviour.

Basically, if a request hit my proxy and the cache version has expired, I want the proxy to return to the client the stale cache version and refresh the cached version by calling my backend.

Thanks

1 7 472
7 REPLIES 7

Hmm, ok that's an interesting scenario.

It's not possible, straightaway. If I correctly understand what you want, then it is possible to do it, with 2 caches.

The first cache will have a TTL of... whatever you want it to be. Let's say, TTL of 60 minutes. The second cache will have a TTL of ... infinite. Then, here's how the flow works.

  • For the first inbound request, in the request flow, the LookupCache policy executes for the first cache; it fails to find a value. Then, wrapped in a Condition element, you perform another LookupCache, this one for cache2. (It doesn't have to be a separate cache resource; you could just use a different cache key in the same cache.) . This lookup will also fail. Then, wrapped in another Condition, you do the call to the backend system to retrieve the live data. Upon response, you populate cache1 and cache2, with TTL of 60 minutes, and infinite (-1), respectively. Then respond to the client request.
  • For a subsequent inbound request, within 60 minutes, a call arrives, the LookupCache policy executes, and retrieves from the first cache. Then, with the appropriate Condition, you check the retrieval, and finding success, the logic flow returns the response to the client.
  • For a subsequent inbound request, after 60 minutes, a call arrives, and the LookupCache policy executes, but does not retrieve from the first cache. With the appropriate tests in the Condition elements, you perform a LookupCache on cahce2, and because the TTL is infinite, this call succeeds. You then *asynchronously* call to the backend to retrieve fresh data, and at the same time you respond with the stale data (from cache2) to the client request. When the backend responds, you populate both cache1 and cache2.

Does this make sense?

The asynchronous step to retrieve data from the backend may need to be wrapped in a self-standing proxy, or possibly a separate ProxyEndpoint. You can use a JS fire-and-forget step to invoke that proxy over HTTP and not wait for the response. This allows you to respond to the client without waiting for the backend response.

Putting the cache population logic into a separate proxy means the Scope of the Cached item must not be "proxy". It should be "Global" or "Application" , depending on whether the cache-maintenance logic is contained in a distinct proxy or a distinct proxyendpoint.

Thanks ! I need few days to setup and test everything. I'll keep you posted.

ok, let me know!

I'll tell you we will need some time. We are still investigating all APIgee potentials to define the best for us. I've jsut discovered that cache can be purged on demand http://docs.apigee.com/management/apis/post/organizations/%7Borg_name%7D/environments/%7Benv_name%7D... that's another alternative for us. I'll keep you posted.

Yes, the caching is quite powerful in Apigee. OK, I am interested to learn what you finally decide. Of course if you have more questions, you can ask here.

I tried to do it as described, but failed on setting up conditions and how to respond with cached result using LookupCache without calling target service. Could you provide any examples of how to setup specific flows and policies?

Hi Sergei

please ask a new question, rather than posting a question as a comment to an answer on a 1-year old thread.

Ask a new question:

6695-ask-a-question.png