Zend Server provides a host of features that can help you improve your PHP application's performance and scalability.
In this presentation, presented at ZendCon 09, we overview them and how and where to use what
2. 2
Welcome!
I am:
▶ A PHP programmer since 2002
▶ At Zend since 2005
▶ Technical Product Manager for Zend Server
Yes, I have a difficult name (at least for English speakers)
▶ Shachar (German, Dutch)
▶ Shajar (Spanish)
▶ Шахар (Russian)
▶ ﺷﺨﺮ (Arabic)
▶ שחר (Hebrew)
3. 3
Agenda
▶ What is Zend Server?
▶ Performance and Scalability
▶ Zend Server's approach to performance
▶ Out of the box performance: Optimizer+
▶ Setting it up to run faster
▶ Caching, Caching, Caching
▶ Task off-loading and off-line processing
5. 5
...Well, Just ask your friend at marketing!
“Zend Server is a complete, enterprise-
ready Web Application Server for
running and managing
PHP applications that require a high
level of reliability, performance and
security.”
6. 6
Let's try to make sense out of that..
Zend Server Is:
▶ A complete, well tested PHP runtime environment
▶ Supported and updated by Zend
▶ Comes with a set of extra features that:
●
Improve the performance and reliability of PHP applications
●
Make it easier to manage a consistent PHP environment
Comes in two flavors:
▶ Zend Server
▶ Zend Server Community Edition
8. 8
What performance are we talking about?
Improving an application's performance can mean:
▶ Making it run faster
●
More requests per second on the server side
●
Less load time on the user side
▶ Making it run more efficiently
●
Use less memory / CPU / disk IO / network bandwidth
●
Distribute load evenly over time / hardware
These are not always the same, and setting goals is
important!
9. 9
What about Scalability?
Scalability can mean different things:
▶ Ability to gracefully handle more users
▶ Ability to gracefully handle more data
▶ Ability to gracefully handle more code
Scalability can be improved by performance
optimizations...
▶ But they are not the same, and sometimes even conflict!
11. 11
Comprehensive Performance
A Web App's performance depends on many factors...
▶ Network
▶ Web server load
▶ PHP processing time
▶ Database load
▶ Application logic
▶ ...?
...that is why improving your applications performance
will require applying a range of measures
▶ Zend Server provides a set of different tools to help you do that!
14. 14
Opcode Caching and Optimization
Zend Server includes Optimizer+:
▶ PHP Opcode is read once from disk and compiled
▶ The compiled opcode goes through a set of optimizations
▶ The optimized opcode is stored in shared memory
●
No need to read from disk again unless the code changes
●
No need to compile the code on each request / file inclusion
● Optimized code potentially executes faster
▶ Works out of the box, no setup needed!
● Can be made even better with some tuning :)
20. 20
Tuning Zend Server for Performance
Zend Server comes with a default “good for all” setup
▶ You should go over the performance guide when moving to
production!
Some things you should definitely do:
▶ Turn off extensions you don't need
▶ Tune PHP configuration
▶ Tune Zend Monitoring Rules
21. 21
Turn off extensions you don't need
Why load extensions you don't use?
▶ Per-process memory can be reduced
▶ Less work at MINIT and possibly at RINIT / RSHUTDOWN
▶ Generally a good security practice
A quick test on per-process Apache memory usage shows:
▶ With 55 extensions: 9.3mb
▶ With 31 extensions: 7.2mb
●
Almost 30% more concurrent requests on an average machine!
Of course, you must know what can be turned off...
22. 22
Tune PHP configuration
Many PHP directives can be optimized for performance
▶ Those have relatively small effect
▶ ...but they are easy to fix, and you should know them!
Here are some examples:
▶ include_path, realpath_cache_size, realpath_cache_ttl
▶ output_buffering
▶ register_long_arrays, register_argc_argv
▶ magic_quotes_gpc
▶ error_reporting
▶ memory_limit, max_execution_time
▶ session.auto_start
24. 24
Caching?
“In computer science, a cache is a collection of
data duplicating original values stored elsewhere
or computed earlier, where the original data is
expensive to fetch or to compute, compared to the
cost of reading the cache.”
-- Wikipedia
25. 25
Caching with Zend Server...
Zend Server provides several caching tools
▶ Zend Data Cache
●
Disk storage
●
Shared memory storage
▶ Zend Page Cache
▶ Common Caching Extensions
● memcache
● APC compatibility layer
You can get the best results by combining some or all of
those
▶ You can abstract storage backends using ZF's Zend_Cache
26. 26
Page Caching
Page Caching allows you to cache full HTTP responses
▶ Super fast!
▶ Based on a rule system – no code changes required
▶ Cached / live decision can be based on request or session data
▶ Variants can be created based on request or session data
▶ Client-side caching is utilized if supported by browser
Setting it up requires some careful planning
...but the performance gains are usually worth the effort
28. 28
Page Cache: A Quick Drupal Benchmark
Caching node pages for guest users
▶ Can be done with no effort using a SESSION based rule
▶ Much better results can be achieved using a COOKIE based rule
●
...no need to run code up to session_start
No Cache Session Rule Other Rule
0
100
200
300
400
500
600
700
21.94
201.17
589.26
29. 29
Data Caching
Zend Server's Data Caching API allows you to:
▶ Programatically place data items into cache
●
And also get them and delete them ;)
▶ Use shared memory or disk storage as needed
▶ Organize cached items into namespaces
▶ Clear cached content from Zend Server's UI
Data Caching more of a “precision guided” caching tool
▶ Requires code changed (but is easy to use)
▶ Not as effective performance wise as Page Cache
▶ Very flexible
30. 30
Data Caching
function getRecentPosts($count = 5) {
// Try to fetch from the cache first
$recentPosts = zend_shm_cache_fetch('recentposts-' . $count);
if ($recentPosts === false) {
// Get the PDO adapter for the DB
$dbAdapter = myBlogDb::getPdoAdapter();
// Build the query
$query = "SELECT title, author, pubdate FROM posts ORDER BY pubdate DESC LIMIT ?";
$dbStatement = $dbAdapter->prepare($query);
// Execute the query
$recentPosts = array();
if ($dbStatement->execute(array($count))) {
while($post = $dbStatement->fetch(PDO::FETCH_ASSOC)) {
$recentPosts[] = $post;
}
// Store the results in cache
zend_shm_cache_store('recentposts-' . $count, $recentPosts, 24 * 3600);
}
}
return $recentPosts;
} Read more: http://devzone.zend.com/article/4457
Or just Google “Zend Server Caching Guide”
32. 32
Job Queue
Off-line processing?
▶ Web applications tend to “live” in HTTP request/response cycles
▶ What do you do when you need to take something off-line?
▶ What do you do when you need periodical execution?
It's also a matter of user experience:
▶ Sometimes, it's just silly to let the user wait
Zend Server Job Queue allows you to take it off-line!
▶ Run things asynchronously, later, on a different server
▶ Run things periodically
33. 33
So is it some glorified Cron?
No!
▶ You can run things now, but without waiting for them to finish
▶ You can run things once, but not right now
▶ You can run things periodically (like cron)
●
But have full control over them – start, stop, suspend, resume from
PHP API
▶ Job Queue gives you full visibility into what's going on
● Get alerts on failed jobs, analyze errors and re-queue
● Keep track of past, current and pending jobs
● Integrates with Zend Monitor
▶ You don't need to hack it all to work for you
40. 40
What is Zend Download Server
If you use Apache in Prefork mode:
▶ Each process handles one request at a time
▶ Your server's # of processes is limited (usually by RAM)
▶ Once your server's processes are maxed, you have a problem!
Solutions:
▶ Add more hardware (← last resort!)
▶ Switch to a threaded web server + FastCGI PHP
●
lighttpd, optimized threaded Apache, nginx etc.
▶ Off-load static downloads
●
To a separate web server, separate machine, CDN / 3rd
party storage
▶ Set up Zend Download Server!
41. 41
How does Zend Download Server help?
Zend Download Server improves Apache's scalability by:
▶ “Hijacking” static downloads to a separate, efficient process
▶ Freeing Apache processes to do the “hard work” (== PHP)
Usage modes:
▶ Can be set up to automatically send specific file types
▶ Can be used through API (zend_send_file, zend_send_buffer)
Benefits:
▶ Easy to set up and use, no code changes
▶ Easy to integrate with your PHP app's logic
● e.g. authenticated downloads