Box of Cards V2
The AWS serverless era — Lambda, DynamoDB and CloudFormation carrying a live card game, and the limitations that eventually ended it. Part of the Box of Cards work.
TS · AWS · Docker · React · NodeJs · AWS CDK · Cloudformation · ChatGPT · AWS Lambda · ApiGateway · Appsync · DynamoDb · S3 · Route53 · Cloudwatch · Cloudfront · GraphQl · Apollo · Websockets · Redux Toolkit · RTK Query · MaterialUi · Jest · Affinity Designer · Affinity Photo
Why V2?
The previous version ran with Kubernetes and a Redis cluster.
While the system worked in test runs, load tests showed that the synchronization of the nodes via Redis was a bottleneck. Despite automatic deployments, maintenance and especially logging proved to be insufficient.
A project with migration parts to AWS Serverless was pending and so I decided to test this on Box of Cards.
This is possible because the card game does not require fast latencies and can also cope with occasional cold starts. First calculations showed that AWS serverless should work out in terms of costs, although the correct parameterization is difficult to estimate. Additional findings on this later.
Short tutorial
There is now an interactive tutorial on the home page and some other pages. One of two answers can be inserted into the sentence. Words in the first answer are interchangeable.
Tech Stack
The technologies used have changed significantly compared to V1. The code was easy to migrate to AWS Lambda. I initially opted for AppSync (with Apollo and GraphQL) for the backend API because it enables websocket subscriptions. Only later did I realize that these subscriptions can only respond to their own topic. In addition, no MQTT protocol is supported. Because of this, I now need a websocket api gateway that maintains websocket connections and triggers Lambdas.
The problem is that this gateway and the sockets do not provide any cache and all links from socketID to userID must be persisted in the DynamoDB. Also the Lambda Cache would not be suitable. I have tried many solutions, but in the end, increased DynamoDB costs remain. This is particularly annoying because at least 1KB is always charged, even if I only read a few bytes.
For the authentication I disconnected from Google-Firestore and tried a few things. AWS Cognito is out because it has compatibility issues with AWS Cloudformation and setting up OAuth was a nightmare. Afterwards I tried Keycloak and although I am a big fan of the interface and documentation, it remains overkill for my purposes. Now there is a 3rd party login via Discord and others will follow later.
The rest went very well. The website is in S3. GameStates and decks are persisted in DynamoDB. The entire application is deployed with AWS Cdk (Cloudformation). There is an AWS root account with domains and permissions and 3 accounts underneath for dev, beta and live.
All tests are Jest end-to-end tests on a dev environment. On the beta environment I test with friends. ChatGPT is now a good time saver when writing tests and analyzing bugs.
Components
As a developer, I can deploy all stacks and test data with Cloudformation with one command. As an end user, I call up the domain and the DNS entries stored in Route 53 are used to resolve to Cloudfront. Cloudfront then forwards to the S3 in which the static web application is located. Cloudfront usually returns the now downloaded web page now accesses AWS Route53 entries and Cloudfront again for the backend, but is forwarded to the AWS Api gateway for websockets and to AWS AppSync for HTTP requests. Caching in Cloudfront shortens these paths.
As a developer, I usually only access Cloudformation for deployment and Cloudwatch for logging and alerts. Occasionally I still look at data in DynamoDB when debugging.
UI adjustments
The original design didn't feel polished yet, so I tried out some other styles. Drop shadows can be used to create nice pop effects and these are now applied to many interactive elements. Responsiveness still works on very low resolutions like 300 x 640.
The Lobby Before
This is how it looks from time to time when core components have been revised but other components have not yet been updated.
The Lobby, Tidied
This is what it will look like when the GameLobby component has been tidied up. It's not final yet, but definitely better than before.
Responsiveness challenge
There are many tabs on the game page, and displaying them in several rows doesn't look good and also causes problems with the maximum height. Therefore, a menu is used here to summarize tabs on narrow screen widths.
History and Layout Tests
The best cards of the last rounds can be viewed in the History tab. First I used grids, but they weren't flexible enough. So I switched to flex boxes.
Cards Scale, Rows Follow
At first I had scrollable cards, but since cards themselves are in a scrollable container, this makes the operation error-prone. Now cards have a minimum size and scale in height with their content. This means that Flex-Box alone is no longer sufficient, as the row size must be based on the largest element. Gamestate-dependent, the upper or lower card container is also given more height. In this GameState, the winner is to be selected, so the upper container has more height available. The remaining height or minimum height goes to the lower container.
Responsiveness in action
The responsive behavior is made possible by a variety of design decisions. Swapping elements into tabs, hiding the tab container, merging the navbar elements into a burger menu and the flex behavior of the card container. There is now a lot of logic in the card containers in particular, as CSS does not enable this level of responsiveness. The problem is that it is not known in advance how much space a text requires and how many line breaks and thus text rows there are.
After rendering the elements, I display the dimensions of the containers and then use the window sizes, map sizes and which map container has priority to determine the optimal layout. Even if 5 cards would fit next to each other in some places, I prefer to display them in 2 rows. All this can make for minimal flickering as it requires 2 renders and heights change, but without writing a completely custom text renderer, it doesn't get any better.
I think it's pretty.
Avatars
Single names are pretty boring and I have to abbreviate longer names for responsiveness. In addition, I need space-saving buttons to remove players and a place where I can display player-related info icons.
Avatar buttons are the optimal solution here. I can display: is host, is cardinal, is ready, is expected and can be thrown out.
AI player
AI players all look like robots and are at least as funny as real players thanks to excellent AI. It would be nice if they could syntactically match answer cards to the sentence correctly, but there are no cheap APIs or good open source solutions for this yet.
KIs can be added or removed at any time and the gamestate fixes itself by making decisions retroactively. I added them primarily because testing with 2 browser windows is tedious.
The dark mode is now also integrated.
Backend scripts
I like to have all my scripts in one place; in this case in the package.json. There are now tools that abstract such scripts better, but so far I haven't liked any of them. These are mostly deployment scripts for the backend and the web application. There are so many scripts due to the different runtime environments.
Lambda Quota
By default, only 10 Lambdas can run in parallel. I noticed this because my end-to-end tests failed randomly at some point. Increases must be requested with longer waiting times.
Even more tests
I can hardly express how much time my tests have saved me. I have written functions that bring my GameState into a certain state so that I can then run it in a targeted manner. This reduces my test sizes enormously. All objects and their fields are based on indices such as: userName1, userName2, etc.. This allows me to test everything at any scale.
Based on this, there are functions that allow me to create a game, add players, start the game and reach a certain gamestate. This allows the test to focus on testing, for example, the selection of a hand card and the transition to the next gamestate.
Bugs
Bugs were at least entertaining — here every field resolved to the first answer, so each sentence read like a broken record.
Many, many tests
There are now thousands of tests, and if I run them all at once, there are still a handful that fail. Two tests either don't clean up sufficiently or don't clean up in time, and as a result subsequent tests fail, but then clean up well again. Individually, the tests work properly and there are at least todos on them.
Cloudformation UI
This is what the application looks like in the AWS UI for Cloudformation.
DynamoDB UI
Being able to analyze objects interactively helped a lot when debugging the errors. However, I am not convinced by DymamoDb per se. The syntax is error-prone and the available clients for DynamoDB make it neither easier nor safer.
Conclusion
Serverless is a wonderful technology, but due to the way it is offered by AWS, it is only suitable for my use case to a limited extent. I often ended up in a dead end because an essential AWS feature still has an open ticket from 2018. This is taking on such abstruse proportions that the community has recreated Appsync and Apollo in AWS Lambda. The core services like AWS S3, DynamoDb, Lambda Cloudfront, Route53 work well. I will avoid many other services.
I will no longer run larger applications serverless in the future.I'm looking forward to building something with dedicated servers and in-memory caches again. A project that ties me less to cloud providers with proprietary solutions.