Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin 2016

4,748 views

Published on

It is believed that the Symfony framework is quite heavy and it can be difficult to develop a website that will be able to work under the high load. It is true, but does this mean that it is impossible to implement a dynamic caching and to update only a small part of entire page as data is updated? This talk will give your an answer to that. It is Varnish, ESI-blocks and load balancing. With hundreds of thousands of unique visitors and million hits a day, we continue to use Symfony, and do not see any problems.

Published in: Technology
  • Be the first to comment

Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin 2016

  1. 1. Handling 10k Requests/second With Symfony and Varnish Alexander Lisachenko
  2. 2. About me: lisachenko lisachenko
  3. 3. ‣ Head of Software Architecture at Alpari (RU) Forex Broker About me: lisachenko lisachenko
  4. 4. ‣ Head of Software Architecture at Alpari (RU) Forex Broker ‣ Have worked with computers since 7 years old About me: lisachenko lisachenko
  5. 5. ‣ Head of Software Architecture at Alpari (RU) Forex Broker ‣ Have worked with computers since 7 years old ‣ Clean code advocate, guru in enterprise architecture About me: lisachenko lisachenko
  6. 6. Author of the Go! AOP Framework ‣ http://go.aopphp.com
  7. 7. Agenda
  8. 8. Agenda ‣ We review briefly the process of typical Symfony site growing
  9. 9. Agenda ‣ We review briefly the process of typical Symfony site growing ‣ What is Varnish and why it’s so popular
  10. 10. Agenda ‣ We review briefly the process of typical Symfony site growing ‣ What is Varnish and why it’s so popular ‣ How to prepare your Symfony site for Varnish?
  11. 11. Agenda ‣ We review briefly the process of typical Symfony site growing ‣ What is Varnish and why it’s so popular ‣ How to prepare your Symfony site for Varnish? ‣ VCL tricks to easily handle 10K requests/second with Symfony
  12. 12. Site Growing
  13. 13. First deploy to the production
  14. 14. First deploy to the production
  15. 15. First deploy to the production 1-30 RPS
  16. 16. First marketing campaign
  17. 17. First marketing campaign
  18. 18. First marketing campaign
  19. 19. First marketing campaign 30-50 RPS
  20. 20. Configuring cacheing layer
  21. 21. Configuring cacheing layer OpCache
  22. 22. Configuring cacheing layer OpCache MemCache 60-250 RPS
  23. 23. Preparing several backends OpCache MemCache
  24. 24. Preparing several backends OpCache MemCache 250-1k RPS
  25. 25. Preparing several backends OpCache MemCache 250-1k RPS
  26. 26. Reverse proxy caching OpCache MemCache
  27. 27. Reverse proxy caching OpCache MemCache Varnish 1k-20k RPS
  28. 28. Varnish is an HTTP accelerator designed for content-heavy dynamic web sites as well as heavily consumed APIs.
  29. 29. Websites using Varnish Source: https://trends.builtwith.com
  30. 30. Websites using Varnish Source: https://trends.builtwith.com
  31. 31. Simple workflow:
  32. 32. Simple workflow: GET /some-web-page
  33. 33. Simple workflow: GET /some-web-page X-Cache: MISS
  34. 34. Simple workflow: GET /some-web-page X-Cache: MISS GET /some-web-page
  35. 35. Simple workflow: GET /some-web-page X-Cache: MISS GET /some-web-page
  36. 36. Simple workflow: GET /some-web-page X-Cache: MISS GET /some-web-page
  37. 37. Simple workflow: GET /some-web-page X-Cache: MISS GET /some-web-page First request to the page - Time To Load 200ms
  38. 38. Simple workflow:
  39. 39. Simple workflow: GET /some-web-page
  40. 40. Simple workflow: GET /some-web-page X-Cache: HIT
  41. 41. Simple workflow: GET /some-web-page X-Cache: HIT
  42. 42. Simple workflow: GET /some-web-page X-Cache: HIT
  43. 43. Simple workflow: GET /some-web-page X-Cache: HIT Subsequent request to the page - Time To Load 10ms
  44. 44. Idea: do not ask your backend as much as possible.
  45. 45. How to speed up your site with Varnish?
  46. 46. How to speed up your site with Varnish?
  47. 47. Read the RFC 7232-7234 Your friends: https://tools.ietf.org/html/rfc7234 - Caching https://tools.ietf.org/html/rfc7232 - Conditional requests
  48. 48. Read the RFC 7232-7234 ‣ Cache-Control Your friends: https://tools.ietf.org/html/rfc7234 - Caching https://tools.ietf.org/html/rfc7232 - Conditional requests
  49. 49. Read the RFC 7232-7234 ‣ Cache-Control ‣ Expires Your friends: https://tools.ietf.org/html/rfc7234 - Caching https://tools.ietf.org/html/rfc7232 - Conditional requests
  50. 50. Read the RFC 7232-7234 ‣ Cache-Control ‣ Expires ‣ ETag Your friends: https://tools.ietf.org/html/rfc7234 - Caching https://tools.ietf.org/html/rfc7232 - Conditional requests
  51. 51. Read the RFC 7232-7234 ‣ Cache-Control ‣ Expires ‣ ETag ‣ Last-Modified Your friends: https://tools.ietf.org/html/rfc7234 - Caching https://tools.ietf.org/html/rfc7232 - Conditional requests
  52. 52. Install Varnish https://www.varnish-cache.org/releases/index.html - Releases and installation guides For Debian For FreeBSD Also available for Amazon Web Services
  53. 53. Configure the backend
  54. 54. Configure the backend
  55. 55. Time for experiments!
  56. 56. Test page with 3 widgets, emulating slow query
  57. 57. Test page with 3 widgets, emulating slow query
  58. 58. …our simple slow action implementation
  59. 59. …our simple slow action implementation
  60. 60. +0.2s +0.5s +1s
  61. 61. Sequential page loads: ~1 second! +0.2s +0.5s +1s
  62. 62. Slow PHP? Slow Symfony? Slow backend?
  63. 63. Make your responses HTTP Cacheable with Varnish
  64. 64. Make your responses HTTP Cacheable with Varnish
  65. 65. Make your responses HTTP Cacheable with Varnish
  66. 66. Looks good…
  67. 67. Looks good…
  68. 68. Looks good…
  69. 69. …until the cache expires
  70. 70. …until the cache expires
  71. 71. …until the cache expires
  72. 72. Pros and cons of simple cacheing + Page can be cached by Varnish, allowing for faster responses. + Very simple to configure and use. - Delays in the response time after cache expiration. - We can not update information in the blocks without a full page refresh. - We need to render all 3 blocks at once.
  73. 73. How can we avoid delays on cache expiration?
  74. 74. Make your backend requests asynchronous!
  75. 75. Make your backend requests asynchronous!
  76. 76. Make your backend requests asynchronous!
  77. 77. Make your backend requests asynchronous!
  78. 78. Serving stale content while invalidating
  79. 79. Serving stale content while invalidating
  80. 80. Serving stale content while invalidating
  81. 81. Serving stale content while invalidating
  82. 82. Serving stale content while invalidating
  83. 83. Pros and cons of async cacheing + Page can be cached by Varnish, allowing for faster responses. + Very simple to configure and use. + No delays after cache expiration. - We can not update information in the blocks without full page refresh. - We need to render all 3 blocks at once.
  84. 84. How can we update each widget separately on the page?
  85. 85. Enable Edge-Side Includes (ESI)
  86. 86. Enable Edge-Side Includes (ESI)
  87. 87. Enable Edge-Side Includes (ESI)
  88. 88. Enable Edge-Side Includes (ESI)
  89. 89. Enable Edge-Side Includes (ESI)
  90. 90. Enable Edge-Side Includes (ESI)
  91. 91. Enable Edge-Side Includes (ESI)
  92. 92. Common mistake - missed cache header for an ESI block!
  93. 93. Common mistake - missed cache header for a ESI block!
  94. 94. Common mistake - missed cache header for a ESI block!
  95. 95. Page with ESI-blocks
  96. 96. Page with ESI-blocks
  97. 97. Pros and cons of async ESI cacheing + Page can be cached by Varnish, allowing for faster responses. + No delays after cache expiration. + We can update information in the blocks without full page refresh. + We render and cache each block separately. This will result in less memory usage and better hit rate. - Can be dangerous if used without control.
  98. 98. VCL tricks for better performance
  99. 99. Those cookies…
  100. 100. Those cookies…
  101. 101. Tips:
  102. 102. Tips: ‣ Always cache top-level GET-responses; remove any cookies for them.
  103. 103. Tips: ‣ Always cache top-level GET-responses; remove any cookies for them. ‣ Only ESI blocks can receive the session cookie.
  104. 104. Tips: ‣ Always cache top-level GET-responses; remove any cookies for them. ‣ Only ESI blocks can receive the session cookie. ‣ Stateless ESI-blocks will not receive any cookies at all.
  105. 105. Remove all cookies except session one
  106. 106. Remove all cookies except session one
  107. 107. Remove all cookies except session one
  108. 108. Restore cookie for ESI requests
  109. 109. Restore cookie for ESI requests
  110. 110. Preparing cookies
  111. 111. Preparing cookies
  112. 112. Preparing cookies
  113. 113. Cacheing with cookies
  114. 114. Cacheing with cookies
  115. 115. Cacheing with cookies
  116. 116. What you will receive?
  117. 117. Some live results
  118. 118. Some live results
  119. 119. There are only two hard things in Computer Science: cache invalidation and naming things. -- Phil Karlton
  120. 120. Defining the ACL for Varnish
  121. 121. Defining the ACL for Varnish
  122. 122. Defining the ACL for Varnish
  123. 123. Preparing friendly headers
  124. 124. Performing PURGE requests
  125. 125. Performing PURGE requests
  126. 126. Performing PURGE requests
  127. 127. Single page refresh
  128. 128. Single page refresh
  129. 129. Use FOSHttpCache http://foshttpcache.readthedocs.io/en/stable/index.html - FOSHttpCache Documentation https://github.com/FriendsOfSymfony/FOSHttpCache - Source code
  130. 130. Thank you for your attention! https://github.com/lisachenko https://twitter.com/lisachenko

×