Drupal performance Common advices Drupal performance / Common Use internal cache • Use PHP 8 or newer version ASAP • Avoid to install or use lot of modules (impact on discovery, code parsing …), be defensive • Use code linter to fix FQFN (function opcode inlining - Ex: \strlen()) • Use the DIC like a Symfonist (tag, compiler pass and other fun things), the ServicesProviderBase is made for this • Avoid to load unnecessary entities • Avoid to use views for any needs (SQL RAW Query + cache are often better) • Disable Watchdog and prefer the usage of Monolog • Log Slow SQL query + EXPLAIN • … • Cache ALL things as soon as possible! (render, block, views, entities …) • Configure your cache backend (next Chapter)
Drupal hosting infrastructure Drupal performance / Common https://aws.amazon.com/fr/efs/resources/aws-refarch-drupal/ Important things to know Drupal needs : • Webserver (Apache or Nginx) • PHP (PHP-FPM) • Database (MySQL or PostgreSQL) • NFS (mount point or S3) But in fact, for production use: • Cache reverse proxy (Nginx / Varnish) • CDN (Cloudfront / Cloudflare / Fastly / Akamaï …) • Memory cache database (Memcached or Redis)
are the cache backend? The cache storage Cache storage is separated into "bins", each containing various cache items. Each bin can be configured separately. A cache backend is like a « handler » to interact with bins. A cache backend class is an implementation of the CacheBackendInterface. Bins are collected by the Compiler Pass ListCacheBinsPass. Drupal cache backend / Cache backend definition Main bins are the followed: • Bootstrap : request needs • Render: avoid to recompute the rendering pipeline • Discovery: tells to Drupal to find things • Default: for the common
cache backend are available The database backend The default cache backend. Everything is stored in the database. « … as the number of cache and session objects grows, frequent actions such as cache invalidation put a high demand on the database layer. This can cause increased network traffic and ultimately increase the load on the database servers.» Unlike a Symfony app, the database is the heart of a Drupal application. You need to preserve it to keep good performance. Drupal cache backend / Cache backend available The null cache backend The memory cache backend The PHP cache backend The fastest cache backend. Thank’s to the PHP Opcode ;) Stores cache items in a PHP file using a storage that implements Drupal\Component\PhpStorage\PhpStorageInterface
cache backend are available Drupal cache backend / Cache backend available The fast chained backend ITEM FOUND GET ITEMS ITEMS NOT FOUND ITEMS ARE RETURNED FETCH OTHER BACKEND APCU STORE IN APCU The Fast Chained backend flow With an APC and backend chain, the cache will check APC first and return immediately if an item is found. If not, it will check the configured backend and then write back to APC if the item is found there; and on cache misses, it will write to both.
further with IgBinary Drupal cache backend / IgBinary What’s IgBinary ? Igbinary is a drop in replacement for the standard php serializer. Igbinary stores php data structures in a compact binary form. Memory savings are significant when using memcached, APCu. The typical reduction in storage requirements are around 50%. https://github.com/igbinary/igbinary Easy Install with Docker Support PHP v7.4 & v8.0 https://pecl.php.net/package/igbinary
is Opcache? Opcache / Overview How to monitor Opcache? From PHP function : opcache-get-status https://www.php.net/manual/en/function.opcache-get-status.php From this phar : https://github.com/gordalina/cachetool OPcache an extension (include from PHP v5.5) which improves PHP performance by storing precompiled script opcode in shared memory, thereby removing the need for PHP to load and parse scripts on each request. Definition https://wp-rocket.me/blog/best-php-accelerators/
config adjustment The Opcache config - Enable Opcache - Increase memory to used - Increase the amount of files that can be cached - Don't Check PHP Files Timestamps - Don’t Check the file size Opcache / Config opcache.file_update_protection = 0 (default "2") Prevents caching files that are less than this number of seconds old. It protects from caching of incompletely updated files. In case all file updates on your site are atomic, you may increase performance setting it to "0".
Settings / Autoloader PHP / Overview You can use IgBinary with APCu : apc.serializer=igbinary Dump the composer autoloader like this : composer dump-autoload --no-dev --classmap-authoritative You can set the composer autoloader into APCU… Ex: "apcu-autoloader": true Tips « So, whenever you'll use a path to access a file (in the Unix meaning), you or your library or at least your Kernel will have to resolve it. Calling stat() is heavy, first because this is a system call, needing a Kernel trap and a context switch, and also because it most likely asks the disk about metadata. » Julien Pauly - http://blog.jpauli.tech/2014-06-30-realpath-cache-html Increase realpath cache settings https://getcomposer.org/doc/articles/autoloader-optimization.md
PHP / Overview Update your PHP-FPM pool configuration PHP-FPM can use one of three process management types: Static, dynamic & ondemand • Ondemand has PHP-FPM fork processes when requests are received. • Dynamic manages the number of available child processes and ensures that at least one child process is always available • Static ensures a fixed number of child processes are always available to handle user requests Always use an Unix Socket instead of TCP Socket => better performance Low Traffic site => use onDemand worker High Traffic site => use static worker Useful links : https://tideways.com/profiler/blog/an-introduction-to-php-fpm-tuning https://docs.platform.sh/languages/php/fpm.html Use hey (https://github.com/rakyll/hey) or K6 (https://k6.io/) to emulate your traffic Þ Stress test Þ Load testing Enable PHP-FPM status and track children
PHP / PHP-FPM PHP-FPM load balancing You can create as many PHP-FPM pool as you want (backend / frontend / api / whatever … => use Nginx to proxy pass to one or other (by path by exemple) => each pool can have his own configuration => better isolation
Fastgi cache Nginx / FastCGI cache Only the first request can be send to to upstream, others wait the response => fastcgi_cache_lock on; If your upstream is suffuring, use stale cached instead of ! => proxy_cache_use_stale error timeout http_500; Avoid to call PHP for nothing http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html Proxy cache http://nginx.org/en/docs/http/ngx_http_proxy_module.html
is code profiling? Drupal cache backend / Cache backend available Profiling Profiling is collecting deep performance metrics while the code is running, and have full details and context on the code’s behavior to find the root cause of performance bottlenecks. WALL TIME CPU TIME MEMORY HTTP CALLS SQL QUERIES Þ Detect code bottleneck Þ Analyse the critical path Þ Analyse Network (TCP calls) Þ Global, analyse how your code is running Support PHP v7+ & v8+ https://pecl.php.net/package/xhprof https://github.com/longxinH/xhprof
your perf with Blackfire assertion Drupal cache backend / Cache backend available Measure performance during build and run phases Over time, performance of most web applications inexorably decline: the size of the response, the time it takes to render the response content, the number of assets, and more. Performance need to be monitor Don’t hesitate to block the CI if performance isn’t suitable. You can ensure your frontend perf with Dareboost / CircleCI …
last advices Searching for performance resolution can be expensive in term of cost & time Drupal performance / Common Search for performance is not for all projects typologies For me, no needs to focus on micro-optimisations It’s collaborative work (PHP team / DevOps team / System Team / Frontend team) Do not focus only on PHP, but on its ecosystem Be not only a site builder, be a “deep dive” PHP developer Sorry, but Drupal is not always the good solution
appreciate your feedback! Please take a moment to fill out: the general conference survey Flash the QR code OR It will be sent by email the Individual session surveys (located under each session description) 1 2