Slide 1

Slide 1 text

Text High Performance Websites with Scalable Workers @JustinCarmony

Slide 2

Slide 2 text

Introduction About Presenter • Director of Development for Deseret Digital Media • President of Utah PHP Usergroup • PHP Developer for 8+ years • Goofy Dad

Slide 3

Slide 3 text

Introduction About Presentation • There is a Vagrant demo project located at: https://github.com/JustinCarmonyDotCom/ PHP-Workers-Tutorial aka http://bit.ly/10WYeUf • Feel free to clone it and follow along

Slide 4

Slide 4 text

Introduction Normally I Have Really Nice Slides...

Slide 5

Slide 5 text

Introduction ... but my work gave me this template (which is ugly) ...

Slide 6

Slide 6 text

Introduction ... and this guy came along ....

Slide 7

Slide 7 text

Introduction

Slide 8

Slide 8 text

Introduction So No Fancy Slides this Time ( Sorry :p )

Slide 9

Slide 9 text

Introduction So Lets Identify The Problem

Slide 10

Slide 10 text

The Problem Example: Submit New Blog Post Fill Out Form Display Success Page

Slide 11

Slide 11 text

The Problem Example: Submit New Blog Post Fill Out Form Display Success Page Save to DB

Slide 12

Slide 12 text

The Problem Example: Submit New Blog Post Fill Out Form Display Success Page Save to DB Post to Twitter & FB

Slide 13

Slide 13 text

The Problem Example: Submit New Blog Post Fill Out Form Display Success Page Save to DB Post to Twitter & FB Send Emails

Slide 14

Slide 14 text

The Problem Example: Submit New Blog Post Fill Out Form Display Success Page Save to DB Post to Twitter & FB Send Emails Warm Cache

Slide 15

Slide 15 text

The Problem Example: Submit New Blog Post Fill Out Form Display Success Page Save to DB Post to Twitter & FB Send Emails Warm Cache Update Queues

Slide 16

Slide 16 text

The Problem Example: Submit New Blog Post Fill Out Form Display Success Page Save to DB Post to Twitter & FB Send Emails Warm Cache Update Queues Create Images

Slide 17

Slide 17 text

The Problem Example: Submit New Blog Post Fill Out Form Display Success Page Save to DB Post to Twitter & FB Send Emails Warm Cache Update Queues Create Images Find Related Posts

Slide 18

Slide 18 text

The Problem Example: Submit New Blog Post Fill Out Form Display Success Page Save to DB Post to Twitter & FB Send Emails Warm Cache Update Queues Create Images Find Related Posts Another Thing Another Thing Another Thing Another Thing Another Thing Another Thing Another Thing

Slide 19

Slide 19 text

The Problem It’s SLOW!

Slide 20

Slide 20 text

The Problem Problems With Linear Approach • Time From “Save” to “Success” Longer & Longer • What happens if you get an error? Will it block all other items afterwards? • What if you have lots of authors? Everyone stuck in this linear approach?

Slide 21

Slide 21 text

The Solution - Workers The Solution: PHP Workers

Slide 22

Slide 22 text

The Solution What are Workers? • Separate PHP Processes that typically run from the CLI • Listen to a Queue for “jobs” • When it receives a job, it does it • When it finished, listens for more jobs

Slide 23

Slide 23 text

The Solution The Workflow

Slide 24

Slide 24 text

Web Application The Solution The Workflow

Slide 25

Slide 25 text

Job Web Application The Solution The Workflow

Slide 26

Slide 26 text

Job Web Application The Solution The Workflow Job Job Job Job Job Job Job Job Job Job Job Queue of Jobs

Slide 27

Slide 27 text

Job Web Application The Solution The Workflow Job Job Job Job Job Job Job Job Job Job Job Queue of Jobs

Slide 28

Slide 28 text

Job Web Application The Solution The Workflow Job Job Job Job Job Job Job Job Job Job Job Queue of Jobs Worker

Slide 29

Slide 29 text

Job Web Application The Solution The Workflow Listen Job Job Job Job Job Job Job Job Job Job Job Queue of Jobs Worker

Slide 30

Slide 30 text

Job Web Application The Solution The Workflow Listen Job Job Job Job Job Job Job Job Job Job Job Queue of Jobs Worker

Slide 31

Slide 31 text

Job Web Application The Solution The Workflow Listen Job Job Job Job Job Job Job Job Job Job Job Queue of Jobs Worker

Slide 32

Slide 32 text

Job Web Application The Solution The Workflow Listen Result Job Job Job Job Job Job Job Job Job Job Job Queue of Jobs Worker

Slide 33

Slide 33 text

Job Web Application The Solution The Workflow Listen Result Job Job Job Job Job Job Job Job Job Job Job Queue of Jobs Worker

Slide 34

Slide 34 text

The Solution Example: Submit New Blog Post Fill Out Form

Slide 35

Slide 35 text

The Solution Example: Submit New Blog Post Fill Out Form Queue Jobs

Slide 36

Slide 36 text

The Solution Example: Submit New Blog Post Fill Out Form Job Queue Jobs Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job

Slide 37

Slide 37 text

The Solution Example: Submit New Blog Post Fill Out Form Display Success Page Job Queue Jobs Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job Job

Slide 38

Slide 38 text

The Solution Okay... the Theory is Pretty Straight forward...

Slide 39

Slide 39 text

The Solution Lets see how you actually do it...

Slide 40

Slide 40 text

Getting Started What We’ll Need • PHP 5.3 • Queue -- Beanstalkd • Manage Queue - Pheanstalk Library • Manage Workers - Solo / Supervisord • Store Worker Status - Redis • Talk with Redis - Predis Library

Slide 41

Slide 41 text

Getting Started Wait, What About Gearman? • It’s great to use! • Requires to install modules. • If you understand how to use Beanstalkd, its easy to use Gearman • Lots of talks about Gearman, like to give some exposure to Beanstalkd

Slide 42

Slide 42 text

Getting Started Install / Setup Beanstalkd • Debian/Ubuntu: apt-get install beanstalkd • Fedora/CentOS/RHEL: su -c 'yum install beanstalkd' • Mac Homebrew: brew install beanstalkd

Slide 43

Slide 43 text

Getting Started Beanstalkd on Windows • ... use a VM. Checkout Vagrant!

Slide 44

Slide 44 text

Getting Started Pheanstalk for PHP • Most Tested PHP Beanstalkd Library • Installable via Composer • Located at: https://github.com/pda/pheanstalk

Slide 45

Slide 45 text

Workers The Trick: Managing Your Workers • Putting stuff in a queue is easy! • Keeping workers up & running consistently is the hard part! • We’ll talk about PHP workers, but you can use any language to write your workers.

Slide 46

Slide 46 text

Workers Features for our PHP Workers • Run for a long time • Self-restarting • Can be restarted • Reports it’s current status • Blocking, Not Polling • Monitor It’s Own Performance

Slide 47

Slide 47 text

Workers Timing For your Workers • Allowing them to run indefinitely: set_timeout_limit(0); • Set a max runtime for them to gracefully restart. Example: $time_limit = 60 * 60 * 1; // Minimum of 1 hour $time_limit += rand(0, 60 * 10); // Adding additional time

Slide 48

Slide 48 text

The Queue Anatomy of a Beanstalkd Job • What makes up a job • Priority • Delay • Time-To-Run • Data

Slide 49

Slide 49 text

Text Beanstalkd Actions • put (creates job) • put with delay • reserve (worker selects job) • release (puts job back into queue) • release with delay • delete (removes job) • bury (bury job at end of queue to wait) • kick (kick a buried job back into the queue) • touch (request more time on a job)

Slide 50

Slide 50 text

The Queue Typical Job Life Cycle Client put Job Worker retrieve delete Poof!

Slide 51

Slide 51 text

Poof! The Queue Possible Job Flow DELAYED put with delay (time passes) READY RESERVED reserve put release with delay release delete BURIED bury kick Poof! delete

Slide 52

Slide 52 text

Demo Time Lets look at the code!

Slide 53

Slide 53 text

High Performance Okay, so... what about the High Performance Part?

Slide 54

Slide 54 text

High Performance Using Workers to Increase Performance • Make Writing to Your “Queue” Extremely Fast (Beanstalkd, Redis, In-Memory) • Do only what you must in your request, Queue Everything Else • Find IO Operations & Queue Them Instead of Perform Them

Slide 55

Slide 55 text

High Peformance Caching is for Reads Queueing is for Writes

Slide 56

Slide 56 text

High Performance Possible Things to Queue: Your Writes • Logging • Analytics • Some Queries • Communication with other APIs • Writing to your Data Sources

Slide 57

Slide 57 text

High Performance Identifying What To Queue • Use XHGui/XHProf to find slow parts of your application • Use StatsD/Graphite to Gather Stats Across Requests • Does it communicate with something else?

Slide 58

Slide 58 text

Questions Questions?

Slide 59

Slide 59 text

The End Thank You! Twitter: @JustinCarmony Website: http://www.justincarmony.com/ Email: [email protected]