How does horizontal scaling work with apps that use websockets?

Can you tell me how horizontal scaling would work, specifically for web sockets? I’m aware that long-lived connections can be difficult to load balance, so was wondering whether App Platform can handle that out of the box or whether we’d need to do something clever ourselves.

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Jon Friesen
DigitalOcean Employee
DigitalOcean Employee badge
October 6, 2020
Accepted Answer

While App Platform supports websockets, the implementation of how this communication works across horizontally scaled instances will require different behaviors based on that app requirements. App Platform provide this for you.

The key is to have some sort of correlation strategy built into your app. This might be a database such as Postgres or MySQL, or an in-memory datastore like Redis.

An example of this is a web chat system with multiple users and multiple instances using Redis pub/sub channels for correlation. When a user sends a message that message will be pushed onto a Redis pub/sub channel where all of the instances of your app are listening. The instances will push the message to the users who are connected to them via websockets.

To paraphrase, the Redis pub/sub channels act as the intermediary so it doesn’t matter which instance your user is connected to, they will messages sent from users on other instances.

Just to add to this. We have been playing with the app platform with our based Node app and it seems to be working well. Our setup, as mentioned above is using

This does as described above but takes some of the complexity out of it by abstracting the redis pub / sub portion

The key with your web sockets is to have the pub / sub maintain a listing of where each socket is connected and to run on / emits on the correct server.

Some things to be aware of though is that redis pub / sub can store some items but not all when it comes to say the full socket handshake etc. This means that custom things outside the socket like say a ‘nickname’ or other properties that may be assigned to a socket will not be readily available so you will have to store these on your own in redis or mongo etc.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel