137

To deploy a new version of our website we do the following:

  1. Zip up the new code, and upload it to the server.
  2. On the live server, delete all the live code from the IIS website directory.
  3. Extract the new code zipfile into the now empty IIS directory

This process is all scripted, and happens quite quickly, but there can still be a 10-20 second downtime when the old files are being deleted, and the new files being deployed.

Any suggestions on a 0 second downtime method?

5
  • Shouldn't this be on ServerFault? Commented Nov 19, 2009 at 2:42
  • 53
    Perhaps, but ServerFault didn't exist in Sep '08 Commented May 13, 2010 at 9:29
  • 3
    Can IIS point to a symlink folder? Will changing the symlink cause IIS process to recycle? Commented May 7, 2012 at 0:36
  • any final solution with full source code script sample?
    – Kiquenet
    Commented Dec 12, 2012 at 7:05
  • Isn't it possible to have multiple app pools and switch the traffic from one app pool to another?
    – Luke
    Commented Jan 12, 2016 at 19:03

13 Answers 13

87

You need 2 servers and a load balancer. Here's in steps:

  1. Turn all traffic on Server 2
  2. Deploy on Server 1
  3. Test Server 1
  4. Turn all traffic on Server 1
  5. Deploy on Server 2
  6. Test Server 2
  7. Turn traffic on both servers

Thing is, even in this case you will still have application restarts and loss of sessions if you are using "sticky sessions". If you have database sessions or a state server, then everything should be fine.

13
  • 4
    You can also configure the load balancer so that it services existing sessions for a given server, but doesn't accept new ones. That allows you to avoid dropping sessions. This technique however requires waiting for the sessions to end, and in general you'll want to script this.
    – user41871
    Commented Oct 7, 2009 at 4:13
  • 44
    This method tends to fall down when the code roll has structural changes to the database. Once you upgrade the DB for Server 1, server 2 will explode. Now you can backup/restore the database for testing on server 1, but then you have the issue of sorting out the data that changed in the live DB while while the parallel copy was running.
    – EBarr
    Commented Aug 26, 2010 at 22:03
  • 1
    @AndreiRinea -- how do you suppose this would work in an high volume OLTP system? Either the system goes out of sync and you loose data when you cut over, or you need to pause data entry and write a script to identify & migrate the transitory data to the new DB structure.
    – EBarr
    Commented Jan 5, 2011 at 21:45
  • 13
    @EBarr: and in any case technically you still have zero downtime on the ASP.NET app -- the question isn't "how to deploy to a sql server db with zero downtime".
    – Sklivvz
    Commented Jan 20, 2011 at 15:48
  • 11
    They key is to develop in a way that your sql changes aren't destructive. You often have to do any destructive sql changes in the following release once it's no longer used. It's not hard to do with practise.
    – Bealer
    Commented Dec 19, 2013 at 17:38
61

The Microsoft Web Deployment Tool supports this to some degree:

Enables Windows Transactional File System (TxF) support. When TxF support is enabled, file operations are atomic; that is, they either succeed or fail completely. This ensures data integrity and prevents data or files from existing in a "half-way" or corrupted state. In MS Deploy, TxF is disabled by default.

It seems the transaction is for the entire sync. Also, TxF is a feature of Windows Server 2008, so this transaction feature will not work with earlier versions.

I believe it's possible to modify your script for 0-downtime using folders as versions and the IIS metabase:

  • for an existing path/url:
  • Copy new (or modified) website to server under
    • \web\app\v2.1\
  • Modify IIS metabase to change the website path
    • from \web\app\2.0\
    • to \web\app\v2.1\

This method offers the following benefits:

  • In the event new version has a problem, you can easily rollback to v2.0
  • To deploy to multiple physical or virtual servers, you could use your script for file deployment. Once all servers have the new version, you can simultaneously change all servers' metabases using the Microsoft Web Deployment Tool.
