django-fancy-cache
django-fancy-cache copied to clipboard
Race condition with find_urls' purge=True
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 |
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
.