<?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: Dhroov Gupta</title>
    <description>The latest articles on DEV Community by Dhroov Gupta (@dhroov7).</description>
    <link>https://dev.to/dhroov7</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%2F236530%2F5d5f46d2-87b9-4f3d-958c-bfdd7b35c6b4.jpeg</url>
      <title>DEV Community: Dhroov Gupta</title>
      <link>https://dev.to/dhroov7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dhroov7"/>
    <language>en</language>
    <item>
      <title>Distributed Rate Limiter</title>
      <dc:creator>Dhroov Gupta</dc:creator>
      <pubDate>Fri, 28 Apr 2023 22:03:04 +0000</pubDate>
      <link>https://dev.to/dhroov7/distributed-rate-limiter-194e</link>
      <guid>https://dev.to/dhroov7/distributed-rate-limiter-194e</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I recently created a rate limiter library for the distributed systems that can be used to control and limit the number of requests made within a specific period of time.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Idea
&lt;/h2&gt;

&lt;p&gt;The idea behind the library is to create a token bucket that replenishes tokens at a certain rate. Each time a request is made, the library checks if there are enough tokens available in the bucket. If there are, it removes a token and allows the request. If not, it rejects the request.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;I implemented the token bucket algorithm using Redis as a distributed storage system. The library, called &lt;code&gt;dist-rate&lt;/code&gt;, takes an options object that includes the number of tokens in the token bucket, the duration for which the tokens are replenished, and an instance of the Redis client to use for distributed locking and storage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;To use the &lt;code&gt;dist-rate-limiter&lt;/code&gt; library in your project, simply install it via npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;dist-rate-limiter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use the package, create an instance of the &lt;code&gt;DistRate&lt;/code&gt; class and call the &lt;code&gt;execute()&lt;/code&gt; method with a unique ID for each request. The method returns a boolean indicating whether the request should be allowed or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;IORedis&lt;/span&gt; &lt;span class="k"&gt;from&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;DistRate&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;distrate&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;redisClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;IORedis&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Cluster&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;rateLimiter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;DistRate&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;tokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;redisClient&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;allowed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;rateLimiter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user123&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can then use the rateLimiter instance to control and limit the number of requests made to your API or server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;p&gt;The dist-rate library is available on both GitHub and npm. You can find the GitHub repository here: &lt;/p&gt;

&lt;p&gt;Github - &lt;a href="https://github.com/Dhroov7/distRate"&gt;https://github.com/Dhroov7/distRate&lt;/a&gt;&lt;br&gt;
NPM -  &lt;a href="https://www.npmjs.com/package/dist-rate"&gt;https://www.npmjs.com/package/dist-rate&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>distributedsystems</category>
      <category>redis</category>
      <category>ratelimiter</category>
    </item>
    <item>
      <title>HacktoberFest 2019</title>
      <dc:creator>Dhroov Gupta</dc:creator>
      <pubDate>Wed, 25 Sep 2019 18:52:08 +0000</pubDate>
      <link>https://dev.to/dhroov7/hacktoberfest-2019-2l2c</link>
      <guid>https://dev.to/dhroov7/hacktoberfest-2019-2l2c</guid>
      <description>&lt;p&gt;Hey Everyone,&lt;br&gt;
As we all know HacktoberFest 2019 is starting in 6 days.&lt;br&gt;
So, for new open source developers i've created a repository having easy and beginner friendly issues, so that every new comer can contribute and earn the T-shirt from DigitalOcean...isn't that great!! :)&lt;/p&gt;

&lt;p&gt;Link to the repository:&lt;br&gt;
&lt;a href="https://github.com/Dhroov7/HacktoberFest2019"&gt;Click here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>hacktoberfest</category>
      <category>opensource</category>
      <category>github</category>
    </item>
  </channel>
</rss>
