Replies: 1 comment 1 reply
-
Hi! I think you need to have a single source of truth, which in your case is Redis (or any kind of database shared between your servers). Redis set/get with pub/sub (in order to notify the other servers that something has changed) is a classic way to handle this use case. Alternatively, you may also want to look into Redis streams, depending on your use case. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi! 👋
I’m currently running an Express server with Socket.io, and now I want to add Redis to support horizontal scaling and keep multiple instances in sync.
"@socket.io/redis-streams-adapter": "^0.2.2",
"redis": "^4.7.0",
"socket.io": "^4.7.4",
SERVER CONSTRUCTOR
I’ve looked through the docs and found the basic setup, but I’m a bit confused about the best practices — especially around syncing custom state in servers.
For example, my Socket server maintains a custom this.rooms state. How would you typically keep that consistent across multiple servers? Is there a common pattern or example for this?
I’ve started pushing room metadata into Redis like this, so any server that’s out of sync can retrieve it:
If another server does not have the room data they could pull it
This kind of works, but it feels a bit hacky — I’m not sure if I’m approaching it the right way. It’s my first time building something like this, so I’d really appreciate any guidance! Especially if you could help paint the big picture in simple terms 🙏🏻
"""
Client 1 joins a room and connects to Server A. On join, Server A updates its internal state, updates the Redis state, and emits a message to everyone in the room that a new user has joined. Perfect — Redis is up to date, Server A’s state is correct, and the UI reflects the change.
But what about Server B and Server C, where other clients might be connected? Sure, the UI may still look fine if it’s relying on the Redis-driven broadcasts, but the internal state on Servers B and C is now out of sync.
How should I handle this? Do I even need to fix it? What’s the recommended pattern here?
For instance, if a user connected to Server B or C needs to access the room state — won’t that be stale or incorrect? How is this usually tackled in horizontally scaled, real-time systems using Redis?
"""
Thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions