When dynamic becomes static

download When dynamic becomes static

If you can't read please download the document

Transcript of When dynamic becomes static

1. When dynamic becomes static (the next step in web caching techniques) Wim Godden Cu.be Solutions @wimgtr 2. Who am I ? Wim Godden (@wimgtr) 3. Where I'm from 4. Where I'm from 5. Where I'm from 6. Where I'm from 7. Where I'm from 8. Where I'm from 9. My town 10. My town 11. Belgium the traffic 12. Who am I ? Wim Godden (@wimgtr) Founder of Cu.be Solutions (http://cu.be) Open Source developer since 1997 Developer of OpenX, PHPCompatibility, PHPConsistent, ... Speaker at Open Source conferences 13. Who are you ? Developers ? System/network engineers ? Managers ? 14. Quick note Whenever you see feel free to think 15. To understand the present Understand the past 16. The Stone Age New blog post by : caveman003 17. Pre-dynamic : draw it and make html 18. The Egyptian Era 19. Old-school dynamic : 'rebuild-every-time' 20. The Industrial Revolution 21. Dynamic : let's cache 22. Extra ! Extra ! 23. Dynamic content in static content 24. The Modern Era 25. More load, more webservers 26. Pushing updates to cache 27. Today 28. Adding reverse proxy caching 29. Typical website structure Article content page Page content Header Latest news Navigation 30. Caching blocks with individual TTLs Article content page Page content Top header (TTL = 2h) Latest news Navigation (TTL = 1h) 31. Caching blocks with individual TTLs Article content page Page content (TTL = 30m) Top header (TTL = 2h) Latest news (TTL = 2m) Navigation (TTL = 1h) 32. ESI how it works GET /pageGET /page 33. ESI how it works ...

... GET /pageGET /page 34. ESI how it works

Login

GET /top 35. ESI how it works ...

... GET /pageGET /page 36. ESI how it works ...

Login

... GET /pageGET /page 37. Varnish - what can/can't be cached ? Can : Static pages Images, js, css Static parts of pages that don't change often (ESI) Can't : POST requests Very large files (it's not a file server !) Requests with Set-Cookie User-specific content 38. ESI no caching on user-specific content ? Logged in as : Wim Godden 5 messages TTL = 5minTTL=1h TTL = 0s ? 39. Error... does not compute ! 40. The semi-functional Varnish way Use VCL to attach session id or UUID to cache entries if( req.http.Cookie ~ "myapp_unique_user_id" ) { set req.http.X-Varnish-Hashed-On = regsub( req.http.Cookie, "^.*? myapp_unique_user_id=([^;]*);*.*$", "1" ); } Painful, messy configuration Inefficient and messy way to update data POST /sendmessagePOST /sendmessage PURGE /top?UUID GET /page GET /top?UUID DB 41. The almost functional Varnish way Varnish module : VMOD-Memcached Connects Varnish directly to Memcached Advantage : It works It will speed a few things up Disadvantage : A pain to set up No plug&play DIY ! Only works on info stored in session 42. Back in 2010 43. Avoid hitting the backend GET /page DB 44. No more backend GET /page + SLIC 45. Requesting /page (1st time) Nginx Shared memory 1 2 3 4 /page /page ... {% slic:include(src="/top", key=top, session=true) %} {% slic:include(src=/nav", key=nav) %} {% slic:include(src="/article/id/732", key=article732) %} ... 46. Requesting /page SLIC subrequests (1st time) Nginx 1 2 3 /nav /article732 /top_slic_ss (in SLIC session) /nav /article/id/732 /top (with session cookie) 47. Requesting /page (next time) Nginx Shared memory 1 2 /page /nav /article732 /top_slic_ss (in SLIC session) /page 48. SLIC on Nginx {% slic:include(key="article732", src="/article/732") %} {% slic:include( key="nav", src="/nav") %} {% slic:include(key="top", src="/top", session="true") %} Logged in as : Wim Godden 5 messages ??? 49. New message is sent... POST /send DB insert into... set(...) top (in SLIC session) 50. Advantages No repeated GET hits to webserver anymore ! At login : POST warm up the cache ! No repeated hits for user-specific content Not even for non-specific content 51. News added addnews() method DB insert into... set(...) Memcache key /news 52. First release : ESI Part of the ESI 1.0 spec Only relevant features implemented Extension for dynamic session support But : unavailable for copyright reasons 53. Rebuilt from scratch : SLIC Control structures : if/else, foreach Variable handling Strings : concatenation, substring, Exception handling, header manipulation, JSON support Much (much) faster 54. SLIC code samples You are logged in as : {% slic:session_var("person_name") %} 55. SLIC code samples {% slic:if (slic:session_var('isAdmin') == 1) %} {% slic:include(key="admin-buttons", src="/admin-buttons.php") %} {% slic:else %}

{% slic:include(key="user-buttons", src="/user-buttons.php") %}

{% slic:endif %} 56. SLIC code samples (old version !) 57. Approaches full block

You are logged in as : Wim Godden

5 messages

Logged in as : Wim Godden 5 messages top_432 58. Approaches individual variables

You are logged in as : {% slic:session_var("person_name") %}

{% slic:session_var(messages) %} messages

Logged in as : Wim Godden 5 messages top_slic_ss (session-specific) 59. Approaches JSON

You are logged in as : %{ slic:session_var("userData").person_name %}

%{ slic:session_var(userData).message_count %} messages

Logged in as : Wim Godden 5 messages top_slic_ss (session-specific) 60. Identifying the user In Nginx configuration : slic_session_cookie Defined by language (or configurable) slic_session_identifier Defined by you Example for PHP : slic_session_cookie PHPSESSID slic_session_identifier UserID 61. Identifying the user Nginx + SLIC Cookie : PHPSESSID = jpsidc1po35sq9q3og4f3hi6e2 get UserID_jpsidc1po35sq9q3og4f3hi6e2432 62. Retrieving user specific content Nginx + SLIC get userData_432 Cookie : PHPSESSID = jpsidc1po35sq9q3og4f3hi6e2 63. Template system find the last template Nginx SLIC Shared memory Template ? Request Last updated ? 64. Template system new/changed template Nginx SLIC LuaJIT Shared memory Fetch template Convert to LUA Compile Store 65. Template system unchanged template Nginx SLIC Shared memory Template ?LuaJIT Last updated ? 66. What's the result ? 67. Why is it so much faster ? 68. Setting it up Use OpenResty (Nginx + tons of Nginx modules) http { lua_package_path '/path/to/slic-and-other-libraries'; init_by_lua_file 'slic.luac'; server { listen 80; server_name www.yoursite.com; location / { content_by_lua 'slic:run()'; } } } 69. Code changes Required Template conversion Push-to-DB Push-to-DB + Push-to-Cache Choice : If user is logged in push updates to cache If user is not logged in warm up cache on login 70. Availability Good news : The concept is solid : ESI version stable at 4 customers Open Source Bad news : First customer holds copyrights Total rebuild Beta 1 almost ready (include, variable handling, ) Final release : Q3 2015 (?) Site : http://slic.cu.be Code : Github (after beta 1 release) 71. Some technical details Written in Lua (with LuaJIT) Each SLIC:include is a subrequest Groups cache key requests together for multiget Shares cache results across all subrequests Template compilation Memcached implemented Redis and others in the pipeline Not RFC compliant yet Future (1.0 or beyond) : Translation functionality Integration with LuaJIT FFI direct C calls Any suggestions ? 72. So... 73. Questions ? 74. Questions ? 75. Thanks ! @wimgtr [email protected] Please provide some feedback : http://joind.in/13725