heavy web optimization: backend
DESCRIPTION
Slide in a seminar about backend optimization with PHP. Talks about php, caching, scalability, amazon web serviceTRANSCRIPT
![Page 1: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/1.jpg)
Heavy Web Optimization
BACK-END<?php //from a PHP dev ?>
PHP Developer from Reader.vn, Picto.vn & more…Vo Duy Tuan
![Page 2: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/2.jpg)
$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
![Page 3: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/3.jpg)
$table_of_content
Warm up: Some best practices Back-end Optimization De-normalization Asynchronous processing Caching Scalability
![Page 4: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/4.jpg)
Warm up: Some best practices1
![Page 5: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/5.jpg)
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);
![Page 6: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/6.jpg)
1.2. Pagination
From
To
![Page 7: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/7.jpg)
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 ?
![Page 8: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/8.jpg)
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
![Page 9: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/9.jpg)
Back-end Optimization2
![Page 10: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/10.jpg)
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.”
![Page 11: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/11.jpg)
2.2. Back-end Optimize Theory
Reduce disk In/Out Reduce complex/fuzzy Reduce budget
![Page 12: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/12.jpg)
2.3. Debugging & Profiling
Nusphere Phped Debugger Xdebug top command Apache bench
![Page 13: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/13.jpg)
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)
![Page 14: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/14.jpg)
2.3.1. Nusphere Phped Debugger
![Page 15: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/15.jpg)
2.3.2. Xdebug
Installation:
Step 1: Install php extension
Step 2: Update PHP.INI config
Restart your server (if needed)
![Page 16: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/16.jpg)
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
![Page 17: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/17.jpg)
2.3.2. Xdebug
![Page 18: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/18.jpg)
2.3.3. linux command top
![Page 19: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/19.jpg)
2.3.4. Apache Bench
![Page 20: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/20.jpg)
Somewhere in the dark…
![Page 21: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/21.jpg)
De-normalization3
![Page 22: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/22.jpg)
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)
![Page 23: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/23.jpg)
3.2. How to de-normalize?
Vertical Split. Pre-calculated value. Duplicate data.
![Page 24: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/24.jpg)
3.2.1. Vertical Split
![Page 25: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/25.jpg)
3.2.1. Vertical Split (cont.)
![Page 26: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/26.jpg)
3.2.2. Pre-calculated value
![Page 27: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/27.jpg)
3.2.3. Duplicate data
![Page 28: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/28.jpg)
3.3. Data correctness
Normalization = Correctness Denormalization = Performance
![Page 29: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/29.jpg)
Asynchronous processing4
![Page 30: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/30.jpg)
4.1. Job queue
![Page 31: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/31.jpg)
4.2. Job queue :: Gearman
http://gearman.org/
![Page 32: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/32.jpg)
But….Apache can be a job queue,
![Page 33: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/33.jpg)
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.1\r\n";$out.= "Host: ".$p['host']."\r\n";$out.= "Content-Type: application/x-www-form-urlencoded\r\n";$out.= "Content-Length: ".strlen($prm)."\r\n";$out.= "Connection: Close\r\n\r\n";if ($prm != '') $out.= $prm;fwrite($fp, $out); fclose($fp);return true;
}}
Usage:$taskUrl = 'http://picto.vn/task/deletephoto';backgroundHttpPost($taskUrl, 'photoid=' . $myPhoto->id);
![Page 34: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/34.jpg)
![Page 35: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/35.jpg)
Caching5
![Page 36: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/36.jpg)
5.1. What is caching?
![Page 37: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/37.jpg)
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
![Page 38: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/38.jpg)
![Page 39: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/39.jpg)
5.3. APC Monitor
![Page 40: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/40.jpg)
![Page 41: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/41.jpg)
![Page 42: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/42.jpg)
5.4. APC Using
![Page 43: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/43.jpg)
![Page 44: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/44.jpg)
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>
![Page 45: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/45.jpg)
2.6. APC Tip..
Cache STRING is better than ARRAY Long Time To Live better than Short Case study:
Caching for Feed System:
![Page 46: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/46.jpg)
BEFORE
![Page 47: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/47.jpg)
BEFORE
![Page 48: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/48.jpg)
ANALYSE
![Page 49: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/49.jpg)
AFTER
![Page 50: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/50.jpg)
Short TTL make fragmentation
![Page 51: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/51.jpg)
Short TTL make fragmentation
![Page 52: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/52.jpg)
Amazon Elastic Compute Cloud – Amazon EC2 + EBS Amazon Simple Storage Service – Amazon S3 Amazon Simple Email Service – Amazon SES
Cloud computing - AWS6
![Page 53: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/53.jpg)
6.1. Amazon EC2
5
![Page 54: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/54.jpg)
6.1. Amazon EC2 (cont.)
5
![Page 55: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/55.jpg)
6.2. Amazon S3
5
![Page 56: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/56.jpg)
6.2. Amazon S3 (cont.)
5
![Page 57: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/57.jpg)
6.2. Amazon S3 (cont.)
5
![Page 58: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/58.jpg)
6.3. Amazon SES
5 1,000,000 emails = $100 Support SMTP
![Page 59: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/59.jpg)
6.3. Amazon SES (cont.)
5
![Page 60: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/60.jpg)
6.4. Adapt to AWS for scalability
5 Local server (Web & RDBMS) + S3 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
![Page 61: Heavy Web Optimization: Backend](https://reader034.vdocuments.us/reader034/viewer/2022052315/555a0027d8b42ad00a8b4e4c/html5/thumbnails/61.jpg)
It’s time to query …
CONTACT
Fullname: Vo Duy Tuan
Email: [email protected]
Phone: 0938 916 902
Website: http://bloghoctap.com
Thank you!