<?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>Pub/Sub</title>
      <dc:creator>Prakash Bhattarai</dc:creator>
      <pubDate>Fri, 12 Jun 2026 07:43:33 +0000</pubDate>
      <link>https://dev.to/mrbprakash06/pubsub-31ke</link>
      <guid>https://dev.to/mrbprakash06/pubsub-31ke</guid>
      <description>&lt;p&gt;Pub/Sub, also known as Publisher-Subscriber, is a communication model where services exchange messages through an intermediary service called a broker. The publisher publishes a message to a topic managed by the broker. Subscribers receive and consume messages from that topic without knowing anything about the producer of the message.&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%2Fmckch9bmg4669r508api.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%2Fmckch9bmg4669r508api.png" alt="Pub/Sub" width="521" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The core components of the Pub/Sub communication model are as follows:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Producers:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Producers create and publish messages to a topic. For example, in an e-commerce website, the producer may be the order service, which publishes order-related messages to the appropriate topic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Subscribers:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Subscribers are services that receive and consume messages from a topic managed by the broker. They process messages without knowing anything about the producers of those messages. For example, in an e-commerce website, the payment service might be the consumer or subscriber that receives order messages and processes payments accordingly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Broker:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The broker manages topics and handles the receiving and delivery of messages within those topics. The message itself contains the data sent by the producer, which is then used by the subscriber.&lt;/p&gt;

&lt;p&gt;The advantages of the Pub/Sub mechanism are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reduced coupling:&lt;/strong&gt; It reduces coupling between the producer and subscriber. The publisher does not need to have direct knowledge of the subscriber, and vice versa. This makes it easier to add new subscribers without changing the publisher itself.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Improved scalability:&lt;/strong&gt; We can increase the number of subscribers when more messages are produced. If needed, we can also independently scale the producer.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Low-latency communication:&lt;/strong&gt; With proper broker configuration, payload size, network conditions, and processing time, the system can support very low-latency message delivery.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The disadvantages are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Increased complexity:&lt;/strong&gt; With the introduction of a broker, there is a whole new system to manage alongside the producers and consumers. This adds another level of complexity to the system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;More difficult debugging:&lt;/strong&gt; Since messages are produced and consumed independently, it becomes more difficult to trace the flow of messages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Message ordering challenge:&lt;/strong&gt; Depending on the broker and its configuration, messages may not always be processed in a strictly sequential order.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The common use cases of Pub/Sub are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Real-time messaging and chat applications&lt;/li&gt;
&lt;li&gt;Distributed systems and microservices&lt;/li&gt;
&lt;li&gt;Event-driven architectures&lt;/li&gt;
&lt;li&gt;Stream processing and pipelines&lt;/li&gt;
&lt;li&gt;Notification and background processing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The common Pub/Sub brokers are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Apache Kafka&lt;/li&gt;
&lt;li&gt;Redis Pub/Sub&lt;/li&gt;
&lt;li&gt;Google Cloud Pub/Sub&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Code snippets given below demonstrate the publisher which publishes count message to the topic &lt;em&gt;messages&lt;/em&gt; managed by &lt;strong&gt;Redis&lt;/strong&gt; every 200 ms and the subscribers which consumes message and prints count to the console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// publisher.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Redis&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ioredis&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;redis&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Redis&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;messages&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// subscriber.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Redis&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ioredis&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;redis&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Redis&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;messages&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="nx"&gt;redis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;messages&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;parsed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parsed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;run&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;References&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://docs.cloud.google.com/pubsub/docs/overview" rel="noopener noreferrer"&gt;https://docs.cloud.google.com/pubsub/docs/overview&lt;/a&gt;&lt;br&gt;
&lt;a href="https://redis.io/glossary/pub-sub/" rel="noopener noreferrer"&gt;https://redis.io/glossary/pub-sub/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=FMhbR_kQeHw" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?v=FMhbR_kQeHw&lt;/a&gt;&lt;/p&gt;

</description>
      <category>pubsub</category>
      <category>redis</category>
      <category>systemdesign</category>
      <category>distributedsystems</category>
    </item>
    <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>
