Leaving Serverless
Why a live card game left AWS serverless — Websocket dead ends, AppSync limits, a missing cache. The replacement: dedicated Digital Ocean droplets that scale horizontally. Part of the Box of Cards work.
TS · AWS · AWS Lambda · Digital Ocean · Appsync · ApiGateway · DynamoDb · AWS Fargate · Cloudformation · Docker · NodeJs · Websockets · MongoDB · Route53
The Plan Meets the AWS Bill
My professor for Software Engineering said: 'Planning is essential but plans are useless'. I repeat this often.
Initial AWS cost calculations showed a very large margin for profit but reality was catching up.
On paper and in the documentation AWS Services provided the features I needed. But sometimes the feature is just not working. The core services like AWS Lambda, S3, DynamoDb, etc. are completely fine. But as soon as you start using something a bit more exotic like Appsync or a Websocket Api Gateway there will be problems and workarounds are costly.
Websockets on AWS
My servers need to inform clients on game state updates and thus I need Websockets. Long polling is just not viable.
The Appsync documentation was meant to provide websocket subscription support but the limitations are so artificially severe that I had to look for an alternative. So I connected my Websockets to the AWS Api Gateway and forwarded them to my AWS Lambda. Sadly the Disconnect handling is only best effort and quite unreliable. I had no way to inform disconnected clients and the only way to solve this would have been Long Polling, yikes. The websocket connection also automatically disconnects after 10 minutes and this can not be changed at all.
Open AWS Bugs
I saw so many open bug reports from 2018 that I wonder whether anybody is supporting these services.
There are minor things like AWS Api Gateway stringifying my forwarded body and me having to convert back my integers.
But there is also the interaction between cloudformation stacks across regions. These interactions are not avoidable as global Services like e.g. Cloudfront are only available in us-east-1 but I got my core services in eu-west-1. A deployment might work fine a few times and suddenly permissions are missing to update or delete a service. Suddenly you spend up to hours to manually add permissions and delete singular services just to get back to start.
The Community Rebuilt AppSync for a Reason
How bad can it really be?
Well the community reimplemented Appsync and Apollo using AWS Lambda.
What were my problems with Appsync? There are several. AWS Appsync uses GraphQl but in a limited way and with proprietary annotations. Due to this most of the eco-system can not be used and the abundant boilerplate can not be generated.
There is a severe mismatch between supported protocols and protocol versions of Apollo & AWS Appsync. Appsync does not support the mqtt websocket protocol and Apollo is so far behind that they say to use some other standard. Nothing remains compatible. Legacy protocols were deprecated and removed while modern standards are not yet supported. Luckily I could heavily downgrade Apollo to use a legacy protocol.
More on Appsync..
The proprietary AWS-Scalar type is incompatible with HttpClients and Key-Value-Pairs are not supported at all. I had to wrap those Pairs in json and I really don't like nesting my data formats.
Appsync subscriptions are a total mystery to me. I need one subscription per topic, I can only respond to a single user at a time, I can't know who those users are and the data I send can only contain values for fields that were provided on the subscription request. The last one is problematic as I can't provide values for fields that were previously not set due to being optional.
Leaving Serverless
Serverless could probably still have carried Box of Cards to profitability — but development had stopped being fun and administration never stopped being clunky.
I had seen this coming for a long time and it is fine for me, as this was a project for learning new technologies from the get go.
I wanted to get rid of Appsync as without websocket subscriptions it was just a more expensive and more cumbersome Api Gateway. But the Api Gateway was also unreliable for websockets and so I wanted to try an alternative via a small prototype. Because this time I don't trust the documentation.
Caching is your friend
AWS Lambda reduced server costs but there was a lack of a cost efficient cache for my use case. As weird as it may sound but DynamoDb was just Overkill for most of my caching requirements.
One Megabyte of shared Lambda Cache would have been enough to reduce my DynamoDb calls by ~ 90% for thousands of simultaneous games. AWS Lambda have a limited shared cache as long as they are hot but this is seriously not meant for this purpose.
So what I need is basically a semi persistent Server. Then I could adjust the server count based on demand and so I tried that out.
Prototyping dedicated AWS instances
I want Box of Cards to remain scalable even though it would probably run fine forever on a single server. Switching to dedicated instances would allow stable websocket connections and cost savings by replacing DynamoDb with in-memory cache.
Most of my code is on AWS and so I will try it on AWS first. So is there a way to have cheap and horizontally scalable node instances on AWS? I tried using AWS Fargate as it is meant for exactly this and migrating my NodeJs Lambda onto a NodeJs Sever in Docker would be easy.
The Fargate-cluster ran fine and there was just one issue to solve: How do I target a specific instance? The documentation said that I could turn the Fargate instances public but that turned out to be wrong. Then it said I could use AWS CloudMap to access a public IP but it only returns a private one.
I could only target the whole Fargate cluster but could I pass on data that would allow e.g. the Load Balancer to select a specific instance? Sticky Sessions allow keeping up the connection to a specific instance but sadly the initial connection is still chosen at random. For a multiplayer game I require players of the same game to end up on the same instance.
I could manually scale dedicated servers without fargate but AWS is frigging expensive and then you always need something costly extra like a Nat Gateway, a VPC Endpoint, etc.
Digital Ocean <3
Wait didn't I run Box of Cards on Digital Ocean in V1? Psssht!
Box of Cards V1 already ran okay-ish on Digital Ocean using Kubernetes but this was not using dedicated Servers but using a shared redis.
So why do I choose Digital Ocean this time? The traffic costs and nodes are way cheaper. The nodes can be public and I don't need any of: Load Balancer, Vpc Endpoint,Vpc Gateway, Nat Gateway. It has everything I need and a wonderful UI.
AWS is fine for bigger projects and if you stick to the core services though. There gotta be a reason I suffered through the AWS Solution Architect Associate certification!
Components
This is also very similar to the Ts-Cluster project with minor additions.
I added a MongoDb instance into the Registrar Droplet to store decks & user settings. The Registrar Droplet has by default a lot of unused space and does not need that much RAM & CPU. This works because only the Registrar Droplet requires a database and the web client forwards decks to the Websocket Droplets. Automated MongoDb backups are stored in Digital Ocean Spaces and used upon starting a Registrar.
Droplets are started using Snapshots from the Digital Ocean Registry.
Finally my Registrar manages horizontal Websocket Droplet scaling and creates/deletes dns records for dedicated server access using AWS Route 53.
I really love the Digital Ocean Api. Overall there were only 2 minor issues and I can't even remember them.