Question

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 socket.io based Node app and it seems to be working well. Our setup, as mentioned above is using socket.io-redis

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.

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.