TS-Cluster
Every player on the same dedicated node — a self-built autoscaler for WebSocket servers on Digital Ocean, because managed options cost 5-20x or can't guarantee it.
TS · Docker · NestJS · NodeJs · Digital Ocean · AWS Cloudformation · AWS Route53 · Swagger · Websockets · Jest · Axios · Nock · Debian
Why Build an Autoscaler at All
I need horizontal cluster scaling with dedicated servers for other projects. Surely that already exists? I thought so too, but I've tried various cloud providers. Many only offer that you get any node back. Sticky sessions for a load balancer only guarantee that you will be assigned the same node after the connection.
But for multiplayer games I want to use in-memory cache and therefore need to guarantee that all users connect to the same node. For example, I have tried a lot with specialized services, Kubernetes, AWS Fargate with AWS CloudMap and the documentation claims that dedicated access is possible, but I have not been able to confirm this.
There are some services and especially some big workarounds that would make this possible, but the costs are at least 5 - 20 times higher for the same resources. So I prototyped a cluster scaling and then implemented it with this project.
All nodes are based on NestJs and therefore on NodeJs. Everything is written in TS and dockerized. For the communication between the nodes (registrar node, websocket node, test node), HTTP clients are generated from the controller classes using Swagger and Nest-OpenApi.
The nodes are created and deleted via Digital Ocean as a cloud provider. My domains are in AWS and for HTTPS/SSL/CORS I have to register A-Records/Subdomains. So after starting up a Digital Ocean Droplet, I also create an A-Record for a subdomain with the IP of the Droplet.
Components
My domains and my websites are currently in AWS and my deployments there are done with AWS Cloudformation. I use the NPM package 'do-wrapper' for Digital Ocean Cluster scaling. Many functionalities are mapped, but I added some with Axios. I start up the registrar-droplet and because no websocket droplets exist yet, one is started up directly.
The end user downloads the web client and it will then request the registrar droplet endpoint https://registrarnode1@mydomain.de/get-websocket-url and get back a URL for the websocket droplet. Depending on the load, this can then be websocketnode1, websocketnode2, etc.
Currently droplets are only shut down if there are no more active sessions on them and at least one other droplet is underloaded. A new Droplet is started when all Droplets are overloaded. There is a websocket-droplet minimum of 1 and an absolute maximum of X to avoid going bankrupt overnight.
Folder structure
The project consists of several NestJs applications that end with ‘node’. There is also a folder with deployment scripts. In '_common' is the code that all applications share, such as array & string utility functions. In '_shared' are generated APIs, models and types. The 'certificates' folder contains wildcard certificates that I use for HTTPS/CORS in the nestJs applications. And '_test' contains tests for each node and between the nodes. The '.env.*' files contain environment variables for different deployments & tests.
Docker Images
The Digital Ocean Droplets have to pull their Docker images from somewhere, so I also store them in Digital Ocean. The NestJs applications are compiled in the 1st step of their Dockerfile and only the compiled applications are transferred to the 2nd step. So unnecessary files are minimized and only necessary files like the finished application and certificates are copied. I need applications for different tests, and therefore there is e.g. the registrar image for beta and dev.
Boot From a Snapshot, Not From Scratch
Digital Ocean offers to make exact copies of active droplets. These enable a faster startup of e.g. new websocket droplets, since the setup of the Debian OS is already completed. This includes: Installing Docker, pulling images, initializing SSL, stopping unnecessary system processes and uninstalling system packages, etc...
Tip: When creating a snapshot, no Docker containers should be active, otherwise they will be stopped and the name of the Docker container will already be unavailable when the Droplet is started. Also all logs are seamlessly transferred and I thought for a while that my snapshot had a failed container. Tldr: Containers should only be started in the snapshot.
Domains
These are the subdomains that are created when the cluster is started. In this case, the subdomains are for a test that created sessions until 3 websocket nodes were active, and then sessions were terminated on websocketnode2 until this droplet was shut down and the subdomain was deleted. Any domain provider with a good API is sufficient for this.
Tests
Jest is used locally to test whether, for example, a registrar node starts websocket nodes via the Digital Ocean API. For this, endpoints are mocked with Nock and endpoints are determined by the .env.* files.
In addition, there are tests that directly address the Digital Ocean Cloud. These tests can take 5-15 minutes as multiple droplets are booted and shut down by snapshots. Sometimes a test fails due to IP address caching on my local operating system. The subdomains are frequently reassigned during tests and this is not a behavior that will occur in live operation.
Droplets
Registrar droplets and websocket-droplets need each other and cannot be tested on their own in a live cloud environment. Therefore I have additional test-droplets with which I can run simpler tests.
Under 10 € a Month
The floor is one registrar droplet and one websocket droplet — together with Snapshot & Image Registry the whole cluster idles at less than 10 € per month. However, there is not much RAM left for active sessions. Of the 500 MB RAM, 400 - 450 are already used by the OS, Docker and the NestJs Docker container. So I run the application directly with 1 GB RAM and increase the value as soon as it makes sense.
I won't use vertical scaling for now, as I want to gain more direct experience with horizontal scaling with this project. I would like to tackle session migrations later. This will allow me to shut down droplets earlier and manage resources more efficiently.
But for now I'm very happy with the project. I'm looking forward to applying it to Box of Cards in the near future.