<?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: Abdulrahman S.</title>
    <description>The latest articles on DEV Community by Abdulrahman S. (@abdulrahman1s).</description>
    <link>https://dev.to/abdulrahman1s</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%2F533590%2F978babd9-8994-471c-aebd-5efce4f29cab.jpg</url>
      <title>DEV Community: Abdulrahman S.</title>
      <link>https://dev.to/abdulrahman1s</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abdulrahman1s"/>
    <language>en</language>
    <item>
      <title>How discord manage 300M socket connection</title>
      <dc:creator>Abdulrahman S.</dc:creator>
      <pubDate>Fri, 15 Jul 2022 19:56:29 +0000</pubDate>
      <link>https://dev.to/abdulrahman1s/how-discord-mange-300m-socket-connection-1ggm</link>
      <guid>https://dev.to/abdulrahman1s/how-discord-mange-300m-socket-connection-1ggm</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdiscord.com%2Fassets%2Fff41b628a47ef3141164bfedb04fb220.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdiscord.com%2Fassets%2Fff41b628a47ef3141164bfedb04fb220.png" alt="discord-banner"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;Discord, the popular platform famous for its complex system of guilds and detailed permissions. how they managed to handle this amount of websocket connections with permissions in mind?&lt;/p&gt;

&lt;h3&gt;
  
  
  Publish–subscribe Pattern (also known as Pub/sub)
&lt;/h3&gt;

&lt;p&gt;The Pubsub pattern is a &lt;a href="https://wikipedia.org/wiki/One-to-many_(data_model)" rel="noopener noreferrer"&gt;one-to-many&lt;/a&gt; relationship that allows to publish specific event/data to multiple subscribers/users at the same time.&lt;br&gt;
This pattern has already been implemented in many technologies such as &lt;a href="https://redis.io/docs/manual/pubsub" rel="noopener noreferrer"&gt;redis&lt;/a&gt; and &lt;a href="https://www.postgresql.org/docs/current/sql-notify.html" rel="noopener noreferrer"&gt;postgres&lt;/a&gt; and much others.&lt;/p&gt;

&lt;h3&gt;
  
  
  Permissions
&lt;/h3&gt;

&lt;p&gt;The magic behind discord it's the permission system&lt;br&gt;
Most of us may think of permissions as a list of strings.&lt;br&gt;
But actually they are numbers exactly &lt;code&gt;64 unsigned integers&lt;/code&gt;&lt;br&gt;
They use a data structure called &lt;a href="https://wikipedia.org/wiki/Bit_field" rel="noopener noreferrer"&gt;bitfields&lt;/a&gt;&lt;br&gt;
It's powerful in terms of performance&lt;/p&gt;

&lt;h3&gt;
  
  
  How Discord uses these technologies?
&lt;/h3&gt;

&lt;p&gt;When you connect to their websocket server, they create a pubsub client and this client subscribes to your friends, servers, groups and other important IDs to send you back updates&lt;br&gt;
But before sending you back anything they check your permissions with the target of X.&lt;/p&gt;

&lt;h3&gt;
  
  
  The tricks to handle the load
&lt;/h3&gt;

&lt;p&gt;Discord doesn't simply use the mentioned technologies alone&lt;br&gt;
Some of the important tricks they currently use are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://wikipedia.org/wiki/MessagePack" rel="noopener noreferrer"&gt;MessagePack&lt;/a&gt; for payload compression&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://wikipedia.org/wiki/Redis" rel="noopener noreferrer"&gt;Redis&lt;/a&gt; for cache and pubsub clients&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://wikipedia.org/wiki/Snowflake_ID" rel="noopener noreferrer"&gt;Snowflake&lt;/a&gt; for generating IDs&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Open-source projects that follow discord philosophy:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/itchatapp/api" rel="noopener noreferrer"&gt;ItChat&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/revoltchat/backend" rel="noopener noreferrer"&gt;Revolt&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/fosscord/fosscord-server" rel="noopener noreferrer"&gt;Fosscord&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>discord</category>
      <category>algorithms</category>
      <category>programming</category>
      <category>bigdata</category>
    </item>
  </channel>
</rss>
