<?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: Bibek Ray</title>
    <description>The latest articles on DEV Community by Bibek Ray (@bibek10).</description>
    <link>https://dev.to/bibek10</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%2F1349295%2F280103b2-78ee-4d12-986b-5f4caded42aa.png</url>
      <title>DEV Community: Bibek Ray</title>
      <link>https://dev.to/bibek10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bibek10"/>
    <language>en</language>
    <item>
      <title>Designing Idempotent APIs</title>
      <dc:creator>Bibek Ray</dc:creator>
      <pubDate>Tue, 15 Oct 2024 06:55:05 +0000</pubDate>
      <link>https://dev.to/bibek10/designing-idempotent-apis-5c40</link>
      <guid>https://dev.to/bibek10/designing-idempotent-apis-5c40</guid>
      <description>&lt;p&gt;Say user A make an API call to the backend and there was some issue with the network and hence the call failed!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What to do ?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We ignore and move forward&lt;/li&gt;
&lt;li&gt;We pass the observed error to the use&lt;/li&gt;
&lt;li&gt;We retry on our own (better user experience hence &lt;strong&gt;preferred&lt;/strong&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;💡 Depending on what we are building, we pick one of these ways to handle&lt;/p&gt;

&lt;h1&gt;
  
  
  Failure Timeline
&lt;/h1&gt;

&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpso85w6gedr81xi9loib.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpso85w6gedr81xi9loib.png" alt="Image description" width="450" height="139"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Client Know that the request didn’t even reach the server, hence safe to retry. &lt;/p&gt;

&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqvrehifko5bg23e3n11b.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqvrehifko5bg23e3n11b.png" alt="Image description" width="436" height="125"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Failure while server is executing. Client cannot be sure of retrying.&lt;/p&gt;

&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnvzgd1da9jgysb32prne.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnvzgd1da9jgysb32prne.png" alt="Image description" width="435" height="123"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Server complete the execution but the client didn’t got the response. Client cannot be sure of retrying.&lt;/p&gt;

&lt;h2&gt;
  
  
  What could go wrong if we always retry?
&lt;/h2&gt;

&lt;p&gt;Say the API was about transferring money form A to B.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/payment/B&lt;/code&gt;  → &lt;code&gt;{ amount: 10000 }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the API call was processed successfully, but we still retried, then the amount will be re-deducted.&lt;/p&gt;

&lt;p&gt;For n retries, the amount deducted = $10000 * n&lt;/p&gt;

&lt;p&gt;Payments and other critical APIs need to ensure &lt;strong&gt;idempotency&lt;/strong&gt;, So that we safely retry.&lt;/p&gt;

&lt;h1&gt;
  
  
  Idempotent API
&lt;/h1&gt;

&lt;p&gt;Core idea of building an idempotent API is that the server should somehow know that it has seen this request earlier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;if seeing it the 1st time
        proceed
otherwise
        ignore/throw error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we are trying to achieve is &lt;strong&gt;Exactly Once Semantics.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The way to achieve this is &lt;strong&gt;Idempotence Keys&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Client first talks to the server to generate a random ID

&lt;ul&gt;
&lt;li&gt;The Id may be operation specific e.g.: “Money transfer”&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Client passes this ID along with regular payload to do actual operation&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Server checks the ID and validate if it has already handled it.&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;if already handled it
        ignore/throw error
if not
        handle it
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If client sees the error. then it retries the request same ID&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Server extracts the ID, validates, and then decides to handle it, resume or ignore&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to pass the idempotency key?
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Request Header&lt;/li&gt;
&lt;li&gt;Query Parameter&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;💡 Stripe requires client to pass Idempotency key in &lt;strong&gt;Request Header&lt;/strong&gt; &lt;strong&gt;“Idempotency-Key”&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Architecture
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Server maintains all idempotency keys in a database.&lt;/li&gt;
&lt;li&gt;When operation is successful. The server mark as checked or delete the key&lt;/li&gt;
&lt;li&gt;For every request, server checks the DB for the key&lt;/li&gt;
&lt;/ol&gt;

&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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8z9h2veq5inze8hic8z.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%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr8z9h2veq5inze8hic8z.png" alt="Image description" width="800" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;More Here : &lt;a href="https://stripe.com/blog/idempotency" rel="noopener noreferrer"&gt;&lt;strong&gt;Stripe Engineering Blog&lt;/strong&gt;&lt;/a&gt;&lt;br&gt;
Reference : &lt;a href="https://www.youtube.com/watch?v=J2IcD9FZvZU&amp;amp;ab_channel=ArpitBhayani" rel="noopener noreferrer"&gt;&lt;strong&gt;Youtube&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>webdev</category>
      <category>stripe</category>
    </item>
    <item>
      <title>Fundamental of internet networking</title>
      <dc:creator>Bibek Ray</dc:creator>
      <pubDate>Wed, 09 Oct 2024 12:38:25 +0000</pubDate>
      <link>https://dev.to/bibek10/fundament-of-internet-networking-2b1a</link>
      <guid>https://dev.to/bibek10/fundament-of-internet-networking-2b1a</guid>
      <description>&lt;h2&gt;
  
  
  Internet
&lt;/h2&gt;

&lt;p&gt;Connection of multiple computer in network to share data.&lt;/p&gt;

&lt;p&gt;Data is share in air (radio wave form) that goes to nearest tower (ISP → internet service provider)&lt;/p&gt;

&lt;h2&gt;
  
  
  IP Address
&lt;/h2&gt;

&lt;p&gt;A unique address of a device that is assign when device is connected to internet. It is given by ISP&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;IPv4&lt;/li&gt;
&lt;li&gt;IPv6&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  MAC Address
&lt;/h2&gt;

&lt;p&gt;Actual physical address of device. Help to identify device in local network like WIFI router help to identify the device with MAC address.&lt;/p&gt;

&lt;h2&gt;
  
  
  Proxy
&lt;/h2&gt;

&lt;p&gt;If A want to talk to B, then instead of directly communicating A will send message it C and C will send message to B and vice versa.&lt;/p&gt;

&lt;p&gt;So, instead of share data directly to the server, we share data to a third server (proxy) and that proxy will share data with targeted server.&lt;/p&gt;

&lt;p&gt;Used to access those server where direct interaction is banned or not allowed.&lt;/p&gt;

&lt;p&gt;It is on client side.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reverse Proxy
&lt;/h2&gt;

&lt;p&gt;This is on server side. When client request the server, the request doesn't directly goes to main server it goes to the some third server where the request is checked either it is malicious or not. And after that the request is sent to me main server.&lt;/p&gt;

&lt;p&gt;Mostly used for rate limiting.&lt;/p&gt;

&lt;h2&gt;
  
  
  VPN
&lt;/h2&gt;

&lt;p&gt;Virtual private network, this is very much similar to the proxy i.e. A will use C to send data to B, but in VPN, data that is shared is encrypted and ISP can’t read those data.&lt;/p&gt;

&lt;p&gt;Used to maintain highest level of privacy on internet and make user anonymous on internet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Protocol
&lt;/h2&gt;

&lt;p&gt;A set to rule that is used to share data on internet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Server
&lt;/h2&gt;

&lt;p&gt;A computer system that is programmed in the way that it can accept request and give response or share file based on request.&lt;/p&gt;

&lt;h2&gt;
  
  
  Client
&lt;/h2&gt;

&lt;p&gt;A normal computer system that fetch data from server.&lt;/p&gt;

&lt;h2&gt;
  
  
  ISP
&lt;/h2&gt;

&lt;p&gt;Internet service provider, this is the pillar that help to transfer data from one place to other via wires, hub, tower&lt;/p&gt;

&lt;h2&gt;
  
  
  TCP
&lt;/h2&gt;

&lt;p&gt;When data is sent to the server, the server acknowledge with the message weather the message is failed or delivered. It guarantee the data is delivered or not.&lt;/p&gt;

&lt;h2&gt;
  
  
  IP
&lt;/h2&gt;

&lt;p&gt;This is the protocol through which the an IP address (unique) is assigned to the device when it is connected to internet and once the device is disconnected from internet, the IP address is released which can be assign to some other device. &lt;/p&gt;

&lt;h2&gt;
  
  
  UDP
&lt;/h2&gt;

&lt;p&gt;Protocol that continually sent data without caring weather the data is delivered or not. It is very fast process of sending data. Generally used during video streaming.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
