SlideShare une entreprise Scribd logo
1  sur  52
Télécharger pour lire hors ligne
Scaling Websites with
Amazon’s EC2
Rusty Conover (rconover@infogears.com)
http://www.infogears.com
About Me
• Co-founder of InfoGears
• NYC via Montana and NJ.
• Computer Science
• Price comparison engine
• @rusty_conover
Audience Survey
Audience Survey
Amazon
Audience Survey
Amazon
Linux
Audience Survey
Amazon
Linux
HTTP
Audience Survey
Amazon
Linux
HTTP
DNS
Audience Survey
Amazon
Linux
HTTP
DNS
Apache
Audience Survey
Amazon
Linux
HTTP
DNS
Apache
TCP/IP
Audience Survey
Amazon
Linux
HTTP
DNS
Apache
TCP/IP
Megabits
Audience Survey
Amazon
Linux
HTTP
DNS
Apache
TCP/IP
Megabits
“Expires”
Header
Audience Survey
Amazon
Linux
HTTP
DNS
Apache
TCP/IP
Megabits
“Expires”
Header
Regular
Expressions
Audience Survey
Amazon
Linux
HTTP
DNS
Apache
TCP/IP
Megabits
“Expires”
Header
Regular
Expressions
Proxy Servers
Audience Survey
Amazon
Linux
HTTP
DNS
Apache
TCP/IP
Megabits
“Expires”
Header
Regular
Expressions
Proxy Servers
Hash
Algorithms
Audience Survey
Amazon
Linux
HTTP
DNS
Apache
TCP/IP
Megabits
“Expires”
Header
Regular
Expressions
Proxy Servers
Hash
Algorithms
TCP Windowing
The Problems
• If the site’s slow, users leave dissatisfied
which often means lost sales.
• Bandwidth is relatively expensive.
• It’s hard to anticipate needs.
• Resources (time, money and people) are
always limited.
Other Concerns
• Thousands of people all want to watch
your video at once.
• Viral campaigns
• The unexpected media mention.
The solutions
Content Distribution Networks
Load Balancers
Reverse Proxying
Content Distribution
Client
Internet
DNS Returns
closest server to
client.
Sydney
New York
Seattle
Tokyo
San Fran
Client
Designer
CDN
Master
Example Providers:
Load Balancer
Internet
Server 4
Server 5
Server 2
Server 1
Server 3
Client Designer
Content is pushed
to each server.
Load balancer sends
requests to a server it
chooses based on a heuristic.
All traffic goes through
the load balancer.
Reverse Proxy
Internet
Actual Server
Client
Designer
Proxy server checks if request is
cached, if not it pulls from the actual
server and caches for future requests.
Content placed
on actual server.
Our Solution
Internet
Client
Proxy/Cache server serves
requests at 250mbit/sec.
Request for
Dynamic Content
Request for
Static Content
Normal bandwidth is 3mbit/sec.
Server Farm
Amazon’s EC2
Allows you to have as many virtual machines as
you’d like.
• Basic - 1.0 Ghz 2007 Xeon 1.7 GB
RAM, 160 GB storage.
• Large - 8 CPUs, 15 GB Ram, 1680
GB storage, 64-bit platform.
Bandwidth...
• Amazon EC2’s bandwidth is about 250 -
1000Mbps.
• You’re only billed for what you use. Making
it cheap, but different than what you’re used
to.
• Amazon is located closer to peering
points. There are East Coast and Europe
DC’s.
Pricing
It’s all usage based, with discounts.
Bandwidth CPU Usage
$0.10 per GB (incoming) $0.03 per hour Small
$0.17 per GB
(first 10 TB outgoing)
$0.12 per hour Large
Free between S3 and EC2
Billed the entire time the
machine is online.
Limitations of EC2
• When an instance is shutdown all of the
disks are wiped. Cache is cleared.
• There are no guarantees that a particular
machine will remain up.
So what’s it good for?
1. Parallel processing tasks without building
a server farm.
2. Building cache servers to serve your
content on quickly and cheaper than you
can.
3. Bragging about how your infrastructure
is in “the cloud”.
How to scale
Most websites have both static and dynamic
content
Serving separately will increase response
time.
Static Dynamic
Images,Videos, CSS, JS ASP, PHP, Perl, CGI, HTML
Files that don’t change. Files that do change
Larger Smaller
Dynamic Proxy/Cache
• Static requests are only sent to the reverse
proxy/cache.
• Redirects to the real server if there an error
http://www.foo.com/movie.mpg
to
http://cache.foo.com/movie.mpg
The Details
EC2 Cache
Failover Server
(redirects)
Real Servers
DNS Servers
Monitoring
HTTP Requests
pulled into the cache
and streamed to client.
Test that the cache is
live and returning good results.
Populate
the active
address
for the cache
PowerBook G4
Client
Request address
of cache.
DNS
DNS Update
HTTP
HTTP Redirect
Traffic Types
Dynamic Content
Requests
A Little About EC2
• Amazon provides a number of disk images,
like ISOs for base installs.
• Fedora Core & Windows.
• You can customize your own install but
start with something small.
Amazon Images
$ ec2-describe-images -o self -o amazon
IMAGE! ami-3c03e655!cache7/image.manifest.xml!
314456711494
! available private!
IMAGE! ami-20b65349!ec2-public-images/fedora-core4-
base.manifest.xml amazon! available! public!
Create an instance
Create an instance:
$ ec2-run-instances ami-2103e648
Showing current instances:
$ ec2-describe-instances
RESERVATION!r-9a8076f3! 314456711494!default
INSTANCE! i-4603fc2f! ami-3c03e655
! ec2-72-44-35-86.z-2.compute-1.amazonaws.com
! domU-12-31-35-00-09-C2.z-2.compute-1.internal
! running! ! 0! ! m1.small
! 2008-02-20T22:07:13+0000
Stopping and cleaning up an instance:
$ ec2-terminate-instances i-4603fc2f
INSTANCE! i-4603fc2f! terminated! terminated
DNS
EC2 will go down, when you least expect it.
You don’t want the users to get errors and you
don’t want to be sending requests to a down
server for very long.
Use dynamic DNS updates and keep very short
TTL times for the records. Or EC2’s static
addresses.
Monitoring and DNS code needs to be reliable,
use more then once separate network.
DNS Redirection
If you host more then one website, generally you
don’t want to setup instances for every domain.
Setup one caching instance, and then create CNAME
records for all of your other domains.
For instance to cache requests at
www.prolitegear.com I can use cache.prolitegear.com
which is a CNAME for c.cache.infogears.com.
DNS Flowchart
Request for c.cache.infogears.com
Reply is CNAME
c.cache.infogears.com
TTL: 4 Hrs
Request for
cache.icebreaker.com
Is Amazon
server online?
Reply is 4.4.4.4
TTL: 10 seconds
Reply is 7.7.7.7
TTL: 10 seconds
Yes No
The Cache Stack
Fedora Core 4 w/Updates
sshd
bind
apache
ntpd
snmpd
Setup
• You need to build in mod_cache, mod_proxy &
mod_rewrite.
• Keep the server as small as possible, no PHP or
mod_perl.
• You can set it up to use a memory or disk
cache.
./configure --enable-cache --enable-mem-cache --enable-disk-cache --enable-proxy --enable-proxy-
http --enable-status --enable-info --enable-rewrite --disable-proxy-ftp --disable-proxy-ajp --
disable-proxy-balancer --enable-deflate --disable-cgi --disable-cgid --disable-userdir --disable-
alias --disable-cgid --disable-actions --disable-negotiation --disable-asis --disable-info --
disable-filter --disable-static --enable-headers
Logging
It’s good to judge cache hits to make sure your cache is
working.
LogFormat "%{Host}i %h %l %u %t "%r" %>s %b "%{Referer}i" "%
{User-Agent}i" %{Age}" proxy
The Age header contains the age of the cached result in
seconds, if not found it logs “-”.
Logs should be sent back to reliable storage every so
often.
Proxy
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
• Make sure you don’t make an open proxy.
• Our proxy requests will only be the result of rewrite
rules.
Rewrite
RewriteMap lowercase int:tolower
RewriteMap cachehost txt:/usr/local/apache2/conf/cache-host.map
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${cachehost:%1} ^(.+)$
RewriteRule ^/(.*.(gif|jpg|jpeg|png))$ http://%1/$1 [P,L,NC]
RewriteRule ^/$ http://www.infogears.com [R,L]
The rewrite rule is what changes the cached url into the real url to
pull for the request.
The map file just lists the cache host name [TAB] destination host
name.
Host Mapping
# Destination Host Source Host
static-cache.gearbuyer.com! static.gearbuyer.com
images-cache.gearbuyer.com! images.gearbuyer.com
cache.gearbuyer.com!
! www.gearbuyer.com
Cache
CacheEnable disk /
CacheRoot /mnt/cache
CacheDirLevels 3
CacheDirLength 1
CacheIgnoreCacheControl Off
CacheDefaultExpire 7200
CacheMaxExpire 604800
CacheMaxFileSize 2000000000
Make sure that the cache root exists before Apache starts, otherwise it
won’t start. /mnt is a good place.
Make sure you have the correct permissions so Apache can write to the
directory.
Change the directory levels and limits to suit your needs.
Scaling
ServerLimit 600
StartServers 20
MinSpareServers 20
MaxSpareServers 60
MaxClients 500
MaxRequestsPerChild 0
MaxKeepAliveRequests 1000
KeepAlive On
KeepAliveTimeout 10
SendBufferSize 98303
Since you’re serving static requests it won’t take much RAM to
scale out more processes.
Keep alive connections should persist as they prevent another
TCP handshake.
Monitoring
Monitoring is important to make
sure that EC2 can reach your
servers, and your EC2 server is still
running.
I use Perl for this since it has
everything I need: a way to update
DNS and a way to send web
requests.
SNMP Traffic Monitoring is also
essential.
Monitoring Do this forever
Try example proxy
request using Amazon
Did it succeed?
Set address to
failback server
Has Amazon address
already been set?
Set Amazon address
Sleep
Yes
No
No
Yes
Hit Rate
Set Expires header on everything you
can.
ExpiresActive on
ExpiresByType image/gif "access plus 8 hours"
ExpiresByType image/png "access plus 8 hours"
ExpiresByType image/jpeg "access plus 8 hours"
ExpiresByType text/css "access plus 8 hours"
ExpiresByType application/x-javascript "access plus 8 hours"
ExpiresByType application/x-shockwave-flash "access plus 8 hours"
ExpiresByType video/x-flv "access plus 8 hours"
ExpiresByType application/pdf "access plus 8 hours"
You can force refreshes by doing a reload,
or using wget --no-cache
SNMP Monitoring
These graphs are generated by Cacti
Building your own CDN using Amazon EC2
Date Hits Misses GB Mean Hit
Size
Hit Ratio Hits Per Second
Oct 1, 2008
Nov 1, 2008
Dec 1, 2008
Jan 1, 2009
Feb 1, 2009
Mar 1, 2009
Apr 1, 2009
May 1, 2009
Jun 1, 2009
Jul 1, 2009
Aug 1, 2009
Sep 1, 2009
44,156,082 4,559,317 936 22 89.7% 16.8
61,403,833 3,475,474 1,291 22 94.3% 23.3
66,850,143 3,896,923 1,342 21 94.2% 25.4
67,415,819 3,877,521 1,302 20 94.2% 25.6
59,252,656 3,817,855 1,107 20 93.6% 22.5
67,494,160 4,391,207 1,452 23 93.5% 25.7
58,472,655 4,823,061 1,277 23 91.8% 22.2
57,608,518 4,719,774 1,327 24 91.8% 21.9
55,105,350 4,658,961 1,326 25 91.5% 21.0
53,533,558 4,993,568 1,626 32 90.7% 20.4
59,870,784 5,642,222 1,371 24 90.6% 22.8
62,931,209 5,519,812 1,490 25 91.2% 23.9
References
• Page Load Time:
• http://www.die.net/musings/page_load_time/
• Amazon EC2 Reference:
• http://docs.amazonwebservices.com/AWSEC2/2007-08-29/GettingStartedGuide/
• Amazon Web Services:
• http://aws.amazon.com
• Perl
• http://www.perl.org
• Bind Dynamic Updates
• http://www.isc.org/sw/bind/arm93/Bv9ARM.ch04.html
• Apache
• http://httpd.apache.org
Amazon Cloudfront
• Price: $0.01 per 10k req, $0.17/gb traffic +
S3 costs
• Sept: $62/hits, $253.30/traffic = $316 not
counting S3 costs.
• Have to preload all resources to S3. Cache
has about 2.2 million objects in 36 gigs.

