More Related Content More from MagentoImagine (6) Magento's Imagine eCommerce Conference 2011 - Do You Queue?1. Do you queue?
Characteristics of
scalability
Kevin Schroeder
Technology Evangelist
Zend Technologies
© All rights reserved. Zend Technologies, Inc.
3. 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.
4. Twtpoll results
Could your PHP apps benefit from being able to
process data or execute asynchronously?
© All rights reserved. Zend Technologies, Inc.
5. 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.
6. Typical anatomy of a PHP Application
Presentation
Application Control
Bad for
Database Access
Business Logic
scala- Presentation
Application Control
bility!
Business Logic
Presentation
|
© All rights reserved. Zend Technologies, Inc. 6
7. Someth Someth Someth Someth Someth Someth Someth Someth
ing.pht
ml
ing.pht
ml
ing.pht
ml
ing.pht
ml Presentation ing.pht
ml
ing.pht
ml
ing.pht
ml
ing.pht
ml
Good for
Application Control
Someth Someth Someth Someth Someth Someth Someth Someth
ing.php ing.php ing.php ing.php ing.php ing.php ing.php ing.php
Someth
ing.php
Someth
ing.php
Someth
ing.php
Scalability
Someth
ing.php Business Logic Someth
ing.php
Someth
ing.php
Someth
ing.php
Someth
ing.php
Someth
ing.php
Someth
ing.php
Someth
ing.php
Someth
ing.php Database Access Someth
ing.php
Someth
ing.php
Someth
ing.php
Someth
ing.php
|
| © All rights reserved. Zend Technologies, Inc. 7
8. What helps make software scalable?
Defined tasks
Loose coupling
Resource discovery
© All rights reserved. Zend Technologies, Inc.
9. The Golden Rule of Scalability
“It can probably wait”
© All rights reserved. Zend Technologies, Inc.
10. 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.
11. 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.
12. 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.
13. 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.
14. Your only real options
Gearman* Zend Server Job Queue
Free Ready to go
Lightweight Integrates with Event Monitoring
Open Source Integrates with Code Tracing
(mostly) language Runs over a widely known protocol
agnostic Load distribution can be Very cloud friendly
Distributed queuing accomplished outside of the queue
* I am not an expert on Gearman. Corrections will be taken in the spirit that they are given.
For obvious reasons, I will focus on Zend Server
© All rights reserved. Zend Technologies, Inc.
15. What the heck is Zend Server?
Zend Server
Application Reliability & Scale-Out Business-grade
Performance Management PHP
Acceleration Monitoring Clustering Hot Fixes
Optimization Root-Cause Job Queue Support
Caching Configuration Downloads Java Bridge
Zend Framework
PHP
Linux Windows IBM i
(rpm/web repositories) (MSI) (PTF)
15 Zend Server © All rights reserved. Zend Technologies, Inc.
16. Job Queue Architecture – Elastic Backend
Web Server
Web Server
/w JQ
Load Balancer
Web Server
Users! Web Server
/w JQ
Web Server
Web Server
/w JQ
• Pros
Scale the backend as necessary
Default (easy) mechanism
• Cons
Getting the job status requires using a DB
© All rights reserved. Zend Technologies, Inc.
17. Job Queue Architecture – Elastic Frontend
Web Server
Web Server
/w JQ
Load Balancer
Web Server
Users! Web Server
/w JQ
Web Server
Web Server
/w JQ
• Pros
• Easy to communicate with the Job Queue server handling the job
Cons
• Requires you to build your own interface (or just use mine!)
© All rights reserved. Zend Technologies, Inc.
18. 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.
19. 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.
20. 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.
22. Neato, but how in the world
does this pertain to Magento?
© All rights reserved. Zend Technologies, Inc.
23. 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.
24. 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.
25. 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.
26. 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.
27. Scaling the solution
Webserver Job Queue/
Webserver
Load Balancer
Webserver Job Queue/
Webserver
Webserver Job Queue/
Webserver
* Duplicate deployments on
ALL machines
Database or
Session
Clustering
© All rights reserved. Zend Technologies, Inc.
28. Downloads
• Zend Server
http://www.zend.com/server
http://bit.ly/hWJBYw
• 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
© All rights reserved. Zend Technologies, Inc.
29. 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.
30. Get this information and all the
examples at eschrade.com…
© All rights reserved. Zend Technologies, Inc.
Editor's Notes We released Zend Server and Zend Server Community Edition in April this year Products written from scratch based on our very extensive experience with Platform technologies Full integrated stack, native installer, ZF and Studio integration, software updates, all new UI, … Both editions have been very well received by users (love performance boost, ease of use, deployment) Great fit to our partners – we’re working with Varien/Magento, KnowledgeTree, MCS, … Next step will round up web app server offering to support high availability and scalability – more on this in the next few months