Streamlit st.cache_data and Docker health‑check integration: does caching persist across container stop/start?
0 reputation · 03 Sept 2024, 05:22 UTC
0 reputation · 03 Sept 2024, 05:22 UTC
Goal: Verify whether the in‑memory cache created by st.cache_data remains available after a Docker container running a Streamlit app is stopped and later restarted by a health‑check that triggers on inactivity.
Constraint: Streamlit’s st.cache_data stores data in the Python process memory; when the container is stopped the process terminates, which normally destroys the cache. The official documentation does not specify any automatic persistence mechanism for this cache across container lifecycles, leaving the behavior undefined for low‑traffic deployments that rely on periodic container shutdown to save compute.
Question: Does st.cache_data survive a container stop/start cycle initiated by a Docker health‑check? If it does not, what is the recommended pattern to preserve cached data between restarts for a low‑traffic workload? Is there a supported way to offload the cache to a volume or external store without rewriting the caching logic?
31775 reputation · 03 Sept 2024, 12:23 UTC
By default, st.cache_data stores objects only in the Python process memory. When the Docker container stops, the process ends and the in‑memory cache is discarded, so a health‑check‑triggered restart does not preserve the cache.
persist argument to st.cache_data (True or \"disk\") that writes cached values to the local filesystem under ~/.streamlit.persist=True the only way to retain data across restarts is to store it externally (e.g., a database, Redis, or object store) and reload it inside the cached function.@st.cache_data(persist=True).mkdir -p ./streamlit_cache.docker run -d -v $(pwd)/streamlit_cache:/home/streamuser/.streamlit -p 8501:8501 your-streamlit-image
To confirm that the persist argument is available, please share the exact Streamlit version you are running (e.g., pip show streamlit). If you are on an older version, the persist option will be ignored and you will need an external store instead.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.