Magento Zero downtime deployment 2

Magento eCommerce availability is one of the criteria considered for the success of your business. It is important to make sure your sales is not impacted due to software deployment activity.

Image for post
Image for post

Magento 2 as a legacy PHP application that doesn't have real 0 downtime deployment. If you are advertising 0 deployments but you cant upgrade/remove/install the new module without downtime during

bin/magento setup:upgrade --keep-generated

it is not zero downtime deployment.

Magento Commerce Cloud runs Magento in maintenance mode during the deploy phase, which takes your site offline until the deployment is complete. The length of time your Production site is in maintenance mode depends on the size of the site, the number of changes applied during the deployment, and the configuration for static content deployment.

Magento official docimentation

Steps to deploy Magento :

  • prepare a package with the new version of the application (static files, di compile). You can do it even locally

Let’s take a look at how we can fix that.

Magento needs maintenance mode for this command. However, his is a core Magento 2 issue. Historically Magento 2 doesn’t follow best software development practices they are reinventing their own bicycle. Why 0 downtime is not possible? Magento has Plugin (also the weirdest invention of Magento spaghetti architecture) and this plugin kills Application and you can’t control this.

Here it is this guy:

if (!$this->cache->load('db_is_up_to_date')) {
list($versionTooLowErrors, $versionTooHighErrors) = array_values($this->getGroupedDbVersionErrors());
if ($versionTooHighErrors) {
$message = 'Please update your modules: '
. "Run \"composer install\" from the Magento root directory.\n"
. "The following modules are outdated:\n%1";
throw new LocalizedException(
new Phrase($message, [implode("\n", $this->formatVersionTooHighErrors($versionTooHighErrors))])
} elseif ($versionTooLowErrors) {
$message = 'Please upgrade your database: '
. "Run \"bin/magento setup:upgrade\" from the Magento root directory.\n"
. "The following modules are outdated:\n%1";

throw new LocalizedException(
new Phrase($message, [implode("\n", $this->formatVersionTooLowErrors($versionTooLowErrors))])
} else {
$this->cache->save('true', 'db_is_up_to_date');

Simply by commenting out this “throw new LocalizedException(“ lines you can achieve DB upgrade on a live project without downtime. Or just log it but not kill. By the way, M1 worked this way but without issues. More you don’t need to run any wires command like setup:upgrade/compile/generate/dancing with M1.

How setup:upgrade works in Magento 1:

When you upload the new code onto your webserver’s filesystem, the next time that you load any Magento page Magento will apply the files in the sql\modulename_setup folder in sequence to bring the data_version up to match the module’s version. Without any Localized exceptions…

However, you should review your code first! Sometimes Magento and extension developers don’t follow “software development best practices” only legacy “Magento certified best practices” and they can delete field or table and brake “backward compatibility with old code. To fix this you should do Incremental Deployments.

You should always explore changes that will bring down the total Magento deployment you can move it to pre-deployment and post-deployment steps.

The downside is that zero-downtime deployments require more baby care with database changes. As two versions of your code need to be able to work at the same time you can’t just remove or rename fields.

Renaming or deleting a column or table needs to be divided into several deployments. This way we make sure that the application still works with every incremental change.

If the old code needs catalog EAV tables however in new deployment you are removing this Magento legacy with simple flat tables without any indexers obfuscation. You should delete EAV tables in the post-deployment step/script (no needs to use Magento XML) or during the next deployment when your code doesn’t depend on the old DB structure.

What are our new steps:

  • deploy new Magento version of your server

Note: you should deploy only good code tested on local or on staging. If your code is doesn’t work with 0 downtime deployment it is not the deployment issue.

There is not the only way to achieve this. But it is the simplest. Up to you how you will achieve 0 downtime. Deployment with maintenance mode also works for some companies but you should know the way to do deployment with database changes without downtime…

Update: Magento community released a plugin to achieve 0 downtimes. Read more below this article.

It is important to automate every step of the Magento deployment. No matter if you want to deploy your code on every merge to some branch (githook will help you) or trigger it manually.

Comments about this deployment approach from the Magento community:

Image for post
Image for post

This is a simple zero-downtime Magento deployment script example. Posting this to make this post bigger for Google and other dictators.


echo “\n Go to Magento root…”
cd $root
echo “\n Current user…”
echo “\n Downloadin latest changes…”
# download deplyment artifacts with in-advance precompiled data
echo “\n Delete old files…”
sudo rm -rf $root/var/report/* \
$root/var/view_preprocessed/ \
echo “\nUpgrading DB…”#important CLI sholdn't use opcahe during this procedure
bin/magento setup:upgrade
echo “\n Copy new files ”
cp -r new_deployment_folder\* $root
## or use rsync can be faster
echo “\n Restart PHP FPM to clean op-cache…”
sudo service php-fpm restart
#echo “\n Hardcode folder permissions asynrouniusly…”

sudo find . -type f -exec chmod g-wx,o-wx {} > /dev/null 2>&1 &

Take a look video about Magento deployment process:

Magento 0 Downtime deployment module from the community:

Module prevents showing database versions exception when you pull new code to the server. The best idea is to use CI/CD with Docker/Kubernetes. Suggested deployment:

  • get docker base image

Git Hub Repo:

Thanks, guys we made Magento 2 greater again together, however, Magento core is still legacy.

Maybe somebody can donate a pull request to the Adobes official repo.

Written by

Magento/APP Cloud Architect. Melting metal server infrastructure into cloud solutions.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store