ESI exists for the case where 95 % of a page is identical for everyone, and the other 5 % — the cart icon, the “Hello $username” banner, the personalized recommendations — destroys the cache hit rate. Varnish supports a subset of the ESI/1.0 specification and assembles the final response by fetching included fragments at edge time. This article is the practical guide to making ESI useful in production: where it pays off, where it does not, and what the operational cost looks like.
How to verify
Before enabling ESI on a site, confirm origin produces <esi:include> tags only on pages where you want them, and confirm Varnish is processing rather than passing them through verbatim.
curl -s -H "User-Agent: Varnish-ESI-Test" http://origin.internal/ | grep -o '<esi:[^>]*>' | head
curl -s -i http://varnish.internal/ | grep -i 'x-cache\|esi'
varnishlog -g request -i ReqUrl,ESI_xmlerror,VCL_call,VCL_return | head -50
varnishstat -1 -f MAIN.esi_errors -f MAIN.esi_warnings
If origin emits <esi:include> and the response delivered to the client still contains the literal tag, Varnish either has not enabled do_esi for that response or the content-type is not text.
What’s happening
ESI is markup the origin embeds in its HTML to say “fetch this URL at edge time and splice the result here.” When Varnish parses an ESI-enabled response, it walks the body, finds <esi:include src="..." /> tags, performs sub-requests for each, and assembles the final page. The shell can be cached aggressively (hours), each fragment can have its own TTL (the cart icon could be 5 seconds, the static footer could be a day), and the whole page assembles at the edge without the application doing anything per-request beyond serving the fragments.
The catch: every page assembly is N+1 requests through the VCL state machine. ESI fragments share a worker thread budget; a slow fragment delays the whole page. The included URLs are subject to the same VCL — they can be cached, passed, or rewritten — but they cannot themselves include further ESI by default (Varnish allows nesting up to a configurable depth, but each level multiplies the work).
ESI is most effective when the shell cache hit rate is high and the fragments are small and fast. It is least effective when the fragments themselves require database access — at which point you have moved the problem from rendering a full page to rendering many small ones.
The procedure
-
Enable ESI processing in VCL. Varnish does not parse ESI unless you tell it to per response.
sub vcl_backend_response { if (beresp.http.Content-Type ~ "text/html") { set beresp.do_esi = true; set beresp.ttl = 1h; } } sub vcl_deliver { if (resp.http.X-ESI-Fragments) { set resp.http.X-ESI-Fragments = obj.hits; } } -
Emit ESI tags from origin. The origin returns the shell with placeholders. Fragments can carry their own cache headers.
<!DOCTYPE html> <html><body> <header>...</header> <esi:include src="/_esi/cart-widget" /> <main>... static product content ...</main> <esi:include src="/_esi/recommendations?user_id=$uid" /> <esi:remove> <!-- fallback HTML for clients that bypass Varnish --> <a href="/cart">View cart</a> </esi:remove> </body></html> -
Define separate TTLs per fragment. The shell is cacheable for hours; the fragments are short-lived.
sub vcl_backend_response { if (bereq.url ~ "^/_esi/cart-widget") { set beresp.ttl = 5s; set beresp.grace = 30s; } else if (bereq.url ~ "^/_esi/recommendations") { set beresp.ttl = 60s; } else if (bereq.url ~ "^/_esi/footer") { set beresp.ttl = 1d; } } -
Handle authentication for personalized fragments. ESI fragments inherit the parent request’s headers — including cookies — so a per-user fragment can read the session.
sub vcl_recv { if (req.esi_level > 0 && req.url ~ "^/_esi/") { # ESI sub-request - skip the cookie-stripping logic return (hash); } } sub vcl_hash { hash_data(req.url); hash_data(req.http.Host); if (req.esi_level > 0 && req.http.Cookie ~ "session=") { hash_data(regsub(req.http.Cookie, ".*session=([^;]+).*", "\1")); } } -
Monitor fragment errors. ESI failures are silent unless you instrument them. Use the
onerror="continue"attribute and watch the stats.varnishstat -1 -f MAIN.esi_errors -f MAIN.esi_warnings -f MAIN.s_resp_bodybytes varnishlog -g request -q 'ESI_xmlerror' -i ReqUrl,ESI_xmlerror | head
Common pitfalls
- Sending ESI fragments to a CDN that does not parse ESI — the literal
<esi:include>tag reaches the browser. Either terminate ESI at the CDN or terminate it at Varnish before the CDN sees the body. - Using ESI for fragments that themselves take 200 ms to render — the page-level latency becomes the slowest fragment, not the slowest origin call.
- Forgetting
<esi:remove>fallback content for clients that hit origin directly (preview environments, dev tools, scrapers). - Letting an authenticated fragment leak cache across users because the hash does not include a session identifier.
- Stacking ESI three layers deep “because it composes” — every layer multiplies the request count and the failure modes.
In the engagements we run, ESI shows up on Drupal, Magento, and bespoke CMS stacks where the editorial team wants page-level caching but the cart and account widgets are uncacheable. Stack Harbor’s role is to make the topology explicit, set the per-fragment TTLs from real measurements, and monitor the assembly. See our managed operations for how we run ESI alongside the rest of the cache.