Heavy Web Optimization
                   BACK-END
                          <?php //from a PHP dev ?>




Vo Duy Tuan   PHP Developer from Reader.vn, Picto.vn & more…
$who_am_I?
   My Name is Võ Duy Tuấn
   PHP 5 Zend Certified Engineer
   Interests: PHP, Social Network, Optimize Web
   Works:
     Thegioididong

     Reader.vn,   Picto.vn CEO/Founder
     Trainer

     Freelancer

     Consultant
$table_of_content
   Warm up: Some best practices
   Back-end Optimization
   De-normalization
   Asynchronous processing
   Caching
   Scalability
1   Warm up: Some best practices
1.1. Smarty Trick
a. Prevent recompile .tpl file:
$smarty = new Smarty();
$smarty->compile_check = false;


b. Compile all .tpl files:
set_time_limit(1000);    //do not interrupt
$smarty->compileAllTemplates('.tpl',true);
1.2. Pagination
   From




   To
1.2. Pagination – Why?
   How do we make pagination feature?

mysql_query(‘SELECT COUNT(*) FROM tbl WHERE col LIKE “%word%”’)
> Get total record > Get toal page > Build page number list with a simple loop


mysql_query(‘SELECT * FROM tbl WHERE col LIKE “%word%” LIMIT 0,20’)
> Get data of current page (page 1, with 20 rows/page)




Do you have the answer  ?
1.3. Get enough data from remote server

   Send HEAD to detect 404 error from remote
    URL

   Read first bytes of remote file to detect image
    type,width/height
2   Back-end Optimization
2.1. Where is Back-end?

“80% of the end-user response time is spent on the front-
end. Most of this time is tied up in downloading all the
components in the page: images, stylesheets, scripts, Flash,
etc. Reducing the number of components in turn reduces the
number of HTTP requests required to render the page. This
is the key to faster pages.”
2.2. Back-end Optimize Theory
   Reduce disk In/Out
   Reduce complex/fuzzy
   Reduce budget
2.3. Debugging & Profiling
   Nusphere Phped Debugger
   Xdebug
   top command
   Apache bench
2.3.1. Nusphere Phped Debugger
   Installation:

       Step 1: Install php extension

       Step 2: Install DBGbar addon for Firefox or DBG

        debugger on Chrome

       Step 3: Update PHP.INI config

       Restart your web server (if needed)
2.3.1. Nusphere Phped Debugger
2.3.2. Xdebug
   Installation:

       Step 1: Install php extension

       Step 2: Update PHP.INI config

       Restart your server (if needed)
2.3.2. Xdebug
 PHP.INI
 [xdebug]
 zend_extension_ts = C:..php_xdebug-2.1.0-5.2-vc6.dll
 xdebug.profiler_output_dir = "C:xdebug"
 xdebug.profiler_output_name = "callgrind.%R.%t"
 xdebug.profiler_enable = 0
 xdebug.profiler_append=0
 xdebug.profiler_enable_trigger = 1
 xdebug.extended_info=1


 Usage:
 http://example.com/index.php?XDEBUG_PROFILE
2.3.2. Xdebug
2.3.3. linux command top
2.3.4. Apache Bench
Somewhere in the dark…
3   De-normalization
3.1. What is De-normalization?

   Most of use live with Relational DB.
   What is normalization?
       Database normalization is the process of organizing
        the fields and tables of a relational database to
        minimize redundancy and dependency .(Wiki)

   What is De-normalization?
       Denormalization is the process of attempting to
        optimise the read performance of a database by
        adding redundant data or by grouping data. (Wiki)
3.2. How to de-normalize?

   Vertical Split.
   Pre-calculated value.
   Duplicate data.
3.2.1. Vertical Split
3.2.1. Vertical Split (cont.)
3.2.2. Pre-calculated value
3.2.3. Duplicate data
3.3. Data correctness
   Normalization = Correctness
   Denormalization = Performance
4   Asynchronous processing
4.1. Job queue
4.2. Job queue :: Gearman




                       http://gearman.org/
But….
Apache can be a job queue,
4.3. Async Request
function backgroundHttpPost($url, $prm = ''){
   $p=parse_url($url);
   $fp = fsockopen($p['host'], isset($p['port'])?$p['port']:80, $en, $es, 30);

    if (!$fp)
          return false;
    else {
          $out = "POST ".$p['path']."?".$p['query']." HTTP/1.1rn";
          $out.= "Host: ".$p['host']."rn";
          $out.= "Content-Type: application/x-www-form-urlencodedrn";
          $out.= "Content-Length: ".strlen($prm)."rn";
          $out.= "Connection: Closernrn";
          if ($prm != '') $out.= $prm;
          fwrite($fp, $out); fclose($fp);
          return true;
    }
}
Usage:
$taskUrl = 'http://picto.vn/task/deletephoto';
backgroundHttpPost($taskUrl, 'photoid=' . $myPhoto->id);
5   Caching
5.1. What is caching?
5.2. What is APC?

   Alternative PHP Cache – APC
   Free PHP Extension
   PHP Opcode cache
   Support User data cache
   Run on SAME machine with PHP
   NOT support multi servers
5.3. APC Monitor
5.4. APC Using
5.5. APC Tip
   Opcode your web code only
   Disable APC for PhpMyAdmin

<Directory "/mysite/public_html/">
    ...
    php_admin_flag apc.cache_by_default On
</Directory

<Directory "/mysite/public_html/phpmyadmin/">
    ...
    php_admin_flag apc.cache_by_default Off
</Directory>
2.6. APC Tip..
   Cache STRING is better than ARRAY
   Long Time To Live better than Short
   Case study:
       Caching for Feed System:
BEFORE
BEFORE
ANALYSE
AFTER
Short TTL make fragmentation
Short TTL make fragmentation
6   Cloud computing - AWS
       Amazon Elastic Compute Cloud – Amazon EC2 + EBS
       Amazon Simple Storage Service – Amazon S3
       Amazon Simple Email Service – Amazon SES
6.1. Amazon EC2

5
6.1. Amazon EC2 (cont.)

5
6.2. Amazon S3

5
6.2. Amazon S3 (cont.)

5
6.2. Amazon S3 (cont.)

5
6.3. Amazon SES
   1,000,000 emails = $100
5
   Support SMTP
6.3. Amazon SES (cont.)

5
6.4. Adapt to AWS for scalability
   Local server (Web & RDBMS) + S3
5
   EC2 (Web) + EBS (RDB) + S3
   EC2 (Web) + EBS (local storage) + Amazon
    RDS (RDB) + S3
   EC2 (Web) + EBS (local storage) + EC2
    (noSQL) + S3

   …Can use with more Amazon Services
Thank you!
It’s time to query …

CONTACT
Fullname: Vo Duy Tuan
Email: tuanmaster2002@yahoo.com
Phone: 0938 916 902
Website: http://bloghoctap.com