Contenu connexe

Tendances

Content Caching with NGINX and NGINX Plus
Content Caching with NGINX and NGINX PlusContent Caching with NGINX and NGINX Plus
Content Caching with NGINX and NGINX PlusKevin Jones
 
Nginx - Tips and Tricks.
Nginx - Tips and Tricks.Nginx - Tips and Tricks.
Nginx - Tips and Tricks.Harish S
 
Load Balancing Applications with NGINX in a CoreOS Cluster
Load Balancing Applications with NGINX in a CoreOS ClusterLoad Balancing Applications with NGINX in a CoreOS Cluster
Load Balancing Applications with NGINX in a CoreOS ClusterKevin Jones
 
High Availability Content Caching with NGINX
High Availability Content Caching with NGINXHigh Availability Content Caching with NGINX
High Availability Content Caching with NGINXNGINX, Inc.
 
Load Balancing MySQL with HAProxy - Slides
Load Balancing MySQL with HAProxy - SlidesLoad Balancing MySQL with HAProxy - Slides
Load Balancing MySQL with HAProxy - SlidesSeveralnines
 
Load Balancing with Nginx
Load Balancing with NginxLoad Balancing with Nginx
Load Balancing with NginxMarian Marinov
 
5 things you didn't know nginx could do
5 things you didn't know nginx could do5 things you didn't know nginx could do
5 things you didn't know nginx could dosarahnovotny
 
