At Factorify we wanted to make deployent of our application unnoticed by users. Application is AngularJS client connected to Spring backend. User's requests are proxied by Nginx running on FreeBSD.
Basic idea was to hold user's requests during application deployment. Nginx does not provide such function by default but it is possible to extend it by scripts written in Lua language.
I wasn't able to compile Nginx with Lua support. Fortunately there is Nginx "distribution" called OpenResty which integrates Lua compiler.
Nginx configuration
-
Nginx listens on port 8080.
-
Request is sent to primary node. If primary node is alive response will be served to user.
-
If primary node fails to return response request will be send to backup node.
-
Backup node suspends request for 10 seconds and then forwards it to the backend. 10 seconds should be sufficient time for application to restart. If no response is returned by backend in 10 seconds then error will be send to user.
http {
upstream backend {
# 2) Primary node.
server localhost:8667;
# 3) Backup node.
server localhost:8666 backup;
}
server {
# 1) Endpoint exposed to users.
listen 8080;
location / {
proxy_pass http://backend;
}
}
server {
listen 8666;
location / {
# 4) Suspend request for 10 second.
access_by_lua '
ngx.sleep(10)
';
proxy_pass http://localhost:8080/;
}
}
server {
# This is primary node that emulates backend application.
listen 8667;
location / {
default_type text/html;
content_by_lua '
ngx.say("Hello World!")
';
}
}
}
To fine-tune the configuration please refer to Nginx documentation.