<?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: karaa</title>
    <description>The latest articles on DEV Community by karaa (@karaa1122).</description>
    <link>https://dev.to/karaa1122</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%2F3171474%2Fc91c84bf-ab01-4ac2-9407-c7184dbf96b7.jpeg</url>
      <title>DEV Community: karaa</title>
      <link>https://dev.to/karaa1122</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/karaa1122"/>
    <language>en</language>
    <item>
      <title>🚀 Mastering select_for_update() in Django: Prevent Race Conditions the Right Way</title>
      <dc:creator>karaa</dc:creator>
      <pubDate>Fri, 06 Jun 2025 20:47:31 +0000</pubDate>
      <link>https://dev.to/karaa1122/mastering-selectforupdate-in-django-prevent-race-conditions-the-right-way-4l56</link>
      <guid>https://dev.to/karaa1122/mastering-selectforupdate-in-django-prevent-race-conditions-the-right-way-4l56</guid>
      <description>&lt;p&gt;Handling concurrency in your Django applications? Then you’ll want to make friends with &lt;code&gt;select_for_update()&lt;/code&gt;. This handy ORM method helps lock database rows during a transaction, ensuring that your data stays consistent—even under high load.&lt;/p&gt;

&lt;p&gt;Let’s break it down with a real-world use case.&lt;/p&gt;

&lt;p&gt;🔍 What Is &lt;code&gt;select_for_update()&lt;/code&gt;?&lt;br&gt;
In short: it locks rows in the database until the end of the transaction, preventing other transactions from modifying or acquiring a lock on those rows.&lt;/p&gt;

&lt;p&gt;It’s especially useful for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inventory systems&lt;/li&gt;
&lt;li&gt;Bank transfers&lt;/li&gt;
&lt;li&gt;Purchasing Ticket&lt;/li&gt;
&lt;li&gt;Preventing double submission&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🛒 Example: Locking a Product Row During Checkout&lt;br&gt;
suppose you’re building an e-commerce backend where users can buy products. You need to ensure that the stock count is accurate—even if 10 users try to buy the same product at once.&lt;/p&gt;

&lt;p&gt;Here’s how to use &lt;code&gt;select_for_update()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.db import transaction
from .models import Product

def purchase_product(product_id, quantity):
    with transaction.atomic():
        product = Product.objects.select_for_update().get(id=product_id)

        if product.stock &amp;gt;= quantity:
            product.stock -= quantity
            product.save()
            return True
        else:
            return False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What’s happening here?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We start a transaction using &lt;code&gt;transaction.atomic()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We lock the product row with &lt;code&gt;select_for_update()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The row stays locked until the block exits.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;⚙️ Advanced Usage&lt;br&gt;
You can customize the locking behavior with a few extra options:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Product.objects.select_for_update(nowait=True).get(id=product_id)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;nowait=True&lt;/code&gt;: raises an error if the row is already locked.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;skip_locked=True&lt;/code&gt;: skips locked rows entirely (useful for task queues).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🧠 Final Thoughts&lt;br&gt;
Locking rows at the database level is a powerful tool—but with great power comes great responsibility. Use &lt;code&gt;select_for_update()&lt;/code&gt; wisely.&lt;/p&gt;

&lt;p&gt;Want to take your Django database handling to the next level? Start by locking smarter. 🔒&lt;/p&gt;

&lt;p&gt;💬 Got questions or use cases? Drop them in the comments—let’s nerd out about transactions and Django internals.&lt;/p&gt;

</description>
      <category>django</category>
      <category>api</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