Mitigating Security Threats with Fastly - Joe Williams at Fastly Altitude 2015
Mitigating Security Threats with Fastly - Joe Williams at Fastly Altitude 2015Mitigating Security Threats with Fastly - Joe Williams at Fastly Altitude 2015
Mitigating Security Threats with Fastly - Joe Williams at Fastly Altitude 2015Fastly
 
Rate Limiting with NGINX and NGINX Plus
Rate Limiting with NGINX and NGINX PlusRate Limiting with NGINX and NGINX Plus
Rate Limiting with NGINX and NGINX PlusNGINX, Inc.
 
Delivering High Performance Websites with NGINX
Delivering High Performance Websites with NGINXDelivering High Performance Websites with NGINX
Delivering High Performance Websites with NGINXNGINX, Inc.
 
cache concepts and varnish-cache
cache concepts and varnish-cachecache concepts and varnish-cache
cache concepts and varnish-cacheMarc Cortinas Val
 
Multi-Layer DDoS Mitigation Strategies
Multi-Layer DDoS Mitigation StrategiesMulti-Layer DDoS Mitigation Strategies
Multi-Layer DDoS Mitigation StrategiesSagi Brody
 
5 things you didn't know nginx could do velocity
5 things you didn't know nginx could do   velocity5 things you didn't know nginx could do   velocity
5 things you didn't know nginx could do velocitysarahnovotny
 
