Live web server migration

I need to replace an aging webserver which runs a busy website. I have the new hardware up and running, all software is installed. There's only one problem left where I'm looking for a solution:

When I change the A record for the domain in DNS, it takes a while for the change to propagate through the internet due to DNS caching. During that time, some users will hit the new server, and some will hit the old one, leading to data inconsistencies because there are now two databases. How can I switch everyone over instantly? I do not run the DNS server and cannot change anything but the A and CNAME records, but I could setup my own if that helps.

I can probably have an hour or two of downtime when moving overnight.

Thanks, Simon

edit: Thank you for all your answers. I'm accepting SmallClanger's answer because it's a viable solution, and I'm upvoting Brian because his answer gave me a new idea: Follow SmallClanger's solution up to the database migration, and then replace the old apache with an HTTP proxy which proxies all requests to the new server. I'd configure the proxy to address the new server by IP address, and it does not involve a temporary subdomain which might (despite the 302 code) end up in some bookmark directory, social network or browser cache.


Solution 1:

The fundamental problem you have is of maintaining database consistency during the switch. To have zero downtime, you'll have no choice but to set the two servers up in an active/active cluster. (even assuming, for the moment, that the cluster setup would involve no downtime itself, which is a bit of a stretch.).

Given that you can't change the TTL, you best step is to minimise downtime:

  • Set the new site up on a second domain name (eg: www2.example.com), but make it answer to www.example.com, too. Point the DNS for www2 at the new server only.
  • Have the site files replicated and locked down from edits on both sides.
  • Then in an advertised maintenance window, replace the live site with a 'down for upgrades' notice.
  • Sync or copy the database across as required, and then start the new server.
  • Then (and only then) make your DNS changes to the www record.
  • Replace the old site config with a simple 302 redirect to www2.example.com
  • Wait for the full TTL to expire (and then a bit) before taking the old server down.

This will make sure all traffic goes to the new site during the DNS propagation, regardless of which server they go to initially, though it assumes your site is capable of being served from multiple domain names at the same time.

For a single-server LAMP stack (if that's what you've got), then this is your route to minimal (but not zero) downtime.

Solution 2:

You could configure the old web server to redirect all requests to the new IP. This can be done with mod_rewrite in apache.