Upgrade to Pro — share decks privately, control downloads, hide ads and more …

High Performance Websites with Scalable Workers

High Performance Websites with Scalable Workers

There comes a point in time with a website when eventually need to do something in the background. There are always cron jobs, but eventually those either don't scale well, or are not responsive enough. Learn about how to help your website efficiently scale by using workers. We'll discuss the fundamental theory behind workers and how to easily implement them. We'll learn about several different technologies to help manage workers such as Beanstalkd, Gearman, Supervisord, Redis, and others.

Justin Carmony

May 02, 2013
Tweet

More Decks by Justin Carmony

Other Decks in Technology

Transcript

  1. Introduction About Presenter • Director of Development for Deseret Digital

    Media • President of Utah PHP Usergroup • PHP Developer for 8+ years • Goofy Dad
  2. 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
  3. The Problem Example: Submit New Blog Post Fill Out Form

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

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

    Display Success Page Save to DB Post to Twitter & FB Send Emails Warm Cache
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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?
  11. 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
  12. Job Web Application The Solution The Workflow Job Job Job

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

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

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

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

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

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

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

    Job Job Job Job Job Job Job Job Job Job Queue of Jobs Worker
  20. 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
  21. 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
  22. 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
  23. 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
  24. Getting Started Install / Setup Beanstalkd • Debian/Ubuntu: apt-get install

    beanstalkd • Fedora/CentOS/RHEL: su -c 'yum install beanstalkd' • Mac Homebrew: brew install beanstalkd
  25. Getting Started Pheanstalk for PHP • Most Tested PHP Beanstalkd

    Library • Installable via Composer • Located at: https://github.com/pda/pheanstalk
  26. 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.
  27. 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
  28. 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
  29. The Queue Anatomy of a Beanstalkd Job • What makes

    up a job • Priority • Delay • Time-To-Run • Data
  30. 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)
  31. 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
  32. 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
  33. High Performance Possible Things to Queue: Your Writes • Logging

    • Analytics • Some Queries • Communication with other APIs • Writing to your Data Sources
  34. 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?