Step 21: Caching for Performance

Caching for Performance

Performance problems might come with popularity. Some typical examples: missing database indexes or tons of SQL requests per page. You won’t have any problems with an empty database, but with more traffic and growing data, it might arise at some point.

Adding HTTP Cache Headers

Using HTTP caching strategies is a great way to maximize the performance for end users with little effort. Add a reverse proxy cache in production to enable caching, and use a CDN to cache on the edge for even better performance.

Let’s cache the homepage for an hour:

patch_file

  1. --- a/src/Controller/ConferenceController.php
  2. +++ b/src/Controller/ConferenceController.php
  3. @@ -33,9 +33,12 @@ class ConferenceController extends AbstractController
  4. #[Route('/', name: 'homepage')]
  5. public function index(ConferenceRepository $conferenceRepository): Response
  6. {
  7. - return new Response($this->twig->render('conference/index.html.twig', [
  8. + $response = new Response($this->twig->render('conference/index.html.twig', [
  9. 'conferences' => $conferenceRepository->findAll(),
  10. ]));
  11. + $response->setSharedMaxAge(3600);
  12. +
  13. + return $response;
  14. }
  15. #[Route('/conference/{slug}', name: 'conference')]

The setSharedMaxAge() method configures the cache expiration for reverse proxies. Use setMaxAge() to control the browser cache. Time is expressed in seconds (1 hour = 60 minutes = 3600 seconds).

Caching the conference page is more challenging as it is more dynamic. Anyone can add a comment anytime, and nobody wants to wait for an hour to see it online. In such cases, use the HTTP validation strategy.

Activating the Symfony HTTP Cache Kernel

To test the HTTP cache strategy, enable the Symfony HTTP reverse proxy:

patch_file

  1. --- a/config/packages/framework.yaml
  2. +++ b/config/packages/framework.yaml
  3. @@ -15,3 +15,5 @@ framework:
  4. #fragments: true
  5. php_errors:
  6. log: true
  7. +
  8. + http_cache: true

Besides being a full-fledged HTTP reverse proxy, the Symfony HTTP reverse proxy (via the HttpCache class) adds some nice debug info as HTTP headers. That helps greatly in validating the cache headers we have set.

Check it on the homepage:

  1. $ curl -s -I -X GET https://127.0.0.1:8000/
  1. HTTP/2 200
  2. age: 0
  3. cache-control: public, s-maxage=3600
  4. content-type: text/html; charset=UTF-8
  5. date: Mon, 28 Oct 2019 08:11:57 GMT
  6. x-content-digest: en63cef7045fe418859d73668c2703fb1324fcc0d35b21d95369a9ed1aca48e73e
  7. x-debug-token: 9eb25a
  8. x-debug-token-link: https://127.0.0.1:8000/_profiler/9eb25a
  9. x-robots-tag: noindex
  10. x-symfony-cache: GET /: miss, store
  11. content-length: 50978

For the very first request, the cache server tells you that it was a miss and that it performed a store to cache the response. Check the cache-control header to see the configured cache strategy.

For subsequent requests, the response is cached (the age has also been updated):

  1. HTTP/2 200
  2. age: 143
  3. cache-control: public, s-maxage=3600
  4. content-type: text/html; charset=UTF-8
  5. date: Mon, 28 Oct 2019 08:11:57 GMT
  6. x-content-digest: en63cef7045fe418859d73668c2703fb1324fcc0d35b21d95369a9ed1aca48e73e
  7. x-debug-token: 9eb25a
  8. x-debug-token-link: https://127.0.0.1:8000/_profiler/9eb25a
  9. x-robots-tag: noindex
  10. x-symfony-cache: GET /: fresh
  11. content-length: 50978

Avoiding SQL Requests with ESI

The TwigEventSubscriber listener injects a global variable in Twig with all conference objects. It does so for every single page of the website. It is probably a great target for optimization.

You won’t add new conferences every day, so the code is querying the exact same data from the database over and over again.

We might want to cache the conference names and slugs with the Symfony Cache, but whenever possible I like to rely on the HTTP caching infrastructure.

When you want to cache a fragment of a page, move it outside of the current HTTP request by creating a sub-request. ESI is a perfect match for this use case. An ESI is a way to embed the result of an HTTP request into another.

Create a controller that only returns the HTML fragment that displays the conferences:

patch_file

  1. --- a/src/Controller/ConferenceController.php
  2. +++ b/src/Controller/ConferenceController.php
  3. @@ -41,6 +41,14 @@ class ConferenceController extends AbstractController
  4. return $response;
  5. }
  6. + #[Route('/conference_header', name: 'conference_header')]
  7. + public function conferenceHeader(ConferenceRepository $conferenceRepository): Response
  8. + {
  9. + return new Response($this->twig->render('conference/header.html.twig', [
  10. + 'conferences' => $conferenceRepository->findAll(),
  11. + ]));
  12. + }
  13. +
  14. #[Route('/conference/{slug}', name: 'conference')]
  15. public function show(Request $request, Conference $conference, CommentRepository $commentRepository, string $photoDir): Response
  16. {

Create the corresponding template:

templates/conference/header.html.twig

  1. <ul>
  2. {% for conference in conferences %}
  3. <li><a href="{{ path('conference', { slug: conference.slug }) }}">{{ conference }}</a></li>
  4. {% endfor %}
  5. </ul>

Hit /conference_header to check that everything works fine.

Time to reveal the trick! Update the Twig layout to call the controller we have just created:

patch_file

  1. --- a/templates/base.html.twig
  2. +++ b/templates/base.html.twig
  3. @@ -8,11 +8,7 @@
  4. <body>
  5. <header>
  6. <h1><a href="{{ path('homepage') }}">Guestbook</a></h1>
  7. - <ul>
  8. - {% for conference in conferences %}
  9. - <li><a href="{{ path('conference', { slug: conference.slug }) }}">{{ conference }}</a></li>
  10. - {% endfor %}
  11. - </ul>
  12. + {{ render(path('conference_header')) }}
  13. <hr />
  14. </header>
  15. {% block body %}{% endblock %}

And voilà. Refresh the page and the website is still displaying the same.

Tip

Use the “Request / Response” Symfony profiler panel to learn more about the main request and its sub-requests.

Now, every time you hit a page in the browser, two HTTP requests are executed, one for the header and one for the main page. You have made performance worse. Congratulations!

The conference header HTTP call is currently done internally by Symfony, so no HTTP round-trip is involved. This also means that there is no way to benefit from HTTP cache headers.

Convert the call to a “real” HTTP one by using an ESI.

First, enable ESI support:

patch_file

  1. --- a/config/packages/framework.yaml
  2. +++ b/config/packages/framework.yaml
  3. @@ -11,7 +11,7 @@ framework:
  4. cookie_secure: auto
  5. cookie_samesite: lax
  6. - #esi: true
  7. + esi: true
  8. #fragments: true
  9. php_errors:
  10. log: true

Then, use render_esi instead of render:

patch_file

  1. --- a/templates/base.html.twig
  2. +++ b/templates/base.html.twig
  3. @@ -10,7 +10,7 @@
  4. <body>
  5. <header>
  6. <h1><a href="{{ path('homepage') }}">Guestbook</a></h1>
  7. - {{ render(path('conference_header')) }}
  8. + {{ render_esi(path('conference_header')) }}
  9. <hr />
  10. </header>
  11. {% block body %}{% endblock %}

If Symfony detects a reverse proxy that knows how to deal with ESIs, it enables support automatically (if not, it falls back to render the sub-request synchronously).

As the Symfony reverse proxy does support ESIs, let’s check its logs (remove the cache first - see “Purging” below):

  1. $ curl -s -I -X GET https://127.0.0.1:8000/
  1. HTTP/2 200
  2. age: 0
  3. cache-control: must-revalidate, no-cache, private
  4. content-type: text/html; charset=UTF-8
  5. date: Mon, 28 Oct 2019 08:20:05 GMT
  6. expires: Mon, 28 Oct 2019 08:20:05 GMT
  7. x-content-digest: en4dd846a34dcd757eb9fd277f43220effd28c00e4117bed41af7f85700eb07f2c
  8. x-debug-token: 719a83
  9. x-debug-token-link: https://127.0.0.1:8000/_profiler/719a83
  10. x-robots-tag: noindex
  11. x-symfony-cache: GET /: miss, store; GET /conference_header: miss
  12. content-length: 50978

Refresh a few times: the / response is cached and the /conference_header one is not. We have achieved something great: having the whole page in the cache but still having one part dynamic.

This is not what we want though. Cache the header page for an hour, independently of everything else:

patch_file

  1. --- a/src/Controller/ConferenceController.php
  2. +++ b/src/Controller/ConferenceController.php
  3. @@ -44,9 +44,12 @@ class ConferenceController extends AbstractController
  4. #[Route('/conference_header', name: 'conference_header')]
  5. public function conferenceHeader(ConferenceRepository $conferenceRepository): Response
  6. {
  7. - return new Response($this->twig->render('conference/header.html.twig', [
  8. + $response = new Response($this->twig->render('conference/header.html.twig', [
  9. 'conferences' => $conferenceRepository->findAll(),
  10. ]));
  11. + $response->setSharedMaxAge(3600);
  12. +
  13. + return $response;
  14. }
  15. #[Route('/conference/{slug}', name: 'conference')]

Cache is now enabled for both requests:

  1. $ curl -s -I -X GET https://127.0.0.1:8000/
  1. HTTP/2 200
  2. age: 613
  3. cache-control: public, s-maxage=3600
  4. content-type: text/html; charset=UTF-8
  5. date: Mon, 28 Oct 2019 07:31:24 GMT
  6. x-content-digest: en15216b0803c7851d3d07071473c9f6a3a3360c6a83ccb0e550b35d5bc484bbd2
  7. x-debug-token: cfb0e9
  8. x-debug-token-link: https://127.0.0.1:8000/_profiler/cfb0e9
  9. x-robots-tag: noindex
  10. x-symfony-cache: GET /: fresh; GET /conference_header: fresh
  11. content-length: 50978

The x-symfony-cache header contains two elements: the main / request and a sub-request (the conference_header ESI). Both are in the cache (fresh).

The cache strategy can be different from the main page and its ESIs. If we have an “about” page, we might want to store it for a week in the cache, and still have the header be updated every hour.

Remove the listener as we don’t need it anymore:

  1. $ rm src/EventSubscriber/TwigEventSubscriber.php

Purging the HTTP Cache for Testing

Testing the website in a browser or via automated tests becomes a little bit more difficult with a caching layer.

You can manually remove all the HTTP cache by removing the var/cache/dev/http_cache/ directory:

  1. $ rm -rf var/cache/dev/http_cache/

This strategy does not work well if you only want to invalidate some URLs or if you want to integrate cache invalidation in your functional tests. Let’s add a small, admin only, HTTP endpoint to invalidate some URLs:

patch_file

  1. --- a/src/Controller/AdminController.php
  2. +++ b/src/Controller/AdminController.php
  3. @@ -6,8 +6,10 @@ use App\Entity\Comment;
  4. use App\Message\CommentMessage;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. +use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. +use Symfony\Component\HttpKernel\KernelInterface;
  11. use Symfony\Component\Messenger\MessageBusInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\Workflow\Registry;
  14. @@ -52,4 +54,17 @@ class AdminController extends AbstractController
  15. 'comment' => $comment,
  16. ]);
  17. }
  18. +
  19. + #[Route('/admin/http-cache/{uri<.*>}', methods: ['PURGE'])]
  20. + public function purgeHttpCache(KernelInterface $kernel, Request $request, string $uri): Response
  21. + {
  22. + if ('prod' === $kernel->getEnvironment()) {
  23. + return new Response('KO', 400);
  24. + }
  25. +
  26. + $store = (new class($kernel) extends HttpCache {})->getStore();
  27. + $store->purge($request->getSchemeAndHttpHost().'/'.$uri);
  28. +
  29. + return new Response('Done');
  30. + }
  31. }

The new controller has been restricted to the PURGE HTTP method. This method is not in the HTTP standard, but it is widely used to invalidate caches.

By default, route parameters cannot contain / as it separates URL segments. You can override this restriction for the last route parameter, like uri, by setting your own requirement pattern (.*).

The way we get the HttpCache instance can also look a bit strange; we are using an anonymous class as accessing the “real” one is not possible. The HttpCache instance wraps the real kernel, which is unaware of the cache layer as it should be.

Invalidate the homepage and the conference header via the following cURL calls:

  1. $ curl -I -X PURGE -u admin:admin `symfony var:export SYMFONY_PROJECT_DEFAULT_ROUTE_URL`/admin/http-cache/
  2. $ curl -I -X PURGE -u admin:admin `symfony var:export SYMFONY_PROJECT_DEFAULT_ROUTE_URL`/admin/http-cache/conference_header

The symfony var:export SYMFONY_PROJECT_DEFAULT_ROUTE_URL sub-command returns the current URL of the local web server.

Note

The controller does not have a route name as it will never be referenced in the code.

Grouping similar Routes with a Prefix

The two routes in the admin controller have the same /admin prefix. Instead of repeating it on all routes, refactor the routes to configure the prefix on the class itself:

patch_file

  1. --- a/src/Controller/AdminController.php
  2. +++ b/src/Controller/AdminController.php
  3. @@ -15,6 +15,7 @@ use Symfony\Component\Routing\Annotation\Route;
  4. use Symfony\Component\Workflow\Registry;
  5. use Twig\Environment;
  6. +#[Route('/admin')]
  7. class AdminController extends AbstractController
  8. {
  9. private $twig;
  10. @@ -28,7 +29,7 @@ class AdminController extends AbstractController
  11. $this->bus = $bus;
  12. }
  13. - #[Route('/admin/comment/review/{id}', name: 'review_comment')]
  14. + #[Route('/comment/review/{id}', name: 'review_comment')]
  15. public function reviewComment(Request $request, Comment $comment, Registry $registry): Response
  16. {
  17. $accepted = !$request->query->get('reject');
  18. @@ -55,7 +56,7 @@ class AdminController extends AbstractController
  19. ]);
  20. }
  21. - #[Route('/admin/http-cache/{uri<.*>}', methods: ['PURGE'])]
  22. + #[Route('/http-cache/{uri<.*>}', methods: ['PURGE'])]
  23. public function purgeHttpCache(KernelInterface $kernel, Request $request, string $uri): Response
  24. {
  25. if ('prod' === $kernel->getEnvironment()) {

Caching CPU/Memory Intensive Operations

We don’t have CPU or memory-intensive algorithms on the website. To talk about local caches, let’s create a command that displays the current step we are working on (to be more precise, the Git tag name attached to the current Git commit).

The Symfony Process component allows you to run a command and get the result back (standard and error output); install it:

  1. $ symfony composer req process

Implement the command:

src/Command/StepInfoCommand.php

  1. namespace App\Command;
  2. use Symfony\Component\Console\Command\Command;
  3. use Symfony\Component\Console\Input\InputInterface;
  4. use Symfony\Component\Console\Output\OutputInterface;
  5. use Symfony\Component\Process\Process;
  6. class StepInfoCommand extends Command
  7. {
  8. protected static $defaultName = 'app:step:info';
  9. protected function execute(InputInterface $input, OutputInterface $output): int
  10. {
  11. $process = new Process(['git', 'tag', '-l', '--points-at', 'HEAD']);
  12. $process->mustRun();
  13. $output->write($process->getOutput());
  14. return 0;
  15. }
  16. }

Note

You could have used make:command to create the command:

  1. $ symfony console make:command app:step:info

What if we want to cache the output for a few minutes? Use the Symfony Cache:

  1. $ symfony composer req cache

And wrap the code with the cache logic:

patch_file

  1. --- a/src/Command/StepInfoCommand.php
  2. +++ b/src/Command/StepInfoCommand.php
  3. @@ -6,16 +6,31 @@ use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Output\OutputInterface;
  6. use Symfony\Component\Process\Process;
  7. +use Symfony\Contracts\Cache\CacheInterface;
  8. class StepInfoCommand extends Command
  9. {
  10. protected static $defaultName = 'app:step:info';
  11. + private $cache;
  12. +
  13. + public function __construct(CacheInterface $cache)
  14. + {
  15. + $this->cache = $cache;
  16. +
  17. + parent::__construct();
  18. + }
  19. +
  20. protected function execute(InputInterface $input, OutputInterface $output): int
  21. {
  22. - $process = new Process(['git', 'tag', '-l', '--points-at', 'HEAD']);
  23. - $process->mustRun();
  24. - $output->write($process->getOutput());
  25. + $step = $this->cache->get('app.current_step', function ($item) {
  26. + $process = new Process(['git', 'tag', '-l', '--points-at', 'HEAD']);
  27. + $process->mustRun();
  28. + $item->expiresAfter(30);
  29. +
  30. + return $process->getOutput();
  31. + });
  32. + $output->writeln($step);
  33. return 0;
  34. }

The process is now only called if the app.current_step item is not in the cache.

Profiling and Comparing Performance

Never add cache blindly. Keep in mind that adding some cache adds a layer of complexity. And as we are all very bad at guessing what will be fast and what is slow, you might end up in a situation where the cache makes your application slower.

Always measure the impact of adding a cache with a profiler tool like Blackfire.

Refer to the step about “Performance” to learn more about how you can use Blackfire to test your code before deploying.

Configuring a Reverse Proxy Cache on Production

Don’t use the Symfony reverse proxy in production. Always prefer a reverse proxy like Varnish on your infrastructure or a commercial CDN.

Add Varnish to the SymfonyCloud services:

patch_file

  1. --- a/.symfony/services.yaml
  2. +++ b/.symfony/services.yaml
  3. @@ -2,3 +2,12 @@ db:
  4. type: postgresql:13
  5. disk: 1024
  6. size: S
  7. +
  8. +varnish:
  9. + type: varnish:6.0
  10. + relationships:
  11. + application: 'app:http'
  12. + configuration:
  13. + vcl: !include
  14. + type: string
  15. + path: config.vcl

Use Varnish as the main entry point in the routes:

patch_file

  1. --- a/.symfony/routes.yaml
  2. +++ b/.symfony/routes.yaml
  3. @@ -1,2 +1,2 @@
  4. -"https://{all}/": { type: upstream, upstream: "app:http" }
  5. +"https://{all}/": { type: upstream, upstream: "varnish:http", cache: { enabled: false } }
  6. "http://{all}/": { type: redirect, to: "https://{all}/" }

Finally, create a config.vcl file to configure Varnish:

.symfony/config.vcl

  1. sub vcl_recv {
  2. set req.backend_hint = application.backend();
  3. }

Enabling ESI Support on Varnish

ESI support on Varnish should be enabled explicitly for each request. To make it universal, Symfony uses the standard Surrogate-Capability and Surrogate-Control headers to negotiate ESI support:

.symfony/config.vcl

  1. sub vcl_recv {
  2. set req.backend_hint = application.backend();
  3. set req.http.Surrogate-Capability = "abc=ESI/1.0";
  4. }
  5. sub vcl_backend_response {
  6. if (beresp.http.Surrogate-Control ~ "ESI/1.0") {
  7. unset beresp.http.Surrogate-Control;
  8. set beresp.do_esi = true;
  9. }
  10. }

Purging the Varnish Cache

Invalidating the cache in production should probably never be needed, except for emergency purposes and maybe on non-master branches. If you need to purge the cache often, it probably means that the caching strategy should be tweaked (by lowering the TTL or by using a validation strategy instead of an expiration one).

Anyway, let’s see how to configure Varnish for cache invalidation:

patch_file

  1. --- a/.symfony/config.vcl
  2. +++ b/.symfony/config.vcl
  3. @@ -1,6 +1,13 @@
  4. sub vcl_recv {
  5. set req.backend_hint = application.backend();
  6. set req.http.Surrogate-Capability = "abc=ESI/1.0";
  7. +
  8. + if (req.method == "PURGE") {
  9. + if (req.http.x-purge-token != "PURGE_NOW") {
  10. + return(synth(405));
  11. + }
  12. + return (purge);
  13. + }
  14. }
  15. sub vcl_backend_response {

In real life, you would probably restrict by IPs instead like described in the Varnish docs.

Purge some URLs now:

  1. $ curl -X PURGE -H 'x-purge-token PURGE_NOW' `symfony env:urls --first`
  2. $ curl -X PURGE -H 'x-purge-token PURGE_NOW' `symfony env:urls --first`conference_header

The URLs looks a bit strange because the URLs returned by env:urls already ends with /.

Going Further


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