Contenu connexe Similaire à Turbocharging php applications with zend server (20) Turbocharging php applications with zend server2. 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.
3. 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
© All rights reserved. Zend Technologies, Inc.
4. What we will not cover (probably)
• Database optimization > caching
• Web service optimization
• Varnish and reverse proxying in general
• CDNs (Content Delivery Networks)
• Caching technology comparisons
• Operating system level optimizations
• JavaScript performance in general
• Network Traffic Analysis
© All rights reserved. Zend Technologies, Inc.
5. 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.
6. The evolution of an organic website
• A new website is born...
Internet
LAMP Server
© All rights reserved. Zend Technologies, Inc.
7. ...too much of a good thing?
• the smoke begins...
Internet
LAMP Server
© All rights reserved. Zend Technologies, Inc.
8. 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.
9. Zend Code Tracing
A black box for your PHP code
© All rights reserved. Zend Technologies, Inc.
12. 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.
13. 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.
15. 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.
17. Zend Page Cache: Configuration
© All rights reserved. Zend Technologies, Inc.
18. 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.
19. One common architecture
• Network data cache
e.g. Memcache
Internet DB Server(s)
LB
Cache
Server
Web Farm
© All rights reserved. Zend Technologies, Inc.
20. 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.
21. A different approach
• Shared memory cache Cache
e.g. Zend Data Cache
Cache
Internet DB Server(s)
X
LB Cache
Cache
Server
Web Farm
© All rights reserved. Zend Technologies, Inc.
22. 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.
23. 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.
24. Zend Data Cache: Usage
• Deleting from the cache
$res = zend_shm_cache_delete($key)
$res = zend_disk_cache_delete($key)
• Wiping the cache
$res = zend_shm_cache_clear()
$res = zend_disk_cache_clear()
© All rights reserved. Zend Technologies, Inc.
25. 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.
26. Remote de-caching
• With Zend Server‘s Job Queue
• ...and the job itself
© All rights reserved. Zend Technologies, Inc.
27. 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.
28. 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.
29. 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.
30. Zend Job Queue: Typical example
© All rights reserved. Zend Technologies, Inc.
31. 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.
32. 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.
33. 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.
34. 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.
36. Optimization vs. Complexity
• With every optimization complexity increaces
Who else dropped into the caching pitfall?
• First: implement functionality
• System performing well?
• If not, check why
• Get rid of bottlenecks
© All rights reserved. Zend Technologies, Inc.
37. 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.
38. 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.
39. 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.
40. 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.
41. 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.
42. So long...
• …and thanks for all the fish.
© All rights reserved. Zend Technologies, Inc.