4 ms·
Basically a page view checks the cache and rebuilds if necessary. Thousands of page hits in the same second before the build is over starts thousands of paralle
by homero 11y ago
Basically a page view checks the cache and rebuilds if necessary. Thousands of page hits in the same second before the build is over starts thousands of parallel rebuilds.
- taf2 11y agoAlso known as the thundering herd...
- homero 11y agoYeah I never really solved it while I was using memcached but I'm not Wikipedia
- andrewingram 11y agoThe way i've seen it done is something like this: * Cache never automatically expires, but you do have some notion of staleness * Whenever you request data, you get it from the cache (if it exists), and check for staleness, so you cached data needs to know when it was cached. * Return the data as usual, but if the data was also stale, you fire off a worker to update the cached data. * If you have lots of requests happening at the same time, you have a system for seeing if a worker already exists, to ensure that you only create one (for each piece of cached data). * For the time it takes for the worker to complete, you have to be okay with serving stale data, in most cases this is okay. There's an edge case missed here, which is what to do when the cache is empty (either because it's one of the first requests, or because the cached data has been evicted). That's up to you, depending on your use case. You can basically either return a default value, you can pre-warm your cache, or you can let the requests hang until the data is ready.
- homero 11y agoI never looked into whether memcached will give you the creation date, I just let memcached expire it itself
- andrewingram 11y agoI should've fixed the formatting whilst it was still editable :( A former colleague of mine built a Django implementation of this pattern, which is pretty useful: https://github.com/codeinthehole/django-cacheback https://github.com/codeinthehole/django-cacheback
- forgotpwtomain 11y agoWhy isn't it a possibility to rebuild and invalidate the cache only when the rebuild is finished?
- homero 11y agoBecause it's usually triggered with the cache expiring then it's not available and every page view attempts rebuilds. Otherwise the cache and time will be separate. I store the cache in memcached with an expiration to detect it expiring
- forgotpwtomain 11y agoYou could queue an async job to rebuild what was stored in the cache right before the expiration time when you update / renew the expiration on the cached item?