+ All Categories
Home > Documents > OPTIMIZING SOA WITH PHP

OPTIMIZING SOA WITH PHP

Date post: 18-Nov-2014
Category:
Upload: zubin67
View: 1,794 times
Download: 1 times
Share this document with a friend
Description:
 
28
By Glen Campbell, Engineering Manager Yahoo! Tech http://tech.yahoo.com OPTIMIZING SOA WITH PHP 1
Transcript
Page 1: OPTIMIZING SOA WITH PHP

By Glen Campbell,Engineering ManagerYahoo! Techhttp://tech.yahoo.com

OPTIMIZING SOA WITH PHP

1

Page 2: OPTIMIZING SOA WITH PHP

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

22

Page 3: OPTIMIZING SOA WITH PHP

INTRODUCTIONLet’s make sure we’re saying the same thing...

3

Page 4: OPTIMIZING SOA WITH PHP

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.

44

Page 6: OPTIMIZING SOA WITH PHP

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

66

Page 7: OPTIMIZING SOA WITH PHP

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

77

Page 8: OPTIMIZING SOA WITH PHP

What, exactly, are you talking about? A real-world example from Yahoo! Tech

8

Page 9: OPTIMIZING SOA WITH PHP

02-Oct-07 OPTIMIZING SOA WITH PHP

Yahoo! Tech traffic patterns

9

Where do the spikes come from?

9

Page 10: OPTIMIZING SOA WITH PHP

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

1010

Page 11: OPTIMIZING SOA WITH PHP

02-Oct-07 OPTIMIZING SOA WITH PHP

Current Architecture

11

FE

API

SQUIDOther Services

MySQL

Maple (PHP5)

PHP4 (PHP5)

11

Page 12: OPTIMIZING SOA WITH PHP

Lessons LearnedTips and Tricks

12

Page 13: OPTIMIZING SOA WITH PHP

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 optionscurl_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 handlescurl_multi_add_handle($mh,$ch1);curl_multi_add_handle($mh,$ch2);$running=null;//execute the handlesdo {    curl_multi_exec($mh,$running);} while ($running > 0);//close the handlescurl_multi_remove_handle($ch1);curl_multi_remove_handle($ch2);

1313

Page 14: OPTIMIZING SOA WITH PHP

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

1414

Page 15: OPTIMIZING SOA WITH PHP

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)

1515

Page 16: OPTIMIZING SOA WITH PHP

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;

}

1616

Page 17: OPTIMIZING SOA WITH PHP

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

1717

Page 18: OPTIMIZING SOA WITH PHP

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

1818

Page 19: OPTIMIZING SOA WITH PHP

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?

1919

Page 20: OPTIMIZING SOA WITH PHP

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)

2020

Page 21: OPTIMIZING SOA WITH PHP

02-Oct-07 OPTIMIZING SOA WITH PHP

Network Interface Optimization

• Um, gigabit anyone?• Seriously, use as fast a NIC as you can afford.

2121

Page 22: OPTIMIZING SOA WITH PHP

02-Oct-07 OPTIMIZING SOA WITH PHP

PHP-specific

• Use absolute pathsrequire ‘/usr/local/lib/php/something.inc’;

instead of relativerequire ‘something.inc’;

• Significant performance improvements

2222

Page 23: OPTIMIZING SOA WITH PHP

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:’));

2323

Page 24: OPTIMIZING SOA WITH PHP

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

2424

Page 25: OPTIMIZING SOA WITH PHP

02-Oct-07 OPTIMIZING SOA WITH PHP

XML performance optimization

• Never parse an XML document twice• Pass a DOM handle or SimpleXML object around

2525

Page 26: OPTIMIZING SOA WITH PHP

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

2626

Page 27: OPTIMIZING SOA WITH PHP

Questions and AnswersSpeak up! I can’t hear you in the back!

27

Page 28: OPTIMIZING SOA WITH PHP

02-Oct-07 OPTIMIZING SOA WITH PHP

More information

• Download these slides:http://files.broadpool.com/zendcon2007/

• Email me:[email protected]

• Listen to me again:Washington, DC, PHP Conference, Nov 9th

• Read my blogs:http://broadpool.comhttp://dailyfunnies.orghttp://suburbanredneck.orghttp://techbreakfast.org

2828


Recommended