Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server-Sent events in scalable backend

I have deployed a Java web application in Heroku.

Now, I want to change the back-end so that it can notify connected users regarding specific events. I thought I could use server-sent events to do that and the way I thought it would work is the following:

  1. When user opens up the front-end, it would establish a connection for the server-sent events.
  2. When the back-end receives such a request, it would create such a connection (basically an EventOutput) and store it somewhere along with the user's ID (let's say in a Map in memory).
  3. When a new event comes along, the back-end will find the user that needs to be notified, retrieve his connection according to his ID and send him the notification.

This works just fine when you have only one machine handling the requests.

My problem starts when I want to scale up my app and introduce more machines. Then, I cannot really store these connections in memory in one machine anymore, I need to use some centralized location. But the centralized location will need to serialize/deserialize the connection, which means that it's not the same connection anymore!

How do you usually do something like that?

like image 321
Alex Ntousias Avatar asked Jan 23 '26 21:01

Alex Ntousias


1 Answers

You could use a pub-sub solution (ex: Redis pub-sub) that is accessible to each of your dynos.

On starting, your app subscribes to the appropriate channels. When an event happens, it is published to a channel. This means all instances of your app (spread across multiple dynos) receive that event, and any of them that have SSE connections open can respond to the event.

like image 156
ack_inc Avatar answered Jan 26 '26 12:01

ack_inc