<?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: Varun Garg</title>
    <description>The latest articles on DEV Community by Varun Garg (@varungarg).</description>
    <link>https://dev.to/varungarg</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%2F1193959%2F87a432a9-3454-40ee-91fc-94f405f15c96.jpeg</url>
      <title>DEV Community: Varun Garg</title>
      <link>https://dev.to/varungarg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/varungarg"/>
    <language>en</language>
    <item>
      <title>From Frustration to Fascination: The Truth About Double Booking Problem</title>
      <dc:creator>Varun Garg</dc:creator>
      <pubDate>Mon, 27 May 2024 15:20:21 +0000</pubDate>
      <link>https://dev.to/varungarg/from-frustration-to-fascination-the-truth-about-double-booking-problem-3apb</link>
      <guid>https://dev.to/varungarg/from-frustration-to-fascination-the-truth-about-double-booking-problem-3apb</guid>
      <description>&lt;p&gt;In 2019, I remember eagerly awaiting the release of "Avengers: Endgame." Like millions of fans around the world, I wanted to experience the epic conclusion to the Marvel saga on the opening night. &lt;br&gt;
I remember sitting at my computer, the clock ticking down to the exact moment tickets went on sale. As soon as they did, I jumped into action, quickly selecting two perfect seats – center row, not too close, not too far. &lt;strong&gt;Seats H10, H11&lt;/strong&gt;! My heart was racing as I clicked "confirm," imagining the triumphant moment when I'd have the tickets in hand.&lt;/p&gt;

&lt;p&gt;But instead of a confirmation screen, I was greeted with an error message: &lt;strong&gt;&lt;em&gt;"Those seats are no longer available."&lt;/em&gt;&lt;/strong&gt; My excitement turned to frustration. How could this happen so quickly? 5 years later, after a deep dive into databases, I finally found my 'technical' answer! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExc2N4cHVrd245OHpwZTZ6dHN5dWphOHFnY2N4ODNkemNhY3hmdzgweiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/ioXhRXap3qODZaPsfU/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExc2N4cHVrd245OHpwZTZ6dHN5dWphOHFnY2N4ODNkemNhY3hmdzgweiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/ioXhRXap3qODZaPsfU/giphy.gif" width="480" height="267"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;Well, as frustrating as it might be, the booking websites employ clever technical tricks to prevent double bookings and ensure a smooth user experience. Let me try to explain in with the help of an analogy! &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Analogy Time&lt;/strong&gt; - Imagine booking a movie ticket as a high-stakes race against the other eager viewers. Whoever 'runs' through the process and clicks "confirm" first secures the perfect seat. &lt;br&gt;
The problem is that with lightning-fast internet and multiple users vying for the same spot, things can get down to the wire. This is where booking websites step in as the race officials, having a powerful tool in the toolkit called &lt;strong&gt;database locks&lt;/strong&gt; to ensure a fair and smooth experience.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;How do we they lock down our seats??!!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Think of your ideal cinema seat – the one with ample legroom and an unobstructed view (H10,H11) in Audi 5. &lt;br&gt;
When you click to select seats H10 and H11, here's what unfolds in the digital realm:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The MVP (SELECT FOR UPDATE):&lt;/strong&gt; When you proceed to checkout, the website initiates a &lt;strong&gt;transaction&lt;/strong&gt; with a special database query which goes like-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT * FROM audi5 WHERE seat_id IN ('H10', 'H11') FOR UPDATE; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This query acts like magic, targeting specific rows (seats H10 and H11) within the audi5 table stored in the database. Notice the &lt;em&gt;SELECT FOR UPDATE&lt;/em&gt; clause at the beginning. This is what instructs the database to lock the rows retrieved by the query, but it's important to understand that this locking attempt might not always be successful.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Verifying the Lock and Retrieving Information:&lt;/strong&gt; The SELECT FOR UPDATE not only retrieves information about the seats' availability (H10 and H11) but also attempts to lock those specific rows in the audi5 table. This lock prevents other users from seeing H10 and H11 as available while you finalize your purchase. Imagine it like putting up a temporary "reserved" sign on the digital records of seats H10 and H11.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmh3bGR0eHcxbHJtYmxqemVpajU5Nm1ic2J5ZTF2OWlxM2o4dHgzYiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3ohjUOFS6bU1oyiAM0/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmh3bGR0eHcxbHJtYmxqemVpajU5Nm1ic2J5ZTF2OWlxM2o4dHgzYiZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/3ohjUOFS6bU1oyiAM0/giphy.gif" width="480" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Success or Sorry? (Depending on Lock Acquisition):&lt;/strong&gt; Crucially, the information retrieved by the SELECT FOR UPDATE also indicates whether the lock acquisition was successful. This is because another user might have grabbed those seats (and successfully acquired the lock) before your query finished executing in the database!&lt;/p&gt;

