Coder Social home page Coder Social logo

zero-downtime-websockets's Introduction

Seamless deployment with Docker Compose + Nginx + Websockets demo

This repo demonstrates how to set up a seamless deployment using Docker Compose and Nginx. Websockets are handled too, by having the websocket server tell its clients to reconnect while both server instances are running.

Preparation

  1. Build the server: run mvn package in the server folder
  2. Run docker-compose up -d in the root
  3. Visit http://localhost, and connect to the websocket

Deployment

  1. Run./deploy.sh in this folder

That's all.

Observe

Run ./deploy.sh again, but this time:

  1. while true; do curl -I https://www.ktbyte.com/login ; done - observe the randomly generated server ID change once the new server starts responding
  2. Open the console in your websocket client - observe the disconnect/reconnect event but no actual connection errors occur

How does it work?

  1. We use docker-compose to spin up a second instance of the server
  2. We tell nginx to reload
    • At this point, Nginx will route new connections will go to the second instance. But long- running HTTP requests and websocket connections may still be active from the first.
  3. We query the second server via IP to wait until it's up
    • actually on Mac this doesn't work, so we just wait 5 seconds
  4. We use docker-compose to tell the first instance to shut down
  5. When the first instance is told to shut down, it tells every websocket client it knows of to disconnect and reconnect. When they do this, their new connection is made to the second server.
  6. We tell nginx to reload

Notes

There is a brief moment where only the new server is active, but Nginx does not realise it yet. Normally with two servers active, Nginx by default alternates requests between the two, but in this case requests to the old server (which no long exists) will time out - and when that does, Nginx will re-send the request to the new server, which will succeed and Nginx will mark the old as down.

This means, in a worst case scenario, some requests will take 2s extra (the timeout is configured in nginx.conf) than they normally would.

Credit

zero-downtime-websockets's People

Contributors

crummy avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.