139
Sensible Scaling Rowan Merewood

Sensible scaling

Embed Size (px)

DESCRIPTION

Talk from 4Developers '12 and PHP Barcelona '11 It’s fun to architect your application to handle millions of pageviews, but in reality that’s time where you could be adding features. We’ll examine some practical solutions for designing your platform to deal with increasing traffic and how to add those features on an incremental basis. This will take us through options for scaling the code and additional methods for scaling the infrastructure.

Citation preview

Page 1: Sensible scaling

Sensible Scaling

Rowan Merewood

Page 2: Sensible scaling

Sensible Scaling

Rowan Merewood

“If your application doesn't scale, it's your fault not mine.” – Rasmus Lerdorf (@rasmus)

Page 3: Sensible scaling

Who?

Page 4: Sensible scaling

Who?

● @rowan_m

Page 5: Sensible scaling

Who?

● @rowan_m

● Software Engineer& Team Lead

Page 6: Sensible scaling

Who?

● @rowan_m

● Software Engineer& Team Lead

● @ibuildings & @techportal

Page 7: Sensible scaling

Why?

Page 8: Sensible scaling

Why?

● I've built small apps

Page 9: Sensible scaling

Why?

● I've built small apps● and pretty large ones

Page 10: Sensible scaling

Why?

● I've built small apps● and pretty large ones

● I've seen massive over-engineering

Page 11: Sensible scaling

Why?

● I've built small apps● and pretty large ones

● I've seen massive over-engineering

● and platforms that cannot scale

Page 12: Sensible scaling

TDD Principles

Page 13: Sensible scaling

TDD Principles

● Write only enough code to pass the test

Page 14: Sensible scaling

TDD Principles

● Write only enough code to pass the test

● Do not over-engineer

Page 15: Sensible scaling

TDD Principles

● Write only enough code to pass the test

● Do not over-engineer

● Don't give away work for free!

Page 16: Sensible scaling

Build for now

Page 17: Sensible scaling

Build for now

● Don't be afraid to throw code away

Page 18: Sensible scaling

Build for now

● Don't be afraid to throw code away

● XP's spikes

Page 19: Sensible scaling

Build for now

● Don't be afraid to throw code away

● XP's spikes

● Take advantage of current tech. without tying yourself to it

Page 20: Sensible scaling

Who are your users?

Page 21: Sensible scaling

Who are your users?

● Lots of short visits?

Page 22: Sensible scaling

Who are your users?

● Lots of short visits?● Anonymous or

session-based?

Page 23: Sensible scaling

Who are your users?

● Lots of short visits?● Anonymous or

session-based?● Long, complex

interactions?

Page 24: Sensible scaling

Who are your users?

● Lots of short visits?● Anonymous or

session-based?● Long, complex

interactions?● Subscribers or

free users?

Page 25: Sensible scaling

Monitor and Analyse

http://community.plus.net/blog/2011/10/24/a-record-setting-manchester-derby/

Page 26: Sensible scaling

Monitor and Analyse

● Predict usage patterns

http://community.plus.net/blog/2011/10/24/a-record-setting-manchester-derby/

Page 27: Sensible scaling

Monitor and Analyse

● Predict usage patterns

● Plan for peaks and troughs

http://community.plus.net/blog/2011/10/24/a-record-setting-manchester-derby/

Page 28: Sensible scaling

Profiling

Page 29: Sensible scaling

Profiling

● You do not know what is slow

Page 30: Sensible scaling

Profiling

● You do not know what is slow

● Ensure you measure more than just code!

Page 31: Sensible scaling

Profiling

Page 32: Sensible scaling

Profiling

● PHP - Xdebug – Derick Rethans (@derickr)● Kcachegrind, Webgrind, etc.

Page 33: Sensible scaling

Profiling

● PHP - Xdebug – Derick Rethans (@derickr)● Kcachegrind, Webgrind, etc.

● PHP – XHProf

Page 34: Sensible scaling

Profiling

● PHP - Xdebug – Derick Rethans (@derickr)● Kcachegrind, Webgrind, etc.

● PHP – XHProf● JavaScript – Firebug

Page 35: Sensible scaling

Profiling

