Suoin Cache: how to only refetch once while serving stale cache for multiple simultaneous requests?

Question: is there any way to configure http.handlers.cache for a single route in such a way that, when 10 simultaneous requests are being served, and all of them are serving stale response, only one request to backend is issued?

Currently, in the logs of my node application, I can see that every time a resource is stale, and it is being requested 10 times simultaneously, Caddy serves the stale version, but also re-fetches a fresh version 10 times simultaneously as well.

What I used to do in nginx, according to their microcaching tutorial, was to use proxy_cache_lock:

proxy_cache_lock – Restricts the number of concurrent attempts to populate the cache, so that when a cached entry is being created, further requests for that resource are queued up in NGINX (see section " Optimized Microcaching with NGINX").

Is there some configuration that would make Caddy behave the same way?

Hello @antonphp, thank you for your interest about Souin/Cache-handler module.
I’m rethinking the singleflight mode to request the backend once when n simultaneous requests tries to request the same endpoint with the same headers. I have to rethink the used key to avoid collision. I plan to work on that this weekend, and I hope a poc could be available at the end of this weekend.

2 Likes

@antonphp here is the PR feat(chore): singleflight when request backend multiple time at once by darkweak · Pull Request #368 · darkweak/souin · GitHub you can try the fix using

xcaddy build \
--with github.com/darkweak/souin/plugins/caddy@29597e542a5ccfed89828d728db759ea0a067ec0 \
--with github.com/darkweak/souin@29597e542a5ccfed89828d728db759ea0a067ec0

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.