Why Managed Service Providers Should Embrace Container Technology
Why Managed Service Providers Should Embrace Container TechnologyWhy Managed Service Providers Should Embrace Container Technology
Why Managed Service Providers Should Embrace Container TechnologySagi Brody
 
Using NGINX as an Effective and Highly Available Content Cache
Using NGINX as an Effective and Highly Available Content CacheUsing NGINX as an Effective and Highly Available Content Cache
Using NGINX as an Effective and Highly Available Content CacheKevin Jones
 
NGINX: Basics & Best Practices - EMEA Broadcast
NGINX: Basics & Best Practices - EMEA BroadcastNGINX: Basics & Best Practices - EMEA Broadcast
NGINX: Basics & Best Practices - EMEA BroadcastNGINX, Inc.
 
NGINX 101 - now with more Docker
NGINX 101 - now with more DockerNGINX 101 - now with more Docker
NGINX 101 - now with more DockerSarah Novotny
 
NginX - good practices, tips and advanced techniques
NginX - good practices, tips and advanced techniquesNginX - good practices, tips and advanced techniques
NginX - good practices, tips and advanced techniquesClaudio Borges
 

Tendances (20)

Content Caching with NGINX and NGINX Plus
Content Caching with NGINX and NGINX PlusContent Caching with NGINX and NGINX Plus
Content Caching with NGINX and NGINX Plus
 
Nginx - Tips and Tricks.
Nginx - Tips and Tricks.Nginx - Tips and Tricks.
Nginx - Tips and Tricks.
 
Oscon 2010 - ATS
Oscon 2010 - ATSOscon 2010 - ATS
Oscon 2010 - ATS
 
Load Balancing Applications with NGINX in a CoreOS Cluster
Load Balancing Applications with NGINX in a CoreOS ClusterLoad Balancing Applications with NGINX in a CoreOS Cluster
Load Balancing Applications with NGINX in a CoreOS Cluster
 
High Availability Content Caching with NGINX
High Availability Content Caching with NGINXHigh Availability Content Caching with NGINX
High Availability Content Caching with NGINX
 
Load Balancing MySQL with HAProxy - Slides
Load Balancing MySQL with HAProxy - SlidesLoad Balancing MySQL with HAProxy - Slides
Load Balancing MySQL with HAProxy - Slides
 
Load Balancing with Nginx
Load Balancing with NginxLoad Balancing with Nginx
Load Balancing with Nginx
 
Usenix lisa 2011
Usenix lisa 2011Usenix lisa 2011
Usenix lisa 2011
 
5 things you didn't know nginx could do
5 things you didn't know nginx could do5 things you didn't know nginx could do
5 things you didn't know nginx could do
 
Mitigating Security Threats with Fastly - Joe Williams at Fastly Altitude 2015
Mitigating Security Threats with Fastly - Joe Williams at Fastly Altitude 2015Mitigating Security Threats with Fastly - Joe Williams at Fastly Altitude 2015
Mitigating Security Threats with Fastly - Joe Williams at Fastly Altitude 2015
 
Rate Limiting with NGINX and NGINX Plus
Rate Limiting with NGINX and NGINX PlusRate Limiting with NGINX and NGINX Plus
Rate Limiting with NGINX and NGINX Plus
 
Delivering High Performance Websites with NGINX
Delivering High Performance Websites with NGINXDelivering High Performance Websites with NGINX
Delivering High Performance Websites with NGINX
 
cache concepts and varnish-cache
cache concepts and varnish-cachecache concepts and varnish-cache
cache concepts and varnish-cache
 
Multi-Layer DDoS Mitigation Strategies
Multi-Layer DDoS Mitigation StrategiesMulti-Layer DDoS Mitigation Strategies
Multi-Layer DDoS Mitigation Strategies
 
5 things you didn't know nginx could do velocity
5 things you didn't know nginx could do   velocity5 things you didn't know nginx could do   velocity
5 things you didn't know nginx could do velocity
 
Why Managed Service Providers Should Embrace Container Technology
Why Managed Service Providers Should Embrace Container TechnologyWhy Managed Service Providers Should Embrace Container Technology
Why Managed Service Providers Should Embrace Container Technology
 
