How to include dynamic data on the page (ESI blocks)
In case of authenticated users, we often have some content which is personalized for the current user. For example, banners with promo info, or special discounts, etc. To include this info on the page, you shouldn't modify your code at all. We have the power of ESI requests implemented in the module, so any block which is placed on the block layout page can be marked as ESI block. This means that this particular block will be replaced by a placeholder (the page will be cached including the placeholder by Varnish, the block will be delivered separately). When Varnish serves the page with such a placeholder it will automatically make a request to Drupal to get content for this placeholder and when the response is retrieved, the placeholder will be replaced with the actual block content and the page will be sent to the user. The advantage of this method is that you don't need to modify your code at all, and there won't be any Ajax request to Drupal after the page is loaded.
Let's see how to do this. First, open the block layout page (admin/structure/block)

Then, click on configuration link and check the block settings page:

You will find an "Advanced Varnish cache" section there.
- ESI - The first checkbox will mark this block as an ESI one, so it will be replaced by a placeholder during the render process.
- TTL - you can set caching time individually for such blocks, so one of the blocks can be storedfor an hour in Varnish, while others are always rendered by Drupal.
- Cache granularity (Cache bin) - like all other things, these blocks can be cached per role per user or can be shared for all users (in case you need this block for anonymous users)
Useful links:
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion