Skip to content

[Performance] Varnish user context hash vcl should use the hash in vcl_hash #496

Closed
@bastnic

Description

@bastnic

We use Varnish and user context hash on very high traffic website. And we experienced various performance and stability issue I wanted to share with you. All credits for the finding does not belong to me but to my client and his exchanges with Varnish support. I think it's important enough to share it here.

User context hash is based on a new header that we vary on. This header itself is calculated on the /_fos_user_context_hash call that itself is vary against the cookie or authorization header.

If we use user context hash it's because we want the cardinality of an user context page to be as low as posible to avoid pointless computation.
But the cardinality of the _fos_user_context_hash is linked to the number of visitor, and it can be very big. We relates on the Vary header to make the distinction and find in all the cached variants.

The vary header is like a sql where on a not indexed field, against a possible enormous table if you have a traffic like this one.
To fix it we just need to index this field.

sub vcl_hash {
    if (req.url == "/_fos_user_context_hash") {
        hash_data(req.http.<Cookie or Autorization>);
    }
    # VS: NOTE: No return, fallthrough to builtin VCL
}

Before on our load test:

image

After:

image

On production, we reduced our number of varnish servers by ten.

The comportment is now explain in the next release varnish documentation along with a warning. The warning is triggered if there is more than 10 variants. And I hope everybody using user context hash has more than 10 logged in users otherwise...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions