DEV Community

Cover image for When a Magento 2 deploy takes the shop down: build steps vs a Laravel deploy
Nikita Sirenko
Nikita Sirenko

Posted on Originally published at linkedin.com

When a Magento 2 deploy takes the shop down: build steps vs a Laravel deploy

The client of a Magento 2 store I support was told the shop had been unavailable for about 10 minutes on 14 September, and asked whether a release had caused it. The nginx logs showed two windows of 503s that evening: 19 minutes from 19:28 and 22 minutes from 20:49. In the worst minute 1,198 of 1,262 requests got a 503. Nothing was broken. Both windows started within a minute of the production repository being advanced. Both releases were mine. The second fixed a bug I'd noticed after the first. The release on 9 September had taken the shop offline for 14 minutes the same way.

As I wrote in my report, the deploy script turns on maintenance mode, then deletes and rebuilds the compiled code and the static files in place. The shop's down for the whole rebuild.

What Magento builds at deploy time

A Laravel deploy runs composer install and writes a few cache files. A Magento deploy also generates code and static files, and three commands do most of it. I timed them in default mode on a Magento 2.4.9 install from GitHub, with no sample data and 3 themes, and on a copy of it without a database.

setup:upgrade applies the declarative schema and the schema and data patches. Without --keep-generated it also empties generated/ and the deployed static files in pub/static. On my install that took 21 to 23 seconds, against about 9 with the flag. Its own help text discourages the flag "except when deploying to production".

setup:di:compile writes the interceptors, proxies and factories: 3,684 files, most of them interceptors and factories, and about 50 MB of compiled DI config. On the copy it took 46 to 59 seconds.

setup:static-content:deploy builds one package per theme and locale, the admin theme included. 3 themes in 1 locale took 29 seconds and wrote 4,545 files. On the copy, 4 locales took 95 seconds and wrote 18,174 files, or 37 seconds with --jobs 4.

bin/magento maintenance:enable
# from here on, every PHP request gets a 503
git pull && composer install --no-dev
# without --keep-generated it also empties generated/ and pub/static
bin/magento setup:upgrade
# interceptors, proxies, factories and the compiled DI config
bin/magento setup:di:compile
# one package per theme and locale
bin/magento setup:static-content:deploy -f
bin/magento cache:clean
bin/magento maintenance:disable
Enter fullscreen mode Exit fullscreen mode

In one in-place run on my install, maintenance mode, setup:upgrade and the static deploy kept the home page at 503 for 64 seconds, without setup:di:compile. How the store's 14 to 22 minutes split between the steps isn't recorded: its deploy script logs no timestamps.

During maintenance the storefront, the admin and the REST API all got the same 808-byte 503 page on my install, with no Retry-After header. An IP allow-list lets you in to check the shop before you turn it off.

A Laravel deploy

The main steps of Deployer's Laravel recipe are composer install in a new release directory, php artisan optimize, migrate --force, the symlink switch and, since Laravel 12.41, php artisan reload, which includes queue:restart. On Laravel 11, 12 and 13, optimize took 0.15 to 0.25 seconds. It writes 3 cache files, for config, events and routes, and compiles the Blade views. One new migration on SQLite took 0.12 seconds.

There's no class generation at deploy time. The container builds objects with reflection when they're needed. The recipe doesn't have a front-end step: a Vite npm run build is added separately.

The migration runs before the switch, while the old release still serves. Write it so it still works with the old code for that moment.

# code in releases/42, .env and storage/ shared; the old release still serves
cd /var/www/releases/42
composer install --no-dev --optimize-autoloader
# caches config, events and routes, compiles the Blade views
php artisan optimize
# runs while the old code still serves: keep it compatible
php artisan migrate --force
# atomic switch: a new link renamed over current
ln -nfs /var/www/releases/42 /var/www/release
mv -T /var/www/release /var/www/current
# since Laravel 12.41: php artisan reload
php artisan queue:restart
Enter fullscreen mode Exit fullscreen mode

Magento can build aside too

Adobe documents a pipeline deployment: compile and deploy the static files on a build system, then copy them to production. Adobe says the build system needs the code, not a database. On 2.4.9 setup:di:compile ran without one as it is. The static deploy didn't work until config.php held the scopes and themes from app:config:dump scopes themes, dumped where the database is, and env.php had no db and no install/date. Without a database it only knows en_US: pass the locales on the command line.

Production isn't free of maintenance in Adobe's steps either: the window runs from pulling the build to the cache clean. Inside it, among a few other steps, come app:config:import and, only if the schema changed, setup:upgrade --keep-generated. setup:db:status tells you: it exits 0 when the database is up to date and 2 when an upgrade is needed. Since 2.4.9 it also exits 2 when one of its checks fails, for example with no database connection in env.php, so run it in production, not on the build system. Deployer's Magento 2 recipe uses that exit code to turn on maintenance mode only when it's needed.

# build system: the code, no database
# config.php with scopes and themes, dumped where the database is:
#   bin/magento app:config:dump scopes themes
# env.php: no db, no install/date
bin/magento setup:di:compile
bin/magento setup:static-content:deploy -f en_US de_DE

# production
bin/magento maintenance:enable
# pull the build: code, config.php, generated/, pub/static
bin/magento app:config:import
rc=0; bin/magento setup:db:status || rc=$?
# 0: up to date, 2: schema or data patches pending
if [ "$rc" -eq 2 ]; then
    bin/magento setup:upgrade --keep-generated
elif [ "$rc" -ne 0 ]; then
    exit "$rc"
fi
bin/magento cache:clean
bin/magento maintenance:disable
Enter fullscreen mode Exit fullscreen mode

What happened next

I proposed that rework for the store: 3 to 5 hours, plus one pause of under 30 minutes to reorganise the files on the server. The client decided not to go ahead for the moment. Since mid-October most of my deploy requests for that store say how long it will be in maintenance, usually 10 to 20 minutes.

The rule

If a Magento deploy runs setup:upgrade, setup:di:compile and the static deploy on the live code, the shop's in maintenance for all of it. Build the code and the static files aside and, with a release switch, keep only the database and config steps inside maintenance. In Laravel the build's composer install, a few cache files and, with a front end, npm run build, all in the new release. The step I'd watch there is the migration that runs while the old code still serves.

That's the last of the 15 parts.

How long does your Magento deploy keep the shop in maintenance?


Part 15 of 15 of the Magento 2 vs Laravel series. Originally published on LinkedIn.

Top comments (0)