Caching has a performance-killing race condition?

guest avatarguest created an issue

If I cache a page that takes 2 seconds to generate, then this solves performance problems nicely for most visitors if I get requests less frequently than every 2 seconds. But if someone hits a freshly-expired page and gets a 2 second wait, and then another person hits it before it's done, their thread goes to work generating the page as well, and I get a snowball effect because the more threads re-generating that page simultaneously, the longer each takes. All of a sudden I have a bunch of threads generating the same thing and the server can become unresponsive.

Ideally I think only one thread should re-generate the content, and any other threads would wait on a lock or something of that nature while the one thread is working. Then, when the content is done generating, the lock would be released and they would all get their content instantly without having had to do redundant work.

My understanding of tools and the caching code is still forming, so even if the cherrypy developers don't have the desire/time to fix this, but some suggestions/tips/ideas on how to code this up could be given, I might try to implement it myself.

Comments (6)

  1. Robert Brewer

    Totally agree; anti-stampeding would be nice. Patches welcome. :) Ideally, you should let a CP developer specify whether to always wait on the new value (being generated in another thread) or return the old cached value (if available).

  2. Robert Brewer

    Fixed in trunk in [2509]. Needs port to python3.

    If you ''don't'' want the antistampeding features, set tools.caching.antistampede_timeout to `None`.

  3. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.