http cache: keeping it fresh
DESCRIPTION
HTTP caching solutions such as Varnish are great for improving web application performance. Filling up the cache is easy, but how do you get rid of cache entries when they become stale? There are three ways: expiration, validation and invalidation. The first two are nice and simple, but have their limitations. The other option, invalidation, is frowned upon for its complexity. Still, it can be very useful. Case in point: an e-learning web platform we built that serves a large number of Dutch pupils and their teachers. I will tell you why and how we added an invalidation scheme to this platform. I will also show you our lightweight Symfony bundle that makes cache invalidation a little less complex.TRANSCRIPT
Keeping it fresh David de Boer
Hello
• I’m David
• Lead developer at Driebit, Amsterdam
• Open source fanatic
The problem
• E-learning platform
• Now used by ~50% of Dutch primary and secondary schools
• Slow
So cache!
• Keep coding fun
• Keep users happy
Caching models
!
• Expiration
• Validation
• Invalidation
What we had
• REST API
• Content changes at predictable moments
• Rush hour
What we needed
• Fresh data at push of button
• Talk to Symfony as little as possible
Back to caching modelsQuick
refreshDon’t talk
to Symfony
Expiration × ✔
Validation ✔ ×
Invalidation ✔ ✔
– Phil Karlton
“There are only two hard things in Computer Science: cache invalidation and naming things.”
– Symfony2 documentation
“You should never need to invalidate cached data…”
“Actually, all reverse proxies provide ways to purge cached data, but you should avoid them as much as possible.”
Varnishsub vcl_hit { if (req.request == "PURGE") { purge; error 200 “Purged"; }}
• Easy to use
• Configure, don’t code
• Lightweight
Easy to use
• Copy original route parameters
• Note: don’t use getRouteCollection()!
• $cacheManager -‐>invalidateRoute('my_route') -‐>flush();
Configure, don’t codedriebit_http_cache: http_cache: varnish: ips: [127.0.0.1, 123.123.123.1] host: yourapp.nl
invalidators: exam_create: origin_routes: [ exams_post ] invalidate_routes: exam_get_collection: ~ # /exams exam_change: origin_routes: [ exam_put, exam_del ] invalidate_routes: exam_get_collection: ~ # /exams exam_get: ~ # /exams/{id} current_exam: ~ # /exams/current
Lightweight
• Collect all routes that will be invalidated.
• Send PURGE requests in kernel.terminate.
• Send them in parallel.
Christmas wish list
• Cache tags (X-‐Id: 123)
• Annotations (Expression Language)
• Message queues
Thanks!• [email protected] / @ddeboer_nl
• driebit/http-cache-bundle
• https://github.com/driebit
• https://github.com/ddeboer
• http://driebit.nl