Using NGINX as an Effective and Highly Available Content Cache
Using NGINX as an Effective and Highly Available Content CacheUsing NGINX as an Effective and Highly Available Content Cache
Using NGINX as an Effective and Highly Available Content Cache
 
NGINX: Basics & Best Practices - EMEA Broadcast
NGINX: Basics & Best Practices - EMEA BroadcastNGINX: Basics & Best Practices - EMEA Broadcast
NGINX: Basics & Best Practices - EMEA Broadcast
 
NGINX 101 - now with more Docker
NGINX 101 - now with more DockerNGINX 101 - now with more Docker
NGINX 101 - now with more Docker
 
NginX - good practices, tips and advanced techniques
NginX - good practices, tips and advanced techniquesNginX - good practices, tips and advanced techniques
NginX - good practices, tips and advanced techniques
 

En vedette

Computo en la nube con open stack
Computo en la nube con open stackComputo en la nube con open stack
Computo en la nube con open stackOmar Lara
 
Market Research Report : IT Managed Services market in India 2012
Market Research Report :  IT Managed Services market in India 2012 Market Research Report :  IT Managed Services market in India 2012
Market Research Report : IT Managed Services market in India 2012 Netscribes, Inc.
 
Scaling Wix with microservices architecture and multi-cloud platforms - Reve...
 Scaling Wix with microservices architecture and multi-cloud platforms - Reve... Scaling Wix with microservices architecture and multi-cloud platforms - Reve...
Scaling Wix with microservices architecture and multi-cloud platforms - Reve...Aviran Mordo
 
2010 2013 sandro suffert memory forensics introdutory work shop - public
2010 2013 sandro suffert memory forensics introdutory work shop - public2010 2013 sandro suffert memory forensics introdutory work shop - public
2010 2013 sandro suffert memory forensics introdutory work shop - publicSandro Suffert
 

En vedette (6)

Computo en la nube con open stack
Computo en la nube con open stackComputo en la nube con open stack
Computo en la nube con open stack
 
Market Research Report : IT Managed Services market in India 2012
Market Research Report :  IT Managed Services market in India 2012 Market Research Report :  IT Managed Services market in India 2012
Market Research Report : IT Managed Services market in India 2012
 
Scaling Wix with microservices architecture and multi-cloud platforms - Reve...
 Scaling Wix with microservices architecture and multi-cloud platforms - Reve... Scaling Wix with microservices architecture and multi-cloud platforms - Reve...
Scaling Wix with microservices architecture and multi-cloud platforms - Reve...
 
Portugal country guide
Portugal country guide      Portugal country guide
Portugal country guide
 
Marco Hogewoning -XS4all
Marco Hogewoning -XS4allMarco Hogewoning -XS4all
Marco Hogewoning -XS4all
 
2010 2013 sandro suffert memory forensics introdutory work shop - public
2010 2013 sandro suffert memory forensics introdutory work shop - public2010 2013 sandro suffert memory forensics introdutory work shop - public
2010 2013 sandro suffert memory forensics introdutory work shop - public
 

Plus de SergeyChernyshev

Capturing speed of user experience using user timing api
Capturing speed of user experience using user timing apiCapturing speed of user experience using user timing api
Capturing speed of user experience using user timing apiSergeyChernyshev
 
Managing application performance by Kwame Thomison
Managing application performance by Kwame ThomisonManaging application performance by Kwame Thomison
Managing application performance by Kwame ThomisonSergeyChernyshev
 
Fastest request is never made
Fastest request is never madeFastest request is never made
Fastest request is never madeSergeyChernyshev
 
Designing speed with progressive enhancement
Designing speed with progressive enhancementDesigning speed with progressive enhancement
Designing speed with progressive enhancementSergeyChernyshev
 
Web performance tools @ WebPerf.camp 2016
Web performance tools @ WebPerf.camp 2016Web performance tools @ WebPerf.camp 2016
Web performance tools @ WebPerf.camp 2016SergeyChernyshev
 
Extending your applications to the edge with CDNs
Extending your applications to the edge with CDNsExtending your applications to the edge with CDNs
Extending your applications to the edge with CDNsSergeyChernyshev
 
What we can learn from CDNs about Web Development, Deployment, and Performance
What we can learn from CDNs about Web Development, Deployment, and PerformanceWhat we can learn from CDNs about Web Development, Deployment, and Performance
What we can learn from CDNs about Web Development, Deployment, and PerformanceSergeyChernyshev
 
Scalability vs. Performance
Scalability vs. PerformanceScalability vs. Performance
Scalability vs. PerformanceSergeyChernyshev
 
Web performance: beyond load testing
Web performance: beyond load testingWeb performance: beyond load testing
Web performance: beyond load testingSergeyChernyshev
 
Introduction to web performance @ IEEE
Introduction to web performance @ IEEEIntroduction to web performance @ IEEE
Introduction to web performance @ IEEESergeyChernyshev
 
Introduction to Web Performance
Introduction to Web PerformanceIntroduction to Web Performance
Introduction to Web PerformanceSergeyChernyshev
 
