When you deploy incremental changes on a production server, which is running and live all the time, you some times see error messages like “Compiler Error Message: The Type ‘XXX’ exists in both…”. Sometimes you find Application_Start event not firing although you shipped a new class, dll or web.config. Sometimes you find static variables not getting initialized and so on. There are so many weird things happen on webservers when you incrementally deploy changes to the server and the server has been up and running for several weeks. So, I came up with a full proof house keeping steps that we always do whenever we deploy some incremental change to our websites. These steps ensure that the web sites are properly recycled , cached are cleared, all the data stored at Application level is initialized.
First of all you should have multiple web servers behind a load balancer. This way you can take one server out of the production traffic, do your deployment and house keeping tasks like restarting IIS, and then put it back. Then you can do it for the second server and so on. This ensures there’s no outage for customer. If you can do it reasonable fast, hopefully customers won’t notice discrepancy between the servers some having new code and some having old code. You should only do this when your changes aren’t drastic. For ex, you aren’t delivering a complete revamped UI. In that case, some users hitting server1 with latest UI will suddenly get a completely different experience and then on next page refresh, they might hit server2 with old code and get a totally different experience. This works for incremental non-dramatic changes only.
During deployment you should follow these steps:
- Take server X out of load balancer so that it does not get any traffic.
- Stop all your .NET windows services on the server.
- Stop IIS.
- Delete the Temporary ASP.NET folders of all .NET versions incase you have multiple .NET versions running. You can follow this link.
- Deploy the changes.
- Flush any distributed cache you have, for ex, Velocity or Memcached.
- Start IIS.
- Start your .NET windows services on the server.
- Warm up all websites by hitting major URLs on the websites. You should have some automated script to do this. You can use tinyget to hit some major URLs, especially pages that take a lot of time to compile. Read my post on keeping websites warm with zero coding.
- Put server X back to load balancer so that it starts receiving traffic.
That’s it. It should give you a clean deployment and prevent unexpected errors. You should print these steps and hang on the desk of your deployment guys so that they never forget during deployment pressure.
Doing all these steps manually is risky. Under deployment time pressure, your production guys can make mistakes and screw up a server for good. So, I always prefer having a batch file that takes a server out and makes it ready for deploying code and then after the deployment is done, use another batch file to put the server back into load balancer traffic rotation after the server is warmed up.
Generally load balancers are configured to hit some page on your website and keep the server alive if that page returns a HTTP 200. If not, it assumes the server is dead and takes it our of rotation. For ex, say you have an alive.txt file on your website which is what load balancer is keeping an eye on. If it’s gone, the server is put out of the rotation. In that case, you can create some batch files that will take the server out, wait for couple of seconds to ensure the in-flight requests complete and then stop IIS, delete temporary ASP.NET files and make server ready to deploy stuff. Something like this:
serverout.bat ===================== Ren alive.txt dead.txt typeperf "ASP.NET Applications(__Total__)Requests Executing" -sc 30 iisreset /stop rmdir /q /s "C:WINDOWSMicrosoft.NETFramework64v1.1.4322Temporary ASP.NET Files" rmdir /q /s "C:WINDOWSMicrosoft.NETFramework64v2.0.50727Temporary ASP.NET Files" md "C:WINDOWSMicrosoft.NETFramework64v1.1.4322Temporary ASP.NET Files" md "C:WINDOWSMicrosoft.NETFramework64v2.0.50727Temporary ASP.NET Files" xcacls "C:WINDOWSMicrosoft.NETFramework64v1.1.4322Temporary ASP.NET Files" /E /G MYMACHINEIIS_WPG:F /Q xcacls "C:WINDOWSMicrosoft.NETFramework64v2.0.50727Temporary ASP.NET Files" /E /G MYMACHINEIIS_WPG:F /Q
Similarly you should have a batch file that starts IIS, warms up some pages, and then puts the server back into load balancer.
serverin.bat ============ SET TINYGET=C:Program Files (x86)IIS ResourcesTinyGettinyget.exe iisreset /start"%TINYGET%" -srv:localhost -uri:http://localhost/ -status:200 ren dead.txt alive.txt typeperf "ASP.NET Applications(__Total__)Requests Executing" -sc 30
Always try to automate this kind of admin chores. It’s difficult to do it right all the time manually under deployment pressure.
One thought on “Safely deploying changes to production servers”
Great article, mr. Omar !!!
I use MsBuild for deploy in remote servers. It will be very interesting automatize all process.
very helpfull scripting samples for:
server out of load balancer so that it does not get any traffic.
Stop all windows services on the server.
Stop IIS. (Exec Command in Msbuild for iisreset /stop)
Delete the Temporary ASP.NET folders ((Exec Command in Msbuild for del /f)
Flush any distributed cache you have,
Start IIS (Exec Command in Msbuild for iisreset /start)
Start the windows services on the server.
Warm up all websites
server back to load balancer