12
  • 5
    I've implemented this approach by adapting our powershell deployment scripts. You can see the part of the script which changes the IIS site folder here: stackoverflow.com/questions/330608/… Thanks for the pointer. Commented Dec 1, 2008 at 16:14
  • 17
    Unfortunately, this method doesn't account for structural changes to the DB. Once you upgrade the DB for v2.1 then v.2.0 explodes.
    – EBarr
    Commented Aug 26, 2010 at 22:07
  • 8
    Using TxF is overkill here, IMO. It doesn't hurt anything to have both v2.0 and v2.1 in the filesystem at the same time. The big change happens when v2.1 goes online, and by that time, the TxF transaction has been committed. The zero downtime really happens because of the way IIS moves from an old AppPool to a new one, not because of TxF.
    – RickNZ
    Commented Aug 29, 2010 at 1:52
  • 5
    Another problem with this is if a large amount of user data is stored in subfolders of the app folders. Commented Sep 6, 2011 at 18:44
  • 5
    This is not 0 second deployment because the new app needs to start up.
    – usr
    Commented Jan 7, 2015 at 14:44
22

You can achieve zero downtime deployment on a single server by utilizing Application Request Routing in IIS as a software load balancer between two local IIS sites on different ports. This is known as a blue green deployment strategy where only one of the two sites is available in the load balancer at any given time. Deploy to the site that is "down", warm it up, and bring it into the load balancer (usually by passing a Application Request Routing health check), then take the original site that was up, out of the "pool" (again by making its health check fail).

A full tutorial can be found here.

9

I went through this recently and the solution I came up with was to have two sites set up in IIS and to switch between them.

For my configuration, I had a web directory for each A and B site like this: c:\Intranet\Live A\Interface c:\Intranet\Live B\Interface

In IIS, I have two identical sites (same ports, authentication etc) each with their own application pool. One of the sites is running (A) and the other is stopped (B). the live one also has the live host header.

When it comes to deploy to live, I simply publish to the STOPPED site's location. Because I can access the B site using its port, I can pre-warm the site so the first user doesn't cause an application start. Then using a batch file I copy the live host header to B, stop A and start B.

7
  • 1
    This helps with downtime due to file copy, but has the same issue as @Sklivvz -- as soon as the code roll has structural changes to the database the site goes boom.
    – EBarr
    Commented Aug 26, 2010 at 22:05
  • This seemed like the intuitive way to me as well, but why isn't there an easy, built-in way to do this? Commented Mar 9, 2012 at 12:23
  • 3
    @Ebarr then don't roll out destructive sql changes. For example, if you need to remove a column, do so in the next release when it's no longer used by A or B.
    – Bealer
    Commented Dec 19, 2013 at 17:40
  • 3
    @Rob how can you "pre-warm" the site if it is stopped?
    – Andrew Gee
    Commented Jan 29, 2014 at 15:37
  • 3
    @Rob "copy the live host header to B" Can you explain this? Commented Mar 15, 2018 at 19:56
8

OK so since everyone is downvoting the answer I wrote way back in 2008*...

I will tell you how we do it now in 2014. We no longer use Web Sites because we are using ASP.NET MVC now.

We certainly do not need a load balancer and two servers to do it, that's fine if you have 3 servers for every website you maintain but it's total overkill for most websites.

Also, we don't rely on the latest wizard from Microsoft - too slow, and too much hidden magic, and too prone to changing its name.

Here's how we do it:

  1. We have a post build step that copies generated DLLs into a 'bin-pub' folder.

  2. We use Beyond Compare (which is excellent**) to verify and sync changed files (over FTP because that is widely supported) up to the production server

  3. We have a secure URL on the website containing a button which copies everything in 'bin-pub' to 'bin' (taking a backup first to enable quick rollback). At this point the app restarts itself. Then our ORM checks if there are any tables or columns that need to be added and creates them.

That is only milliseconds downtime. The app restart can take a second or two but during the restart requests are buffered so there is effectively zero downtime.

The whole deployment process takes anywhere from 5 seconds to 30 minutes, depending how many files are changed and how many changes to review.

This way you do not have to copy an entire website to a different directory but just the bin folder. You also have complete control over the process and know exactly what is changing.

**We always do a quick eyeball of the changes we are deploying - as a last minute double check, so we know what to test and if anything breaks we ready. We use Beyond Compare because it lets you easily diff files over FTP. I would never do this without BC, you have no idea what you are overwriting.

*Scroll to the bottom to see it :( BTW I would no longer recommend Web Sites because they are slower to build and can crash badly with half compiled temp files. We used them in the past because they allowed more agile file-by-file deployment. Very quick to fix a minor issue and you can see exactly what you are deploying (if using Beyond Compare of course - otherwise forget it).

2
  • But, you will still get downtime because the app pool recycles. Commented Jan 7, 2016 at 10:37
  • Nope, no downtime because requests are buffered automatically by IIS during the app restart Commented Jan 7, 2016 at 19:26
7

Using Microsoft.Web.Administration's ServerManager class you can develop your own deployment agent.

The trick is to change the PhysicalPath of the VirtualDirectory, which results in an online atomic switch between old and new web apps.

Be aware that this can result in old and new AppDomains executing in parallel!

The problem is how to synchronize changes to databases etc.

By polling for the existence of AppDomains with old or new PhysicalPaths it is possible to detect when the old AppDomain(s) have terminated, and if the new AppDomain(s) have started up.

To force an AppDomain to start you must make an HTTP request (IIS 7.5 supports Autostart feature)

Now you need a way to block requests for the new AppDomain. I use a named mutex - which is created and owned by the deployment agent, waited on by the Application_Start of the new web app, and then released by the deployment agent once the database updates have been made.

(I use a marker file in the web app to enable the mutex wait behaviour) Once the new web app is running I delete the marker file.

0
5

The only zero downtime methods I can think of involve hosting on at least 2 servers.

1

I would refine George's answer a bit, as follows, for a single server:

  1. Use a Web Deployment Project to pre-compile the site into a single DLL
  2. Zip up the new site, and upload it to the server
  3. Unzip it to a new folder located in a folder with the right permissions for the site, so the unzipped files inherit the permissions correctly (perhaps e:\web, with subfolders v20090901, v20090916, etc)
  4. Use IIS Manager to change the name of folder containing the site
  5. Keep the old folder around for a while, so you can fallback to it in the event of problems

Step 4 will cause the IIS worker process to recycle.

This is only zero downtime if you're not using InProc sessions; use SQL mode instead if you can (even better, avoid session state entirely).

Of course, it's a little more involved when there are multiple servers and/or database changes....

2
  • 1
    Same issue as @Sklivvz -- This method falls down as soon as the code roll has structural changes to the database.
    – EBarr
    Commented Aug 26, 2010 at 22:04
  • 4
    That's why I said it was more involved when there are DB changes... Rolling out code with structural changes to the DB is not just a deployment issue; there also has to be support in the code, and probably in the DB too.
    – RickNZ
    Commented Aug 29, 2010 at 1:48
1

To expand on sklivvz's answer, which relied on having some kind of load balancer (or just a standby copy on the same server)

  1. Direct all traffic to Site/Server 2
  2. Optionally wait a bit, to ensure that as few users as possible have pending workflows on the deployed version
  3. Deploy to Site/Server 1 and warm it up as much as possible
  4. Execute database migrations transactionally (strive to make this possible)
  5. Immediately direct all traffic to Site/Server 1
  6. Deploy to Site/Server 2
  7. Direct traffic to both sites/servers

It is possible to introduce a bit of smoke testing, by creating a database snapshot/copy, but that's not always feasible.

If possible and needed use "routing differences", such as different tenant URL:s (customerX.myapp.net) or different users, to deploy to an unknowing group of guinea pigs first. If nothing fails, release to everyone.

Since database migrations are involved, rolling back to a previous version is often impossible.

There are ways to make applications play nicer in these scenarios, such as using event queues and playback mechanisms, but since we're talking about deploying changes to something that is in use, there's really no fool proof way.

1

This is how I do it:

Absolute minimum system requirements:
1 server with

  • 1 load balancer/reverse proxy (e.g. nginx) running on port 80
  • 2 ASP.NET-Core/mono reverse-proxy/fastcgi chroot-jails or docker-containers listening on 2 different TCP ports
    (or even just two reverse-proxy applications on 2 different TCP ports without any sandbox)

Workflow:

start transaction myupdate

try
    Web-Service: Tell all applications on all web-servers to go into primary read-only mode 
    Application switch to primary read-only mode, and responds 
    Web sockets begin notifying all clients 
    Wait for all applications to respond

    wait (custom short interval)

    Web-Service: Tell all applications on all web-servers to go into secondary read-only mode 
    Application switch to secondary read-only mode (data-entry fuse)
    Updatedb - secondary read-only mode (switches database to read-only)

    Web-Service: Create backup of database 
    Web-Service: Restore backup to new database
    Web-Service: Update new database with new schema 

    Deploy new application to apt-repository 
    (for windows, you will have to write your own custom deployment web-service)
    ssh into every machine in array_of_new_webapps
    run apt-get update
    then either 
    apt-get dist-upgrade
    OR
    apt-get install <packagename>
    OR 
    apt-get install --only-upgrade <packagename>
    depending on what you need
    -- This deploys the new application to all new chroots (or servers/VMs)

    Test: Test new application under test.domain.xxx
    -- everything that fails should throw an exception here
    commit myupdate;

    Web-Service: Tell all applications to send web-socket request to reload the pages to all clients at time x (+/- random number)
    @client: notify of reload and that this causes loss of unsafed data, with option to abort 

    @ time x:  Switch load balancer from array_of_old_webapps to array_of_new_webapps 
    Decomission/Recycle array_of_old_webapps, etc.

catch
        rollback myupdate 
        switch to read-write mode
        Web-Service: Tell all applications to send web-socket request to unblock read-only mode
end try 
0

A workaround with no down time and I am regularly using is:

  1. Rename running .NET core application dll to filename.dll.backup

  2. Upload the new .dll (web application is available and serving the requests while file is being uploaded)

  3. Once upload is complete recycle the Application Pool. Either Requires RDP Access to server or function to recycle application pool in your hosting control panel.

IIS overlaps the app pool when recycling so there usually isn’t any downtime during a recycle. So requests still come in without every knowing the app pool has been recycled and the requests are served seamlessly with no downtime.

I am still searching for more better method than this..!! :)

0

IIS/Windows

After trying every possible solution we use this very simple technique:

  1. IIS application points to a folder /app that is a symlink (!) to /app_green
  2. We deploy the app to /app_blue
  3. We change the symlink to point to /app_blue (the app keeps working)
  4. We recycle the application pool

Zero downtime, but the app does choke for 3-5 seconds (JIT compilation and other initialization tasks)

Someone called it a "poor man's blue-green deployment" without a load balancer.

Nginx/linux

On nginx/linux we use "proper" blue-green deployment:

  1. nginx reverse proxy points to localhost:3000
  2. we deploy to localhost:3001
  3. warmup the localhost:3001
  4. switch the reverse proxy
  5. shot down localhost:3000

(or use docker)

Both windows and linux solutions can be easily automated with powershell/bash scripts and invoked via Github Actions or a similar CD/CI engine.

1
  • For Linux and Windows, how is a database connection and migration handled with the EF Core if the service had one? Commented Mar 15, 2023 at 19:13
-8

I would suggest keeping the old files there and simply overwriting them. That way the downtime is limited to single-file overwrite times and there is only ever one file missing at a time.

Not sure this helps in a "web application" though (i think you are saying that's what you're using), which is why we always use "web sites". Also with "web sites" deploying doesn't restart your site and drop all the user sessions.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.