&lt;p&gt;Now let us say that you made it to the checkout page! What happens here? Remember I mentioned that the database initiates a transaction?&lt;/p&gt;

&lt;p&gt;Well, Transactions guarantee that everything goes smoothly.They act like mini-programs that ensure data consistency within the database. When you confirm your booking, the website initiates a transaction in a 2 step manner:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;It leverages the information retrieved by the SELECT FOR UPDATE query. This information includes both the availability of the seats (H10 and H11) and whether the lock acquisition was successful.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If the lock acquisition was successful and the seats are still free, the transaction updates the database to mark H10 and H11 as booked, associating them with your ticket purchase.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;However, if something goes wrong during checkout (like the lock expiring, or the lock acquisition failing), the entire transaction is rolled back. This releases any lock attempts on H10 and H11, and the database remains unchanged, ready for someone else to try booking those seats.&lt;/p&gt;

&lt;p&gt;For the geeks reading this, the final query will look something like this in the database (Although a lot of this is done through the code in the server)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;START TRANSACTION;  -- Begin the transaction

SELECT * FROM audi5 WHERE seat_id IN ('H10', 'H11') FOR UPDATE;  -- Lock seats H10 and H11 (may fail due to timeout or other transactions)

-- Check if lock acquisition was successful and seats are available (addresses query 1)
IF @@ROWCOUNT = 2 AND available IN (1, 1) THEN  -- Check both rows returned and availability is true (1)

  UPDATE audi5
  SET available = 0,  -- Mark seats as booked
      booked_by = USER_ID()  -- Assign booked_by to current user
  WHERE seat_id IN ('H10', 'H11');

  COMMIT;  -- Commit the transaction if successful
ELSE
  ROLLBACK;  -- Rollback if lock acquisition fails or seats unavailable (addresses query 2)
END IF;