● PHP - Xdebug – Derick Rethans (@derickr)● Kcachegrind, Webgrind, etc.

● PHP – XHProf● JavaScript – Firebug● JavaScript – Venkman

Page 36: Sensible scaling

Profiling

● PHP - Xdebug – Derick Rethans (@derickr)● Kcachegrind, Webgrind, etc.

● PHP – XHProf● JavaScript – Firebug● JavaScript – Venkman

● General page display - YSlow

Page 37: Sensible scaling

Profiling

● CPU Load – top & /proc/loadavg

Page 38: Sensible scaling

Profiling

● CPU Load – top & /proc/loadavg● Disk IO – iotop

Page 39: Sensible scaling

Profiling

● CPU Load – top & /proc/loadavg● Disk IO – iotop● Memory – free

Page 40: Sensible scaling

Profiling

● CPU Load – top & /proc/loadavg● Disk IO – iotop● Memory – free● Network – netstat

Page 41: Sensible scaling

Profiling

● CPU Load – top & /proc/loadavg● Disk IO – iotop● Memory – free● Network – netstat

● Monitoring – watch & time

Page 42: Sensible scaling

Profiling under load

Page 43: Sensible scaling

Profiling under load

● Code bottlenecks !=Platform bottlenecks

Page 44: Sensible scaling

Profiling under load

● Code bottlenecks !=Platform bottlenecks

● Test in production,if possible

Page 45: Sensible scaling

Profiling under load

● Code bottlenecks !=Platform bottlenecks

● Test in production,if possible

● Use multiple VMs (Vagrant can help automate this)

Page 46: Sensible scaling

Profiling under load

● Code bottlenecks !=Platform bottlenecks

● Test in production,if possible

● Use multiple VMs (Vagrant can help automate this)

● Use siege & ab

Page 47: Sensible scaling

Finding bottlenecks

Page 48: Sensible scaling

Finding bottlenecks

Right Wrong

Page 49: Sensible scaling

Finding bottlenecks

Right● Database

Wrong

Page 50: Sensible scaling

Finding bottlenecks

Right● Database● Disk IO

Wrong

Page 51: Sensible scaling

Finding bottlenecks

Right● Database● Disk IO● External services

Wrong

Page 52: Sensible scaling

Finding bottlenecks

Right● Database● Disk IO● External services● Application work

Wrong

Page 53: Sensible scaling

Finding bottlenecks

Right● Database● Disk IO● External services● Application work

Wrong● Autoloaders

Page 54: Sensible scaling

Finding bottlenecks

Right● Database● Disk IO● External services● Application work

Wrong● Autoloaders● Config files

Page 55: Sensible scaling

Finding bottlenecks

Right● Database● Disk IO● External services● Application work

Wrong● Autoloaders● Config files● Logging

Page 56: Sensible scaling

Finding bottlenecks

Right● Database● Disk IO● External services● Application work

Wrong● Autoloaders● Config files● Logging● Object instantiation

Page 57: Sensible scaling

Finding bottlenecks

Right● Database● Disk IO● External services● Application work

Wrong● Autoloaders● Config files● Logging● Object instantiation● Frameworks

Page 58: Sensible scaling

Finding bottlenecks

Right● Database● Disk IO● External services● Application work

Wrong● Autoloaders● Config files● Logging● Object instantiation● Frameworks

Ask Jo (@juokas)about Doctrine!

Page 59: Sensible scaling

Tell users it's slow

Page 60: Sensible scaling

Tell users it's slow

● User experience !=Raw speed

Page 61: Sensible scaling

Tell users it's slow

● User experience !=Raw speed

● Keep the UI responsive

Page 62: Sensible scaling

Tell users it's slow

● User experience !=Raw speed

● Keep the UI responsive

● Set expectations

Page 63: Sensible scaling

Tell users it's slow

● User experience !=Raw speed

● Keep the UI responsive

● Set expectations

● Adapt to changes(@blongden)

Page 64: Sensible scaling

Fail gracefully

Page 65: Sensible scaling

Fail gracefully

● Actively chooseto time-out

Page 66: Sensible scaling

Fail gracefully

● Actively chooseto time-out

● Prioritise core/profitable functionality

Page 67: Sensible scaling

Fail gracefully

