Flask-Caching serves stale values after data source changes
0 reputation · 06 Aug 2021, 04:52 UTC
Stale cache after data update
Goal: Ensure that a view decorated with @cached returns up-to-date data immediately after the underlying data source (e.g., a database row) is modified, without waiting for the timeout to expire.
Constraint: The Flask-Caching extension does not automatically invalidate cached entries when the data source changes; developers must rely on timeout expiration or manual cache clearing. The stale‑while‑revalidate feature can serve an expired value while refreshing in the background, but it is not enabled by default and requires explicit configuration.
Uncertainty: Whether enabling stale‑while‑revalidate with a short refresh window provides acceptable consistency for typical read‑heavy workloads, and how to configure it safely to avoid thundering‑herd regeneration.
- What timeout and stale‑while‑revalidate values balance freshness and backend load for a frequently updated table?
- How can a cache‑key prefix be constructed to include request‑specific data (e.g., query strings) without writing a custom callable for every route?
- Is there a recommended pattern for triggering automatic cache invalidation from SQLAlchemy model change events?