DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Varnish: Purging the Cache

Varnish: Purging the Cache

Mark Needham user avatar by
Mark Needham
·
Oct. 15, 12 · Interview
Like (0)
Save
Tweet
Share
11.69K Views

Join the DZone community and get the full member experience.

Join For Free

We’re using varnish to cache all the requests that come through our web servers and especially in our pre-production environments we deploy quite frequently and want to see the changes that we’ve made.

This means that we need to purge the pages we’re accessing from varnish so that it will actually pass the request through to the application server and serve up the latest version of the page.

For some reason my google-fu when trying to remember/work out how to do this has always been weak but my colleague Shodhan helped me understand how to do this today so I thought I better record it so I don’t forget!

The way we’ve configured varnish to allow us to purge entries is similar to that described on the purging and banning page.

We have the following code in our /etc/varnish/default.vcl:

acl purge_acl {
  "localhost";
}

# purge individual URLs from the cache
if(req.request == "PURGE") {
  if(!client.ip ~ purge_acl) {
    error 405 "Not allowed";
  } else {
    purge("req.url == " req.url);

    error 200 "Purged";
  }
}

We only allow purging to be done if you’re on the machine i.e. your IP needs to be localhost and if you pass a request method of ‘PURGE’ that will remove that specific URL from varnish.

Any requests made will go first through nginx before being passed onto varnish.

If we start off with a URL which is already cached:

$ curl --insecure -I -H "Host: www.realhostgoeshere.com" https://localhost
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 10 Oct 2012 23:21:36 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 12929
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
Vary: Accept-Encoding
Status: 200 OK
Cache-Control: max-age=1800, public
X-UA-Compatible: IE=Edge,chrome=1
ETag: "9b2569322b89a71aca8c67f5f2b3a873"
X-Request-Id: 9c783ff4386fa9a7937f31d22854dcc8
X-Varnish: 1045982044 1045982042
Via: 1.1 varnish
age: 0
X-Cache: HIT
Strict-Transport-Security: max-age=2419200

We can see that we got a varnish cache hit based on the ‘X-Cache’ header returned on the second last line.

To purge the cache for the root URL we’d do this:

$ curl --insecure -I -H "Host: www.realhostgoeshere.com" https://localhost -X PURGE
HTTP/1.1 200 Purged
Server: nginx
Date: Wed, 10 Oct 2012 23:22:46 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 449
Connection: keep-alive
Vary: Accept-Encoding
Retry-After: 0
X-Varnish: 1045982047
Age: 0
Via: 1.1 varnish
X-Cache: MISS
Strict-Transport-Security: max-age=2419200

The 200 response code tells us that we were successful with our purging although from my brief experiments it seems like you would get that response code even if you purged a URL that didn’t exist!

If we hit that URL again:

curl --insecure -I -H "Host: www.realhostgoeshere.com" https://localhost
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 10 Oct 2012 23:23:17 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 12929
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
Vary: Accept-Encoding
Status: 200 OK
Cache-Control: max-age=1800, public
X-UA-Compatible: IE=Edge,chrome=1
ETag: "9b2569322b89a71aca8c67f5f2b3a873"
X-Request-Id: bfcf1568382ce42ee0f9d4245b23f5cd
X-Varnish: 1045982048
Via: 1.1 varnish
age: 0
X-Cache: MISS
Strict-Transport-Security: max-age=2419200

We can see that it’s been purge and varnish therefore had to go and request the page from the application server. If we hit it one more time we can see that it’s now cached again!

curl --insecure -I -H "Host: www.realhostgoeshere.com" https://localhost
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 10 Oct 2012 23:26:43 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 12929
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
Vary: Accept-Encoding
Status: 200 OK
Cache-Control: max-age=1800, public
X-UA-Compatible: IE=Edge,chrome=1
ETag: "9b2569322b89a71aca8c67f5f2b3a873"
X-Request-Id: bfcf1568382ce42ee0f9d4245b23f5cd
X-Varnish: 1045982050 1045982048
Via: 1.1 varnish
age: 0
X-Cache: HIT
Strict-Transport-Security: max-age=2419200
Cache (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 31 Flavors of Data Lineage and Why Vanilla Doesn’t Cut It
  • DevOps Roadmap for 2022
  • Data Mesh vs. Data Fabric: A Tale of Two New Data Paradigms
  • Three SQL Keywords in QuestDB for Finding Missing Data

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: