Continuous Delivery with ZeroDownTime (ZDT) Deployment for PHP Applications
Learn about a new approach for zero downtime deployments for PHP apps running on Apache.
Join the DZone community and get the full member experience.Join For Free
the modern enterprise web-services’ provision is based on 24/7 accessibility, which becomes especially important for those of them, that are dependent on frequent re-deployments (i.e. updates). normally, at this time your server goes down, which causes not just a temporary profit loss, but also the reduction of users’ trust, which is unacceptable for any paid service.
a huge branch of tools like capistrano, fabric, etc can help you to solve this problem – for example, through setting multiple servers with a load-balancer as a frontend, which will update them alternately. however, all of such utilities have certain additional requirements, which results in increased expenses, and needs some time and specialist knowledge for being integrated and properly adjusted. obviously, such implementations appear to be rather complicated and require a lot of extra resources, thus a better method is needed.
recently, a completely new approach in this area was proposed for php applications, running on top of apache, by the founder of this programming language and, simultaneously, our technical advisor – rasmus lerdorf. this is actively used at etsy, and, therefore, being a battle-tested solution, it was taken as the basis for the zero downtime deployment feature implementation in jelastic. its main idea rests on the following two points:
- each time a new deployment process is run, the corresponding app’s files are duplicated, being stored in a separate server directory (which is automatically named after its creation date/time for easy identification).
- a special requests’ redirector, called symlink (i.e. symbolic link), switches between different app versions after each update, pointing to the one that should be currently used.
in such a way, the updated project files can be seamlessly deployed, while the initial code version continues working and handling users’ sessions. and when the deployment is fully completed, the symlink instantly switches to the most recent version of the successfully deployed app, starting to redirect all the incoming requests to it. all of these together makes the deployment process fully atomic and implicit for your customers, simultaneously relieving you from performing plenty of envisioned manual operations.
note that the availability of this functionality requires jelastic platform version 3.3 or higher and depends on your hosting provider’s settings.
below, we’ll discover this mechanism in more detail by consequentially describing the zdt deployment workflow and the way this functionality has been implemented at jelastic. and in order to make sure this approach is efficient, at the very end of this article you can find the comparison of zdt and classic deployment methods, performed through running the set of load tests with the help of jmeter.
so, let’s get started!
zdt deployment workflow
first of all, we’ll consider more specifically how the above described php zero-downtime deployment mechanism actually works on jelastic – let’s examine all of these processes step-by-step with a real example.
- to start with, you’ll need a php environment (either a new or the already existing one) – we’ll use apache for this example:
- next, proceed to the deployment of the required application. during this procedure, you need to tick the corresponding checkbox at the appropriate confirmation frame (depending on the project source type used) in order to enable the zdt deployment option:
- for deployment via local file or direct url
note: that while performing this for the first time for the already existing application, deployed to the root context, all the previous data will normally be erased and overwritten with the “bare” app installation (for the deployment via archive/url only).
- the enable zero-downtime deployment flag becomes active only when deploying to the root context of your php application server. otherwise, the classic method will be used
- while working with vcs repos, the chosen deployment mode will be remembered and used for all further auto-updates of this application until you change it manually
- generally, we recommend not to use the “hard-coded” absolute paths in your app’s code and configs while using the zdt deployment feature, for ensuring it remains operative, regardless of the project’s directory name
- during the initial deployment , a root_timestamp (i.e. root_year.mm.dd-hh.mm.ss ) folder and a special root file as a symlink to this folder are created inside the webroot directory of your application server.
as normal, the application is ready to handle requests just after the deployment process is finished.
if navigating inside the root directory, circled above, the content of the currently used application version will be viewed, i.e. it is changed each time the symlink is switched.
this can be clearly seen if entering your application server’s container via ssh and executing the long format file-listing command for your webroot folder, i.e.:
ls -l /var/www/webroot
in such a way, you are able to easily locate the symlink, as it is color-marked within the list, and to see the actual redirection path.
- during the second deployment (i.e. when deploying an update), a new root_timestamp folder is created – in such a way, the actual application version and customers, that are currently working with it, are not influenced.
just after the new files are unpacked, symlink switches to this new folder, redirecting all the newly received requests to it. herewith, the first folder is kept for processing the “old” users’ sessions (i.e. where handling started before the symlink switching).
note that while updating an app version using archive/url, all the comprised user-generated content (if there is any) should be manually moved to the newly created app directory from the older one, stored alongside (herewith, previously such an operation implies the full overriding of all the context data).
if using vcs, the app’s directory content is entirely copied (both tracked and untracked files), so no manual operations are required. however, we recommend to adopt the practice of the .gitignore list usage for the unnecessary files of your project, as this would save you some amount of resources and time during repetitive re-deploys.
- all the following deployments will be performed in the similar way. during each of them, the oldest project folder is removed, while a new root_timestamp directory for the most recent project version is added.
in such a way, only 2 versions of a deployed application – the latest and the previous one – are stored within an app server simultaneously (however, the older one can also be easily removed manually when it is no longer needed). this ensures no extra disk space consumption.
note: if you’d like to avoid some project version from being automatically erased, just rename the corresponding folder before running the new deployment.
all of the operations are fully automated, thus no additional developer’s involvement is required, while the deployment itself is performed in a “soft” mode, i.e. even without an app server restart needed and, as a result, without any application downtime.
zdt implementation at php servers
delving into the details of technical implementation, the zero-downtime deployment option support at jelastic is ensured by the following adjustments, applied to the corresponding php instances:
- apache php the appropriate functionality is handled with the help of the mod_realdoc module, which controls the abovementioned symlink switching. it can be additionally configured (if required) through the jelastic dashboard within the conf.d > mod_realdoc.conf file.
tip: here, the realpathevery parameter defines the period of time for which the symbolic link path is stored and the frequency of its refreshing. it’s default value ( 0 , as denoted within the config comments) was changed to 2 for ensuring all of the required operations (i.e. deployment and switching) to be completed before redirecting the requests to the new project version and as such, preventing the i/o slowdowns.
this value can be easily changed to your custom one if needed (just do not forget to restart your app server node for its appliance). however, if using the zdt deployment feature, we do not recommend to set it too high, as this would cause the delays in symlink switching.
for more information on this module’s specifics, visit its source page .
- nginx-php here, zdt deployment is ensured by means of the in-built functionality with no additional modules inclusion – the corresponding settings can be found at the very end of the conf > nginx.conf file:
for now, as you know how all of this works, we can compare both classic and zdt deployment methods.
comparison and summary
to prove the benefits of the zdt update approach, a simple load test was run, with the following parameters as a basis:
- application – a basic version of wordpress cms deployed (i.e. its default distribution with no heavy content comprised).
- load generation tool – apache jmeter , configured to continuously send the required amount of concurrent requests to our application during the redeployment process.
- time frame – the test starts a short time before the redeployment process is run and finishes a few seconds after it is completed.
so, let’s evaluate the results for both deployment methods with the simple statistics we’ve received.
let’s start with the most commonly used variant of the project deployment, namely – classic , i.e. installation from a single archived package with no extra options like zdt enabled:
as you can see, we actually get pretty good results:
- quick and steady response time (the blue graph), only 1.2 seconds on average
- fast restoration to the normal operability (i.e. when all of the incoming requests are successfully processed (the green line) with no errors (the red graph) occurred) after the new package deployment
- fails to appear for two seconds only – see the red line spike (however, deploying a more heavy and content-replete project will increase this interval for certain)
now, let’s perform the same test with the second competitor – zdt . for a better comparison perception, we’ll keep the same color legend as before:
response time remains stable and almost unchanged, but you can notice its slight enlargement during the update procedure, which is caused by the additional deployment process running alongside the serving of requests. herewith, there isn’t even a single error during the whole test.
so, in such a way, we can assume that zero downtime deployment overcomes the problem of failed requests during application redeployment, simultaneously keeping the average response time on the same level. in addition, the zdt option leaves you a possibility to save all of the user-generated content, located inside the application directory, and easily move it to the new app’s version if necessary (while the classic method normally implies just the entirely new app version deployment)
you may also notice , that the minimum requests’ handling time for the classic method is significantly lower than for the zdt one and, therefore, seems to bring better performance. but don’t be mislead, as it’s just a side effect of the failed requests presence (where serving time is also counted, despite it being not processed), while the average response time is almost the same for both methods.
next, let’s repeat our test for the second jelastic deployment type (i.e. if using git/svn repos) in order to find out whether zdt retains its advantages in this case. and again we’ll start with the classic method:
as the deployment sources are placed at the remote resource, this will require a little more time compared to the installation from the already uploaded archive, which, actually, helps us to clearly see the difference. the response time now has a pretty long drop down (for nearly 4 seconds in our case), caused by the unavailability of the application (you can see that incoming requests start to fail at the same time – this is shown with a spike at the errors graph). everything else remains similar to the previous deployment type.
note that unlike the archive deploy (where the old project is entirely removed before redeployment, which will always cause downtime), here the update procedure assumes changing of the differing files only. therefore, you may not face any interruption in the service work if the files that are required to be changed, are currently unused.
finally, the last test for the zdt deployment approach via vcs also goes in line with our expectations by bringing a stable response time with its small incrementation during the simultaneous running of such operations as users’ sessions handling and project copying/updating.
at the same time, you can see that no errors appeared and all of the incoming requests are successfully processed .
now, when you have all the information (both raw technical and visualized in graphs practical one) on the investigation and have seen how it’s easy to use the zdt option within jelastic cloud, it’s time to summarize and make a conclusion on the key benefits it brings for your php app’s hosting:
- zdt doesn’t require any additional resources like separate instances/tools for being applied – all you need is just enough disk space for storing two project versions (current and the previous one). it could be considered as an almost free of charge solution, especially compared with the majority of other possible options, which may require additional app servers, balancers, external services, etc.
- the deployment remains just as simple as before – no additional configurations or human intervention is required.
- the time needed for the zdt deployment is exactly the same as for the classic method, so no delays are expected.
- finally, zero-downtime deployment stands by its name by ensuring it’s completely implicit for your customers with an errorless updating procedure (in contrast to the classic variant, which, without being additionally improved, causes a pretty big amount of errors even in the case of a small application redeployment).
in such a way, zdt deployment usage makes updating of your projects completely painless and invisible to customers, helping you to get the most from your application!
Published at DZone with permission of Tetiana Markova, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.