php performance tuning for drupal 8
TRANSCRIPT
©2016 Acquia Inc. — Confidential and Proprietary
Erin Rasmussen, Support Engineer,
Acquia
PHP Performance
Tuning
for Drupal 8
©2016 Acquia Inc. — Confidential and Proprietary
Make that Drupal 8 site fly!!
– Brief glance over Best Practices for Performance
Tuning.
– PHP tuning including Opcache, Interned strings and
apcu
– Varnish, Memcache, and Database tips
– The Software Stack, with Drupal 8 integration tips.
©2016 Acquia Inc. — Confidential and Proprietary
Mitigate against Failure by planning for Success!!
– Test, test and then Test again! (Include Load & Security
Testing!)
– Performance Review is Key: Understand the factors
that Affect your Site’s Performance
– Incorporate these strategies in your Continuous
Improvement Process
We want your project to succeed as much as you do!
©2016 Acquia Inc. — Confidential and Proprietary
This is an example stack, there
are multiple ways configure
them, your software may be
different.
Note: Development environments are
almost always Different than
Production environments.
Know the Stack, Love the Stack
Balancer / Varnish
Memcached
Apache PHP/FPM
MySQL
File System
Code Repo
©2016 Acquia Inc. — Confidential and Proprietary
Multi-Tier, the Stack gets bigger
With multi-tier, these the
software stack can be split
among server instances.
With redundancy and
failover.
Balancer / Varnish
Balancer / Varnish
Memcached
Apache PHP/FPM
Apache PHP/FPM
MySQL & File System
MySQL & File System
Code Repo
Server AMI Type #Cores Date Time 1m 5m 15m
bal-1*5*8 m3.2xlarge 8 2017-02-20 00:08:21 0.00 0.03 0.08
bal-1*5*9 m3.2xlarge 8 2017-02-20 00:08:22 0.33 0.29 0.25
web-18**7 c3.large 2 2017-02-20 00:08:21 0.02 0.03 0.05
web-18**8 c3.large 2 2017-02-20 00:08:21 0.00 0.01 0.05
fsdb-1**25 c3.large 2 2017-02-20 00:08:22 0.07 0.05 0.05
fsdb-1**26 c3.large 2 2017-02-20 00:08:23 0.03 0.04 0.05
©2016 Acquia Inc. — Confidential and Proprietary
Balancers / Caching a short intro
Drupal 7, has URL-based page caching
and empties the page cache for every edited piece of content!
2009-2011 Varnish wasn’t widely used, so Drupal 7 caches
pages, but doesn’t invalidate them intelligently.
Cache-Control = public, max-age=300
Works great – until there’s a DDOS or back-end server issue
Cache-Control = public, max-age=31536000
Works great – until content updates aren’t reflected on the site.
©2016 Acquia Inc. — Confidential and Proprietary
Drupal 8 Caching is re-architected
• Has a proper internal cache API, allows tagging of cache
items.
• Each single item is described with tags.
Tags "bubble" up to all rendering parents.
These tags are stored in the cache API.
Cache::invalidateTags(["tag"])
CacheTagsListenersInterface
The content API's call ::invalidateTags().
The render and page caches are transparently invalidated.
©2016 Acquia Inc. — Confidential and Proprietary
Varnish https://varnish-cache.org
Support for Drupal 8 Cache Tags with Varnish
Acquia Purge for Drupal 8 is a fully-featured API
and you plug in a 'purger' to it, to support your CDN
©2016 Acquia Inc. — Confidential and Proprietary
PHP / FPM http://php.net https://php-fpm.org
– Memory Limits
– Max Execution Time
– Zend OPcache
– APC user cache
– Memory Allocation Math
©2016 Acquia Inc. — Confidential and Proprietary
PHP Memory Limit http://php.net/manual/en/ini.core.php#ini.memory-
limit
The PHP memory_limit is the maximum number of M (in
integers) that a single PHP/FPM proc can consume, by
default memory_limit =128M
Clear signs this is set too low:
– php-errors.log: [20-Feb-2017 19:26:10 America/New_York] PHP Fatal error: Allowed memory
size of 268435456 bytes exhausted (tried to allocate 15990062 bytes) in
docroot/includes/database/database.inc on line 2227
– PHP WSOD errors or “Temporarily Unavailable” errors.
©2016 Acquia Inc. — Confidential and Proprietary
Max Execution Time
http://php.net/manual/en/features.connection-handling.php
The amount of time a single PHP process can run (in
seconds) max_execution_time
– Resolve this problems by identifying slow processes
and resolving the request or set_time_limit() function.
Clear signs that your processes are taking too long
–fpm-error.log max_children errors
–White Screen of Death (Temporary Unavailable) errors.
©2016 Acquia Inc. — Confidential and Proprietary
OPcache (opcode cache) http://php.net/manual/en/intro.opcache.php
Caches pre-compiled bytecode. Faster than using PHP of
load & parse each script for each & every HTTP request.
key variable: opcache.memory_consumption(128M)
Needs to be large enough to store compiled code for your
applications PHP scripts Clear sign that this is too small:
– php-errors.log Warning: require_once() [function.require-once]: Unable to allocate memory for
pool.
OPcache Status monitor: https://github.com/rlerdorf/opcache-status
©2016 Acquia Inc. — Confidential and Proprietary
OPcache http://php.net/manual/en/book.opcache.php
Interned Strings - the amount of memory (M) used to store
identical strings (detected by PHP). By default this is a shared
buffer allowing all Drupal PHP processes to reference it across multiple
PHP/FPM procs - saving memory.
Clear sign that this is set too low:
– Fpm-error.log: Warning Interned string buffer overflow
More indepth information: http://jpauli.github.io/2015/03/05/opcache.html
©2016 Acquia Inc. — Confidential and Proprietary
APC User Cache http://php.net/manual/en/intro.apcu.php
APC User cache is PHP APC minus OPcache, rarely
used in Drupal 7 (apc.shm_size= 8M is ok), increase for
Drupal 8 (apc.shm_size=16 or 32M)
Clear sign that the APC User cache is set too small:– In php-errors.log : [20-Feb-2017 13:54:16 America/New_York] PHP Warning: apcu_store(): Unable to allocate
memory for pool. in /mnt/www/html/somenamedev/docroot/core/lib/Drupal/Core/Cache/ApcuBackend.php on line
177
Get usage Details!: use apc.php script in the docroot and
view in your browser: note: the charts are less accurate than the text
©2016 Acquia Inc. — Confidential and Proprietary
APC User Cache
Drupal 8.3 will reduce the amount stored in APC user
cache in response to community issues:
– Rationalize use of the 'discovery' cache bin, since it's stored in the limited
size APCu by default https://www.drupal.org/node/2765271
– Change ViewsData to use the default cache bin instead of discovery
https://www.drupal.org/node/2824547
©2016 Acquia Inc. — Confidential and Proprietary
PHP Procs and PHP Memory Math
Total RAM 3860
OS 50
MySQL 1930
Memcache 64
Sites 4
Sitename Memory Limit Opcache APCu FPM
d7sitedev 256 128 8 1
d7sitestg 256 128 8 1
d8sitedev 128 96 16 1
d8sitestg 128 96 16 1
Calculate the available
memory for PHP (Total - OS - MySQL -Memcache )
Memory use by 1 Process:
– Use top
– memory_get_peak_usage()
– Devel (Drupal 7 & 8)
Calculate how many procs
you can afford.
©2016 Acquia Inc. — Confidential and Proprietary
PHP/FPM max_children errors
When there are more incoming requests than PHP/FPM
can handle you get max_children errors.Clear sign that the site is exhausting PHP/FPM procs:
– fpm-error.log srv-**35m notice [22-Feb-2017 16:32:38] WARNING: [pool someuser]
server reached max_children setting (27), consider raising it.
Causes are Varied – Large amount of HTTP traffic,
PHP/FPM procs that are looping, or not terminating effectively. Or calls to External Services
hanging/timing out
©2016 Acquia Inc. — Confidential and Proprietary
Adding the Impact of Traffic
What happens when the new site launches – or it gets
retweeted a million times:
©2016 Acquia Inc. — Confidential and Proprietary
What’s in that Traffic?
©2016 Acquia Inc. — Confidential and Proprietary
Memcached https://memcached.org
Is a open source, high-performance, distributed memory object caching system
Drupal 8 Memcache Module is Alpha
https://www.drupal.org/project/memcache
The Drupal 8 module will make GETS and SETS with
properly configured Memcached, and supports
key_prefixing.
However, Memcached slabs are not encrypted by default
Community Development is needed for a Stable Release
of the Memcahed Drupal modue
©2016 Acquia Inc. — Confidential and Proprietary
Keep the Database Layer Happy
– Enable Syslog, disable database Watchdog Logging.
– Caching to the Database does not always Scale, keep
an eye on your capacity and I/O errors.
– Optimize queries, and keep an eye on database tables.
Even the most efficient Views query will be slow on a 50 Gb table.
©2016 Acquia Inc. — Confidential and Proprietary
Keep the Front End Performant
– Compress Site Images, PDFs and other Files before
uploading them to the site. (Even a cached 7MB photo is a major
performance hog when used 27 times on a site.)
– Compress your CSS, and minify your JS.
– Keep an eye on External calls. Use timeouts, and allow your
application to fail gracefully when those resources cannot be reached.
©2016 Acquia Inc. — Confidential and Proprietary
Thank you! Drupal
✩ Modern PHP: New Features and Good Practices by
Josh Lockhart http://shop.oreilly.com/product/0636920033868.do
✩ Drupal 8 Cache API https://www.drupal.org/docs/8/api/cache-api/cache-
api
✩ Acquia Purge https://www.drupal.org/project/acquia_purge
✩ Drupal’s PHP requirements https://www.drupal.org/docs/7/system-
requirements/php