SlideShare a Scribd company logo
Tips for Faster Web Site Rayed Alrashed www.alriyadh.com
Contents Front End tips Application & DB tips Web Server tips Miscellaneous tips
Contents Front End tips Application & DB tips Web Server tips Miscellaneous tips
Front End: Test Case Qaym.com
Front End: Fewer HTTP Requests Why HTTP Request are slow Fewer requests = Faster Loading 40-60% of daily visitors come in with an empty cache Making your page fast for first time visitors is key to a better user experience How Combine Files: All JavaScripts files in one file All CSS files in one file CSS Sprites Combine your background images into a single image CSS background-image and background-position  Image maps
Front End: Fewer HTTP Requests 25 requests became 19 requests
Front End: Expires Header First Request GET /image.png HTTP/1.0 Host: rayed.com HTTP/1.1 200 OK Content-Length: 290 Content-Type: image/png : image file : Second Request GET /image.png HTTP/1.0 Host: rayed.com If-Modified-Since: Sun, 11 Jun 2006 09:41:33 GMT HTTP/1.1 304 Not Modified Content-Type: image/png
Front End: Expires Header First Request GET /image.png HTTP/1.0 Host: rayed.com HTTP/1.1 200 OK Content-Length: 290 Content-Type: image/png Expires: Mon, 28 Jul 2014 23:30:00 GMT : image file : Second Request NO REQUEST
Front End: Expires Header Static component never expires Images JavaScript CSS Flash Benefit returning visitor What if I change it? Use versioning:  jquery-1.3.2.min.js
Front End: Expires Header 19 requests became 1 request 172KB became 37KB Static Files Cached
Front End: Gzip Components Why Smaller files are fast to transfer Compress all components 90% of browsers support compressed content Compressed Text = 15% of original Dynamic content php.ini zlib.output_compression = On <?php ob_start(&quot;ob_gzhandler&quot;);  JavaScript & CSS Apache mod_deflate
Front End: Gzip Components 172KB became 64K 37KB became 6.5K
Front End: Gzip Components What about Images? Already compressed Smush.it: could compress further Try different format: Sometimes PNG is smaller than GIF Don't Scale Images in HTML
Front End: Conclusion From 25 requests to 19 From 174K to 64K 270% Faster From 25 requests to 1 From 174K to 6.5K 2700% Faster
Front End: YSlow! Firefox > Firebug > YSlow! Analyzes web pages and suggests ways to improve their performance  http://developer.yahoo.com/yslow/
Contents Front End tips Application & DB tips Web Server tips Miscellaneous tips
Application & DB: First page Front page count for 30% of visits Store it in a file wget –O index.html http://mysite.com/index.php Refresh every minute! Cheap trick! Use in emergency Full page cache!
Application & DB: PHP Accelerators Caching PHP compiled bytecode Reduces server load Increases the speed from 2-10 times
Application & DB: Optimize Queries Use  “Explain” to optimize complex queries Slow query log long_query_time = 1 log-queries-not-using-indexes
Application & DB: Optimize Queries EXPLAIN SELECT * FROM `test_posts` WHERE user_id=1 ORDER BY timestamp ALTER TABLE `test_posts` ADD INDEX ( `user_id` )  ALTER TABLE `test_posts` ADD INDEX (`user_id` ,`timestamp` ) ;
Application & DB: Cache Query is optimized but still slow!! Normal Large data need long time to process Solution: Caching!! Store the result in cache Ask cache first, when not found ask DB Cache invalidation: Expiry Application invalidation
Application & DB: Cache What is cache? fast storage Where to store? File Database (cache slow result in simple table) Memory local: APC Memory remote: Memcached Cache Performance Comparison http://www.mysqlperformanceblog.com/2006/08/09/cache-performance-comparison/
Application & DB: Cache What is Memcached? Network server Store in memory Key->Value Distributed Very fast
Application & DB: Cache function get_post_by_cat($blog_id, $cat_id) { // Check Cache first $cache_id =  “post_by_cat_{$blog_id}_{$cat_id}&quot;; $cache = $this->memcached->get($cache_id); if ($cache!==false) return $cache; // Very long and time consuming query  : : $posts = $data; // Set cache $this->memcached->set($cache_id, $posts); return $posts; } Execution 30ms Execution 1000ms
Application & DB: Do it offline Do don ’t do everything at once Do the minimum to respond to the user Delay other tasks for later Don ’t make the user wait Flickr Engineers Do It Offline http://code.flickr.com/blog/2008/09/26/flickr-engineers-do-it-offline/   Queue everything and delight everyone http://decafbad.com/blog/2008/07/04/queue-everything-and-delight-everyone
Application & DB: Do it offline Post Picture Page Add to database 50ms Client Create thumbnail 300ms Add to search engine 300ms Email notifications 350ms Total time 1000 ms!!! Do everything at once
Application & DB: Do it offline Post Picture Page Add to database 50ms Client Add to Message Queue 50ms Total time 100 ms!!! Move what you can to offline Message Queue Offline process Create thumbnail 300ms Add to search engine 300ms Email notifications 350ms
Application & DB: Do it offline What is message queue?!! Database table Message Queue Server: RabbitMQ, zeromq Messages Asynchronous: do don ’t wait for an answer (synchronous: will wait) KISS!
Application & DB: Denormalize Break the rules for speed Add redundant data to avoid joins and improve query execution Extend tables vs New tables How to keep consistent? Triggers Application Pros: Faster Cons: Larger, Complicated
Application & DB: Scalability and Replication MySQL Replication: One master -> Many Slaves Application can read from any slave …  But write to one master
Contents Front End tips Application & DB tips Web Server tips Miscellaneous tips
Web Server: Architectures Forking / Threading Create Process (or thread) for each client Web Server Problem: New process takes long time Examples: Email Servers Process Client Process Client Process Client
Web Server: Architectures Preforking / Prethreading Create X Process (or thread) for potential clients Web Server Process Process Process Problem: X+1 clients Examples: Apache, IIS Client Client Client
Web Server: Architectures Event based (select, poll) One process handles all clients Web Server Process Problem: Maturity, Flexibly Example: Nginx, Lighttpd, Varnish, Squid Client Client Client
Web Server: Architectures Event based (select, poll) Very High Performance & Scalability for static files (html, images, JS, CSS, Flash) Same as preforking on dynamic content Web Server Process Client Client Client Disk PHP Process PHP Process PHP Process Other Server
Web Server: Deployment Options Apache only: Couldn ’t scale Poor performance on high load blocked.igw.net.sa + alriyadh.com Lighttpd only: Maturity & Support Issues Configuration inflexibility Mix and Match?!
Web Server: Deployment Options Apache for dynamic, Lighttpd for static: www.alriyadh.com => Apache img.alriyadh.com => lighttpd Apache Process Client Process Process Lighttpd Process Dynamic content Static content (images,js,css,html,pdf)
Web Server: Deployment Options Lighttpd serve static, and proxy dynamic to Apache Apache Process Client Process Process Lighttpd Process Disk
Web Server: Deployment Options Squid cache and proxy to Apache Squid only cache, doesn ’t have original data Squid in reverse proxy setup Varnish (http://varnish.projects.linpro.no/) Apache Process Client Process Process Squid Process Disk Disk
Contents Front End tips Application & DB tips Web Server tips Miscellaneous tips
Misc: Measuring I use Cricket Does your change really work
Misc:  If it works, don ’t fix it! KISS: Keep It Simple, Stupid design simplicity should be a key goal and that unnecessary complexity should be avoided http://highscalability.com/
Thank you

More Related Content

PPTX
Ansible presentation
PPTX
Ansible Automation Best Practices From Startups to Enterprises - Minnebar 12
PPT
Ansible presentation
PDF
IT Automation with Ansible
PDF
Ansible Automation to Rule Them All
PDF
#OktoCampus - Workshop : An introduction to Ansible
PPTX
Ansible - Crash course
Ansible presentation
Ansible Automation Best Practices From Startups to Enterprises - Minnebar 12
Ansible presentation
IT Automation with Ansible
Ansible Automation to Rule Them All
#OktoCampus - Workshop : An introduction to Ansible
Ansible - Crash course

What's hot (20)

PDF
Automation with ansible
PDF
Ansible roles done right
PDF
Ansible is the simplest way to automate. MoldCamp, 2015
PPTX
Best practices for ansible
PDF
Network Automation with Ansible
ODP
ansible why ?
PPTX
Ansible for beginners
PDF
Getting started with Ansible
PDF
Ansible Meetup Hamburg / Quickstart
PDF
Network Automation: Ansible 102
PDF
Ansible
PPTX
Introduction to Ansible
PDF
Introducing Ansible
PPTX
Ansible presentation
PDF
Ansible Introduction - Ansible Brno #1 - David Karban
PDF
Ansible : what's ansible & use case by REX
PDF
A tour of Ansible
PDF
Configuration Management in Ansible
PPT
Python Deployment with Fabric
PPTX
Introduction to ansible
Automation with ansible
Ansible roles done right
Ansible is the simplest way to automate. MoldCamp, 2015
Best practices for ansible
Network Automation with Ansible
ansible why ?
Ansible for beginners
Getting started with Ansible
Ansible Meetup Hamburg / Quickstart
Network Automation: Ansible 102
Ansible
Introduction to Ansible
Introducing Ansible
Ansible presentation
Ansible Introduction - Ansible Brno #1 - David Karban
Ansible : what's ansible & use case by REX
A tour of Ansible
Configuration Management in Ansible
Python Deployment with Fabric
Introduction to ansible
Ad

Viewers also liked (20)

ODP
дом. задания
PPTX
Diapositivas elvia blogs
PPT
Keramik hicheel
ODS
тест булгаа
PPTX
The 20th Century New Wave of Argentine Literature
PDF
Cphi licensing pavillion 2011
PDF
How to find new products to license (plg journal july 2010)
DOC
Pharma mag being seen by major clients online
PPT
I am thankful for... emir bilgili
PPTX
Diseminare mobilitate Grundtvig - MALTA - Empowerment in ict skills
PDF
Talkport i believe
PPT
PPTX
DOC
הערות הקואליציה לדיור בר השגה לתזכיר חוק התכנית הכלכלית לשנת
PPTX
Lollypop Farm Volunteering in Rochester NY
PPT
Tema X C. clasica
PDF
Uzuulen2
PPTX
Internet intro
PPT
Progetto irene simone nicole daniele 2
PPT
ประวัติส่วนตัว
дом. задания
Diapositivas elvia blogs
Keramik hicheel
тест булгаа
The 20th Century New Wave of Argentine Literature
Cphi licensing pavillion 2011
How to find new products to license (plg journal july 2010)
Pharma mag being seen by major clients online
I am thankful for... emir bilgili
Diseminare mobilitate Grundtvig - MALTA - Empowerment in ict skills
Talkport i believe
הערות הקואליציה לדיור בר השגה לתזכיר חוק התכנית הכלכלית לשנת
Lollypop Farm Volunteering in Rochester NY
Tema X C. clasica
Uzuulen2
Internet intro
Progetto irene simone nicole daniele 2
ประวัติส่วนตัว
Ad

Similar to Tips for a Faster Website (20)

ODP
Caching and tuning fun for high scalability @ FOSDEM 2012
ODP
Caching and tuning fun for high scalability @ PHPTour
PPTX
Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
ODP
Clug 2011 March web server optimisation
PPT
Top 10 Scalability Mistakes
PPT
Apache Con 2008 Top 10 Mistakes
PPTX
Joomla! Performance on Steroids
PDF
Php go vrooom!
PDF
Top ten-list
PPT
Top 30 Scalability Mistakes
PPT
Startups to Scale
ODP
Caching and tuning fun for high scalability
ODP
Caching and tuning fun for high scalability
PPT
Roy foubister (hosting high traffic sites on a tight budget)
PDF
2013 - Dustin whittle - Escalando PHP en la vida real
PPT
Zend Con 2008 Slides
ODP
Caching and tuning fun for high scalability
PPT
Scaling 101 test
PPT
Scaling 101
PPT
Web Speed And Scalability
Caching and tuning fun for high scalability @ FOSDEM 2012
Caching and tuning fun for high scalability @ PHPTour
Web Performance, Scalability, and Testing Techniques - Boston PHP Meetup
Clug 2011 March web server optimisation
Top 10 Scalability Mistakes
Apache Con 2008 Top 10 Mistakes
Joomla! Performance on Steroids
Php go vrooom!
Top ten-list
Top 30 Scalability Mistakes
Startups to Scale
Caching and tuning fun for high scalability
Caching and tuning fun for high scalability
Roy foubister (hosting high traffic sites on a tight budget)
2013 - Dustin whittle - Escalando PHP en la vida real
Zend Con 2008 Slides
Caching and tuning fun for high scalability
Scaling 101 test
Scaling 101
Web Speed And Scalability

Recently uploaded (20)

PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
cuic standard and advanced reporting.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Modernizing your data center with Dell and AMD
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Big Data Technologies - Introduction.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Cloud computing and distributed systems.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Encapsulation_ Review paper, used for researhc scholars
Chapter 3 Spatial Domain Image Processing.pdf
A Presentation on Artificial Intelligence
The Rise and Fall of 3GPP – Time for a Sabbatical?
cuic standard and advanced reporting.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Review of recent advances in non-invasive hemoglobin estimation
Modernizing your data center with Dell and AMD
NewMind AI Monthly Chronicles - July 2025
NewMind AI Weekly Chronicles - August'25 Week I
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
MYSQL Presentation for SQL database connectivity
Big Data Technologies - Introduction.pptx
Empathic Computing: Creating Shared Understanding
Diabetes mellitus diagnosis method based random forest with bat algorithm
Cloud computing and distributed systems.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

Tips for a Faster Website

  • 1. Tips for Faster Web Site Rayed Alrashed www.alriyadh.com
  • 2. Contents Front End tips Application & DB tips Web Server tips Miscellaneous tips
  • 3. Contents Front End tips Application & DB tips Web Server tips Miscellaneous tips
  • 4. Front End: Test Case Qaym.com
  • 5. Front End: Fewer HTTP Requests Why HTTP Request are slow Fewer requests = Faster Loading 40-60% of daily visitors come in with an empty cache Making your page fast for first time visitors is key to a better user experience How Combine Files: All JavaScripts files in one file All CSS files in one file CSS Sprites Combine your background images into a single image CSS background-image and background-position Image maps
  • 6. Front End: Fewer HTTP Requests 25 requests became 19 requests
  • 7. Front End: Expires Header First Request GET /image.png HTTP/1.0 Host: rayed.com HTTP/1.1 200 OK Content-Length: 290 Content-Type: image/png : image file : Second Request GET /image.png HTTP/1.0 Host: rayed.com If-Modified-Since: Sun, 11 Jun 2006 09:41:33 GMT HTTP/1.1 304 Not Modified Content-Type: image/png
  • 8. Front End: Expires Header First Request GET /image.png HTTP/1.0 Host: rayed.com HTTP/1.1 200 OK Content-Length: 290 Content-Type: image/png Expires: Mon, 28 Jul 2014 23:30:00 GMT : image file : Second Request NO REQUEST
  • 9. Front End: Expires Header Static component never expires Images JavaScript CSS Flash Benefit returning visitor What if I change it? Use versioning: jquery-1.3.2.min.js
  • 10. Front End: Expires Header 19 requests became 1 request 172KB became 37KB Static Files Cached
  • 11. Front End: Gzip Components Why Smaller files are fast to transfer Compress all components 90% of browsers support compressed content Compressed Text = 15% of original Dynamic content php.ini zlib.output_compression = On <?php ob_start(&quot;ob_gzhandler&quot;); JavaScript & CSS Apache mod_deflate
  • 12. Front End: Gzip Components 172KB became 64K 37KB became 6.5K
  • 13. Front End: Gzip Components What about Images? Already compressed Smush.it: could compress further Try different format: Sometimes PNG is smaller than GIF Don't Scale Images in HTML
  • 14. Front End: Conclusion From 25 requests to 19 From 174K to 64K 270% Faster From 25 requests to 1 From 174K to 6.5K 2700% Faster
  • 15. Front End: YSlow! Firefox > Firebug > YSlow! Analyzes web pages and suggests ways to improve their performance http://developer.yahoo.com/yslow/
  • 16. Contents Front End tips Application & DB tips Web Server tips Miscellaneous tips
  • 17. Application & DB: First page Front page count for 30% of visits Store it in a file wget –O index.html http://mysite.com/index.php Refresh every minute! Cheap trick! Use in emergency Full page cache!
  • 18. Application & DB: PHP Accelerators Caching PHP compiled bytecode Reduces server load Increases the speed from 2-10 times
  • 19. Application & DB: Optimize Queries Use “Explain” to optimize complex queries Slow query log long_query_time = 1 log-queries-not-using-indexes
  • 20. Application & DB: Optimize Queries EXPLAIN SELECT * FROM `test_posts` WHERE user_id=1 ORDER BY timestamp ALTER TABLE `test_posts` ADD INDEX ( `user_id` ) ALTER TABLE `test_posts` ADD INDEX (`user_id` ,`timestamp` ) ;
  • 21. Application & DB: Cache Query is optimized but still slow!! Normal Large data need long time to process Solution: Caching!! Store the result in cache Ask cache first, when not found ask DB Cache invalidation: Expiry Application invalidation
  • 22. Application & DB: Cache What is cache? fast storage Where to store? File Database (cache slow result in simple table) Memory local: APC Memory remote: Memcached Cache Performance Comparison http://www.mysqlperformanceblog.com/2006/08/09/cache-performance-comparison/
  • 23. Application & DB: Cache What is Memcached? Network server Store in memory Key->Value Distributed Very fast
  • 24. Application & DB: Cache function get_post_by_cat($blog_id, $cat_id) { // Check Cache first $cache_id = “post_by_cat_{$blog_id}_{$cat_id}&quot;; $cache = $this->memcached->get($cache_id); if ($cache!==false) return $cache; // Very long and time consuming query : : $posts = $data; // Set cache $this->memcached->set($cache_id, $posts); return $posts; } Execution 30ms Execution 1000ms
  • 25. Application & DB: Do it offline Do don ’t do everything at once Do the minimum to respond to the user Delay other tasks for later Don ’t make the user wait Flickr Engineers Do It Offline http://code.flickr.com/blog/2008/09/26/flickr-engineers-do-it-offline/ Queue everything and delight everyone http://decafbad.com/blog/2008/07/04/queue-everything-and-delight-everyone
  • 26. Application & DB: Do it offline Post Picture Page Add to database 50ms Client Create thumbnail 300ms Add to search engine 300ms Email notifications 350ms Total time 1000 ms!!! Do everything at once
  • 27. Application & DB: Do it offline Post Picture Page Add to database 50ms Client Add to Message Queue 50ms Total time 100 ms!!! Move what you can to offline Message Queue Offline process Create thumbnail 300ms Add to search engine 300ms Email notifications 350ms
  • 28. Application & DB: Do it offline What is message queue?!! Database table Message Queue Server: RabbitMQ, zeromq Messages Asynchronous: do don ’t wait for an answer (synchronous: will wait) KISS!
  • 29. Application & DB: Denormalize Break the rules for speed Add redundant data to avoid joins and improve query execution Extend tables vs New tables How to keep consistent? Triggers Application Pros: Faster Cons: Larger, Complicated
  • 30. Application & DB: Scalability and Replication MySQL Replication: One master -> Many Slaves Application can read from any slave … But write to one master
  • 31. Contents Front End tips Application & DB tips Web Server tips Miscellaneous tips
  • 32. Web Server: Architectures Forking / Threading Create Process (or thread) for each client Web Server Problem: New process takes long time Examples: Email Servers Process Client Process Client Process Client
  • 33. Web Server: Architectures Preforking / Prethreading Create X Process (or thread) for potential clients Web Server Process Process Process Problem: X+1 clients Examples: Apache, IIS Client Client Client
  • 34. Web Server: Architectures Event based (select, poll) One process handles all clients Web Server Process Problem: Maturity, Flexibly Example: Nginx, Lighttpd, Varnish, Squid Client Client Client
  • 35. Web Server: Architectures Event based (select, poll) Very High Performance & Scalability for static files (html, images, JS, CSS, Flash) Same as preforking on dynamic content Web Server Process Client Client Client Disk PHP Process PHP Process PHP Process Other Server
  • 36. Web Server: Deployment Options Apache only: Couldn ’t scale Poor performance on high load blocked.igw.net.sa + alriyadh.com Lighttpd only: Maturity & Support Issues Configuration inflexibility Mix and Match?!
  • 37. Web Server: Deployment Options Apache for dynamic, Lighttpd for static: www.alriyadh.com => Apache img.alriyadh.com => lighttpd Apache Process Client Process Process Lighttpd Process Dynamic content Static content (images,js,css,html,pdf)
  • 38. Web Server: Deployment Options Lighttpd serve static, and proxy dynamic to Apache Apache Process Client Process Process Lighttpd Process Disk
  • 39. Web Server: Deployment Options Squid cache and proxy to Apache Squid only cache, doesn ’t have original data Squid in reverse proxy setup Varnish (http://varnish.projects.linpro.no/) Apache Process Client Process Process Squid Process Disk Disk
  • 40. Contents Front End tips Application & DB tips Web Server tips Miscellaneous tips
  • 41. Misc: Measuring I use Cricket Does your change really work
  • 42. Misc: If it works, don ’t fix it! KISS: Keep It Simple, Stupid design simplicity should be a key goal and that unnecessary complexity should be avoided http://highscalability.com/