© All rights reserved. Zend Technologies, Inc . Do you queue? Characteristics of scalability Kevin Schroeder Technology Evangelist Zend Technologies
©All rights reserved. Zend Technologies, Inc.
Do you queue? Characteristics of scalabilityKevin Schroeder
Technology Evangelist
Zend Technologies
©All rights reserved. Zend Technologies, Inc.
About me
Past: Programming/Sys Admin
Current: Technology Evangelist/Author/Composer
@kpschrade
©All rights reserved. Zend Technologies, Inc.
I blog at eschrade.com
Follow us!(good things will happen to you)
Zend Technologies
http://twitter.com/zend
http://twitter.com/kpschrade (me!)
©All rights reserved. Zend Technologies, Inc.
Twtpoll results
Could your PHP apps benefit from being able to process data or execute
asynchronously?
©All rights reserved. Zend Technologies, Inc.
Why would you want to queue?
• Performance Execute logic apart from the main request (asynchronicity)
• Scalability The ability to handle non-immediate logic as resources are
available
©All rights reserved. Zend Technologies, Inc.
Typical anatomy of a PHP Application
| 6
Presentation
Application Control
Database Access
Business Logic
Presentation
Application Control
Business Logic
Presentation
Bad for scala-bility!
©All rights reserved. Zend Technologies, Inc.
| 7 |
Apr 10, 2023
Presentation
Database Access
Business Logic
Application Control
Something.phtml
Something.phtml
Something.phtml
Something.phtml
Something.phtml
Something.phtml
Something.phtml
Something.phtml
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Something.p
hp
Good for Scalabilit
y
©All rights reserved. Zend Technologies, Inc.
What helps make software scalable?
Defined tasks
Loose coupling
Resource discovery
©All rights reserved. Zend Technologies, Inc.
The Golden Rule of Scalability
“It can probably wait”
©All rights reserved. Zend Technologies, Inc.
Asynchronous execution uses
• Pre-caching data
• Data analysis
• Data processing
• Pre-calculating (preparing data for the next request)
Data is “out of date” once it leaves the web server
Immediacy is seldom necessary
©All rights reserved. Zend Technologies, Inc.
Considerations
• Waste disk space
• Control usage (don’t let users do anything they want)
• Pre-calculate as much as possible Calculate and cache/store
• Keep data processing off the front end servers
• Don’t just cache Don’t let it substitute for thought
Cache hit rates can be meaningless if you have hundreds of cache hits for a request
©All rights reserved. Zend Technologies, Inc.
Considerations
• Build a deployment mechanism with NO hardcoded values like directory or resource locations• Make as much as possible configurable/discoverable
• Decouple/Partition Don’t tie everything (relationships and such) into the
database
• Use queues/messaging Stomp interfaces are really good for PHP – Can also use
Java Bridge
Zend_Queue has several interfaces
• Try to use stateless interfaces (polling is more scalable than idle connections)
©All rights reserved. Zend Technologies, Inc.
Options
• Use Cron /w PHP CLI (not the best idea) People mostly use cron simply due to availability
• It’s an available option, not necessarily the best option.
• Use Gearman
• Use home-grown (don’t do this)
• Use pcntl_fork() (NEVER do this)
• Use Zend Server Job Queue
©All rights reserved. Zend Technologies, Inc.
Your only real options
Gearman* Zend Server Job Queue
FreeLightweightOpen Source(mostly) language agnosticDistributed queuing
Ready to goIntegrates with Event MonitoringIntegrates with Code TracingRuns over a widely known protocolLoad distribution can be accomplished outside of the queue* I am not an expert on Gearman. Corrections will be taken in the spirit that they are given.
Very cloud friendly
For obvious reasons, I will focus on Zend Server
©All rights reserved. Zend Technologies, Inc.
Linux(rpm/web repositories)
IBM i(PTF)
Windows(MSI)
What the heck is Zend Server?
ApplicationPerformance
Acceleration
Optimization
Caching
Reliability &Management
Monitoring
Root-Cause
Configuration
Scale-Out
Clustering
Job Queue
Downloads
Business-gradePHP
Hot Fixes
Support
Java Bridge
Zend Framework
PHP
Zend Server
Zend Server15
©All rights reserved. Zend Technologies, Inc.
Job Queue Architecture – Elastic Backend
• Pros Scale the backend as necessary
Default (easy) mechanism
• Cons Getting the job status requires using a DB
Users!
Web Server /w
JQ
Web Server /w
JQ
Web Server /w
JQ
Web Server
Web Server
Web Server
Load B
ala
nce
r
©All rights reserved. Zend Technologies, Inc.
Job Queue Architecture – Elastic Frontend
• Pros• Easy to communicate with the Job Queue server handling the job
Cons• Requires you to build your own interface (or just use mine!)
Users!
Web Server
Web Server
Web Server
Web Server /w
JQ
Web Server /w
JQ
Web Server /w
JQ
Load B
ala
nce
r
©All rights reserved. Zend Technologies, Inc.
Kevin’s favorite way to implement it
• Create a task-handling controller
• Create an abstract task class Understands the Job Queue
Self contained• If Elastic Backend: connects to localhost
• If Elastic Frontend: connects to load balancer (my preferred), load balanced JQ servers manage themselves
• Execute the task, have it serialize itself and send it to send to the task handler
©All rights reserved. Zend Technologies, Inc.
Classes involved in the demo
Manager
Handles connecting to the queue and passing results back and forth
JobAbstract
Abstract class that a job would be based off of
Response
The response from the manager when a job is queued. Contains the server name and job number
GetRemoteLinks• Scans a given web page and reports back a list of all the links
on the page
©All rights reserved. Zend Technologies, Inc.
Execution Flow
1. Create job and set data
2. Execute job• Job passes itself to the queue manager
• Manager serializes job
• Manager uses HTTP call through a load balancer to queue the job
• The queue on the other end returns the job id and server name
• Job ID and server name is passed to the client
3. Client polls the manager to get a completed job• When the job is returned pass the serialized version of the
executed job
©All rights reserved. Zend Technologies, Inc.
Demo
©All rights reserved. Zend Technologies, Inc.
Neato, but how in the world does this pertain
to Magento?
©All rights reserved. Zend Technologies, Inc.
How?
• Long running front-end requests kill front-end responsiveness which kills the user experience 40% will wait no more than 4 seconds for a website
(Forrester)
• Calculating cache data on-the-fly can lead to bad user experiences (abruptly slow page load times) and consistency problems
• Long running page requests can push up against max_execution_time For example, handling payments from a slow CC API
Simply setting it to run longer is a band-aid on a broken bone
©All rights reserved. Zend Technologies, Inc.
Solution?
1. Build Zend Server integration extensions Zendserver_Jobqueue
Zendserver_Monitor
2. Utilize the event system to intercept actions Async_Payment (for payments, in this case)
3. Profit! (scalability AND performance, actually)
©All rights reserved. Zend Technologies, Inc.
Building a job
• Install the Zend Server Magento extension
• Create a class that extends Zendserver_Jobqueue_JobAbstract
• Call $job->execute();
©All rights reserved. Zend Technologies, Inc.
Payment as an example
• Hooks an observer into controller_action_predispatch_checkout_onepage_saveOrder, core_block_abstract_to_html_after
• Checks if a defined template is being rendered
• Injects JavaScript that changes the review.save() method
• Injects a new URL for submitting the transaction which creates a job Passes POSTed data to a job
• Job executes by submitting the payment URL on behalf of the browser, storing the result
• Browser pings the server to see if the job has completed executing yet
©All rights reserved. Zend Technologies, Inc.
Scaling the solution
Webserver
Webserver
Webserver
Database or Session
Clustering
Load B
ala
nce
r Job Queue/ Webserver
Job Queue/ Webserver
Job Queue/ Webserver
* Duplicate deployments on ALL machines
©All rights reserved. Zend Technologies, Inc.
Downloads
• Zend Server http://www.zend.com/server
• Zendserver_Jobqueue https://github.com/kschroeder/Magento-ZendServer-
JobQueue
• Zendserver_Monitor https://github.com/kschroeder/Magento-ZendServer-
Monitor
• Async_Payment https://github.com/kschroeder/Magento-Async-Payment
• Job Queue library & demo https://github.com/kschroeder/ZendServer-JobQueue-Job-
API
http://bit.ly/hWJBYw
©All rights reserved. Zend Technologies, Inc.
Magento/Zend Case Study - http://bit.ly/horpFF
Follow us!
Zend Technologies
http://twitter.com/zend
http://twitter.com/kpschrade (me!)
©All rights reserved. Zend Technologies, Inc.
Get this information and all the examples at eschrade.com…