● Actively chooseto time-out

● Prioritise core/profitable functionality

● Have aBIG RED BUTTON

Page 68: Sensible scaling

Separate functionality

Page 69: Sensible scaling

Separate functionality

Admin Batch

Page 70: Sensible scaling

Separate functionality

Admin● Different users ==

diff. requirements

Batch

Page 71: Sensible scaling

Separate functionality

Admin● Different users ==

diff. requirements

● No cache

Batch

Page 72: Sensible scaling

Separate functionality

Admin● Different users ==

diff. requirements

● No cache● “Master” storage

Batch

Page 73: Sensible scaling

Separate functionality

Admin● Different users ==

diff. requirements

● No cache● “Master” storage

● Security

Batch

Page 74: Sensible scaling

Separate functionality

Admin● Different users ==

diff. requirements

● No cache● “Master” storage

● Security

Batch● Not time critical

Page 75: Sensible scaling

Separate functionality

Admin● Different users ==

diff. requirements

● No cache● “Master” storage

● Security

Batch● Not time critical● CPU/RAM hungry

Page 76: Sensible scaling

Separate functionality

Admin● Different users ==

diff. requirements

● No cache● “Master” storage

● Security

Batch● Not time critical● CPU/RAM hungry

● nice / ionice

Page 77: Sensible scaling

Separate functionality

Admin● Different users ==

diff. requirements

● No cache● “Master” storage

● Security

Batch● Not time critical● CPU/RAM hungry

● nice / ionice● Network rate limiting

Page 78: Sensible scaling

Separate functionality

Admin● Different users ==

diff. requirements

● No cache● “Master” storage

● Security

Batch● Not time critical● CPU/RAM hungry

● nice / ionice● Network rate limiting

● Read only?

Page 79: Sensible scaling

Configure hardware correctly

Page 80: Sensible scaling

Configure hardware correctly

● What is required for1 request?

Page 81: Sensible scaling

Configure hardware correctly

● What is required for1 request?

● How manyconcurrent requests?

Page 82: Sensible scaling

Configure hardware correctly

● What is required for1 request?

● How manyconcurrent requests?

● Is it a linear scale?

Page 83: Sensible scaling

Configure hardware correctly

● What is required for1 request?

● How manyconcurrent requests?

● Is it a linear scale?

● Ask yourhosting company

Page 84: Sensible scaling

Aim for services

Page 85: Sensible scaling

Aim for services

Page 86: Sensible scaling

Obey the “Law of Demeter”

Page 87: Sensible scaling

Obey the “Law of Demeter”

● Talk to your friends, don't talk to strangers

Page 88: Sensible scaling

Obey the “Law of Demeter”

● Talk to your friends, don't talk to strangers

● Promotesloose coupling

Page 89: Sensible scaling

Obey the “Law of Demeter”

Good

Bad

Page 90: Sensible scaling

Obey the “Law of Demeter”

Good● $person->requestPayment($amount);

Bad

Page 91: Sensible scaling

Obey the “Law of Demeter”

Good● $person->requestPayment($amount);

Bad● $wallet = $person->getWallet();● $wallet->getMoney($amount);

Page 92: Sensible scaling

Obey the “Law of Demeter”

Good

Bad

Page 93: Sensible scaling

Obey the “Law of Demeter”

Good

Bad

Page 94: Sensible scaling

Obey the “Law of Demeter”

Good

Bad

Page 95: Sensible scaling

Create immutable objects

Page 96: Sensible scaling

Create immutable objects

● Objects that cannot change after creation

Page 97: Sensible scaling

Create immutable objects

● Objects that cannot change after creation● Copy On Write (COW - � unicode cow← )

Page 98: Sensible scaling

Create immutable objects

● Objects that cannot change after creation● Copy On Write (COW - � unicode cow← )● More memory-hungry, but easy to roll back

Page 99: Sensible scaling

Create immutable objects

● Objects that cannot change after creation● Copy On Write (COW - � unicode cow← )● More memory-hungry, but easy to roll back● Value objects should be immutable

Page 100: Sensible scaling

Create immutable objects

● Objects that cannot change after creation● Copy On Write (COW - � unicode cow← )● More memory-hungry, but easy to roll back● Value objects should be immutable

● Think of them like a response from a service

Page 101: Sensible scaling

Identify “single server” factors

Page 102: Sensible scaling

Identify “single server” factors

● File uploads / user content

Page 103: Sensible scaling

Identify “single server” factors

● File uploads / user content● IP restrictions / server access

Page 104: Sensible scaling

Identify “single server” factors

● File uploads / user content● IP restrictions / server access● Licensing?

Page 105: Sensible scaling

Create services

Page 106: Sensible scaling

Create services

Your API should be:

Page 107: Sensible scaling

Create services

Your API should be:● Independent of

application state

Page 108: Sensible scaling

Create services

Your API should be:● Independent of

application state● Loosely coupled

Page 109: Sensible scaling

Create services

Your API should be:● Independent of

application state● Loosely coupled● Accepting/returning

immutable objects

Page 110: Sensible scaling

Use caches

Page 111: Sensible scaling

Use caches

● REST-ful services over HTTP let you cache easily

Page 112: Sensible scaling

Use caches

● REST-ful services over HTTP let you cache easily

● Internal caching is trickier, but “out-of-the-box” with most frameworks

Page 113: Sensible scaling

Use caches

● REST-ful services over HTTP let you cache easily

● Internal caching is trickier, but “out-of-the-box” with most frameworks

Proxy other people's services through your own cache!

Page 114: Sensible scaling

Use queues

Page 115: Sensible scaling

Use queues

● Full blown job server:Gearman

Page 116: Sensible scaling

Use queues

● Full blown job server:Gearman

● AMQP implementation:RabbitMQ

Page 117: Sensible scaling

Use queues

● Full blown job server:Gearman

● AMQP implementation:RabbitMQ

● Light-weight sockets:0MQ

Page 118: Sensible scaling

Use the right storage

Page 119: Sensible scaling

Use the right storage

Page 120: Sensible scaling

Use the right storage

Availability

Partitioning

Consistency

Page 121: Sensible scaling

Use the right storage

Availability

Partitioning

Consistency

Enforced consistency(RDBMS)

Eventual consistency(NoSQL)

Page 122: Sensible scaling

Scale your storage

Page 123: Sensible scaling

Scale your storage

● Master/slave replication

Page 124: Sensible scaling

Scale your storage

● Master/slave replication

● Table partitioning

Page 125: Sensible scaling

Scale your storage

● Master/slave replication

● Table partitioning● Row partitioning

Page 126: Sensible scaling

Scale your storage

● Master/slave replication

● Table partitioning● Row partitioning

● rsync

Page 127: Sensible scaling

Scale your storage

● Master/slave replication

● Table partitioning● Row partitioning

● rsync● NFS

Page 128: Sensible scaling

Scale your storage

● Master/slave replication

● Table partitioning● Row partitioning

● rsync● NFS● GlusterFS

Page 129: Sensible scaling

Now you can use the cloud

Page 130: Sensible scaling

Now you can use the cloud

● Full service – orchestra.io

Page 131: Sensible scaling

Now you can use the cloud

● Full serviceorchestra.io

● Infrastructure mgmt.Scalr

Page 132: Sensible scaling

Now you can use the cloud

● Full serviceorchestra.io

● Infrastructure mgmt.Scalr

● Manually$your_code_here

Page 133: Sensible scaling

Now you can use the cloud

● Full serviceorchestra.io

● Infrastructure mgmt.Scalr

● Manually$your_code_here

● Automate!Chef & Puppet

Page 134: Sensible scaling

Review

Page 135: Sensible scaling

Review

● Start with only what you need

Page 136: Sensible scaling

Review

● Start with only what you need

● Identify the problems

Page 137: Sensible scaling

Review

● Start with only what you need

● Identify the problems

● Pull the problem out to a service

Page 138: Sensible scaling

Review

● Start with only what you need

● Identify the problems

● Pull the problem out to a service

● Distribute

Page 139: Sensible scaling

Thank you!

● Feedback:

http://joind.in/6324

● Photos & Transformers:Nina Merewood

● Legal?Takara & Hasbro

● ElePHPant smuggling:Johannes Schlüter (@phperror)

● Vintage photo nonsense:http://pixlr.com/o-matic/