20130714 php matsuri - highly available php

57
High availability PHP Building scalable PHP systems Graham Weldon Platform as a Service Development & Operations Group Architecture Committee Office Rakuten, Inc.

Transcript of 20130714 php matsuri - highly available php

Page 1: 20130714   php matsuri - highly available php

High availability PHPBuilding scalable PHP systems

Graham Weldon

Platform as a Service Development & Operations GroupArchitecture Committee OfficeRakuten, Inc.

Page 2: 20130714   php matsuri - highly available php

About me

• PHP Developer (15 years)

• Game Developer

• Dev Ops

• CakePHP Contributor

• Public Speaker for PHP

• Public Speaker for Open Source

• Used to live in Australia

• Moved to Tokyo, Japan to work for Rakuten, Inc.

• I love Tokyo (and Sapporo!)

Page 3: 20130714   php matsuri - highly available php

(CakeMatsuri) PHP Matsuri

Page 4: 20130714   php matsuri - highly available php

(CakeMatsuri) PHP Matsuri

2009

Page 5: 20130714   php matsuri - highly available php

(CakeMatsuri) PHP Matsuri

2009 2010

Page 6: 20130714   php matsuri - highly available php

(CakeMatsuri) PHP Matsuri

2009 2010 2011

Page 7: 20130714   php matsuri - highly available php

(CakeMatsuri) PHP Matsuri

2009 20122010 2011

Page 8: 20130714   php matsuri - highly available php

(CakeMatsuri) PHP Matsuri

2009 201320122010 2011

Page 9: 20130714   php matsuri - highly available php
Page 10: 20130714   php matsuri - highly available php

What I do at work

Page 11: 20130714   php matsuri - highly available php

Rakuten’s PaaS

Page 12: 20130714   php matsuri - highly available php

Rakuten’s PaaS

• We build on top of Cloud Foundry

• Build new services

• Build new runtimes

• Support application developers

• Ensure a stable, forward-thinking platform is available

• Internal use only

• Highly available

• Easily scalable

Page 13: 20130714   php matsuri - highly available php

By the way ...

• We’re hiring!

• If you’re interested in:

• large systems

• scalable architecture

• solving challenging problems

• Ask me for information!

• @predominant

[email protected]

Page 14: 20130714   php matsuri - highly available php

We’re Hiring!

• RakutenPlatform as a Service

• http://bit.ly/rakutenpaas

• RakutenPublic Cloud Section

• http://bit.ly/rakutenpcs

Page 15: 20130714   php matsuri - highly available php

What does “Highly available” mean?

• No “single point of failure”

• Redundancy

• Failover systems

Page 16: 20130714   php matsuri - highly available php

What does “Highly available” mean?

• In the event of a system failure there are backup / alternative systems available to take over

• No system failure should result in applications being “down” or offline

• The system is designed to continue operating in the event of failure

Page 17: 20130714   php matsuri - highly available php

What does “scalable” mean?

• Easy to add more servers

• Distribute the load of users amongst many servers

• Transparently add to the compute pool to provide faster response times

+

+

+

Page 18: 20130714   php matsuri - highly available php

System design / architecture

Page 19: 20130714   php matsuri - highly available php

Simple Web Setup

Internet

Page 20: 20130714   php matsuri - highly available php

Simple Web Setup

Internet Limited Limited BandwidthBandwidth

Page 21: 20130714   php matsuri - highly available php

Simple Web Setup

Internet Limited Limited BandwidthBandwidth

Single Single WebserverWebserver

Page 22: 20130714   php matsuri - highly available php

Simple Web Setup

Internet Limited Limited BandwidthBandwidth

Single Single WebserverWebserver

Single Single DatabaseDatabase

Page 23: 20130714   php matsuri - highly available php

Simple Web Setup

Internet Limited Limited BandwidthBandwidth

Single Single WebserverWebserver

Single Single DatabaseDatabase

No Failover No Failover solutionsolution

Page 24: 20130714   php matsuri - highly available php

Simple Web Setup

Internet Limited Limited BandwidthBandwidth

Single Single WebserverWebserver

Single Single DatabaseDatabase

No Failover No Failover solutionsolution

No scalingNo scaling

Page 25: 20130714   php matsuri - highly available php

Simple Setup

• We have all done this before

• It works well for very small sites

• You will encounter problems when the number of users grows

• There is no way to recover from system failure

• Any failure will produce extensive downtime

Page 26: 20130714   php matsuri - highly available php

A better solution

Internet

Page 27: 20130714   php matsuri - highly available php

A better solution

Internet

PrimaryPrimary

Backup / Backup / FailoverFailover

Traffic goes to the primary server

Page 28: 20130714   php matsuri - highly available php

A better solution

Internet

PrimaryPrimary

Backup / Backup / FailoverFailover

When a failure occurs on theprimary server...

Page 29: 20130714   php matsuri - highly available php

A better solution

Internet

PrimaryPrimary

Backup / Backup / FailoverFailover

Traffic is redirected to thebackup server

Page 30: 20130714   php matsuri - highly available php

A better solution

Internet

PrimaryPrimary

Backup / Backup / FailoverFailover

FailoverFailoveravailableavailable

Page 31: 20130714   php matsuri - highly available php

A better solution

Internet

PrimaryPrimary

Backup / Backup / FailoverFailover

FailoverFailoveravailableavailableFailover Failover

relies on relies on DNSDNS

Page 32: 20130714   php matsuri - highly available php

A better solution

Internet

PrimaryPrimary

Backup / Backup / FailoverFailover

FailoverFailoveravailableavailableFailover Failover

relies on relies on DNSDNS

NotNotscalablescalable

Page 33: 20130714   php matsuri - highly available php

A better solution

Internet

PrimaryPrimary

Backup / Backup / FailoverFailover

FailoverFailoveravailableavailableFailover Failover

relies on relies on DNSDNS

NotNotscalablescalable

Database Database Sync Sync

problems?problems?

Page 34: 20130714   php matsuri - highly available php

A better solution

• A backup is now available

• We can switch traffic to the backup server in case of failure

• Highly available?

• A little bit

• Scalable?

• No

• Its a better solution than the previous example

• But its not ideal

Page 35: 20130714   php matsuri - highly available php

An even better solution

Internet

Load Load BalancersBalancers

Load balancers distribute the load

Page 36: 20130714   php matsuri - highly available php

An even better solution

Internet

Load Load BalancersBalancers

WebWebServersServers

A pool of webservers are availableto handle the incoming traffic

Page 37: 20130714   php matsuri - highly available php

An even better solution

Internet

Load Load BalancersBalancers

WebWebServersServers

DBDBServersServers

Webservers connect to a database clusterto distribute load

Page 38: 20130714   php matsuri - highly available php

An even better solution

Internet

Load Load BalancersBalancers

WebWebServersServers

DBDBServersServers

LoadLoadbalancedbalanced

Page 39: 20130714   php matsuri - highly available php

An even better solution

Internet

Load Load BalancersBalancers

WebWebServersServers

DBDBServersServers

ScalableScalableLoadLoadbalancedbalanced

Page 40: 20130714   php matsuri - highly available php

An even better solution

Internet

Load Load BalancersBalancers

WebWebServersServers

DBDBServersServers

FailoverFailoveravailableavailable

ScalableScalableLoadLoadbalancedbalanced

Page 41: 20130714   php matsuri - highly available php

A perfect solution?

• There are good solutions available

• No solution is perfect

• Consider the parts of the system that can fail

• Build it in a highly available way

• Allow additional servers / services to be added through configuration

• Or.. through auto detection

Page 42: 20130714   php matsuri - highly available php

Scaling PHP

A Case Study of the Rakuten Platform

Page 43: 20130714   php matsuri - highly available php

Web Server

• Nginx

• Why?

• Fast

• Reliable

• Stable

• Low resource usage

• Designed for high speed

• Easy configuration

Page 44: 20130714   php matsuri - highly available php

Optimization

• PageSpeed

• Optimization tool from Google

• Open Source

• Easy to install

• Lots of options

• Optimizes your pages

• Image processing

• JS/CSS compression

Page 45: 20130714   php matsuri - highly available php

PHP

• PHP-FPM

• “Fast Process Manager”

• Easy to configure

• Fast when configured right

• Process pooling

• Simple to setup

Page 46: 20130714   php matsuri - highly available php

Databases

• A range of options

• Redis

• MongoDB

• Clusterix (MySQL Compatible)

• Each in a clustered setup

• Highly available

• Scalable

Page 47: 20130714   php matsuri - highly available php

Database considerations

• Optimize queries

• Use fast disks!

• Large memory

• Lots of CPU

• Database is often a bottleneck

• Make sure you have enough resources

Page 48: 20130714   php matsuri - highly available php

Content Delivery

• Use a Content Delivery Network for static content

• Or.. use a separate server for delivering static content to ensure PHP has the processing power it needs

• Speeds up browser requests for static files

• Allows concurrent requests with less work / effort

Page 49: 20130714   php matsuri - highly available php

Varnish

• HTTP accelerator

• In-memory caching

• Huge performance increases

• 300-1000x speed increase

Page 50: 20130714   php matsuri - highly available php

Special considerations

Page 51: 20130714   php matsuri - highly available php

Session Storage

• You need a common place for all servers to access for session storage

• We recommend Redis

• Common storage ensures that sessions will be maintained

• Without common storage, users will lose their session data between page requests.

Page 52: 20130714   php matsuri - highly available php

Session Storage

Web Servers Session Store

Page 53: 20130714   php matsuri - highly available php

Session Storage

• php.ini

• session.save_handler = ‘redis’

• session.save_path = ‘tcp://host:port, ...’

Page 54: 20130714   php matsuri - highly available php

Opcode Caching

• OpCache (Extension)

• Bundled with PHP 5.5+

• Available as an extension for earlier versions

• Faster than XCache

• Simple to setup

• OpCache will cache per-server

• You might want to pre-warm the cache for all servers

• I don’t have a better solution for this yet

Page 55: 20130714   php matsuri - highly available php

Thankyou!

Any questions?

Page 56: 20130714   php matsuri - highly available php

• We’re looking for talented engineers!

• Interested in DevOps?

• Interested in highly available systems?

• Contact me!

• @predominant

• RakutenPlatform as a Servicehttp://bit.ly/rakutenpaas

• RakutenPublic Cloud Sectionhttp://bit.ly/rakutenpcs

[email protected]

Page 57: 20130714   php matsuri - highly available php