Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Like this? Share it with your network

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. OPTIMIZING SOA WITH PHP By Glen Campbell, Engineering Manager Yahoo! Tech http://tech.yahoo.com 1
  • 2. About Yahoo! Tech • Launched May 1, 2006 • First new site launched by the Yahoo! Media group in over five years • First Yahoo! site to use a complete service- oriented architecture • First Yahoo! site to use MAPLE (the Media Presentation Layer) written in PHP • #1/#2 site in the Technology/Gadgets niche 02-Oct-07 OPTIMIZING SOA WITH PHP 2 2
  • 3. INTRODUCTION Let’s make sure we’re saying the same thing... 3
  • 4. Some Definitions • SOA—Service-Oriented Architecture. A structure for delivering content via services (as opposed to via databases or static content). • REST—Representational State Transfer. An “architectural style” for implementing services using an existing HTTP infrastructure. 02-Oct-07 OPTIMIZING SOA WITH PHP 4 4
  • 5. What is REST? • Resources are represented by URLs • Actions use HTTP methods (GET, POST, PUT, DELETE) • Some resources: § Representational State Transfer http://www.ics.uci.edu/~fielding/pubs/dissertation/ rest_arch_style.htm § How I Explained REST to my Wife http://tomayko.com/articles/2004/12/12/rest-to- my-wife 02-Oct-07 OPTIMIZING SOA WITH PHP 5 5
  • 6. The Data Transfer Stack • Time to read 1K of data: § CPU cache = 80ns § PC-3200 DDR RAM = 350ns § SATA hard disk = 17μs § 1000Mbit network = 20μs (plus latency) § 100Mbit network = 160μs • Faster is better • Closer is faster 02-Oct-07 OPTIMIZING SOA WITH PHP 6 6
  • 7. Implications of SOA • Content is stored on the network • Content is non-local, and therefore slow • Content is highly redundant, and therefore cacheable • Performance is contingent upon several things: § Network optimization § Parallelism § Caching 02-Oct-07 OPTIMIZING SOA WITH PHP 7 7
  • 8. What, exactly, are you talking about? A real-world example from Yahoo! Tech 8
  • 9. Yahoo! Tech traffic patterns Where do the spikes come from? 02-Oct-07 OPTIMIZING SOA WITH PHP 9 9
  • 10. The Yahoo! Front Page • The most-visited page on the Internet • Featured position F1 drives a huge amount of traffic 02-Oct-07 OPTIMIZING SOA WITH PHP 10 10
  • 11. Current Architecture Maple (PHP5) FE Other Services SQUID API MySQL PHP4 (PHP5) 02-Oct-07 OPTIMIZING SOA WITH PHP 11 11
  • 12. Lessons Learned Tips and Tricks 12
  • 13. Parallelism • For services, the latency is in the network; using parallel requests can save time • The multi-curl extension to PHP: // create both cURL resources $ch1 = curl_init(); $ch2 = curl_init(); // set URL and other appropriate options curl_setopt($ch1, CURLOPT_URL, "http://www.example.com/"); curl_setopt($ch1, CURLOPT_HEADER, 0); curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/"); curl_setopt($ch2, CURLOPT_HEADER, 0); //create the multiple cURL handle $mh = curl_multi_init(); //add the two handles curl_multi_add_handle($mh,$ch1); curl_multi_add_handle($mh,$ch2); $running=null; //execute the handles do {     curl_multi_exec($mh,$running); } while ($running > 0); //close the handles curl_multi_remove_handle($ch1); curl_multi_remove_handle($ch2); 02-Oct-07 OPTIMIZING SOA WITH PHP 13 13
  • 14. Parallelism • Preceding example is not optimal; still waits until all services are fetched before proceeding • Maple begins rendering a component as soon as the service finishes executing 02-Oct-07 OPTIMIZING SOA WITH PHP 14 14
  • 15. HTTP Cache • REST services are cacheable • Using HTTP allows any HTTP-compatible caching intermediary or proxy (e.g., squid) • Services should use Expires: or Cache-Control: headers: Cache-Control: max-age=600 • HTTP controls caching (GET, POST, PUT, DELETE) 02-Oct-07 OPTIMIZING SOA WITH PHP 15 15
  • 16. How to use a caching proxy in PHP (curl) // retries a single URL via CURL function fetch_curl($url, $timeout=5) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_PROXY, ‘’); $payload = curl_exec($ch); return $payload; } 02-Oct-07 OPTIMIZING SOA WITH PHP 16 16
  • 17. How much effect does caching have? • Yahoo! Tech, without caching: 0.6 requests/second per server • With squid cache: 15.5 requests/second per server • 2,400% increase! • Or, in hardware terms, 16 servers instead of 400 02-Oct-07 OPTIMIZING SOA WITH PHP 17 17
  • 18. Squid • Open-source, industry-standard HTTP proxy • Highly configurable • Single-threaded (sigh) • Handles ~7,000 requests/second on average hardware 02-Oct-07 OPTIMIZING SOA WITH PHP 18 18
  • 19. How to make Squid faster • Give it more RAM • Use heap LFUDA cache replacement policy (keeps frequently-accessed objects in cache longer) • Give it more RAM • Use refresh_stale_hit • Give it more RAM • Use collapsed_forwarding • Did I mention to give it more RAM? 02-Oct-07 OPTIMIZING SOA WITH PHP 19 19
  • 20. Other things you can do with Squid • Clustering (cache_peer) effectively doubles the throughput of the cache (think multi-threaded) • Always use timeouts, if possible (never wait for anything) 02-Oct-07 OPTIMIZING SOA WITH PHP 20 20
  • 21. Network Interface Optimization • Um, gigabit anyone? • Seriously, use as fast a NIC as you can afford. 02-Oct-07 OPTIMIZING SOA WITH PHP 21 21
  • 22. PHP-specific • Use absolute paths require ‘/usr/local/lib/php/something.inc’; instead of relative require ‘something.inc’; • Significant performance improvements 02-Oct-07 OPTIMIZING SOA WITH PHP 22 22
  • 23. PHP-specific, 2 • CURL sends Pragma: no-cache by default, ensuring that your content is never cached • To avoid this: curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Pragma:’)); 02-Oct-07 OPTIMIZING SOA WITH PHP 23 23
  • 24. PHP-specific, 3 • Avoid open_basedir • Ok, it’s a security risk, but it’s also a performance hit • Weigh the options and decide 02-Oct-07 OPTIMIZING SOA WITH PHP 24 24
  • 25. XML performance optimization • Never parse an XML document twice • Pass a DOM handle or SimpleXML object around 02-Oct-07 OPTIMIZING SOA WITH PHP 25 25
  • 26. Plan for failure • Always specify a timeout • Always have behavior planned in case of a timeout • Tune your timeouts for optimal effect 02-Oct-07 OPTIMIZING SOA WITH PHP 26 26
  • 27. Questions and Answers Speak up! I can’t hear you in the back! 27
  • 28. More information • Download these slides: http://files.broadpool.com/zendcon2007/ • Email me: glen.campbell@mac.com • Listen to me again: Washington, DC, PHP Conference, Nov 9th • Read my blogs: http://broadpool.com http://dailyfunnies.org http://suburbanredneck.org http://techbreakfast.org 02-Oct-07 OPTIMIZING SOA WITH PHP 28 28