-- Additional logic can follow here, like processing payment or finalizing ticket confirmation`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Ever wonder why some websites give you a time limit to complete your ticket purchase?  This isn't just about creating a sense of urgency (although it can do that too). It's also a technical safeguard (by &lt;strong&gt;rolling back&lt;/strong&gt; the above-mentioned transactions) to prevent seats from being held indefinitely by someone who might not complete their purchase.  This time limit ensures that seats become available again quickly for others who are eager to snag them!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExeGsxMGRrZWtsZjFhYXFlM2I4cHMyMGJ6ZmwxMmFhY3lmZjgzd2xpbCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/n8SkNR77udWlG/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExeGsxMGRrZWtsZjFhYXFlM2I4cHMyMGJ6ZmwxMmFhY3lmZjgzd2xpbCZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/n8SkNR77udWlG/giphy.gif" width="245" height="170"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, the next time you encounter the dreaded "seats no longer available" message, remember, it's not bad luck or slow internet. It's a complex dance between you, other eager moviegoers, and the booking website's sophisticated system, all working behind the scenes to ensure there are no double bookings and it is a smooth and frustration-free experience. &lt;br&gt;
Have you ever been in a similar situation? Let me know your thoughts!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bonus:&lt;/strong&gt;&lt;br&gt;
You just might be also wondering how different ticketing platforms (Paytm, BookMyShow etc) show the same available seats? It's all thanks to &lt;strong&gt;channel managers&lt;/strong&gt;! These act like middlemen, constantly checking seat availability across theaters and using special protocols to ensure all systems are in sync. This eliminates conflicts and guarantees the info you see is up-to-date. More on them later! &lt;/p&gt;

</description>
      <category>database</category>
      <category>webdev</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Who/What is a backend engineer?</title>
      <dc:creator>Varun Garg</dc:creator>
      <pubDate>Wed, 25 Oct 2023 18:53:19 +0000</pubDate>
      <link>https://dev.to/varungarg/whowhat-is-a-backend-engineer-262p</link>
      <guid>https://dev.to/varungarg/whowhat-is-a-backend-engineer-262p</guid>
      <description>&lt;p&gt;&lt;strong&gt;Stranger:&lt;/strong&gt; Hey Varun, what do you do for a living? &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; Well, I work as a backend engineer! &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stranger:&lt;/strong&gt; Sounds fascinating! What is a backend engineer?!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; Ah, you know those wizards behind the digital curtain making sure everything runs smoothly? That's me, weaving code spells to keep the website enchanting and the servers spellbound!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stranger:&lt;/strong&gt; What a weirdo! I do not understand! &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; &lt;em&gt;sigh&lt;/em&gt;. I work as a software engineer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stranger:&lt;/strong&gt; Got it! Could have said this in the first place! &lt;/p&gt;

&lt;p&gt;The above is just an age-old example of the problems I and many other backend engineers or software engineers face while interacting with people unfamiliar with the IT industry.&lt;/p&gt;

&lt;p&gt;And honestly, can you blame them? If I meet a 'Hepatologist' and they try to explain what exactly they do with the liver, it will take me a million years to understand! &lt;/p&gt;

&lt;p&gt;So I thought, why don't I answer and document this question (&lt;strong&gt;What is a backend engineer?&lt;/strong&gt;) with my favorite &lt;strong&gt;'Restaurant analogy'&lt;/strong&gt; ?! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/WsNbxuFkLi3IuGI9NU/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/WsNbxuFkLi3IuGI9NU/giphy.gif" alt="Alt text of image" width="480" height="400"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;The Restaurant Analogy:&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When a &lt;em&gt;&lt;u&gt;customer&lt;/u&gt;&lt;/em&gt; places an &lt;em&gt;&lt;u&gt;order&lt;/u&gt;&lt;/em&gt; after looking at the &lt;em&gt;&lt;u&gt;menu&lt;/u&gt;&lt;/em&gt;, the &lt;em&gt;&lt;u&gt;waiter&lt;/u&gt;&lt;/em&gt; takes it to the &lt;em&gt;&lt;u&gt;kitchen&lt;/u&gt;&lt;/em&gt; and gives it to the &lt;em&gt;&lt;u&gt;chefs&lt;/u&gt;&lt;/em&gt;. The chefs then prepare the food according to the customer's specifications. Once the &lt;em&gt;&lt;u&gt;food&lt;/u&gt;&lt;/em&gt; is ready, the chefs send it back to the waiter, who then delivers it to the customer.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the above scenario:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;&lt;u&gt;customer&lt;/u&gt;&lt;/em&gt; is the web browser.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;&lt;u&gt;waiter&lt;/u&gt;&lt;/em&gt; is the front-end developer.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;&lt;u&gt;chef&lt;/u&gt;&lt;/em&gt; is the backend engineer.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;&lt;u&gt;kitchen&lt;/u&gt;&lt;/em&gt; is the database.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;&lt;u&gt;menu&lt;/u&gt;&lt;/em&gt; is the API.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;&lt;u&gt;order&lt;/u&gt;&lt;/em&gt; is the HTTP request.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;&lt;u&gt;food&lt;/u&gt;&lt;/em&gt; is the HTTP response.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;&lt;u&gt;lights&lt;/u&gt;, &lt;em&gt;&lt;u&gt;furniture&lt;/u&gt;&lt;/em&gt;, and &lt;u&gt;utensils&lt;/u&gt;&lt;/em&gt; are the servers!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To elaborate further, the backend engineer, akin to a &lt;em&gt;chef&lt;/em&gt;, is in charge of the kitchen (database), managing and preparing the ingredients (data) to fulfill orders (HTTP requests) from the customers (web browsers). They ensure a smooth operation of the kitchen equipment (servers), guaranteeing efficient delivery of the prepared dishes (HTTP responses) to the customers.&lt;/p&gt;




&lt;p&gt;Sounds good?! Good! Enlightened with the above analogy, let us reimagine our initial conversation and add some grandeur to it!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; Well, I work as a backend engineer!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stranger:&lt;/strong&gt; Sounds fascinating! What is a backend engineer?!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Me:&lt;/strong&gt; Imagine a restaurant. I'm like the chef who designs the kitchen, ensures the ingredients are always fresh and ready, and coordinates with the front of the house to ensure every dish comes out perfectly, even if you only see the waiter bringing it to your table!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stranger:&lt;/strong&gt; Wow! That sounds awesome! &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Me&lt;/strong&gt;: &lt;br&gt;
&lt;a href="https://i.giphy.com/media/cXblnKXr2BQOaYnTni/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/cXblnKXr2BQOaYnTni/giphy.gif" alt="Alt text of image" width="480" height="400"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;One of the practices in my life is to use relatable analogies to describe key situations! Especially when it comes to explaining what I do for a living! &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Relatable comparisons build bridges, making complex concepts understandable, and are a great segue to strike an insightful conversation! &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What is your definition of a backend engineer? Let me know your thoughts! &lt;/p&gt;

</description>
      <category>backend</category>
      <category>beginners</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
