Skip to main content
Handling 10k Requests/second
With Symfony and Varnish
Alexander Lisachenko
About me:
lisachenko
lisachenko
‣ Head of Software Architecture
at Alpari (RU) Forex Broker
About me:
lisachenko
lisachenko
‣ Head of Software Architecture
at Alpari (RU) Forex Broker
‣ Have worked with computers
since 7 years old
About me:
lisachenko
lisachenko
‣ 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
Author of the Go! AOP
Framework
‣ http://go.aopphp.com
Agenda
Agenda
‣ We review briefly the process of typical
Symfony site growing
Agenda
‣ We review briefly the process of typical
Symfony site growing
‣ What is Varnish and why it’s so popular
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?
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
Site Growing
First deploy to the production
First deploy to the production
First deploy to the production
1-30 RPS
First marketing campaign
First marketing campaign
First marketing campaign
First marketing campaign
30-50 RPS
Configuring cacheing layer
Configuring cacheing layer
OpCache
Configuring cacheing layer
OpCache MemCache
60-250 RPS
Preparing several backends
OpCache MemCache
Preparing several backends
OpCache MemCache
250-1k RPS
Preparing several backends
OpCache MemCache
250-1k RPS
Reverse proxy caching
OpCache MemCache
Reverse proxy caching
OpCache MemCache
Varnish
1k-20k RPS
Varnish is an HTTP accelerator designed
for content-heavy dynamic web sites as
well as heavily consumed APIs.
Websites using Varnish
Source: https://trends.builtwith.com
Websites using Varnish
Source: https://trends.builtwith.com
Simple workflow:
Simple workflow:
GET /some-web-page
Simple workflow:
GET /some-web-page
X-Cache: MISS
Simple workflow:
GET /some-web-page
X-Cache: MISS
GET /some-web-page
Simple workflow:
GET /some-web-page
X-Cache: MISS
GET /some-web-page
Simple workflow:
GET /some-web-page
X-Cache: MISS
GET /some-web-page
Simple workflow:
GET /some-web-page
X-Cache: MISS
GET /some-web-page
First request to the page - Time To Load 200ms
Simple workflow:
Simple workflow:
GET /some-web-page
Simple workflow:
GET /some-web-page
X-Cache: HIT
Simple workflow:
GET /some-web-page
X-Cache: HIT
Simple workflow:
GET /some-web-page
X-Cache: HIT
Simple workflow:
GET /some-web-page
X-Cache: HIT
Subsequent request to the page - Time To Load 10ms
Idea: do not ask your backend
as much as possible.
How to speed up your site
with Varnish?
How to speed up your site
with Varnish?
Read the RFC 7232-7234
Your friends:
https://tools.ietf.org/html/rfc7234 - Caching
https://tools.ietf.org/html/rfc7232 - Conditional requests
Read the RFC 7232-7234
‣ Cache-Control
Your friends:
https://tools.ietf.org/html/rfc7234 - Caching
https://tools.ietf.org/html/rfc7232 - Conditional requests
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
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
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
Install Varnish
https://www.varnish-cache.org/releases/index.html - Releases and installation guides
For Debian
For FreeBSD
Also available for Amazon Web Services
Configure the backend
Configure the backend
Time for experiments!
Test page with 3 widgets, emulating slow query
Test page with 3 widgets, emulating slow query
…our simple slow action implementation
…our simple slow action implementation
+0.2s +0.5s
+1s
Sequential page loads: ~1 second!
+0.2s +0.5s
+1s
Slow PHP? Slow Symfony?
Slow backend?
Make your responses HTTP
Cacheable with Varnish
Make your responses HTTP
Cacheable with Varnish
Make your responses HTTP
Cacheable with Varnish
Looks good…
Looks good…
Looks good…
…until the cache expires
…until the cache expires
…until the cache expires
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.
How can we avoid delays on cache
expiration?
Make your backend
requests asynchronous!
Make your backend
requests asynchronous!
Make your backend
requests asynchronous!
Make your backend
requests asynchronous!
Serving stale content while invalidating
Serving stale content while invalidating
Serving stale content while invalidating
Serving stale content while invalidating
Serving stale content while invalidating
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.
How can we update each widget
separately on the page?
Enable Edge-Side Includes
(ESI)
Enable Edge-Side Includes
(ESI)
Enable Edge-Side Includes
(ESI)
Enable Edge-Side Includes
(ESI)
Enable Edge-Side Includes
(ESI)
Enable Edge-Side Includes
(ESI)
Enable Edge-Side Includes
(ESI)
Common mistake - missed cache header
for an ESI block!
Common mistake - missed cache header
for a ESI block!
Common mistake - missed cache header
for a ESI block!
Page with ESI-blocks
Page with ESI-blocks