Performance anti patterns in ajax applications
Performance anti patterns in ajax applicationsPerformance anti patterns in ajax applications
Performance anti patterns in ajax applicationsSergeyChernyshev
 
Keeping track of your performance using Show Slow
Keeping track of your performance using Show SlowKeeping track of your performance using Show Slow
Keeping track of your performance using Show SlowSergeyChernyshev
 
Keeping track of your performance using show slow
Keeping track of your performance using show slowKeeping track of your performance using show slow
Keeping track of your performance using show slowSergeyChernyshev
 
Php Code Profiling Using X Debug
Php Code Profiling Using X DebugPhp Code Profiling Using X Debug
Php Code Profiling Using X DebugSergeyChernyshev
 

Plus de SergeyChernyshev (20)

Capturing speed of user experience using user timing api
Capturing speed of user experience using user timing apiCapturing speed of user experience using user timing api
Capturing speed of user experience using user timing api
 
Managing application performance by Kwame Thomison
Managing application performance by Kwame ThomisonManaging application performance by Kwame Thomison
Managing application performance by Kwame Thomison
 
Fastest request is never made
Fastest request is never madeFastest request is never made
Fastest request is never made
 
Designing speed with progressive enhancement
Designing speed with progressive enhancementDesigning speed with progressive enhancement
Designing speed with progressive enhancement
 
Web performance tools @ WebPerf.camp 2016
Web performance tools @ WebPerf.camp 2016Web performance tools @ WebPerf.camp 2016
Web performance tools @ WebPerf.camp 2016
 
Extending your applications to the edge with CDNs
Extending your applications to the edge with CDNsExtending your applications to the edge with CDNs
Extending your applications to the edge with CDNs
 
Speed is feature #1
Speed is feature #1Speed is feature #1
Speed is feature #1
 
Tools of the trade 2014
Tools of the trade 2014Tools of the trade 2014
Tools of the trade 2014
 
What we can learn from CDNs about Web Development, Deployment, and Performance
What we can learn from CDNs about Web Development, Deployment, and PerformanceWhat we can learn from CDNs about Web Development, Deployment, and Performance
What we can learn from CDNs about Web Development, Deployment, and Performance
 
Scalability vs. Performance
Scalability vs. PerformanceScalability vs. Performance
Scalability vs. Performance
 
Web performance: beyond load testing
Web performance: beyond load testingWeb performance: beyond load testing
Web performance: beyond load testing
 
Introduction to web performance @ IEEE
Introduction to web performance @ IEEEIntroduction to web performance @ IEEE
Introduction to web performance @ IEEE
 
Introduction to Web Performance
Introduction to Web PerformanceIntroduction to Web Performance
Introduction to Web Performance
 
Performance anti patterns in ajax applications
Performance anti patterns in ajax applicationsPerformance anti patterns in ajax applications
Performance anti patterns in ajax applications
 
Taming 3rd party content
Taming 3rd party contentTaming 3rd party content
Taming 3rd party content
 
Velocity 2010 review
Velocity 2010 reviewVelocity 2010 review
Velocity 2010 review
 
Keeping track of your performance using Show Slow
Keeping track of your performance using Show SlowKeeping track of your performance using Show Slow
Keeping track of your performance using Show Slow
 
Keeping track of your performance using show slow
Keeping track of your performance using show slowKeeping track of your performance using show slow
Keeping track of your performance using show slow
 
In Search of Speed
In Search of SpeedIn Search of Speed
In Search of Speed
 
Php Code Profiling Using X Debug
Php Code Profiling Using X DebugPhp Code Profiling Using X Debug
Php Code Profiling Using X Debug
 