Heavy Web Optimization: Backend

  • 1.
    Heavy Web Optimization BACK-END <?php //from a PHP dev ?> Vo Duy Tuan PHP Developer from Reader.vn, Picto.vn & more…
  • 2.
    $who_am_I?  My Name is Võ Duy Tuấn  PHP 5 Zend Certified Engineer  Interests: PHP, Social Network, Optimize Web  Works:  Thegioididong  Reader.vn, Picto.vn CEO/Founder  Trainer  Freelancer  Consultant
  • 3.
    $table_of_content  Warm up: Some best practices  Back-end Optimization  De-normalization  Asynchronous processing  Caching  Scalability
  • 4.
    1 Warm up: Some best practices
  • 5.
    1.1. Smarty Trick a.Prevent recompile .tpl file: $smarty = new Smarty(); $smarty->compile_check = false; b. Compile all .tpl files: set_time_limit(1000); //do not interrupt $smarty->compileAllTemplates('.tpl',true);
  • 6.
  • 7.
    1.2. Pagination –Why?  How do we make pagination feature? mysql_query(‘SELECT COUNT(*) FROM tbl WHERE col LIKE “%word%”’) > Get total record > Get toal page > Build page number list with a simple loop mysql_query(‘SELECT * FROM tbl WHERE col LIKE “%word%” LIMIT 0,20’) > Get data of current page (page 1, with 20 rows/page) Do you have the answer  ?
  • 8.
    1.3. Get enoughdata from remote server  Send HEAD to detect 404 error from remote URL  Read first bytes of remote file to detect image type,width/height
  • 9.
    2 Back-end Optimization
  • 10.
    2.1. Where isBack-end? “80% of the end-user response time is spent on the front- end. Most of this time is tied up in downloading all the components in the page: images, stylesheets, scripts, Flash, etc. Reducing the number of components in turn reduces the number of HTTP requests required to render the page. This is the key to faster pages.”
  • 11.
    2.2. Back-end OptimizeTheory  Reduce disk In/Out  Reduce complex/fuzzy  Reduce budget
  • 12.
    2.3. Debugging &Profiling  Nusphere Phped Debugger  Xdebug  top command  Apache bench
  • 13.
    2.3.1. Nusphere PhpedDebugger  Installation:  Step 1: Install php extension  Step 2: Install DBGbar addon for Firefox or DBG debugger on Chrome  Step 3: Update PHP.INI config  Restart your web server (if needed)
  • 14.
  • 15.
    2.3.2. Xdebug  Installation:  Step 1: Install php extension  Step 2: Update PHP.INI config  Restart your server (if needed)
  • 16.
    2.3.2. Xdebug PHP.INI [xdebug] zend_extension_ts = C:..php_xdebug-2.1.0-5.2-vc6.dll xdebug.profiler_output_dir = "C:xdebug" xdebug.profiler_output_name = "callgrind.%R.%t" xdebug.profiler_enable = 0 xdebug.profiler_append=0 xdebug.profiler_enable_trigger = 1 xdebug.extended_info=1 Usage: http://example.com/index.php?XDEBUG_PROFILE
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
    3 De-normalization
  • 22.
    3.1. What isDe-normalization?  Most of use live with Relational DB.  What is normalization?  Database normalization is the process of organizing the fields and tables of a relational database to minimize redundancy and dependency .(Wiki)  What is De-normalization?  Denormalization is the process of attempting to optimise the read performance of a database by adding redundant data or by grouping data. (Wiki)
  • 23.
    3.2. How tode-normalize?  Vertical Split.  Pre-calculated value.  Duplicate data.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
    3.3. Data correctness  Normalization = Correctness  Denormalization = Performance
  • 29.
    4 Asynchronous processing
  • 30.
  • 31.
    4.2. Job queue:: Gearman  http://gearman.org/
  • 32.
  • 33.
    4.3. Async Request functionbackgroundHttpPost($url, $prm = ''){ $p=parse_url($url); $fp = fsockopen($p['host'], isset($p['port'])?$p['port']:80, $en, $es, 30); if (!$fp) return false; else { $out = "POST ".$p['path']."?".$p['query']." HTTP/1.1rn"; $out.= "Host: ".$p['host']."rn"; $out.= "Content-Type: application/x-www-form-urlencodedrn"; $out.= "Content-Length: ".strlen($prm)."rn"; $out.= "Connection: Closernrn"; if ($prm != '') $out.= $prm; fwrite($fp, $out); fclose($fp); return true; } } Usage: $taskUrl = 'http://picto.vn/task/deletephoto'; backgroundHttpPost($taskUrl, 'photoid=' . $myPhoto->id);
  • 35.
    5 Caching
  • 36.
    5.1. What iscaching?
  • 37.
    5.2. What isAPC?  Alternative PHP Cache – APC  Free PHP Extension  PHP Opcode cache  Support User data cache  Run on SAME machine with PHP  NOT support multi servers
  • 39.
  • 42.
  • 44.
    5.5. APC Tip  Opcode your web code only  Disable APC for PhpMyAdmin <Directory "/mysite/public_html/"> ... php_admin_flag apc.cache_by_default On </Directory <Directory "/mysite/public_html/phpmyadmin/"> ... php_admin_flag apc.cache_by_default Off </Directory>
  • 45.
    2.6. APC Tip..  Cache STRING is better than ARRAY  Long Time To Live better than Short  Case study:  Caching for Feed System:
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
    Short TTL makefragmentation
  • 51.
    Short TTL makefragmentation
  • 52.
    6 Cloud computing - AWS  Amazon Elastic Compute Cloud – Amazon EC2 + EBS  Amazon Simple Storage Service – Amazon S3  Amazon Simple Email Service – Amazon SES
  • 53.
  • 54.
    6.1. Amazon EC2(cont.) 5
  • 55.
  • 56.
    6.2. Amazon S3(cont.) 5
  • 57.
    6.2. Amazon S3(cont.) 5
  • 58.
    6.3. Amazon SES  1,000,000 emails = $100 5  Support SMTP
  • 59.
    6.3. Amazon SES(cont.) 5
  • 60.
    6.4. Adapt toAWS for scalability  Local server (Web & RDBMS) + S3 5  EC2 (Web) + EBS (RDB) + S3  EC2 (Web) + EBS (local storage) + Amazon RDS (RDB) + S3  EC2 (Web) + EBS (local storage) + EC2 (noSQL) + S3  …Can use with more Amazon Services
  • 61.
    Thank you! It’s timeto query … CONTACT Fullname: Vo Duy Tuan Email: tuanmaster2002@yahoo.com Phone: 0938 916 902 Website: http://bloghoctap.com

Editor's Notes

  • #51 Determine the existing size of your shared memory segment: sysctl -a | grep shmmax Set sysctl -w kernel.shmmax=50331648
  • #52 Determine the existing size of your shared memory segment: sysctl -a | grep shmmax Set sysctl -w kernel.shmmax=50331648
  • #54 More installation LAMP: http://jonathanhui.com/building-ec2-amazon-linux-lamp
  • #59 Mailchimp: http://mailchimp.com/pricing/