Make your Drupal maintenance mode stealthy using Varnish

First, some background on what exactly Drupal maintenance mode is. Maintenance mode is an option in Drupal (typically found under Configuration > Development > Maintenance Mode:

You put your site into maintenance mode when you anticipate doing database updates or scripting that has the chance to show a user a broken page. Generally speaking, anytime you do a database update put your site into maintenance mode. The problem with maintenance mode usually is you only intend for your site to be down for X time. We all know sometimes things don't go as planned and it winds up being longer. If you utilize Varnish as a caching layer on top of Apache or Nginx, you can bump up your "Maximum cache lifetime" so Varnish will cache those pages longer than your maintenance window. Maximum cache lifetime sets the time an external cache (and Varnish is an external cache to Drupal) is allowed to store the page before forcing a refresh. That is found under Configuration > Development > Performance as "the Expiration of cached pages" settings:

If you need to do this on many sites, you can easily set this on multiple sites using drush (10800 is 3 hours in seconds from the above):

drush @sites vset page_cache_maximum_age 10800

Before you complete your maintenance window, you will want to set this value back to your normal setting.

For us this proved to be an easy way to maximize the chances that our end users will not see the maintenance mode message for the most frequently accessed pages. A drawback to this method is that it will not cache all links on your sites, but for us that's OK. There are recipes to crawl your entire site to prime the Varnish cache, but if you have a lot of links (like we do) that can be time consuming and bloat your cache with potentially unviewed content. If you want to go this route with crawling your site to cache most frequently requested pages, have a look at your analytics dashboard to get the top viewed pages for your site and start using a cache primer. :)