Slide 1: Evolving architecture
make development easy and your site faster
Leo Lapworth @ YAPC::EU 2007 http://leo.cuckoo.org/
Slide 2: Covering
Slide 3: Covering
How running websites has evolved
Slide 4: Covering
How running websites has evolved Servers improved
Slide 5: Covering
How running websites has evolved Servers improved Architecture improved
Slide 6: Covering
How running websites has evolved Servers improved Architecture improved Development tools improved
Slide 7: Covering
How running websites has evolved Servers improved Architecture improved Development tools improved Code (and code libraries) got better
Slide 8: but not...
Slide 9: but not...
Covering anything in depth
Slide 10: Concepts stolen from
Slide 11: Concepts stolen from
Mod_perl guide
Slide 12: Concepts stolen from
Mod_perl guide Léon Brocard
Slide 13: Concepts stolen from
Mod_perl guide Léon Brocard Six Apart
Slide 14: Concepts stolen from
Mod_perl guide Léon Brocard Six Apart Yahoo!
Slide 15: Concepts stolen from
Mod_perl guide Léon Brocard Six Apart Yahoo! + many other people...
Slide 16: Concepts stolen from
Mod_perl guide Léon Brocard Six Apart Yahoo! + many other people... ... this is social Perl
Slide 17: Templates
Slide 18: Templates
An example of how things have improved...
Slide 19: Recognise this?
Slide 20: Recognise this?
use CGI; $q = CGI->new(); $name = $q->param('name'); print $q->header('text/html'); $link = 'http://foo.com/'; print "<html><head><title>HTML and code together</title></head><body>"; print "<a href=\"$link/$name\">"; print "Click here $name</a><br />\n"; print "<table>" . join("\n", map { "<tr><td>$_</td></tr>" } qw(this is hard to maintain)); print "</table>";
Slide 21: How about...
Slide 22: How about...
Now, was it cellspacing, or cellpadding I need for ie4.02?
Slide 23: and...
Slide 24: and...
I'm a developer, not a designer. I shouldn't have to know this £@$%!
Slide 25: Use templates...
Slide 26: Use templates...
use Template;
Slide 27: Code, not HTML
Slide 28: Code, not HTML
use CGI; use Template; $q = CGI->new(); print $q->header('text/html'); my %vals = ( name = $q->param('name'), ); $template = Template->new({ INCLUDE => '/path/' }); $template->process('hello.html',\%vals);
Slide 29: I just code, I have clean code and I don't have to know HTML or browser compatibility issues
Slide 30: Simple changes make a huge difference
Slide 31: Servers
Slide 32: Single server
Slide 33: Single server
Internet
Web server (static & dynamic) Database server
All editing
Slide 34: Lots of...
Slide 35: and...
I thought I only changed one thing, but it's still broken
Slide 36: 1 live server 1 development server
Slide 37: 1 live server 1 development server
Internet
Live server Web server (static & dynamic) Database server
Development server Web server (static & dynamic) Database server All editing
Slide 38: Does not solve:
What did I change last? Who changed what / when?
Slide 39: You need...
Slide 40: You need...
Slide 41: Version control & docs
Slide 42: Version control & docs
Use SVK with Subversion
Slide 43: Version control & docs
Use SVK with Subversion
Slide 44: Version control & docs
Use SVK with Subversion
Clkao - SVK
Slide 45: Version control & docs
Use SVK with Subversion Use Trac (http://trac.edgewall.org)
Clkao - SVK
Slide 46: Version control & docs
Use SVK with Subversion Use Trac (http://trac.edgewall.org) - wiki
Clkao - SVK
Slide 47: Version control & docs
Use SVK with Subversion Use Trac (http://trac.edgewall.org) - wiki - task tracker Clkao - SVK
Slide 48: Version control & docs
Use SVK with Subversion Use Trac (http://trac.edgewall.org) - wiki - task tracker - web interface to Subversion Clkao - SVK
Slide 50: Tests - all you need on one slide
Slide 51: Tests - all you need on one slide
Write tests, write tests often
Slide 52: Tests - all you need on one slide
Write tests, write tests often Run tests, run tests often
Slide 53: Tests - all you need on one slide
Write tests, write tests often Run tests, run tests often Tests are good
Slide 54: Tests - all you need on one slide
Write tests, write tests often Run tests, run tests often Tests are good Found a bug? - write the test that replicates it. You know it's fixed when your test passes
Slide 55: Tests - all you need on one slide
Write tests, write tests often Run tests, run tests often Tests are good Found a bug? - write the test that replicates it. You know it's fixed when your test passes Use Test::More, and then test some more!
Slide 56: Making your site faster
Slide 57: mod_perl
Slide 58: mod_perl
Code caching (loaded/compiled once)
Slide 59: mod_perl
Code caching (loaded/compiled once) Server only spends time running the code
Slide 60: mod_perl
Code caching (loaded/compiled once) Server only spends time running the code Apache::Registry for old/existing code
Slide 61: mod_perl
Code caching (loaded/compiled once) Server only spends time running the code Apache::Registry for old/existing code Apache::SizeLimit (safety net)
Slide 62: mod_perl
Code caching (loaded/compiled once) Server only spends time running the code Apache::Registry for old/existing code Apache::SizeLimit (safety net) Set it high
Slide 63: mod_perl
Code caching (loaded/compiled once) Server only spends time running the code Apache::Registry for old/existing code Apache::SizeLimit (safety net) Set it high Check it
Slide 64: Front/back end split
Slide 65: Front/back end split
Internet
Slow Front end Web server (static & proxy to application server) Fast Web server (dynamic) Database server Development server Back end (application server)
Slide 66: or put another way...
Slide 67: or put another way...
Internet slow and demanding kids (users)
Slide 68: or put another way...
Canteen
Internet slow and demanding kids (users)
Front end Dinner-ladies - good at serving precooked (static) food. Can order from à la carte chefs and then serve
Slide 69: or put another way...
Canteen Kitchen
Internet slow and demanding kids (users)
Front end Dinner-ladies - good at serving precooked (static) food. Can order from à la carte chefs and then serve
Back end Chefs - good at making à la carte (dynamic) food
Slide 70: Internet
Slow Front end Web server (static & proxy to application server) Fast Web server (dynamic) Database server Development server Back end (application server)
Slide 71: Internet
Slow Front end Web server (static & proxy to application server) Fast Web server (dynamic) Database server Development server Back end (application server)
Slide 72: Speed up the frequent...
Slide 73: Speed up the frequent...
Are you re-creating the same objects/data structures again and again?
Slide 74: Speed up the frequent...
Are you re-creating the same objects/data structures again and again? Can this information persist for a period of time?
Slide 75: Add caching
Slide 76: Add caching
Persisting data for a period of time
Slide 77: Add caching
Persisting data for a period of time Simple to implement:
Slide 78: Add caching
Persisting data for a period of time Simple to implement: Does cache exist?
Slide 79: Add caching
Persisting data for a period of time Simple to implement: Does cache exist? YES -> retrieve it and return it
Slide 80: Add caching
Persisting data for a period of time Simple to implement: Does cache exist? YES -> retrieve it and return it NO -> create data/object, store in cache and return it
Slide 81: Things you might cache
Slide 82: Things you might cache
Search result sets (a list of ids)
Slide 83: Things you might cache
Search result sets (a list of ids) Individual items
Slide 84: Things you might cache
Search result sets (a list of ids) Individual items Look-ups of information from a database
Slide 85: Things you might cache
Search result sets (a list of ids) Individual items Look-ups of information from a database Fetching of information from external sources
Slide 86: Centralise
Slide 87: Centralise
Put caching methods in one package
Slide 88: Centralise
Put caching methods in one package Put any generic methods in a package
Slide 89: We now have a cache
Internet
Slow Front end Web server (static & proxy to application server) Fast Web server (dynamic)
Cache
Back end (application server)
Database server Development server
Slide 90: We now have a cache
Internet
Slow Front end Web server (static & proxy to application server) Fast Web server Cache Cache (dynamic) Database server Development server Back end (application server)
Slide 91: Adding in the cache...
Slide 92: Adding in the cache...
Internet Kids
Slide 93: Adding in the cache...
Internet Kids
Front end Dinner-ladies
Slide 94: Adding in the cache...
Internet Kids
Front end Dinner-ladies
Back end Chefs
Slide 95: Adding in the cache...
Internet Kids
Front end Dinner-ladies
Back end Chefs
Cache Magic freezer
Slide 96: Coping with more traffic
Slide 97: Coping with more traffic
Internet Stage server Development server (in the office)
Front end Web server (static & proxy to application server) Back end (application server) Web server (dynamic) Back end (application server) Web server (dynamic) Database server
Slide 98: Coping with more traffic
Internet Stage server Development server (in the office)
Front end Web server (static & proxy to application server) Back end (application server) Web server (dynamic) Back end (application server) Web server (dynamic) Database server
Slide 99: That's two separate disks
Slide 100: That's two separate disks
Internet Stage server Development server (in the office)
Front end Web server (static & proxy to application server) Back end (application server) Web server (dynamic)
Cache
Database server
Back end (application server) Web server (dynamic)
Cache
Slide 101: Second kitchen, two freezers...
Slide 102: Second kitchen, two freezers...
Internet Kids
Slide 103: Second kitchen, two freezers...
Internet Kids
Front end Dinner-ladies
Slide 104: Second kitchen, two freezers...
Kitchen 1
Kitchen 2
Internet Kids
Front end Dinner-ladies
Back end Chefs
Slide 105: Second kitchen, two freezers...
Kitchen 1
Kitchen 2
Internet Kids
Front end Dinner-ladies
Back end Chefs
Cache Freezers
Slide 106: We want a cache across servers...
Slide 107: We want a cache across servers...
Enter Memcache
Slide 108: We want a cache across servers...
Enter Memcache
See Léon Brocard's talk at 16:20 for the details
Slide 109: Internet Stage server
Development server (in the office)
Front end Web server (static & proxy to application server) Back end (application server) Web server (dynamic)
Cache
Database server
Back end (application server) Web server (dynamic)
Slide 110: Shared freezer
Slide 111: Shared freezer
Internet Kids
Slide 112: Shared freezer
Internet Kids
Front end Dinner-ladies
Slide 113: Shared freezer
Kitchen 1
Kitchen 2 Internet Kids Front end Dinner-ladies
Back end Chefs
Slide 114: Shared freezer
Kitchen 1
Kitchen 2 Internet Kids Front end Dinner-ladies
Back end Chefs
Cache Freezer
Slide 115: Maintaining servers
Slide 116: Maintaining servers
Keep all servers identical where possible
Slide 117: Maintaining servers
Keep all servers identical where possible Package your code (e.g. .debs)
Slide 118: Maintaining servers
Keep all servers identical where possible Package your code (e.g. .debs) Centralise your crontabs and configuration files (and put in version control)
Slide 119: Maintaining servers
Keep all servers identical where possible Package your code (e.g. .debs) Centralise your crontabs and configuration files (and put in version control) Deploy with one script: rsync + ssh + apt-get update & apt-get upgrade (or your OS equivalent) to each server
Slide 120: Simplify further
Internet Stage server Development server (in the office)
Front end Perlbal load balancer / proxy
Back end Web server (static+dynamic)
Cache
Database server
Back end Web server (static+dynamic)
Slide 121: Simplify further
Internet Stage server Development server (in the office)
Front end Perlbal load balancer / proxy
Back end Web server (static+dynamic)
Cache
Database server
Back end Web server (static+dynamic)
Slide 122: Simplify further
Internet Stage server Development server (in the office)
Front end Perlbal load balancer / proxy
Back end Web server (static+dynamic)
Cache
Database server
Back end Web server (static+dynamic)
Slide 123: Waiters: cheaper, faster and less demanding
Slide 124: Waiters: cheaper, faster and less demanding
Internet Kids
Slide 125: Waiters: cheaper, faster and less demanding
Internet Kids
Front end Waiters - good at serving quickly and dealing with annoying customers or Kitchens
Slide 126: Waiters: cheaper, faster and less demanding
Kitchen 1
Kitchen 2
Internet Kids Front end Waiters - good at serving Back end quickly and dealing with annoying customers or Chefs - simple and complex meals (static and dynamic) Kitchens
Slide 127: Waiters: cheaper, faster and less demanding
Kitchen 1
Kitchen 2
Internet Kids Front end Waiters - good at serving Back end quickly and dealing with annoying customers or Chefs - simple and complex meals (static and dynamic) Kitchens
Cache Freezer
Slide 128: More tips for faster user experience
Slide 129: mod_gzip
Slide 130: mod_gzip
HTML/css/xml
Slide 131: mod_gzip
HTML/css/xml
Slide 132: mod_gzip
HTML/css/xml
gzip
Slide 133: mod_gzip
HTML/css/xml
gzip
Slide 134: mod_gzip
HTML/css/xml
gzip
Users
Slide 135: mod_gzip
HTML/css/xml
gzip
Users
If a browser support compressions... compress (javascript can be tricky - at least minify)
Slide 136: Cache headers (expiry)
Slide 137: Cache headers (expiry)
Slide 138: Cache headers (expiry)
Let web browsers know how long to cache
Slide 139: Cache somethings forever
Slide 140: Cache somethings forever
/includes/js/<VERSION>/common.js
Slide 141: Cache somethings forever
/includes/js/<VERSION>/common.js Ensures user has version which matches HTML (client could have cache of old HTML)
Slide 142: Cache somethings forever
/includes/js/<VERSION>/common.js Ensures user has version which matches HTML (client could have cache of old HTML) Use include file to update all pages
Slide 143: Handling images: MogileFS
Slide 144: Handling images: MogileFS
Store images by group (replication levels)
Slide 145: Handling images: MogileFS
Store images by group (replication levels) Spread IO across disks & servers
Slide 146: Handling images: MogileFS
Store images by group (replication levels) Spread IO across disks & servers Retrieve from fastest server
Slide 147: Handling images: MogileFS
Store images by group (replication levels) Spread IO across disks & servers Retrieve from fastest server Integrate with Perlbal, hidden from the user
Slide 148: Handling images: MogileFS
Store images by group (replication levels) Spread IO across disks & servers Retrieve from fastest server Integrate with Perlbal, hidden from the user Automatic resilience
Slide 149: Handling images: MogileFS
Store images by group (replication levels) Spread IO across disks & servers Retrieve from fastest server Integrate with Perlbal, hidden from the user Automatic resilience
Slide 150: Basic rules to consider