Skip to content

[vcl] don't set TTL to 0 #28927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/code/Magento/PageCache/etc/varnish4.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ sub vcl_backend_response {

# cache only successfully responses and 404s
if (beresp.status != 200 && beresp.status != 404) {
set beresp.ttl = 0s;
set beresp.ttl = 120s;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of caching 500 / 503 / 403?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't cache them because of the following set beresp.uncacheable = true;. Instead Varnish will remember that these objects are uncacheable, and will disable request collapsing for them.

If you keep the TTL to 0, you get into something like this:

  • 20 users request with the same object
  • varnish only goes to the backend once (request coalescing)
  • varnish realizes it cannot reuse the object, so it only serves it to one client
  • we now have 19 users request with the same object
  • varnish only goes to the backend once
  • varnish realizes ....
    rinse, repeat. Ultimately, the last request needs to wait 20 round-trip to the backend before being delivered.

If you set the TTL to some positive value AND the uncacheable bit to true:

  • 20 users request with the same object
  • varnish only goes to the backend once (request coalescing)
  • varnish realizes it cannot reuse the object, so it only serves it to one client, but remembers it as uncacheable
  • the 19 remaining requests are woken up, and can all go to the backend in parallel

it's one of the trickiest Varnish gotchas, so please let me know if I wasn't clear.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gquintard Your explanation sounds fine for me. ✔️

set beresp.uncacheable = true;
return (deliver);
} elsif (beresp.http.Cache-Control ~ "private") {
Expand Down
8 changes: 3 additions & 5 deletions app/code/Magento/PageCache/etc/varnish5.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,9 @@ sub vcl_backend_response {
}

# cache only successfully responses and 404s
if (beresp.status != 200 && beresp.status != 404) {
set beresp.ttl = 0s;
set beresp.uncacheable = true;
return (deliver);
} elsif (beresp.http.Cache-Control ~ "private") {
if (beresp.status != 200 &&
beresp.status != 404 &&
beresp.http.Cache-Control ~ "private") {
set beresp.uncacheable = true;
set beresp.ttl = 86400s;
return (deliver);
Expand Down
10 changes: 4 additions & 6 deletions app/code/Magento/PageCache/etc/varnish6.vcl
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,10 @@ sub vcl_backend_response {
set beresp.http.X-Magento-Cache-Control = beresp.http.Cache-Control;
}

# cache only successfully responses and 404s
if (beresp.status != 200 && beresp.status != 404) {
set beresp.ttl = 0s;
set beresp.uncacheable = true;
return (deliver);
} elsif (beresp.http.Cache-Control ~ "private") {
# cache only successfully responses and 404s that are not marked as private
if (beresp.status != 200 &&
beresp.status != 404 &&
beresp.http.Cache-Control ~ "private") {
set beresp.uncacheable = true;
set beresp.ttl = 86400s;
return (deliver);
Expand Down