Cache Invalidation

Cache Invalidation

“There are only two hard things in Computer Science: cache invalidation and naming things.” – Phil Karlton

Once a URL is cached by a gateway cache, the cache will not ask the application for that content anymore. This allows the cache to provide fast responses and reduces the load on your application. However, you risk delivering outdated content. A way out of this dilemma is to use long cache lifetimes, but to actively notify the gateway cache when content changes. Reverse proxies usually provide a channel to receive such notifications, typically through special HTTP requests.

Caution

While cache invalidation is powerful, avoid it when possible. If you fail to invalidate something, outdated caches will be served for a potentially long time. Instead, use short cache lifetimes or use the validation model, and adjust your controllers to perform efficient validation checks as explained in Optimizing your Code with Validation.

Furthermore, since invalidation is a topic specific to each type of reverse proxy, using this concept will tie you to a specific reverse proxy or need additional efforts to support different proxies.

Sometimes, however, you need that extra performance you can get when explicitly invalidating. For invalidation, your application needs to detect when content changes and tell the cache to remove the URLs which contain that data from its cache.

Tip

If you want to use cache invalidation, have a look at the FOSHttpCacheBundle. This bundle provides services to help with various cache invalidation concepts and also documents the configuration for a couple of common caching proxies.

If one content corresponds to one URL, the PURGE model works well. You send a request to the cache proxy with the HTTP method PURGE (using the word “PURGE” is a convention, technically this can be any string) instead of GET and make the cache proxy detect this and remove the data from the cache instead of going to the application to get a response.

Here is how you can configure the Symfony reverse proxy (See HTTP Cache) to support the PURGE HTTP method:

  1. // src/CacheKernel.php
  2. namespace App;
  3. use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. // ...
  7. class CacheKernel extends HttpCache
  8. {
  9. protected function invalidate(Request $request, $catch = false)
  10. {
  11. if ('PURGE' !== $request->getMethod()) {
  12. return parent::invalidate($request, $catch);
  13. }
  14. if ('127.0.0.1' !== $request->getClientIp()) {
  15. return new Response(
  16. 'Invalid HTTP method',
  17. Response::HTTP_BAD_REQUEST
  18. );
  19. }
  20. $response = new Response();
  21. if ($this->getStore()->purge($request->getUri())) {
  22. $response->setStatusCode(Response::HTTP_OK, 'Purged');
  23. } else {
  24. $response->setStatusCode(Response::HTTP_NOT_FOUND, 'Not found');
  25. }
  26. return $response;
  27. }
  28. }

Caution

You must protect the PURGE HTTP method somehow to avoid random people purging your cached data.

Purge instructs the cache to drop a resource in all its variants (according to the Vary header, see Varying the Response for HTTP Cache). An alternative to purging is refreshing a content. Refreshing means that the caching proxy is instructed to discard its local cache and fetch the content again. This way, the new content is already available in the cache. The drawback of refreshing is that variants are not invalidated.

In many applications, the same content bit is used on various pages with different URLs. More flexible concepts exist for those cases:

  • Banning invalidates responses matching regular expressions on the URL or other criteria;
  • Cache tagging lets you add a tag for each content used in a response so that you can invalidate all URLs containing a certain content.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.