Date post: | 08-May-2015 |
Category: |
Technology |
Upload: | eric-ritchie |
View: | 884 times |
Download: | 0 times |
© All rights reserved. Zend Technologies, Inc.
Turbocharging PHP
Applications With Zend
Server… The Tutorial
Eric Ritchie ([email protected])
Jan Burkl ([email protected])
Slavey Karadzhov ([email protected])
© All rights reserved. Zend Technologies, Inc.
Eric Ritchie bids you welcome!
• Senior Technical Consultant and Trainer at Zend Technologies
• Zend Framework and PHP 5.3 ZCE
• Eighteen years of system administration experience
• Twelve years of PHP (3,4 & 5) and five years Zend Framework development experience
• Hobbies: Sampling good wines/whiskies (gifts welcome)
© All rights reserved. Zend Technologies, Inc.
Slavey Karadzhov welcomes you!
• Senior Technical Consultant and Trainer at Zend Technologies
• Zend Framework and PHP 5.3 ZCE
• Many years of PHP (3,4 & 5), ZF 1 and galaxy of
programming languages.
• Hobbies: Travelling, sampling exotic dishes, outdoor
sports, hype technologies. (in no particular order)
© All rights reserved. Zend Technologies, Inc.
Jan Burkl
• Senior Solution Consultant at Zend Technologies
• Working with the dark side of the company
• Zend Framework and PHP 5.3 ZCE
• Moved 10 years ago from Java to PHP.
• Hobbies: Soccer, Automation of Life.
© All rights reserved. Zend Technologies, Inc.
Agenda
• Defining the problem
• The sharpest tool in the shed… Code Tracing
• Laziness is good (at least for web servers):
Using caching to avoid work
• If we must work, then procrastinate: Use the Job Queue
• Taking the heat off the disk: Ways to reduce disk I/O
• The ghost in the machine:
Don‘t forget about network performance
• Don‘t talk too much: Less is more when sending data
© All rights reserved. Zend Technologies, Inc.
What we will not cover (probably)
• Database optimisation > caching
• Webservice optimisation
• Varnish and reverse proxying in general
• CDNs (Content Delivery Networks)
• Caching technology comparisons
• Operating system level optimisations
• Javascript performance in general (well, maybe…)
• Network Traffic Analysis
© All rights reserved. Zend Technologies, Inc.
Defining the problem Why is it that we always discover that we have performance
problems AFTER we go live?
© All rights reserved. Zend Technologies, Inc.
The evolution of an organic website
• A new website is born...
Internet
LAMP Server
© All rights reserved. Zend Technologies, Inc.
...too much of a good thing?
• the smoke begins...
Internet
LAMP Server
© All rights reserved. Zend Technologies, Inc.
First job: Bottleneck identification
• Many possible tools:
Zend Server Event Monitoring
Profiling
microtime()
Slow query logs
• One Swiss army knife:
Zend Server Code Tracing
© All rights reserved. Zend Technologies, Inc.
Zend Code Tracing A black box for your PHP code
© All rights reserved. Zend Technologies, Inc.
Zend Code Tracing: Main view
© All rights reserved. Zend Technologies, Inc.
Zend Code Tracing: Statistics view
© All rights reserved. Zend Technologies, Inc.
Zend Code Tracing: How to store a trace?
• Best way (in most cases)
… Use a web browser
Very quick and easy, but obviously not so good for POST requests
Also, we may not be allowed!
© All rights reserved. Zend Technologies, Inc.
Zend Code Tracing: How to store a trace?
• The official way
… Use event monitoring
Works for all requests where an event is generated
Great for catching random problems
Custom events allow for „programmatic“ generation of traces
© All rights reserved. Zend Technologies, Inc.
Zend Code Tracing: What can go wrong.
• Sometimes you fail to store a code trace, because:
The module is disabled
Code tracing is running in standby mode
Lack of permission when using „dump_data“
The triggered event was aggregated
The code trace was never collected
© All rights reserved. Zend Technologies, Inc.
Caching Reducing the work load of our application architecture
© All rights reserved. Zend Technologies, Inc.
Zend Page Cache
• Dynamic content is expensive, so don’t regenerate
Low impact, since no code changes may be required
Complete request cached, so a hit is like a static request
Multiple copies of pages can be maintained
Controlled by flexable and comprehensive rules
• Sadly, nothing comes for free
Page design can render page caching useless
Highly dynamic content cannot be reasonably cached
Problems with stale content
© All rights reserved. Zend Technologies, Inc.
Zend Page Caching: Custom content
© All rights reserved. Zend Technologies, Inc.
Zend Page Cache: Configuration
© All rights reserved. Zend Technologies, Inc.
Excursion
© All rights reserved. Zend Technologies, Inc.
Javascript - AJAX
• Reload stuff on demand
User name, shopping cart, navigation...
• It‘s not that hard
If the application is structured well...
© All rights reserved. Zend Technologies, Inc.
© All rights reserved. Zend Technologies, Inc.
Full Blown
• Don‘t ajaxify everything!
Ever profiled 15 MVC runs in 1 request?
• MVC really needed for displaying User Name?
Be pragmatic
Lightweight gateway
Reduce Bootstrapping time
• Load ressources which are really needed
Remember that you‘re using PHP!
$application->bootstrap(array('session')) ->run();
© All rights reserved. Zend Technologies, Inc.
JS Architecture
• Dojo, ExtJS, jQuery, ...
Is a library enough?
• Javascript MVC, Angular.js, EmberJS, ...
TodoMVC: http://addyosmani.github.com/todomvc/
Comparison of JS-Framework
• File loading strategy
Reduce amount of requests – CDN?
RequireJS
YUI compress
Rhino
© All rights reserved. Zend Technologies, Inc.
Example – Dojo - build.js
./dojo-1.7.3/util/buildscripts/build.sh \ profile=./profiles/wikizf.profile.js \ action=release \ releaseDir=/var/www/wiki-zf-good.session.dev/public \ cssOptimize=comments \ layerOptimize=closure \ mini=true \ optimize=closure \ stripConsole=all \ selectorEngine=acme
© All rights reserved. Zend Technologies, Inc.
Example – Dojo - xyz.profile.js
dependencies = { layers: [{ name: "dojo.js", dependencies: [ "dojox.data.QueryReadStore", "dijit.form.ComboBox", "dijit.form.Form", "dojo.parser", ] }], prefixes: [ ["dojox", "../dojox" ], ["dijit", "../dijit"] ] };
© All rights reserved. Zend Technologies, Inc.
Automation
• Integration
• Build process
• Continuous Integration
Jenkins
© All rights reserved. Zend Technologies, Inc.
© All rights reserved. Zend Technologies, Inc.
© All rights reserved. Zend Technologies, Inc.
© All rights reserved. Zend Technologies, Inc.
Data caching
• Protects the data layer:
Helps prevent repetitive DB/web service calls
Far easier than scaling the database
• But...
Requires code changes
Custom data is also problematic
Risk of delivering stale content
De-caching can bring down the data source
© All rights reserved. Zend Technologies, Inc.
One common architecture
• Network data cache
e.g. Memcache
Internet
LB
Cache
Server
Web Farm
DB Server(s)
© All rights reserved. Zend Technologies, Inc.
Network data cache
• Some advantages
Only one cache to update/invalidate
Most effective way of protecting the data source
• Many disadvantages...
Single point of failure (or uncertainty when in distributed mode)
Limits scalability
Performance bottleneck
Slower
© All rights reserved. Zend Technologies, Inc.
A different approach
• Shared memory cache
e.g. Zend Data Cache
Internet
LB
Cache
Server Web Farm
DB Server(s)
X Cache
Cache
Cache
© All rights reserved. Zend Technologies, Inc.
Zend Data Cache: A shared memory cache
• Many advantages
Completely scalable (copy/paste architecture)
No single point of failure
Does not require TCP/IP
Very fast
• Some disadvantages...
Not the path of least resistance
More work for the data source or the developer
Your colleagues may laugh at you (but they would be wrong)
© All rights reserved. Zend Technologies, Inc.
Zend Data Cache: Usage
• Inserting into the cache
$res = zend_shm_cache_store($key, $value, $ttl)
$res = zend_disk_cache_store($key, $value, $ttl)
• Reading from the cache
$value = zend_shm_cache_fetch($key)
$value = zend_disk_cache_fetch($key)
• In all cases the key can contain a namespace to allow
grouping of data, e.g. namespace::key
© All rights reserved. Zend Technologies, Inc.
Zend Data Cache: Usage
• Deleteing from the cache
$res = zend_shm_cache_delete($key)
$res = zend_disk_cache_delete($key)
• Wiping the cache
$res = zend_shm_cache_clear($namespace)
$res = zend_disk_cache_clear($namespace)
© All rights reserved. Zend Technologies, Inc.
Shared memory caching
• Taming the disadvantages
True, we need to work a little, but...
• We can use Zend Server‘s Job Queue to perform remote
cache maintenance
• We can get a list of active servers from the Web API
provided by Zend Server
• Really, we don‘t have to code much ourselves
© All rights reserved. Zend Technologies, Inc.
Remote de-caching
• With Zend Server‘s Job Queue
• ...and the job itself
© All rights reserved. Zend Technologies, Inc.
Shared memory cache on steroids
• Don‘t decache, update
Regenerate the data and insert into the cache
At a minimum do this for all components of the index page
Reduces data source load
© All rights reserved. Zend Technologies, Inc.
Zend Job Queue Why do now what you can do a little later?
Or, genius is being calm on the surface while being calculating in
the background. Your application should do just that.
Or, your server‘s marketing department.
© All rights reserved. Zend Technologies, Inc.
Zend Job Queue
• Doesn’t make PHP faster, but makes it look that way
Break slow tasks out of the user workflow
Offload the heavy lifting to a background server.
Delay expensive tasks to off peak hours
Helps to prevent repetitve work reducing overall load
• Possible to create jobs which depend on other jobs, run
regularly and have set priorites
• Hooks into Zend Server‘s Event Monitoring component
Jobs can feedback status information
Problems appear in the central Zend Server event list
© All rights reserved. Zend Technologies, Inc.
Zend Job Queue: Typical example
© All rights reserved. Zend Technologies, Inc.
Zend Job Queue: Basic use
• Would be hard to make it easier…
• We can also pass information to our job...
© All rights reserved. Zend Technologies, Inc.
Zend Job Queue: Basic use
• Possible to name our jobs and set an earliest start time…
• Once sheduled, it is possible to check up on jobs
© All rights reserved. Zend Technologies, Inc.
Zend Job Queue: Querying jobs
• Finding out the status of our job using the job id
Find out if the job is still queued, running, completed or failed
When finished we get a copy of the script‘s output
© All rights reserved. Zend Technologies, Inc.
Zend Job Queue: Querying jobs
• Searching for jobs
Search for an existing queued job of the same type
Useful for avoiding repetitive work
Returns the number of matching jobs along with job details
© All rights reserved. Zend Technologies, Inc.
Optimization vs. Complexity
© All rights reserved. Zend Technologies, Inc.
Optimization vs. Complexity
• With every optimization level complexity gets higher
Who else drapped into the Caching pitfall?
• First: implement functionality
• System performing?
• If not, check why
• Get rid of bottlenecks
© All rights reserved. Zend Technologies, Inc.
Reducing Disk I/O The only component of a web server slower than the disk
subsystem is the guy you ask to set it up.
© All rights reserved. Zend Technologies, Inc.
Reducing disk I/O
• Use an opcode cache e.g. Zend Optimizer+
Avoids the need to open PHP files and compile the contents for
every request.
Compiled „opcodes“ are instead held in shared memory for later
reuse.
Particularly important for framework based projects where tens
of files may be needed to answer one request.
Also saves some compiler time, but disk I/O savings are usually far
more significant.
Most obvious benefit for scripts with short run times or running on
loaded servers.
© All rights reserved. Zend Technologies, Inc.
Reducing disk I/O: Zend Optimizer+
• Performs opcode caching
• Also adds multiple optimisation passes
• Allows for the blacklisting of files/directories
• Simple configuration:
© All rights reserved. Zend Technologies, Inc.
Check what‘s cached
• Zend Server‘s API
accelerator_get_status()
accelerator_get_configuration()
accelerator_reset()
© All rights reserved. Zend Technologies, Inc.
Reducing disk I/O
• Store static files elsewhere
• Do not cache to the file system
• Must have local storage? ...Use a ramdisk
• Reduce PHP/Apache logging to minimal levels
E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
• Don‘t forget the Zend Server logs!
Search for log_verbosity_level in the configuration
© All rights reserved. Zend Technologies, Inc.
File system concerns
• EXT3 – The default problem
For a web server ReiserFS can be 20x (or more) faster, really!
Even XFS is 2.5x faster
If you must use EXT3/4 then throw memory at the server
• My NFS/Samba server is fast... Honest!
Even „fast“ NFS servers have terrible performance under load
Older implementations cannot use buffer cache
Poor scalability
• Distributed file systems
Extremely poor scalability
© All rights reserved. Zend Technologies, Inc.
PHP uses your network
• PHP communicates with network services like
Databases (ex: MySQL, Oracle)
Caching systems (ex: Memcache, Redis ..)
Job Queue Systems (ex: Zend Job Queue, RabbitMQ )
Session Clustering Daemon (ex: Zend Session Clustering)
• If one of these services overloads the network then all
other suffer from slowdown
Network congestion
Insufficient bandwidth
High latency
© All rights reserved. Zend Technologies, Inc.
Overloaded network, slow application
• The caching systems have to be very fast
If they are slow then the complete application can be slow
• The session clustering has to be fast
Lots of PHP requests wait to get a read/write lock -> the
application looks slow
© All rights reserved. Zend Technologies, Inc.
Know your traffic
• A bit of Linux/Unix networking knowledge is required
• Count the incoming and outgoing traffic
iptables
• Inspect the network streams
Tcpdump
Wireshark
© All rights reserved. Zend Technologies, Inc.
Traffic counters
• Network traffic can be
On different interfaces:
• eth0, lo
Incoming/Outgoing
On different ports
Uses different transport protocols
© All rights reserved. Zend Technologies, Inc.
Traffic counters (2)
• Iptables can help you
Zeroing the counters
• iptables -t mangle -L -Z INPUT -v -n
• iptables -t mangle -L -Z OUTPUT -v -n
Start measuring
• Ex: Memcache outgoing
– iptables -t mangle -I OUTPUT -p tcp --dport 11211
• Ex: Memcache incoming via udp
– iptables -t mangle -I INPUT -p udp --sport 11211
© All rights reserved. Zend Technologies, Inc.
Network stream inspectors
• Tcpdump
Helps you to record the traffic based on some rules
• tcpdump -vv -x -X -s 1500 -i eth0 'port 11211‘
© All rights reserved. Zend Technologies, Inc.
Network stream inspectors (2)
• Wireshark
Helps you understand better the data
© All rights reserved. Zend Technologies, Inc.
Hands on session...
Over to you ;)
© All rights reserved. Zend Technologies, Inc.
So long...
• …and thanks for all the fish.
© All rights reserved. Zend Technologies, Inc.
Turbocharging PHP
Applications With Zend
Server… The Tutorial
Eric Ritchie ([email protected])
Jan Burkl ([email protected])
Slavey Karadzhov ([email protected])