I thought of this method to update server files without downtime:
Are there any downsides to this? Is there a better way?
Could there be any race condition errors when a file is being accessed at the exact moment of the restart?
There is no single way that will always work - even if you prepare and deploy a new server and then change a load balancer to point to the new machine. Someone could have just requested a page from the original server and then be fetching parts of the page from the new copy of the site on a new machine. This is unlikely to cause a big problem however in practice.
On larger sites, with complex needs, things will be more difficult - and at a cost. Most sites have simpler requirements. Even database migrations can be planned to have minimal disruptions - but will also be complicated and have to be worked around with planned downtime.
Since switching between copies of the site on the same machine (or a small number of machines) is easy (effectively moving the Apache, or Nginx DocumentRoot
) a technique that is easily available (and does not require multiple machines and a load balancer to achieve) is thus:
composer install
or asset fetches/preparation as requiredIn practice, this is what Capistrano (and other similar tools) does. The base directory is laid out like this:
base-directory
releases
20151010-0925/
vendor/ (composer-installed files)
web/index.php
20151120-1007/
vendor/ (composer-installed files)
web/index.php
current (symlink to ../releases/20151010-0925/)
shared/ (files shared between releases)
When deploying a new version of the website - create a new fully-formed release and then when it is complete, change the 'current' symlink.
The Apache document root in this instance would point to .../base/current/web/
going through the symlink to the base of the website.
If, at any point the deployment fails to be ready you can simply abort it, and optionally delete the released version you were trying to deploy, and not alter the symlink.
I've used this technique from trivial, single page, HTML/CSS sites to three-machine clusters running thousands of concurrent users.
The best solution would be to setup a load balancer for high availability (e.g. HAProxy) of your server.
Otherwise, you will always have a downtime. Think about the following cases: resetarting apache, database updates, assets updates, unexpected errors during update, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With