By Glen Campbell,
Engineering Manager
Yahoo! Tech
http://tech.yahoo.com
OPTIMIZING SOA WITH PHP
1
02-Oct-07 OPTIMIZING SOA WITH PHP
About Yahoo! Tech
• Launched May 1, 2006
• First new site launched by the Yahoo! Media
g...
INTRODUCTION
Let’s make sure we’re saying the same thing...
3
02-Oct-07 OPTIMIZING SOA WITH PHP
Some Definitions
• SOA—Service-Oriented Architecture. A structure
for delivering content ...
02-Oct-07 OPTIMIZING SOA WITH PHP
What is REST?
• Resources are represented by URLs
• Actions use HTTP methods (GET, POST,...
02-Oct-07 OPTIMIZING SOA WITH PHP
The Data Transfer Stack
• Time to read 1K of data:
§ CPU cache = 80ns
§ PC-3200 DDR RAM ...
02-Oct-07 OPTIMIZING SOA WITH PHP
Implications of SOA
• Content is stored on the network
• Content is non-local, and there...
What, exactly, are you talking about?
A real-world example from Yahoo! Tech
8
02-Oct-07 OPTIMIZING SOA WITH PHP
Yahoo! Tech traffic patterns
9
Where do the spikes come from?
9
02-Oct-07 OPTIMIZING SOA WITH PHP
The Yahoo! Front Page
• The most-visited page on the Internet
• Featured position F1 dri...
02-Oct-07 OPTIMIZING SOA WITH PHP
Current Architecture
11
FE
API
SQUID
Other Services
MySQL
Maple (PHP5)
PHP4 (PHP5)
11
Lessons Learned
Tips and Tricks
12
02-Oct-07 OPTIMIZING SOA WITH PHP
Parallelism
• For services, the latency is in the network; using
parallel requests can s...
02-Oct-07 OPTIMIZING SOA WITH PHP
Parallelism
• Preceding example is not optimal; still waits
until all services are fetch...
02-Oct-07 OPTIMIZING SOA WITH PHP
HTTP Cache
• REST services are cacheable
• Using HTTP allows any HTTP-compatible
caching...
02-Oct-07 OPTIMIZING SOA WITH PHP
How to use a caching proxy in PHP (curl)
// retries a single URL via CURL
function fetch...
02-Oct-07 OPTIMIZING SOA WITH PHP
How much effect does caching have?
• Yahoo! Tech, without caching:
0.6 requests/second p...
02-Oct-07 OPTIMIZING SOA WITH PHP
Squid
• Open-source, industry-standard HTTP proxy
• Highly configurable
• Single-threaded...
02-Oct-07 OPTIMIZING SOA WITH PHP
How to make Squid faster
• Give it more RAM
• Use heap LFUDA cache replacement policy (k...
02-Oct-07 OPTIMIZING SOA WITH PHP
Other things you can do with Squid
• Clustering (cache_peer) effectively doubles the
thr...
02-Oct-07 OPTIMIZING SOA WITH PHP
Network Interface Optimization
• Um, gigabit anyone?
• Seriously, use as fast a NIC as y...
02-Oct-07 OPTIMIZING SOA WITH PHP
PHP-specific
• Use absolute paths
require ‘/usr/local/lib/php/something.inc’;
instead of ...
02-Oct-07 OPTIMIZING SOA WITH PHP
PHP-specific, 2
• CURL sends Pragma: no-cache by default, ensuring
that your content is n...
02-Oct-07 OPTIMIZING SOA WITH PHP
PHP-specific, 3
• Avoid open_basedir
• Ok, it’s a security risk, but it’s also a
performa...
02-Oct-07 OPTIMIZING SOA WITH PHP
XML performance optimization
• Never parse an XML document twice
• Pass a DOM handle or ...
02-Oct-07 OPTIMIZING SOA WITH PHP
Plan for failure
• Always specify a timeout
• Always have behavior planned in case of a
...
Questions and Answers
Speak up! I can’t hear you in the back!
27
02-Oct-07 OPTIMIZING SOA WITH PHP
More information
• Download these slides:
http://files.broadpool.com/zendcon2007/
• Email...
Upcoming SlideShare
Loading in...5
×

OPTIMIZING SOA WITH PHP

1,378

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,378
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
31
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

OPTIMIZING SOA WITH PHP

  1. 1. By Glen Campbell, Engineering Manager Yahoo! Tech http://tech.yahoo.com OPTIMIZING SOA WITH PHP 1
  2. 2. 02-Oct-07 OPTIMIZING SOA WITH PHP 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 2 2
  3. 3. INTRODUCTION Let’s make sure we’re saying the same thing... 3
  4. 4. 02-Oct-07 OPTIMIZING SOA WITH PHP 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. 4 4
  5. 5. 02-Oct-07 OPTIMIZING SOA WITH PHP 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 5 5
  6. 6. 02-Oct-07 OPTIMIZING SOA WITH PHP 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 6 6
  7. 7. 02-Oct-07 OPTIMIZING SOA WITH PHP 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 7 7
  8. 8. What, exactly, are you talking about? A real-world example from Yahoo! Tech 8
  9. 9. 02-Oct-07 OPTIMIZING SOA WITH PHP Yahoo! Tech traffic patterns 9 Where do the spikes come from? 9
  10. 10. 02-Oct-07 OPTIMIZING SOA WITH PHP The Yahoo! Front Page • The most-visited page on the Internet • Featured position F1 drives a huge amount of traffic 10 10
  11. 11. 02-Oct-07 OPTIMIZING SOA WITH PHP Current Architecture 11 FE API SQUID Other Services MySQL Maple (PHP5) PHP4 (PHP5) 11
  12. 12. Lessons Learned Tips and Tricks 12
  13. 13. 02-Oct-07 OPTIMIZING SOA WITH PHP 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); 13 13
  14. 14. 02-Oct-07 OPTIMIZING SOA WITH PHP 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 14 14
  15. 15. 02-Oct-07 OPTIMIZING SOA WITH PHP 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) 15 15
  16. 16. 02-Oct-07 OPTIMIZING SOA WITH PHP 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, ‘127.0.0.1:3128’); $payload = curl_exec($ch); return $payload; } 16 16
  17. 17. 02-Oct-07 OPTIMIZING SOA WITH PHP 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 17 17
  18. 18. 02-Oct-07 OPTIMIZING SOA WITH PHP Squid • Open-source, industry-standard HTTP proxy • Highly configurable • Single-threaded (sigh) • Handles ~7,000 requests/second on average hardware 18 18
  19. 19. 02-Oct-07 OPTIMIZING SOA WITH PHP 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? 19 19
  20. 20. 02-Oct-07 OPTIMIZING SOA WITH PHP 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) 20 20
  21. 21. 02-Oct-07 OPTIMIZING SOA WITH PHP Network Interface Optimization • Um, gigabit anyone? • Seriously, use as fast a NIC as you can afford. 21 21
  22. 22. 02-Oct-07 OPTIMIZING SOA WITH PHP PHP-specific • Use absolute paths require ‘/usr/local/lib/php/something.inc’; instead of relative require ‘something.inc’; • Significant performance improvements 22 22
  23. 23. 02-Oct-07 OPTIMIZING SOA WITH PHP 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:’)); 23 23
  24. 24. 02-Oct-07 OPTIMIZING SOA WITH PHP PHP-specific, 3 • Avoid open_basedir • Ok, it’s a security risk, but it’s also a performance hit • Weigh the options and decide 24 24
  25. 25. 02-Oct-07 OPTIMIZING SOA WITH PHP XML performance optimization • Never parse an XML document twice • Pass a DOM handle or SimpleXML object around 25 25
  26. 26. 02-Oct-07 OPTIMIZING SOA WITH PHP Plan for failure • Always specify a timeout • Always have behavior planned in case of a timeout • Tune your timeouts for optimal effect 26 26
  27. 27. Questions and Answers Speak up! I can’t hear you in the back! 27
  28. 28. 02-Oct-07 OPTIMIZING SOA WITH PHP 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 28 28
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×