Header image

Symfony Finland
Random things on PHP, Symfony and web development

Cache enhancements in Symfony 3.1 and 3.2: PSR-6 and tag invalidation

It's been said that there are only two hard things in Computer Science: cache invalidation and naming things. It seems that this statement from Phil Karlton continues to be true. Naming things is as close as code can be to poetry, so I'll leave that for the philosophers to discuss.

Invalidating items from cache is much more clear-cut problem to define. The more efficient and easy the solution, the better. Symfony3 already has some improvements for that, with more coming in the near future.

Caching obviously comes in multiple favours ranging from CPU level data fetching optimisation to your browser storing images and scripts so that they won't be downloaded on each request. Even in web development there are obviously countless flavours and mechanisms of caching in place.

Since the inception of Symfony2 in the PHP world, caching in terms of HTTP traffic has become a lot more consistent and predictable. As you are always returning responses, you know that you can define certain TTL (Time to Live) parameters and then the configured front end caching system will work with that.

Together with the HVMC model and powerful reverse proxies like Varnish, developers have been to use the sophisticated caching mechanisms built into the components and the framework itself.

To add to the capabilities of the framework the FOSHttpCacheBundle, which eases purging and offers global configuration of caches based on path, controller and other characteristics of a request.

Caching improvements in Symfony 3.1

Symfony 3.1 was the first version of the new Symfony3 family to offer new features. The first 3.0 release had feature parity with the last of the Symfony2 series. Symfony 3.1 was launched in May 2016, with a moderate list of added individual features, but one that was a big step in terms of caching:

Cache: Symfony 3.1 comes with an implementation of the cache PSR (PSR-6). The new component is also automatically wired in FrameworkBundle and Symfony Standard Edition (nicolas-grekas) (17408)

This cache component implements PSR-6 from the PHP-FIG, which is a body that helps various PHP projects standardize fundamental features. Caching is present in most web applications in some for of another and without standards in place there has been a lot of reinventing the wheel.

From rolling your own adhoc APCU cache to complex caching libraries have defined their own terms (remember naming things...?) and mechanisms. PSR-6 does not define how caching is done, it simply defines and interface for how higher level applications should talk to caches.

What this means is that Symfony's PSR-6 implementation is just one of many like Stash Cache and PHP Cache. This means that instead of making improvements to the interface developers use, library developers can focus on the caching implementation itself.

This is illustrated in the PSR-6 presentation slide below that shows how many round trips the library has to make to the cache page for different types of requests:

Cache roundtrip in PHP and PSR-6

Note: Doctrine Cache is not PSR-6 compatible, but still comparable for this case.

Cache tagging coming in Symfony 3.2

Adopting PSR-6 in Symfony 3.1 also opened the road for more high level purging methods available for developers. Symfony 3.2 will add the functionality of tagging cache entries. In addition to item specific cache keys, tags can be used to purge a large number of related items at once.

With this feature the developer can mark certain items as being related to a certain thing with tags. Purging the tag will purge all related items. A similar functionality has been available for HTTP Caching in Symfony2 and Symfony3 in FOSHttpCacheBundle for Varnish:

If your application has many intricate relationships between cached items, which makes it complex to invalidate them by route, cache tagging will be useful. It helps you with invalidating many-to-many relationships between content items.

The above functionality will be included in the 3.2 release scheduled for November 2016.

Tag caching plays together especially well with the HMVC architecture of the Symfony Framework when you're rendering your pages fully on the server side. In addition it is also very useful when you're building rich front end applications of Mobile applications on top of PHP APIs that are just as hard to purge as individual pages. Just maybe a whole lot harder to debug.

Together with PSR-6 and the improvements already in place in Symfony 3.1 it is safe to say that in a years time more time will be spent using caches instead of trying to figure out how to name their own adhoc caching library. And this is a good thing.

Read more about Caching and PSR-6:

Finally big thanks to André Rømcke and Nicolas Grekas for reviewing this article!

Written by Jani Tarvainen on Wednesday June 22, 2016
Permalink - Tags: symfony, cache, php

Leave a comment

comments powered by Disqus

« Using JSON fields with Doctrine ORM on PostgreSQL & MySQL - The JavaScript language reboot is done »