Thymeleaf & Spring MVC: Can live template edits propagate across rolling instances during zero‑downtime migration?
0 reputation · 15 Oct 2020, 08:05 UTC
0 reputation · 15 Oct 2020, 08:05 UTC
Upgrade a small Spring MVC application to a new Thymeleaf version while keeping the service available. The objective is to deploy the new template engine without a full restart, ensuring that all users receive consistent views during the transition.
Thymeleaf caches parsed templates by default in Spring Boot production mode. When a template file is modified, the change is not seen by existing instances until the cache is evicted or the JVM restarts. Rolling restarts can leave a mix of old and new instances, each potentially holding different template versions or dialects. Mixing StandardDialect with SpringStandardDialect across instances can cause rendering errors. Spring Boot devtools can disable caching and enable live reload, but it is intended for development and may introduce performance overhead in production.
Is there a supported, production‑grade approach to force a live cache refresh across all instances during a rolling deployment, or must the migration accept a brief period of inconsistent rendering?
35775 reputation · 15 Oct 2020, 09:26 UTC
In a production Spring Boot application, Thymeleaf’s default behaviour is to cache parsed templates indefinitely. Changing a template file will not be reflected in any running JVM until the cache is evicted or the process restarts. Therefore, a purely “live” propagation across all instances during a rolling deployment is not supported out‑of‑the‑box. The practical workaround is to reduce the cache TTL so that a change is picked up automatically after a short period, and to ensure that all instances share the same template repository and dialect configuration.
StandardDialect and SpringStandardDialect across instances can cause ClassCastException or missing variable resolution, as the dialects expose different variable resolvers.templates/ directory so that all pods read the same file set.application.yml:
spring:
thymeleaf:
cache: true
cacheTTLSeconds: 5 # or 0 for no caching
This forces a cache miss on the next request after a file change, making the new markup visible within seconds.spring.thymeleaf.dialect identically.TemplateEngine statistics via Actuator to confirm cache misses.To fine‑tune the strategy, I need to know whether your application’s template files are stored on a shared volume or each pod has its own local copy. This determines whether a low TTL guarantees that all instances see the edit within the same timeframe.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.