http cache: keeping it fresh

18
Keeping it fresh David de Boer

Upload: david-de-boer

Post on 11-May-2015

221 views

Category:

Technology


1 download

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

Page 1: HTTP cache: keeping it fresh

Keeping it fresh David de Boer

Page 2: HTTP cache: keeping it fresh

Hello

• I’m David

• Lead developer at Driebit, Amsterdam

• Open source fanatic

Page 3: HTTP cache: keeping it fresh

The problem

• E-learning platform

• Now used by ~50% of Dutch primary and secondary schools

• Slow

Page 4: HTTP cache: keeping it fresh

So cache!

• Keep coding fun

• Keep users happy

Page 5: HTTP cache: keeping it fresh

Caching models

!

• Expiration

• Validation

• Invalidation

Page 6: HTTP cache: keeping it fresh

What we had

• REST API

• Content changes at predictable moments

• Rush hour

Page 7: HTTP cache: keeping it fresh

What we needed

• Fresh data at push of button

• Talk to Symfony as little as possible

Page 8: HTTP cache: keeping it fresh

Back to caching modelsQuick

refreshDon’t talk

to Symfony

Expiration × ✔

Validation ✔ ×

Invalidation ✔ ✔

Page 9: HTTP cache: keeping it fresh

– Phil Karlton

“There are only two hard things in Computer Science: cache invalidation and naming things.”

Page 10: HTTP cache: keeping it fresh

– 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.”

Page 11: HTTP cache: keeping it fresh

Varnishsub  vcl_hit  {    if  (req.request  ==  "PURGE")  {        purge;        error  200  “Purged";    }}

Page 12: HTTP cache: keeping it fresh
Page 13: HTTP cache: keeping it fresh

• Easy to use

• Configure, don’t code

• Lightweight

Page 14: HTTP cache: keeping it fresh

Easy to use

• Copy original route parameters

• Note: don’t use getRouteCollection()!

• $cacheManager    -­‐>invalidateRoute('my_route')    -­‐>flush();

Page 15: HTTP cache: keeping it fresh

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

Page 16: HTTP cache: keeping it fresh

Lightweight

• Collect all routes that will be invalidated.

• Send PURGE requests in kernel.terminate.

• Send them in parallel.

Page 17: HTTP cache: keeping it fresh

Christmas wish list

• Cache tags (X-­‐Id:  123)

• Annotations (Expression Language)

• Message queues

Page 18: HTTP cache: keeping it fresh

Thanks!• [email protected] / @ddeboer_nl

• driebit/http-cache-bundle

• https://github.com/driebit

• https://github.com/ddeboer

• http://driebit.nl