<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Edem Tsalah on Medium]]></title>
        <description><![CDATA[Stories by Edem Tsalah on Medium]]></description>
        <link>https://medium.com/@edem.tsalah?source=rss-4900c56e6090------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*j4nx_CzaAmKrQ4m2.</url>
            <title>Stories by Edem Tsalah on Medium</title>
            <link>https://medium.com/@edem.tsalah?source=rss-4900c56e6090------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 06:47:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@edem.tsalah/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A race condition we created by just missing a where condition in an upsert]]></title>
            <link>https://medium.com/@edem.tsalah/a-race-condition-we-created-by-just-missing-a-where-condition-in-an-upsert-5919c4cbfb89?source=rss-4900c56e6090------2</link>
            <guid isPermaLink="false">https://medium.com/p/5919c4cbfb89</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[rabbitmq]]></category>
            <category><![CDATA[mongodb]]></category>
            <dc:creator><![CDATA[Edem Tsalah]]></dc:creator>
            <pubDate>Fri, 24 Sep 2021 02:08:41 GMT</pubDate>
            <atom:updated>2021-09-24T07:39:11.328Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Members of the battalions from Japan, Bangladesh, Ghana and Nepal serving with the UN Mission in South Sudan (UNMISS) take part in the first round of a tug of war contest, with the last round to take place on 24 October, United Nations Day. A marathon is also planned for UN Day. UN Photo/JC McIlwaine 22 October 2014" src="https://cdn-images-1.medium.com/max/799/1*AiHzLjLqOaaQ20VrzhXnyg.jpeg" /></figure><p>A race<strong> </strong>condition<strong> </strong>is the condition of an electronics, software, or other system where the system’s substantive behaviour is dependent on the sequence or timing of other uncontrollable events. (<a href="https://en.wikipedia.org/wiki/Race_condition">source</a>)</p><p>The application in this case, was a backend system for getting posts, comments from facebook, twitter and instagram for accounts connected through the frontend of my employer’s web application. These posts and comments were then converted to reviews for our customers who could embed them using a widget on their online shops on sites like <a href="https://www.shopify.com/">Shopify</a>, <a href="https://tictail.com/">Tictail</a> and <a href="https://woocommerce.com/">Woocommerce</a>.</p><p>The language of choice was python (<a href="https://www.python.org/">CPython</a>) and to make it scale, avoiding the python <a href="https://realpython.com/python-gil/">GIL</a>, we decided to use <a href="https://www.rabbitmq.com/">Rabbitmq</a> to queue requests for fetching data from the social media accounts our clients had connected to the application. We would then fetch the data using several background tasks and the data was then stored in <a href="https://www.mongodb.com/">MongoDb</a>.</p><p>The shared resource in this application that caused the race condition was the data from the social media accounts we were storing. Well to be more accurate it was both the social media accounts and the data from those social accounts. My employer wanted to cater for the scenario where there were multiple managers of an online shop. These managers could individual create an account on our application and connect the same social media accounts.</p><p>These accounts where queued independently, and when the data was being written to storage we would use an upsert statement to ensure that there were no duplicate data in the social media data collections. And it’s at this point that the race condition occurs. In the upsert where condition, we forgot to add the id of the account for which we were writing the data but in the data being written, there was a field that indicated the account the data being written belong to.</p><p>This led to the funky behaviour of disappearing data from the account’s dashboard. We would store the id of a post and check if it was still in the database after it miraculously disappeared from the customer’s dashboard, only to realise it was still there.</p><p>We kept pondering over this issue for over a week and we couldn’t identify the source of the bug. One day while off from work and driving around town, it just occurred to me that we might be missing a where condition clause in our upsert statement. So I called a colleague at work to check if we had in fact missed the account id field, and lo and behold we had.</p><p>We quickly fixed the bug the next day, tested and deployed it.</p><p>If had automated testing setup on the application we might have detected this issues before it had ended up in production.</p><p>On hindsight, we had created the social accounts as embed list of documents of our application accounts, but if they were a collection of their own with the ids of the accounts they belonged to as an embedded list. We would have just had to queue a social media account once and create multiple entries of social media post for each connected account. Or even just one post for each social media account with an embedded list of application accounts they belong too.</p><p>We got caught up with the embedded documents of document stores and missed that opportunity.</p><p>PS:</p><p>I have not been at this company for over 4 years now and this is a description of a system that most definitely has improved since then</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5919c4cbfb89" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>