django-fancy-cache icon indicating copy to clipboard operation
django-fancy-cache copied to clipboard

Race condition with find_urls' purge=True

Open YPCrumble opened this issue 1 year ago • 1 comments

I'm starting to see frequent race conditions when using find_urls with purge=True when there is no existing cache entry.

The mechanism is as follows:

Request A (POST object Z) Request B (GET list of objects)
Begin INSERT operation (slow) Check for cache (nothing found, fast)
Begin SELECT operation (fast)
SELECT operation returns a list of objects without object Z
INSERT finishes creating object Z, begin find_urls via the object's post_save signal Begin setting the cache with stale data
find_urls finishes and finds no URLs
Finish setting the cache with stale data

YPCrumble avatar Feb 13 '23 15:02 YPCrumble

The issue only happens the first time the cache is set for an endpoint, because otherwise Request B wouldn't be updating the cache at the same time as Request A.

The exact order of the steps isn't necessarily in this relative order between Request A and Request B, but the key is that Request B ends up setting its stale cache after Request A has tried (and failed) to find a cache entry to clear via find_urls.

YPCrumble avatar Feb 13 '23 15:02 YPCrumble