Docker Swarm rolling update on Raspberry Pi leaves in‑memory cache stale after task replacement
0 reputation · 14 Nov 2024, 17:03 UTC
0 reputation · 14 Nov 2024, 17:03 UTC
Goal: achieve a zero‑downtime migration of a small application on a Raspberry Pi Docker Swarm cluster while preserving any in‑memory state such as caches or session data during a rolling update.
Constraint: Docker Swarm’s rolling update mechanism drains the old task, waits for the replacement to become healthy, and then proceeds, but it provides no built‑in pre‑stop hook to flush or replicate in‑memory data before the old task is terminated. The update’s success therefore depends on external storage or on the application’s ability to transfer state within the healthcheck window.
What mechanisms can be used to synchronize in‑memory state before the old task is stopped?
How can a healthcheck be designed to confirm that state transfer has completed without blocking the update?
Is there a way to extend the Swarm update strategy with a custom pre‑stop step using configs, sidecar containers, or external orchestration?
33275 reputation · 14 Nov 2024, 22:27 UTC
Use a shared storage location (a Docker volume or a tmpfs mount) together with a lightweight side‑car container that flushes the cache before the main task stops, and let the healthcheck wait for a readiness signal that the new task has loaded the cache.
Docker Swarm’s rolling update stops the old task only after the replacement passes its healthcheck; there is no built‑in pre‑stop hook, so any purely in‑memory state is lost unless the application moves it elsewhere before termination.
update-delay, update-parallelism, failure-action) control timing but do not invoke user scripts.docker volume create cache-shared or --mount type=tmpfs,destination=/cache) and attach it to both the main and side‑car containers./cache/snapshot.dat). If present, load it into memory; otherwise start with an empty cache./cache/ready) or a UDP/Unix‑socket message to the side‑car.SIGTERM (or watches for the readiness file).0 only when the readiness file (/cache/ready) exists, indicating the new task has loaded the cache. Example:healthcheck:
test: ["CMD", "test", "-f", "/cache/ready"]
interval: 5s
timeout: 2s
retries: 3
If the cache is large (> 10 MiB) or the Pi’s SD‑card I/O is slow, a tmpfs mount may be preferable to avoid wear and latency. Please confirm the approximate size of the in‑memory cache you need to preserve so we can advise whether a tmpfs or a persistent volume is appropriate.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.