Tangled in the Template Cache

I started writing responses to all the great comments I got from my last post, but those rapidly grew so large and… ummm… tangled that it just seemed more manageable to just write a new post to address all of them!

Simon raised concerns around the behaviour of the “save class files to disk” setting for sites with a large number of templates. All I can say is that once disk drives and file systems start functioning as efficiently as processors and memory, this shouldn’t be as much of a problem. For now, I agree with Simon - if your file system is running slow trying to locate compiled templates (I assume this would be the issue with a large number of files in a single folder), then you might as well let your processor and memory do all the work and compile templates, rather than attempt to retrieve compiled templates from the file system. Then again, if you’re dealing with large, complex templates, retrieving a compiled template from the file system might be faster than compiling on the fly. I’m not sure what the guidelines around this should be (Simon suggests switching off save class files at 5000 templates and over), but I would suppose it would be extremely dependent on hardware. It would be best to benchmark your systems with various combinations of settings, and see which works for you.

Bob asked how a live environment could be updated if the the trusted cache setting was switched on. The simplest way to do this would be to push your new templates in, clear all class files under wwwroot/WEB-INF/cfclasses, and then push the “Clear Template Cache Now” button on the CF Administrator (under Server Settings > Caching). This will force the new templates to be compiled and loaded into the cache.

Given both Simon’s and Bob’s comments, I got curious as to how our web team manages deployments to the Adobe website. Vijay Shah, who used to work on the CF engineering team, manages the group here. This is what he had to say:

The Adobe site is run on a cluster of servers, all of which have the trusted cache switch turned on. Every time a new version of the site is to be deployed, the servers are brought down one at a time, the new set of precompiled templates pushed in, and then restarted.

Note that the web team pushes out precompiled templates to the production servers. This is another technique that you could adopt if you want to save the one-time cost of compilation in production environments. This is especially important for servers that function under high loads - when a server is restarted and brought back into the cluster it will brought under load very suddenly. The cost of simultaneously having to compile a large number of different templates due to sudden load could bring a server to its knees! In case you’re wondering how to precompile templates, see the documentation for cfcompile here.

Claude asked about how these settings could affect multi-instance deployments on Linux. The template cache is maintained as an in-process memory cache, one per instance. The settings I described should have the same effect on a multi-instance deployment as on a standalone deployment. Less compilation and less file I/O will always improve your server performance. The Adobe web team’s approach to deployment and Simon’s comment should give you some ideas for managing your own servers. In fact, pushing in precompiled templates might have an even greater effect on a multi-instance deployment - individual instances would no longer have to do compilation themselves, and so the performance of the cluster as a whole should go up.

Charlie pointed out that the template cache pops/sec statistic is available via the cfstat tool. While cfstat is a great utility, I much prefer my own tools… ;-) Damon and Jason will have my head if I say any more now - but if you’re coming to MAX, I can promise you an interesting show. Perhaps not one that to rival the attractions of Las Vegas… unless, of course, you’re a die hard CF-er!

Last, but by no means least, thanks to Phillip for asking about email subscription (though you really should get a feed reader) - turns out the nice folks at FeedBurner provide this service free of charge. For those who’d like to get email for new posts, sign up using the “Get Email” box on the sidebar off the main page.