handling 10k requests per second with symfony and varnish - symfonycon berlin 2016

135
Handling 10k Requests/second With Symfony and Varnish Alexander Lisachenko

Upload: alexander-lisachenko

Post on 07-Jan-2017

1.962 views

Category:

Technology


0 download

TRANSCRIPT

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

Handling 10k Requests/second With Symfony and VarnishAlexander Lisachenko

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

‣ Head of Software Architecture at Alpari (RU) Forex Broker

About me:

lisachenkolisachenko

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

‣ Head of Software Architecture at Alpari (RU) Forex Broker

‣ Have worked with computers since 7 years old

About me:

lisachenkolisachenko

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

‣ 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:

lisachenkolisachenko

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

Author of the Go! AOP Framework‣ http://go.aopphp.com

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

Agenda

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

Agenda‣ We review briefly the process of typical

Symfony site growing

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

Agenda‣ We review briefly the process of typical

Symfony site growing‣ What is Varnish and why it’s so popular

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

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?

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

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

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

Site Growing

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

First deploy to the production

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

First deploy to the production

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

First deploy to the production

1-30 RPS

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

First marketing campaign

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

First marketing campaign

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

First marketing campaign

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

First marketing campaign

30-50 RPS

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

Configuring cacheing layer

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

Configuring cacheing layer

OpCache

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

Configuring cacheing layer

OpCache MemCache

60-250 RPS

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

Preparing several backends

OpCache MemCache

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

Preparing several backends

OpCache MemCache

250-1k RPS

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

Preparing several backends

OpCache MemCache

250-1k RPS

Page 26: Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin 2016
Page 27: Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin 2016
Page 28: Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin 2016

Reverse proxy caching

OpCache MemCache

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

Reverse proxy caching

OpCache MemCache

Varnish

1k-20k RPS

Page 30: Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin 2016
Page 31: Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin 2016

Varnish is an HTTP accelerator designed for content-heavy dynamic web sites as well as heavily consumed APIs.

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

Websites using Varnish

Source: https://trends.builtwith.com

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

Websites using Varnish

Source: https://trends.builtwith.com

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

Simple workflow:

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

Simple workflow:GET /some-web-page

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

Simple workflow:GET /some-web-page

X-Cache: MISS

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

Simple workflow:GET /some-web-page

X-Cache: MISS

GET /some-web-page

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

Simple workflow:GET /some-web-page

X-Cache: MISS

GET /some-web-page

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

Simple workflow:GET /some-web-page

X-Cache: MISS

GET /some-web-page

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

Simple workflow:GET /some-web-page

X-Cache: MISS

GET /some-web-page

First request to the page - Time To Load 200ms

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

Simple workflow:

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

Simple workflow:GET /some-web-page

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

Simple workflow:GET /some-web-page

X-Cache: HIT

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

Simple workflow:GET /some-web-page

X-Cache: HIT

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

Simple workflow:GET /some-web-page

X-Cache: HIT

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

Simple workflow:GET /some-web-page

X-Cache: HIT

Subsequent request to the page - Time To Load 10ms

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

Idea: do not ask your backend as much as possible.

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

How to speed up your site with Varnish?

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

How to speed up your site with Varnish?

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

Read the RFC 7232-7234Your friends:

https://tools.ietf.org/html/rfc7234 - Caching https://tools.ietf.org/html/rfc7232 - Conditional requests

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

Read the RFC 7232-7234

‣ Cache-Control

Your friends:

https://tools.ietf.org/html/rfc7234 - Caching https://tools.ietf.org/html/rfc7232 - Conditional requests

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

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

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

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

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

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

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

Install Varnish

https://www.varnish-cache.org/releases/index.html - Releases and installation guides

For Debian

For FreeBSD

Also available for Amazon Web Services

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

Configure the backend

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

Configure the backend

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

Time for experiments!

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

Test page with 3 widgets, emulating slow query

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

Test page with 3 widgets, emulating slow query

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

…our simple slow action implementation

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

…our simple slow action implementation

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

+0.2s +0.5s+1s

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

Sequential page loads: ~1 second!

+0.2s +0.5s+1s

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

Slow PHP? Slow Symfony? Slow backend?

Page 66: Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin 2016
Page 67: Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin 2016

Make your responses HTTP Cacheable with Varnish

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

Make your responses HTTP Cacheable with Varnish

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

Make your responses HTTP Cacheable with Varnish

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

Looks good…

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

Looks good…

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

Looks good…

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

…until the cache expires

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

…until the cache expires

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

…until the cache expires

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

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.

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

How can we avoid delays on cache expiration?

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

Make your backend requests asynchronous!

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

Make your backend requests asynchronous!

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

Make your backend requests asynchronous!

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

Make your backend requests asynchronous!

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

Serving stale content while invalidating

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

Serving stale content while invalidating

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

Serving stale content while invalidating

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

Serving stale content while invalidating

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

Serving stale content while invalidating

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

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.

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

How can we update each widget separately on the page?

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

Enable Edge-Side Includes (ESI)

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

Enable Edge-Side Includes (ESI)

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

Enable Edge-Side Includes (ESI)

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

Enable Edge-Side Includes (ESI)

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

Enable Edge-Side Includes (ESI)

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

Enable Edge-Side Includes (ESI)

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

Enable Edge-Side Includes (ESI)

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

Common mistake - missed cache header for an ESI block!

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

Common mistake - missed cache header for a ESI block!

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

Common mistake - missed cache header for a ESI block!

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

Page with ESI-blocks

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

Page with ESI-blocks

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

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.

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

VCL tricks for better performance

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

Those cookies…

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

Those cookies…

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

Tips:

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

Tips:‣ Always cache top-level GET-responses;

remove any cookies for them.

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

Tips:‣ Always cache top-level GET-responses;

remove any cookies for them.‣ Only ESI blocks can receive the session

cookie.

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

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.

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

Remove all cookies except session one

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

Remove all cookies except session one

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

Remove all cookies except session one

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

Restore cookie for ESI requests

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

Restore cookie for ESI requests

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

Preparing cookies

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

Preparing cookies

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

Preparing cookies

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

Cacheing with cookies

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

Cacheing with cookies

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

Cacheing with cookies

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

What you will receive?

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

Some live results

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

Some live results

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

There are only two hard things in Computer Science:

cache invalidation and naming things.

-- Phil Karlton

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

Defining the ACL for Varnish

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

Defining the ACL for Varnish

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

Defining the ACL for Varnish

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

Preparing friendly headers

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

Performing PURGE requests

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

Performing PURGE requests

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

Performing PURGE requests

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

Single page refresh

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

Single page refresh

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

Use FOSHttpCache

http://foshttpcache.readthedocs.io/en/stable/index.html - FOSHttpCache Documentation https://github.com/FriendsOfSymfony/FOSHttpCache - Source code

Page 134: Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin 2016
Page 135: Handling 10k requests per second with Symfony and Varnish - SymfonyCon Berlin 2016

Thank you for your attention!

https://github.com/lisachenko https://twitter.com/lisachenko