Invalidation Notifications—Notifications—Addendum Notification ...
Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA...
Transcript of Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA...
![Page 1: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/1.jpg)
Data Driven Cache Invalidation
JDCon-East 2011New York City, USA
Magnus [email protected]
PRODUCTS • CONSULTING • APPLICATION MANAGEMENT • IT OPERATIONS • SUPPORT • TRAINING
![Page 2: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/2.jpg)
What's this all about● We like to build advanced websites● We want them to be popular● Hopefully they become popular● They fall over
● So we add caching● Which adds new issues
![Page 3: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/3.jpg)
Where to cache● Many different locations to cache:
● In the database server (buffer cache)● In the database server (“query cache”)● In the application server● Before the application server (“http
cache”)● In the client
● The closer to the client, the more efficient
![Page 4: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/4.jpg)
Typical architecture
DatabaseApplication
serverCache
![Page 5: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/5.jpg)
Typical architecture
DatabaseApplication
serverCache
GET /news/123GET /news/123GET /news/123GET /news/123...
GET /news/123SELECT * FROMnews INNER JOIN ……WHERE id=123
![Page 6: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/6.jpg)
In this presentation● Let's build a blog!● A really simple one!● But we're popular! (right?)
![Page 7: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/7.jpg)
Intro to the application● We'll use PostgreSQL (d'uh)● We'll use django
● Theory is framework independent● Django-admin makes life easy...
● We'll use Varnish
![Page 8: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/8.jpg)
Let's create the application
$ django-admin startproject demo
$ manage.py startapp blog
● Configure our database connection● Enable the django admin site● Add our application
![Page 9: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/9.jpg)
Create a simple model● We're just going to hold blog posts● Title, date/time, and contents● And add it to the admin site● Sync the database
![Page 10: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/10.jpg)
Create simple views● One view to list all blog posts
● Served up as /● One view to show the posts
● Served up as /<blog post id>/● (let's ignore nice URLs and CSS for now)
![Page 11: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/11.jpg)
Try it out● Using local django web server
$ ./manage.py runserver● Each click causes appserver call● Which in turn causes db query
![Page 12: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/12.jpg)
Add varnish to the mix● Very simple default install:
● Relay all URLs to localhost:8000● Cache all pages for 1 hour● Ignore cookie-related issues
– (this is not a varnish session, after all...)
![Page 13: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/13.jpg)
Issues with caching● Some content rapidly go stale
● Solution: cache only a short time!● Some content is very long-lived
● Solution: cache a long time!● Sometimes unpredictable
● Per-url or per-request cache values become sub-optimal
![Page 14: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/14.jpg)
On-demand invalidation● Cache most objects for a long time● Explicitly remove them from the cache
● When object modified in app● When dependent object modified in app● ...
![Page 15: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/15.jpg)
Cache invalidation
DatabaseApplication
serverCache
purge /news/*
DatabaseApplication
serverCache
GET /news/123GET /news/123GET /news/123GET /news/123...
GET /news/123SELECT * FROMnews INNER JOIN ……WHERE id=123
![Page 16: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/16.jpg)
What about other apps!
DatabaseApplication
serverCache
purge /news/*
DatabaseApplication
serverCache
GET /news/123GET /news/123GET /news/123GET /news/123...
GET /news/123 SELECT * FROM
news INNER JOIN ……WHERE id=123
Application X
Application Y
Application Z
![Page 17: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/17.jpg)
Invalidate from the db!
DatabaseApplication
serverCache
purge /news/*
DatabaseApplication
serverCache
GET /news/123GET /news/123GET /news/123GET /news/123...
GET /news/123 SELECT * FROM
news INNER JOIN ……WHERE id=123
Application X
Application Y
Application Z
![Page 18: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/18.jpg)
Ups and Downs● Upside
● Cache invalidation when relevant data changes
● Regardless of origin of change● Downside
● Database needs to gain URL knowledge● Breaks “clean abstraction model”
![Page 19: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/19.jpg)
Invalidation: method 1● Create a trigger on the blog_post table● Have it send off a varnish purge request
![Page 20: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/20.jpg)
Invalidation: method 2● Create a trigger on the blog_post table● Trigger inserts the request in a queue
table● Trigger on the queue table fires a NOTIFY● Daemon listens to notifies and sends
purge requests
![Page 21: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/21.jpg)
Invalidation: method 3● We just created a trivial queue● There are ready-made queues out there● Pgq!● High performance and efficient● Offload work of dealing with multiple
caches etc
![Page 22: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/22.jpg)
Install pgq● Install pgq in the database● Create our queue● Start a “ticker” process
![Page 23: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/23.jpg)
Write simple consumer● Consumes events from pgq● Generates varnish purge requests● Run one consumer for each varnish
server
![Page 24: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/24.jpg)
Pgq hand-holding● Needs almost nothing● But don't forget to monitor it!● pgqadm.py pgq.ini status● Or munin plugin
![Page 25: Data Driven Cache Invalidation · Data Driven Cache Invalidation JDCon-East 2011 New York City, USA Magnus Hagander magnus@hagander.net PRODUCTS • CONSULTING • APPLICATION MANAGEMENT](https://reader034.fdocuments.in/reader034/viewer/2022051918/600a87dc34b5c25b8e083276/html5/thumbnails/25.jpg)
Final words● Keep setting cache expiry times
● s-maxage etc● Then use purging only when needed● You don't want thousands per second