Thymeleaf Template Caching vs. Memory Overhead for Low-Traffic JVM Workloads
0 reputation · 06 Oct 2021, 22:08 UTC
0 reputation · 06 Oct 2021, 22:08 UTC
In a low-traffic Java application using Thymeleaf, there is a trade-off between CPU utilization and JVM heap consumption regarding the spring.thymeleaf.cache configuration.
Enabling the template cache reduces the CPU overhead by storing parsed TemplateModel objects, but it increases the memory footprint. Conversely, disabling the cache forces the engine to re-parse HTML files on every request, which consumes more CPU cycles but minimizes heap usage.
For environments where memory is the primary constraint and request volume is minimal, it is unclear if the CPU cost of constant re-parsing outweighs the benefit of a smaller memory footprint.
35275 reputation · 07 Oct 2021, 09:51 UTC
For a low-traffic production JVM workload, disabling spring.thymeleaf.cache does not reliably produce unacceptable latency spikes for very small, simple templates, but it does introduce per-request parse and I/O cost that becomes noticeable with template complexity or request bursts. The memory overhead of the cache is typically small and proportional to the number of unique templates, not request volume.
Confirmed facts: With spring.thymeleaf.cache=true Thymeleaf 3.x stores parsed TemplateModel structures in memory after first load and reuses them. With spring.thymeleaf.cache=false the engine resolves, reads and parses the template source on every request.
Memory overhead is dominated by the set of distinct templates, fragments and layouts loaded. In low-traffic apps this is usually negligible compared to JVM baseline. CPU cost of re-parsing is low for trivial pages and rises with nesting, fragments, conditionals and expressions. A burst of concurrent requests can amplify that cost because parsing is CPU bound and involves file I/O.
Missing diagnostic detail that changes the recommendation: the number of unique templates and their average size/complexity. That determines the actual heap delta and the parse cost.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.