How to Scale your WebSocket Server with DynamoDB and SNS.
Tags: webdev, aws, tutorial, javascript
Published: 2023-03-05 10:16:50

Scaling WebSockets can be a challenging task. Although there are different approaches to handle this, we will explore how to horizontally scale using AWS SNS to broadcast events across instances. This ensures that all clients receive the appropriate broadcast.

One of the main difficulties in horizontally scaling services is that if you have multiple instances of your WebSocket server running, there is a risk of users joining different servers. This is not an issue if these users do not need to be aware of each other's actions. However, as soon as multiple users need to receive each other's messages, things can become complicated.

Let’s take a chatroom as an example. Given a client sends a message to the WebSocket server, then I want all users in that room to send and receive messages to each other. Now imagine I have two instances of my WebSocket server running. John joins server one and his friend Lucy joins server two. When John sends a message “Hello world” he wants this broadcast to his friend Lucy, but because she is connected to a different server, she doesn’t receive the message. Only users on server one will have that message broadcast to them.

One way to get around this is to utilise a message service to notify all socket servers that there has been a message received. We can use AWS SNS as a messaging service here. Even better, we can trigger AWS SNS whenever there is an update to DynamoDB. Below is a diagram of how this might look.


![AWS Architecture Diagram](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3v9gstzsz6k10atizq88.png)

In the above diagram, we can see we have two clients (John and Lucy) connecting to different WebSocket servers. The WebSocket server will receive a message such as “Hello World” and then send that to our EC2 instance which will then write the message to our DynamoDB. Our DynamoDB will then stream the updated data into a Lambda function that will publish a message to our SNS service. The SNS service will then publish a message to any service that is subscribed to that topic, in our case it will be our two WebSocket services.

Each of our WebSocket service will receive a message that says John sent a message “Hello World” and will be able to emit this back out to any client that is connected to that instance. This means Lucy will receive the message "Hello world" from John even though she is on a different server.

The great thing about doing it this way is that we can easily scale our WebSocket servers horizontally without worrying about users being connected to a different instance of the server. As long as all of our WebSocket servers are subscribed to the same SNS topic, they will receive all of the broadcasted messages, regardless of which instance of the server the users are connected to. This allows us to add or remove WebSocket servers as needed to handle changes in load, without having to worry about users being disconnected or messages being missed.

Scalability is a major concern for any WebSocket server application. By leveraging AWS services like DynamoDB and SNS, we can easily horizontally scale our WebSocket servers without worrying about users being connected to different instances of the server. Using SNS to broadcast events across instances ensures that all clients receive the appropriate broadcast and that users are not disconnected or messages missed. With this approach, we can add or remove WebSocket servers as needed to handle changes in load, making our WebSocket server application highly scalable.