Building your own CDN using Amazon EC2

  • 1. Scaling Websites with Amazon’s EC2 Rusty Conover (rconover@infogears.com) http://www.infogears.com
  • 2. About Me • Co-founder of InfoGears • NYC via Montana and NJ. • Computer Science • Price comparison engine • @rusty_conover
  • 16. The Problems • If the site’s slow, users leave dissatisfied which often means lost sales. • Bandwidth is relatively expensive. • It’s hard to anticipate needs. • Resources (time, money and people) are always limited.
  • 17. Other Concerns • Thousands of people all want to watch your video at once. • Viral campaigns • The unexpected media mention.
  • 18. The solutions Content Distribution Networks Load Balancers Reverse Proxying
  • 19. Content Distribution Client Internet DNS Returns closest server to client. Sydney New York Seattle Tokyo San Fran Client Designer CDN Master Example Providers:
  • 20. Load Balancer Internet Server 4 Server 5 Server 2 Server 1 Server 3 Client Designer Content is pushed to each server. Load balancer sends requests to a server it chooses based on a heuristic. All traffic goes through the load balancer.
  • 21. Reverse Proxy Internet Actual Server Client Designer Proxy server checks if request is cached, if not it pulls from the actual server and caches for future requests. Content placed on actual server.
  • 22. Our Solution Internet Client Proxy/Cache server serves requests at 250mbit/sec. Request for Dynamic Content Request for Static Content Normal bandwidth is 3mbit/sec. Server Farm
  • 23. Amazon’s EC2 Allows you to have as many virtual machines as you’d like. • Basic - 1.0 Ghz 2007 Xeon 1.7 GB RAM, 160 GB storage. • Large - 8 CPUs, 15 GB Ram, 1680 GB storage, 64-bit platform.
  • 24. Bandwidth... • Amazon EC2’s bandwidth is about 250 - 1000Mbps. • You’re only billed for what you use. Making it cheap, but different than what you’re used to. • Amazon is located closer to peering points. There are East Coast and Europe DC’s.
  • 25. Pricing It’s all usage based, with discounts. Bandwidth CPU Usage $0.10 per GB (incoming) $0.03 per hour Small $0.17 per GB (first 10 TB outgoing) $0.12 per hour Large Free between S3 and EC2 Billed the entire time the machine is online.
  • 26. Limitations of EC2 • When an instance is shutdown all of the disks are wiped. Cache is cleared. • There are no guarantees that a particular machine will remain up.
  • 27. So what’s it good for? 1. Parallel processing tasks without building a server farm. 2. Building cache servers to serve your content on quickly and cheaper than you can. 3. Bragging about how your infrastructure is in “the cloud”.
  • 28. How to scale Most websites have both static and dynamic content Serving separately will increase response time. Static Dynamic Images,Videos, CSS, JS ASP, PHP, Perl, CGI, HTML Files that don’t change. Files that do change Larger Smaller
  • 29. Dynamic Proxy/Cache • Static requests are only sent to the reverse proxy/cache. • Redirects to the real server if there an error http://www.foo.com/movie.mpg to http://cache.foo.com/movie.mpg
  • 30. The Details EC2 Cache Failover Server (redirects) Real Servers DNS Servers Monitoring HTTP Requests pulled into the cache and streamed to client. Test that the cache is live and returning good results. Populate the active address for the cache PowerBook G4 Client Request address of cache. DNS DNS Update HTTP HTTP Redirect Traffic Types Dynamic Content Requests
  • 31. A Little About EC2 • Amazon provides a number of disk images, like ISOs for base installs. • Fedora Core & Windows. • You can customize your own install but start with something small.
  • 32. Amazon Images $ ec2-describe-images -o self -o amazon IMAGE! ami-3c03e655!cache7/image.manifest.xml! 314456711494 ! available private! IMAGE! ami-20b65349!ec2-public-images/fedora-core4- base.manifest.xml amazon! available! public!
  • 33. Create an instance Create an instance: $ ec2-run-instances ami-2103e648 Showing current instances: $ ec2-describe-instances RESERVATION!r-9a8076f3! 314456711494!default INSTANCE! i-4603fc2f! ami-3c03e655 ! ec2-72-44-35-86.z-2.compute-1.amazonaws.com ! domU-12-31-35-00-09-C2.z-2.compute-1.internal ! running! ! 0! ! m1.small ! 2008-02-20T22:07:13+0000 Stopping and cleaning up an instance: $ ec2-terminate-instances i-4603fc2f INSTANCE! i-4603fc2f! terminated! terminated
  • 34. DNS EC2 will go down, when you least expect it. You don’t want the users to get errors and you don’t want to be sending requests to a down server for very long. Use dynamic DNS updates and keep very short TTL times for the records. Or EC2’s static addresses. Monitoring and DNS code needs to be reliable, use more then once separate network.
  • 35. DNS Redirection If you host more then one website, generally you don’t want to setup instances for every domain. Setup one caching instance, and then create CNAME records for all of your other domains. For instance to cache requests at www.prolitegear.com I can use cache.prolitegear.com which is a CNAME for c.cache.infogears.com.
  • 36. DNS Flowchart Request for c.cache.infogears.com Reply is CNAME c.cache.infogears.com TTL: 4 Hrs Request for cache.icebreaker.com Is Amazon server online? Reply is 4.4.4.4 TTL: 10 seconds Reply is 7.7.7.7 TTL: 10 seconds Yes No
  • 37. The Cache Stack Fedora Core 4 w/Updates sshd bind apache ntpd snmpd
  • 38. Setup • You need to build in mod_cache, mod_proxy & mod_rewrite. • Keep the server as small as possible, no PHP or mod_perl. • You can set it up to use a memory or disk cache. ./configure --enable-cache --enable-mem-cache --enable-disk-cache --enable-proxy --enable-proxy- http --enable-status --enable-info --enable-rewrite --disable-proxy-ftp --disable-proxy-ajp -- disable-proxy-balancer --enable-deflate --disable-cgi --disable-cgid --disable-userdir --disable- alias --disable-cgid --disable-actions --disable-negotiation --disable-asis --disable-info -- disable-filter --disable-static --enable-headers
  • 39. Logging It’s good to judge cache hits to make sure your cache is working. LogFormat "%{Host}i %h %l %u %t "%r" %>s %b "%{Referer}i" "% {User-Agent}i" %{Age}" proxy The Age header contains the age of the cached result in seconds, if not found it logs “-”. Logs should be sent back to reliable storage every so often.
  • 40. Proxy ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy> • Make sure you don’t make an open proxy. • Our proxy requests will only be the result of rewrite rules.
  • 41. Rewrite RewriteMap lowercase int:tolower RewriteMap cachehost txt:/usr/local/apache2/conf/cache-host.map RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$ RewriteCond ${cachehost:%1} ^(.+)$ RewriteRule ^/(.*.(gif|jpg|jpeg|png))$ http://%1/$1 [P,L,NC] RewriteRule ^/$ http://www.infogears.com [R,L] The rewrite rule is what changes the cached url into the real url to pull for the request. The map file just lists the cache host name [TAB] destination host name.
  • 42. Host Mapping # Destination Host Source Host static-cache.gearbuyer.com! static.gearbuyer.com images-cache.gearbuyer.com! images.gearbuyer.com cache.gearbuyer.com! ! www.gearbuyer.com
  • 43. Cache CacheEnable disk / CacheRoot /mnt/cache CacheDirLevels 3 CacheDirLength 1 CacheIgnoreCacheControl Off CacheDefaultExpire 7200 CacheMaxExpire 604800 CacheMaxFileSize 2000000000 Make sure that the cache root exists before Apache starts, otherwise it won’t start. /mnt is a good place. Make sure you have the correct permissions so Apache can write to the directory. Change the directory levels and limits to suit your needs.
  • 44. Scaling ServerLimit 600 StartServers 20 MinSpareServers 20 MaxSpareServers 60 MaxClients 500 MaxRequestsPerChild 0 MaxKeepAliveRequests 1000 KeepAlive On KeepAliveTimeout 10 SendBufferSize 98303 Since you’re serving static requests it won’t take much RAM to scale out more processes. Keep alive connections should persist as they prevent another TCP handshake.
  • 45. Monitoring Monitoring is important to make sure that EC2 can reach your servers, and your EC2 server is still running. I use Perl for this since it has everything I need: a way to update DNS and a way to send web requests. SNMP Traffic Monitoring is also essential.
  • 46. Monitoring Do this forever Try example proxy request using Amazon Did it succeed? Set address to failback server Has Amazon address already been set? Set Amazon address Sleep Yes No No Yes
  • 47. Hit Rate Set Expires header on everything you can. ExpiresActive on ExpiresByType image/gif "access plus 8 hours" ExpiresByType image/png "access plus 8 hours" ExpiresByType image/jpeg "access plus 8 hours" ExpiresByType text/css "access plus 8 hours" ExpiresByType application/x-javascript "access plus 8 hours" ExpiresByType application/x-shockwave-flash "access plus 8 hours" ExpiresByType video/x-flv "access plus 8 hours" ExpiresByType application/pdf "access plus 8 hours" You can force refreshes by doing a reload, or using wget --no-cache
  • 48. SNMP Monitoring These graphs are generated by Cacti
  • 50. Date Hits Misses GB Mean Hit Size Hit Ratio Hits Per Second Oct 1, 2008 Nov 1, 2008 Dec 1, 2008 Jan 1, 2009 Feb 1, 2009 Mar 1, 2009 Apr 1, 2009 May 1, 2009 Jun 1, 2009 Jul 1, 2009 Aug 1, 2009 Sep 1, 2009 44,156,082 4,559,317 936 22 89.7% 16.8 61,403,833 3,475,474 1,291 22 94.3% 23.3 66,850,143 3,896,923 1,342 21 94.2% 25.4 67,415,819 3,877,521 1,302 20 94.2% 25.6 59,252,656 3,817,855 1,107 20 93.6% 22.5 67,494,160 4,391,207 1,452 23 93.5% 25.7 58,472,655 4,823,061 1,277 23 91.8% 22.2 57,608,518 4,719,774 1,327 24 91.8% 21.9 55,105,350 4,658,961 1,326 25 91.5% 21.0 53,533,558 4,993,568 1,626 32 90.7% 20.4 59,870,784 5,642,222 1,371 24 90.6% 22.8 62,931,209 5,519,812 1,490 25 91.2% 23.9
  • 51. References • Page Load Time: • http://www.die.net/musings/page_load_time/ • Amazon EC2 Reference: • http://docs.amazonwebservices.com/AWSEC2/2007-08-29/GettingStartedGuide/ • Amazon Web Services: • http://aws.amazon.com • Perl • http://www.perl.org • Bind Dynamic Updates • http://www.isc.org/sw/bind/arm93/Bv9ARM.ch04.html • Apache • http://httpd.apache.org
  • 52. Amazon Cloudfront • Price: $0.01 per 10k req, $0.17/gb traffic + S3 costs • Sept: $62/hits, $253.30/traffic = $316 not counting S3 costs. • Have to preload all resources to S3. Cache has about 2.2 million objects in 36 gigs.