<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prakash Bhattarai</title>
    <description>The latest articles on DEV Community by Prakash Bhattarai (@mrbprakash06).</description>
    <link>https://dev.to/mrbprakash06</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2776360%2F5b4d2e51-8cd5-4aea-9743-ec832dfd9044.png</url>
      <title>DEV Community: Prakash Bhattarai</title>
      <link>https://dev.to/mrbprakash06</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mrbprakash06"/>
    <language>en</language>
    <item>
      <title>Scaling WebSocket</title>
      <dc:creator>Prakash Bhattarai</dc:creator>
      <pubDate>Fri, 15 May 2026 05:08:41 +0000</pubDate>
      <link>https://dev.to/mrbprakash06/scaling-websocket-1dfa</link>
      <guid>https://dev.to/mrbprakash06/scaling-websocket-1dfa</guid>
      <description>&lt;p&gt;WebSocket provides low-latency, bidirectional communication between clients and servers. It is a widely used solution for building real-time systems. Once a WebSocket connection is established, the underlying TCP connection remains open until either the client or the server closes it, allowing both sides to exchange messages continuously throughout the lifetime of the connection.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fceljw9x0c10oriwvqk84.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fceljw9x0c10oriwvqk84.png" alt="WebSocket Connection" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As systems grow, the challenge of scaling WebSocket infrastructure becomes important. Broadly, there are two approaches: vertical scaling and horizontal scaling.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rw36o223mxbsdretzef.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0rw36o223mxbsdretzef.png" alt="Vertical vs Horizontal Scaling" width="800" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In vertical scaling, the resources of a single server — CPU, RAM, and network bandwidth — are increased so it can handle more WebSocket connections. This approach works well for small to medium-scale systems, but only to a certain extent. A single machine cannot be scaled indefinitely, and relying on one server also introduces a single point of failure in systems where high availability is critical.&lt;/p&gt;

&lt;p&gt;Horizontal scaling, on the other hand, increases the number of WebSocket servers. This approach improves redundancy and allows the system to scale using inexpensive commodity servers. However, horizontally scaling WebSocket servers introduces additional challenges.&lt;/p&gt;

&lt;p&gt;To understand the problem, consider a multiplayer game with two WebSocket servers: A and B. Initially, players P1, P2, and P3 are connected to server A, and game events flow normally between them. Now suppose P1 disconnects and reconnects, but this time the load balancer routes P1 to server B instead of server A. How will P1 continue receiving events generated by P2 and P3?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fieqemiengaksp49xe6hr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fieqemiengaksp49xe6hr.png" alt="Scaling Issue" width="800" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It turns out, the problem can be solved using a shared message broker such as Redis Pub/Sub. When a WebSocket server receives an event, it publishes the event to Redis. Other WebSocket servers subscribed to the same channel receive the event and forward it to their connected clients. In this example, when P2 or P3 sends a game event to server A, server A publishes the event through Redis Pub/Sub. Server B then receives the event and forwards it to P1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96m7dqxj6g2cb7hawy3o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96m7dqxj6g2cb7hawy3o.png" alt="Redis Pub/Sub Forwarding Events" width="800" height="928"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One important limitation of Redis Pub/Sub is that messages are not persisted. If Redis or a subscriber becomes temporarily unavailable, some events may be lost. Because of this, WebSocket systems are often designed so that occasional dropped events do not break the application. In systems where message loss is unacceptable, additional application-level mechanisms are used, such as acknowledgments, durable queues, event replay, and message persistence.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://socket.io/docs/v4/tutorial/step-9" rel="noopener noreferrer"&gt;https://socket.io/docs/v4/tutorial/step-9&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://redis.io/docs/latest/develop/pubsub/" rel="noopener noreferrer"&gt;https://redis.io/docs/latest/develop/pubsub/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=gzIcGhJC8hA&amp;amp;t=175s" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=gzIcGhJC8hA&amp;amp;t=175s&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>websocket</category>
      <category>systemdesign</category>
      <category>redis</category>
      <category>architecture</category>
    </item>
  </channel>
</rss>
