Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Zend Server: Scalability & Performance


Published on

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

Published in: Technology

Zend Server: Scalability & Performance

  1. Zend Server: Scalability & Performance By Shahahr Evron Technical Product Manager, Zend Technologies
  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) 2
  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 3
  4. What is Zend Server? ...and why should I care? 4
  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.” 5
  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 6
  7. Performance & Scalability Some terminology 7
  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! 8
  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! 9
  10. Zend Server's approach to Performance 10
  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! 11
  12. It's just a series of tubes, right? 12
  13. Out of the box performance Performance 101 13
  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 :) 14
  15. How Optimizer+ Works? 15
  16. How Optimizer+ Works? 16
  17. How Optimizer+ Works? 17
  18. Optimizer+: A quick Benchmark Bare PHP Optimizer+ 50 45.55 45 41.73 40 35 30 25 20 15 13.96 12.69 10 5.3 5 2.24 0 Magento Drupal SugarCRM CE 18
  19. Setting it up to run faster Tuning tips 19
  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 20
  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... 21
  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 22
  23. Caching, Caching, Caching 23
  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 24
  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 25
  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 26
  27. Page Caching 27
  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 ● need to run code up to session_start 700 589.26 600 500 400 300 201.17 200 100 21.94 0 No Cache Session Rule Other Rule 28
  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 29
  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: Or just Google “Zend Server Caching Guide” 30
  31. Off-line processing & Task off-loading Why do now the stuff you can do later? 31
  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 32
  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 33
  34. Job Queue – a Common Execution Flow 34
  35. Job Queue – a Common Execution Flow 35
  36. Job Queue Front-end code: if (GlobalConfig::get('useJobQueue')) { // Use job queue to off-load email sending $params = array( 'recipients' => $recipients, 'message' => $message ); $queue = new ZendJobQueue(); $queue->createHttpJob('http://localhost/mail/job.php', $params); } else { // No job queue, send emails directly send_mail($recipients, $message); } 36
  37. Job Queue Back-end code: $params = ZendJobQueue::getCurrentJobParams(); if (isset($params['recipients']) && isset($params['message'])) { send_mail($params['recipients'], $params['message']); ZendJobQueue::setCurrentJobStatus(ZendJobQueue::OK); } else { ZendJobQueue::setCurrentJobStatus(ZendJobQueue::FAILURE, "Missing recipients list or message"); } 37
  38. Job Queue Baseline Job Queue 140 128.81 127.82 128.61 120 100 80 60 40 20 9.39 3.38 1.5 0 1 recipient 3 recipients 6 recipients 38
  39. Zend Download Server Quick & Easy Apache Efficiency 39
  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! 40
  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 41
  42. Epilogue 42
  43. Conclusions 43
  44. Conclusions 44
  45. Conclusions 45
  46. Conclusions 46
  47. Conclusions 47
  48. Conclusions 48
  49. Conclusions 49
  50. Thanks! Email me: Learn more at Copyright © 2009 Zend Technologies Ltd. This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. To view a copy of this license, visit or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. 50