<?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 Patryk Bąk on Medium]]></title>
        <description><![CDATA[Stories by Patryk Bąk on Medium]]></description>
        <link>https://medium.com/@patrykbak?source=rss-b7e28ccb88fb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*EA1i0w3STXUHbhTUn8RPNQ.jpeg</url>
            <title>Stories by Patryk Bąk on Medium</title>
            <link>https://medium.com/@patrykbak?source=rss-b7e28ccb88fb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 07:27:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@patrykbak/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[10 things you, as a buddy, should know whenever a new developer joins your team]]></title>
            <link>https://patrykbak.medium.com/10-things-you-as-a-buddy-should-know-whenever-a-new-developer-joins-your-team-98e3d6582271?source=rss-b7e28ccb88fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/98e3d6582271</guid>
            <category><![CDATA[mentoring]]></category>
            <category><![CDATA[leadership]]></category>
            <category><![CDATA[buddy]]></category>
            <dc:creator><![CDATA[Patryk Bąk]]></dc:creator>
            <pubDate>Tue, 02 Feb 2021 13:18:43 GMT</pubDate>
            <atom:updated>2021-02-02T13:20:11.044Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XPOHjC6EUPAtiI-n5bSbwg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/Ju-ITc1Cc0w">J W</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption></figure><p>It’s a day like any other one. Focused on your work, you suddenly noticed someone who had walked into the office and is standing confused. At this point, you realized that he’s probably a new developer who’s joining your team today.</p><p>As your role is to be his buddy, you decide to introduce all members and say a few words about the project, tools the team uses, and how you work. That’s all. It’s time to get back to work. There’s no time for details or baby-sitting. With or without your help, sooner or later, a new employee will know the ropes and gain enough knowledge and skills to start working on their own.</p><p>If that’s what you want, you can stop reading this article. You won’t find anything interesting here.</p><h3>What does a team expect?</h3><p>You stayed, I’m glad.</p><p>So, do we want to wait an unknown amount of time until a new person is self-reliant? Or do we want a new developer to have appropriate skills and knowledge as soon as possible?</p><p>How would you feel if you got hired to a team which does not care about you at all? No help, no support, you can only rely on yourself. It doesn’t feel good, does it?</p><p>What do a new developer and we, as a team, want? Fast growth! Why? A new employee wants to be a skilled programmer who does a great job. It’ll boost their self-esteem and make them feel great. It’s simple, isn’t it?</p><p>As a team, we want to have a member who, first of all, will be satisfied and then quickly become independent. Thanks to that, he or she will take ownership of all the things they do and start to bring significant value to the product.</p><h3>What is a buddy role?</h3><p>It’s all in your hands. A newly hired person can fail, not develop themselves as they should’ve, or will not give value to a team or company. If that happens, in most cases, it’s a team failure, not this person’s one.</p><p>The team’s main aim should be to support, mentor, and do anything, so a new employee achieves as much as possible in the shortest possible time.</p><h3>Be prepared</h3><p>Prepare everything required for the first day. You can create a checklist to ensure that you will provide a new employee with what’s important. What’s more, iterate through that checklist, commenting and improving it. Thanks to that, it’ll be much easier to onboard new people in the future.</p><p>A good practice is to keep everything in one place — tips connected with project setup, information about the team, and the product itself. Each time someone joins your time, you can just send a link, so they get acquainted with it and come back to that whenever he or she needs it.</p><h3>Gradually pass knowledge and prioritize it</h3><p>Analyze the project and decide what’s the most important. If something is not crucial, put it off. Nobody can learn everything at once.</p><p>Collect some materials, i.e., books, articles, video courses. It’ll speed up the learning process and guarantee that a new developer won’t squander their time on unnecessary knowledge for now.</p><p>Set up a pair programming session. It’s an excellent opportunity to show how you work on functionality that needs to be implemented. Don’t forget it doesn’t mean that you should write code for a new developer. Rather observe, seek things that can be improved, give tips, and steer towards a solution instead of giving it on a plate.</p><h3>Be supportive</h3><p>Clearly signal that a new employee can rely on you and ask you anything. It’s quite normal that when a new developer joins a new project, they don’t know many things. Explain that this is the role of the whole team to share the knowledge and be helpful. That’s why he or she shouldn’t be concerned about the questions and doubts which arise.</p><p>Do not treat even an inexperienced developer worse than other teammates. You are all equal and the new employee should be aware that their opinion is as important as others one.</p><h3>Set goals and celebrate successes</h3><p>As new employees, especially junior developers, improve their skills very quickly, set goals for each week, especially the first one. Planning small goals, you make sure that a new employee develops themselves in a right direction. What’s more, achieving goals by a less experienced person is always encouraging. It gives a boost of energy and self-confidence.</p><p>Let the first commit on the first day be the very first goal!</p><p>What’s more, they don’t even have to achieve the previously set goal to be appreciated. If a new developer does something for the first time or succeeds in solving an issue, praise them for it in public. Remember, if they fail for any reason, try to pass the feedback during the one-on-one meeting or talk.</p><h3>Let new teammates prove themselves</h3><p>Although less experienced people should be supported from the beginning of their career, we shouldn’t do everything for them. We learn the most if we solve problems that are difficult for us but to some extent. When we need to put some effort into finding the solution. Let them do what all the team does, keeping track and explaining what they do incorrectly and how they can improve. Let them know that they should take the ownership of what he or she does. Build the mindset of responsibility. They can rely on us, but don’t let them treat us as a group of people who will always lead a new developer by the hand and get them off the hook.</p><h3>Set up outing</h3><p>If it’s possible, set up an outing with the entire team and go out for dinner. It’s always a great opportunity to get to know each other, lighten up, and show that you’re easy-going guys on whom a new person can rely.</p><h3>Be patient</h3><p>Things that are obvious for you are not necessarily clear for a new developer. If you discuss something, try to explain everything with as many details as possible to make sure you’re on the same page.</p><h3>You don’t need to know everything</h3><p>You were asked a question and you don’t know the answer? No worries. Just point to someone who does.</p><p><em>Originally published at </em><a href="https://appunite.com/blog/10-things-a-buddy-should-know"><em>https://appunite.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=98e3d6582271" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Renaming column and table in database migrations with Elixir and PostgreSQL]]></title>
            <link>https://patrykbak.medium.com/renaming-column-and-table-in-database-migrations-with-elixir-and-postgresql-d91ffbb135c4?source=rss-b7e28ccb88fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/d91ffbb135c4</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[migration]]></category>
            <category><![CDATA[zero-downtime-deployment]]></category>
            <dc:creator><![CDATA[Patryk Bąk]]></dc:creator>
            <pubDate>Mon, 21 Dec 2020 00:00:00 GMT</pubDate>
            <atom:updated>2020-12-23T08:38:39.715Z</atom:updated>
            <content:encoded><![CDATA[<p>To guarantee zero downtime deployment, while deploying a new version of our application on more than one node, we can use rolling updates. What if this new version has a migration which renames either column or table? Will rolling updates protect our application against downtime?</p><p>Let’s assume that we have a <strong>users</strong> table. As the name is not clear, we have decided to rename it to <strong>participants</strong>, as we keep there records of people who signed up for a triathlon race.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/396f1a61dab1275c426de602b7caa472/href">https://medium.com/media/396f1a61dab1275c426de602b7caa472/href</a></iframe><p>Not only did we write a migration which renames table, but also changed both the module and schema name.</p><p>Now, let’s deploy this new code for both instances of our application using rolling update.</p><p>It means that its version will be updated incrementally. The first instance will be terminated and replaced with the new version of the application. Afterwards, if the first instance starts up properly, then the second instance will be terminated and also replaced with the new one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/0*O5gT5REbjuI2Orof.png" /></figure><p>As you can see, there is a moment, before the second instance is replaced with the new one, when we have two versions of the application.</p><p>Both of them can be used by users. The first new instance ran the migration which renamed a table from <strong>users</strong> to <strong>participants</strong> and started using a new schema name — <strong>participant</strong>. Both instances of the application use the same database. Due to that, until the second instance is replaced, every request handled by it will return error as this instance uses the old schema name — <strong>users</strong>. The one which does not exist anymore in our database structure.</p><h3>How can we solve that?</h3><p>One way to solve the above-mentioned problem is to use <a href="https://www.postgresql.org/docs/9.2/sql-createview.html">PostgreSQL views</a>. In simple terms, a PostgreSQL view gives us a possibility to create a query and assign it a name. To make it clearer, I’ll give an example in a moment.</p><p>Previously, while deploying the new version of our application, the change contained the migration which renames the table from <strong>users</strong> to <strong>participants</strong>. In this migration we can also create the following PostgreSQL view:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9e5b647ec48d2aec972f901c291bc00/href">https://medium.com/media/d9e5b647ec48d2aec972f901c291bc00/href</a></iframe><p>The query which fetches all records from table <strong>participants</strong>, which we’ve just renamed, has been assigned <strong>users</strong> name — the old table name. It means that from now both instances of the application can fetch everything from this table using both <strong>users</strong> and <strong>participants</strong> name, so the new table name and the old one.</p><p>If our application starts up properly and all the instances will be replaced with the new one, we can create another migration which drops this PostgreSQL view, as it won’t be required anymore.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31615f4d2ea3115eb2efa85fdd6094c3/href">https://medium.com/media/31615f4d2ea3115eb2efa85fdd6094c3/href</a></iframe><h3>What about changing the column name?</h3><p>Assuming that we deploy the application on two instances, we will face the same problem once we run a migration which renames the column on the first instance. The same problem as the one with changing a table name. The second instance, which hasn’t been replaced yet with the new one containing new code, will use the old column name — the one which doesn’t exist anymore in the database.</p><h3>What can we do about it?</h3><p>Do not rename the column — write a migration which creates a new one. Do all the steps which I described in my previous article entitled “ <a href="https://patrykbak.com/2020/05/28/how-to-migrate-live-production-data.html">How to migrate live production data</a> “ in order to implement these changes without any downtime.</p><p><em>Originally published at </em><a href="https://patrykbak.com/2020/12/21/renaming-column-and-table-in-migrations.html"><em>https://patrykbak.com</em></a><em> on December 21, 2020.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d91ffbb135c4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Concurrency and parallelism with Elixir and Beam]]></title>
            <link>https://patrykbak.medium.com/concurrency-and-parallelism-with-elixir-and-beam-c683b2215c38?source=rss-b7e28ccb88fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/c683b2215c38</guid>
            <category><![CDATA[beam]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[parallelism]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <dc:creator><![CDATA[Patryk Bąk]]></dc:creator>
            <pubDate>Tue, 25 Aug 2020 18:00:33 GMT</pubDate>
            <atom:updated>2020-08-27T07:54:36.320Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Takogyfw3bJegkYS884qRA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/ymf4_9Y9S_A">Randy Fath</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption></figure><h3>Concurrency and parallelism with Elixir and BEAM</h3><p>Imagine coming to an overcrowded fast food restaurant to order the best taco in the country and there’s only one cook working there. He prepares each taco one by one. Although the cook has an extra time while waiting until a tortilla heats up, he doesn’t start preparing another order until the first one is finished.</p><p>That’s your software without concurrency and parallelism.</p><p>But what are the differences between those two? How are they handled in Elixir? Let’s start from the beginning.</p><h3>Concurrency vs parallelism</h3><p>In order to better understand the difference, let’s take a closer look at the above mentioned restaurant problem.</p><p>Imagine that the cook has just received orders for two tacos. So far, he’s prepared each order separately which is preparing the first taco from the beginning to the end and then the same with the second one. However, he decides to do it at the same time. Steps that he’s taking look as follows:</p><ol><li>The cook’s preparing a tortilla for the <strong>first order</strong>,</li><li>The cook’s preparing a tortilla for the <strong>second order</strong>,</li><li>For the <strong>first order</strong>,<strong> </strong>the cook’s putting vegetables and chicken on a tortilla,</li><li>For the <strong>second order</strong>, the cook’s putting vegetables and chicken on a tortilla,</li><li>The cook’s putting a taco from the <strong>first order</strong> into the cooker to heat it up,</li><li>The cook’s putting a taco from the <strong>second order</strong> into the cooker to heat it up,</li><li>The taco from the <strong>first order</strong> is ready to serve,</li><li>The taco from the <strong>second order</strong> is ready to serve.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*X2sqESX_aKVHzZA_" /></figure><p>The action above represents a concurrent execution. The cook was preparing two orders at the same time, but in fact, he was switching back and forth between each process (preparing).</p><p>We could wonder what the time difference between such an approach and preparing each order separately is. Imagine that before the cook serves a taco, he needs to wait until it heats up. It’d be a waste of time to wait doing nothing at that time.</p><p>Concurrency lets us start preparing another order while the first taco is heating up.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Upvq6Kqpt2dMFOf7" /></figure><p>What if the restaurant hired another cook for help? Then they would prepare tacos together. The main cook would prepare the first order and the newly hired employee would prepare the second order. Finally the process of preparing tacos would be finished twice as fast.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NDplil_Pf2xhQYLO" /></figure><p>Such an approach, when the system is able to handle two or more processes exactly at the same time, is called parallel processing.</p><p>Note, you need at least two CPU cores to take advantage of the parallelism.</p><p>To provide the highest performance, you can use both parallelism and concurrency at once.</p><h3>So how exactly does BEAM work?</h3><p><a href="https://elixir-lang.org/">Elixir</a> is built on top of the Erlang Virtual Machine, also called BEAM (Bogdan’s Erlang Abstract Machine) and along with its <a href="https://phoenixframework.org/">Phoenix framework</a> have all the benefits of <a href="http://erlang.org/">Erlang</a>. It means that we can create systems with a high concurrency and parallel processing which significantly influence their performance.</p><p><strong>BEAM</strong> uses exactly one <strong>Operating System Thread</strong> from each processor’s core. On each <strong>thread</strong>, <strong>Erlang VM</strong> (I’ll use it interchangeably with <strong>BEAM</strong>) runs its own <strong>scheduler</strong>. In turn, each <strong>scheduler</strong> has its own run queue. A run queue contains awaiting processes which are pulled by the <strong>scheduler</strong> so they can be run.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/967/0*6937N9pJ-LIcPi3o" /></figure><p>Data is not shared between queues. There are no locks on data to avoid dirty reads/writes as each <strong>scheduler</strong> has its own, independent <strong>run queue</strong>. That’s why all the <strong>OS threads</strong> can be used all the time. <strong>Schedulers</strong> don’t have delays due to slowly running processes.</p><p>When it comes to processes, they are not typical Operating System ones. They are Erlang processes which are lightweight (<a href="http://erlang.org/doc/efficiency_guide/processes.html">a newly spawned one uses 309 words of memory</a>) compared to the threads and processes in OS. What’s more, each process leaves a small memory footprint, is fast to create and be terminated, and scheduling overhead is low.</p><p><strong>BEAM</strong> is also responsible for distributing processes between run queues on the separate cores. It’s done by <strong>BEAM’s Load Balancer</strong>. The load balancer has its two techniques for balancing the incoming load: <strong>task stealing</strong> and <strong>migration</strong>. Using this logic, it takes tasks away from the run queues which are overloaded and migrates them to the ones with free resources.</p><p><strong>BEAM’s Load Balancer</strong> main purpose is to keep a high number of running processes allocated equally between each <strong>scheduler</strong>. It tries to use as few <strong>schedulers</strong> as possible so that none of the CPUs are overloaded.</p><h3>Summing up</h3><p>When building a fault tolerant and concurrent system, we should be aware of its possibilities. The knowledge about how the Erlang VM works can help us understand and solve everyday problems. What’s more, it gives us a chance to seize all its features to build reliable applications.</p><p>To get more details about how Erlang works, read the book entitled “<a href="https://blog.stenmans.org/theBeamBook">The Erlang Runtime System</a>” written by Erik Stenman or visit <a href="https://www.erlang.org/docs">erlang.org</a> to get acquainted with its documentation.</p><p><em>Originally published at </em><a href="https://appunite.com/blog/concurrency-and-parallelism-with-elixir-and-beam"><em>https://appunite.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c683b2215c38" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Handling failures in background workers with Elixir and supervisors]]></title>
            <link>https://patrykbak.medium.com/handling-failures-in-background-workers-with-elixir-and-supervisors-fdd8317ef2c5?source=rss-b7e28ccb88fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/fdd8317ef2c5</guid>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[genserver]]></category>
            <category><![CDATA[supervision-tree]]></category>
            <category><![CDATA[supervisor]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Patryk Bąk]]></dc:creator>
            <pubDate>Tue, 07 Jul 2020 09:51:37 GMT</pubDate>
            <atom:updated>2020-07-07T19:00:20.274Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w3qa8A_QGFIsnP3oITY1AA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/SIU1Glk6v5k">Neil Thomas</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption></figure><p>Elixir is built on the top of the Erlang Virtual Machine. It allows us to write highly available systems that can run practically forever. Does that mean that we don’t have to do anything to make our systems reliable?</p><p>In our system, we have a worker which pays drivers money for their job.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5460911f009ad5cf0be2e4b0debdbb3f/href">https://medium.com/media/5460911f009ad5cf0be2e4b0debdbb3f/href</a></iframe><p>Let’s take a closer look at our <strong>Payment.pay_the_driver/1</strong> function to see what it does.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2def93b58741046bcd180e58034fc7c/href">https://medium.com/media/d2def93b58741046bcd180e58034fc7c/href</a></iframe><p>The system compares money already paid with the amount which a driver should receive. It guarantees that a driver won’t receive more than they should.</p><p>Unfortunately, developers make mistakes and there’s a chance that an incorrect code is released. Luckily, the <strong>verify_payment/1</strong> function prevents incorrect payments. But what happens to our application if such a scenario occurs and the function raises the error?</p><p>To understand consequences, let’s see how the supervision tree works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*8JYAFfG8eYRcTr9sxOnJrA.png" /></figure><p>In the picture above, the main process supervises its child process — the <strong>Worker</strong> module.</p><p>Starting a supervisor, you can set what happens when one of the children gets crashed. <a href="https://hexdocs.pm/elixir/Supervisor.html#module-start_link-2-init-2-and-strategies">By default, a supervisor crashes when a child is restarted 3 times in 5 seconds</a>.</p><p>Our supervisor is configured with all these default values above:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28dacca9d320a7bc6a77464804ed1b09/href">https://medium.com/media/28dacca9d320a7bc6a77464804ed1b09/href</a></iframe><p>It means that each time the <strong>verify_payment/1</strong> function raises an error, our worker will be restarted.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d483e4b46e3345501299c58f9f055bb9/href">https://medium.com/media/d483e4b46e3345501299c58f9f055bb9/href</a></iframe><p>If it happens more than 3 times in 5 seconds, the main supervisor will also crash. As we can see, our worker handles its first message every second. If the logic within it raises an error, it will be able to reach more than 3 restarts within 5 seconds and consequently, our application will crash.</p><h3>So what’s now?</h3><p>Even if there’s a problem with that part of the code, we still want the rest of the application to be up while we’re investigating the issue.</p><p>That’s why we can add a separate supervisor just for our worker:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/0*1wZzWfgqal3KpqOa" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d3fd48b3e59abe6d0790991cb98e353e/href">https://medium.com/media/d3fd48b3e59abe6d0790991cb98e353e/href</a></iframe><p>The supervisor needs to know the worker interval, so we have to replace the <strong>@interval</strong> attribute in the worker with the <strong>interval()</strong> public function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ce1b145c5f73d79c621930277146c98/href">https://medium.com/media/8ce1b145c5f73d79c621930277146c98/href</a></iframe><p>Now our supervisor will crash if the child is restarted within 5 seconds more times than the value of <strong>max_restarts</strong>.</p><p>The worker executes its function every second. Setting the limit to 6 restarts in 5 seconds guarantees that the supervisor will never crash.</p><p>It’s time to modify the main supervisor to look after the worker supervisor instead of the worker itself.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/213a2db70038944f17bb8bf6c5157b96/href">https://medium.com/media/213a2db70038944f17bb8bf6c5157b96/href</a></iframe><h3>Summing up</h3><p>It’s sometimes hard to avoid temporary failures. You have to make sure that you have a plan for what happens if some parts of the system stop working correctly.</p><p><em>Originally published at </em><a href="https://appunite.com/blog/handling-failures-in-background-workers-with-Elixir-and-supervisors"><em>https://appunite.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fdd8317ef2c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why shouldn’t you use Elixir code in database migrations?]]></title>
            <link>https://patrykbak.medium.com/why-shouldnt-you-use-elixir-code-in-database-migrations-544015120beb?source=rss-b7e28ccb88fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/544015120beb</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[migration]]></category>
            <dc:creator><![CDATA[Patryk Bąk]]></dc:creator>
            <pubDate>Mon, 15 Jun 2020 00:00:00 GMT</pubDate>
            <atom:updated>2020-06-15T21:11:59.359Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qvqomzy7wgavOJeZSXBxCA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/5_bAKRGvkq8">Barth Bailey</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption></figure><p>Elixir code used in migrations can cause troubles when we decide to start up our system with an empty database. To understand the problem, let’s take the same example as the one used in my previous article entitled “<a href="https://medium.com/@patrykbak/how-to-migrate-live-production-data-baa762cb8a80">How to migrate live production data</a>“.</p><p>Each year, triathlon races are held in many countries. Triathletes from around the world can sign up for one of 4 distances.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f83386dd1e48a7120d28a820dd8d9ce9/href">https://medium.com/media/f83386dd1e48a7120d28a820dd8d9ce9/href</a></iframe><p>Notice that every time someone tries to register, we have to fetch the participant count. We also have to use a lock to prevent race conditions. This can be slow when many people try to register at the same time.</p><p>To solve that, we decide to add a new field with confusing name <strong>entries</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1db892448df0ad832dc68718b570d7c/href">https://medium.com/media/f1db892448df0ad832dc68718b570d7c/href</a></iframe><p>While signing up for a race, we can check the left entries based on the <strong>triathlon_races</strong> table. When a user successfully registers, the number of available <strong>entries</strong> decreases.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/952bd048aa68f6621c628d7492955afd/href">https://medium.com/media/952bd048aa68f6621c628d7492955afd/href</a></iframe><p>After a few months, we got confused whether the <strong>entries</strong> field represented the number of entries which either remained or were already sold. To make this field explicit, its name is changed to <strong>entries_left</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87c3e75093965252edc5a2f2678c74f5/href">https://medium.com/media/87c3e75093965252edc5a2f2678c74f5/href</a></iframe><p>The code is more understandable now. Success!</p><h3>What could go wrong?</h3><p>Let’s wonder what will happen if we run these all migrations on an empty database.</p><p>Every migration will be executed one by one, by the time the system meets the migration with the Elixir code.</p><p>In the moment of running this migration our schema looks as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1f668893c5114a92b6f905d2bad9547/href">https://medium.com/media/a1f668893c5114a92b6f905d2bad9547/href</a></iframe><p>We don’t have our <strong>entries</strong> field anymore. It has been changed for <strong>entries_left</strong>.</p><p>Execution of the Elixir code in the migration will fail and return error as there are no <strong>entries</strong> key in the <strong>triathlon_races</strong> schema.</p><p>The project’s setup is destroyed and until we remove the Elixir code from the migration, the system won’t start up.</p><h3>How to solve it?</h3><p>One way to avoid such implications is to use pure SQL in migrations instead of writing code in Elixir.</p><p>Both approaches, theoretically, do the same. Notwithstanding, there is a small difference between them.</p><p>SQL query uses a database structure with such a version of it which exists in the moment of executing migration. The code always uses the most recent version of the database schema.</p><p>As you can see on the diagram below, the database’s structure changes along with the running migrations. The Elixir code is always the same during that process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DmzFFePO35JcCsv6uccXHw.png" /></figure><p>Such migration with SQL query can look like the one below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94f8a4444e7fd2be3bd4385b4dd3c0ca/href">https://medium.com/media/94f8a4444e7fd2be3bd4385b4dd3c0ca/href</a></iframe><p>Problem solved! Now, if we change code in the project and then start up it from scratch, it won’t cause any troubles as the migrations rely only on a database’s structure.</p><h3>SUM UP</h3><p>It’s worth remembering that migrations are an immutable log of changes. These adjustments should always give the same effect. Some serious problems may arise, when references to code, which can be amended in every moment, appear in that changes log. Suddenly, the migration written some months ago uses stale code from that period instead of the new one which is currently in a project.</p><p><em>Originally published at </em><a href="https://patrykbak.com/2020/06/15/why-should-not-you-use-elixir-code-in-database-migrations.html"><em>https://patrykbak.com</em></a><em> on June 15, 2020.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=544015120beb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to do well during a recruitment process and a job interview for a junior developer position]]></title>
            <link>https://patrykbak.medium.com/how-to-do-well-during-a-recruitment-process-and-a-job-interview-for-a-junior-developer-position-e11e3b6d2df0?source=rss-b7e28ccb88fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/e11e3b6d2df0</guid>
            <category><![CDATA[junior-developer]]></category>
            <category><![CDATA[first-job]]></category>
            <category><![CDATA[recruitment-process]]></category>
            <category><![CDATA[job-interview]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Patryk Bąk]]></dc:creator>
            <pubDate>Sat, 06 Jun 2020 00:00:00 GMT</pubDate>
            <atom:updated>2020-09-19T15:16:57.559Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FZHAUgCKafUzjg8HQ7Lu0g.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/KmKZV8pso-s">Ricardo Gomez Angel</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption></figure><h3>12 things recruiters look for during interviews with junior devs (which you’re probably NOT doing)</h3><p>Don’t be another applicant who doesn’t stand out from the hundreds of candidates.</p><h3>Show that you care</h3><p>While preparing your CV and a cover letter pay attention to details.</p><p>The recruiter should see that you really want to work with them. Write something about the company and the position you apply for. Don’t send the same CV to multiple companies. Personalising your CV is the best way to stand out.</p><p>Show that it matters to you who you will work for. After all, you don’t want to find whichever job as a junior developer. The atmosphere, a company’s mission or the duties are also important to you.</p><h3>Meet the requirements</h3><p>Get acquainted with all the requirements for the chosen position. Try to possibly enrich your knowledge and skills if you don’t meet the <strong>must-have</strong> section. don’t take the easy way, do more and gain abilities that are listed in the <strong>nice-to-have</strong> section. While you don’t have to know every of the required skills, be prepared to be asked about them.</p><h3>Create a mini project</h3><p>Words themselves are not very convincing. Instead of listing a bunch of skills that you have, try showing them in the project you’ve created.</p><p>It doesn’t have to be a huge application. It doesn’t have to be finished. If you don’t have any idea for the project, create a clone of popular applications available on the Internet.</p><p>The most important part is presenting all the skills you possess. Especially the ones which are listed in the requirements on the position you apply for.</p><h3>Build your online presence</h3><p>Even if you don’t have any experience, it’s good to start leaving a trail on the Internet.</p><p>Sign up on LinkedIn and gradually build your community.</p><p>Use GitHub to store your projects and code. You should share all your accomplishments when applying for a new position.</p><p>If you think about creating a blog or a YouTube channel, do it! It doesn’t have to be perfect — your goal is to learn and show a great attitude.</p><h3>Gather information about the company</h3><p>Visit the company website, social media and try to get acquainted with what the company does, what projects it runs and what technology it uses. Not only will it help you prepare questions you may ask, but also show recruiters that you put in some effort and prepared for the interview.</p><h3>Attend events</h3><p>Seize each opportunity to enrich your knowledge, get to know people from the industry or even the company you’d like to work for.</p><p>Check if your dream company organizes local meetups or workshops. Join these events so you can meet the developers who work there and have a chat with them.</p><h3>Write a cover letter</h3><p>Do write a cover letter. It’s hard to present your skills, motivation and attitude in your CV. That’s why a cover letter gives you an opportunity to make the recruiter interested. Show them that you are the person that they are looking for.</p><h3>Care about the details</h3><p>Read both your CV and the cover letter several times. Try to remove all typos, fix the indentations, normalize the font and its size keeping these two documents readable, smart and neat.</p><h3>Keep a positive attitude</h3><p>Be positive no matter what. A job interview is stressful for both sides.</p><p>Most candidates mainly focus on technical skills forgetting that attitude is just as important.</p><p>Technical skills can be quickly learned on the job, especially in the junior roles, but if the recruiters see a hard-working attitude and willingness to learn, you’ll score additional points.</p><h3>Don’t be afraid to speak</h3><p>There are many questions which appear on each job interview. They are usually connected with your experience, interests, materials you use for learning or the projects you’ve done.</p><p>Be prepared. Practice answers in front of a mirror to make sure that they are clear and consistent. Don’t let yourself be surprised.</p><p>For other questions try to answer in as many details as you can, but don’t digress. If there’s a need don’t be afraid of speaking about problems and failures.</p><h3>Ask questions</h3><p>When the time for asking questions comes, don’t hesitate. Ask about everything. Really.</p><p>After all, you want to know about the company’s projects, style of working and the responsibilities on the position. As you don’t have much experience, I guess that you wonder what support you’ll receive from your co-workers. Find out how the work of junior developers looks like during the first weeks.</p><h3>Don’t give up</h3><p>Even if you won’t be hired for some reason, don’t give up. Ask for feedback, follow the guidance, brush up your skills and fill in the gaps in knowledge and try again in a few months.</p><p>Companies seek self-motivated and determined people. If someone improves their skills according to the feedback and tries again, they’ll have much higher chances. It shows that a candidate is able to draw conclusions from constructive criticism and improve their skills, and what’s more, they really want to work in a specific company.</p><p><em>Originally published at </em><a href="https://patrykbak.com/2020/06/06/how-to-do-well-during-a-recruitment-process-and-a-job-interview-for-a-junior-developer-position.html"><em>https://patrykbak.com</em></a><em> on June 6, 2020.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e11e3b6d2df0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to migrate live production data]]></title>
            <link>https://patrykbak.medium.com/how-to-migrate-live-production-data-baa762cb8a80?source=rss-b7e28ccb88fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/baa762cb8a80</guid>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[production]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[migration]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Patryk Bąk]]></dc:creator>
            <pubDate>Thu, 28 May 2020 10:17:31 GMT</pubDate>
            <atom:updated>2020-07-01T11:05:10.001Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3_ytG34-1rMXHy1vT9Xm4w.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/rtX4wxMEI2M">Muukii</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption></figure><p>Migrating live data is risky. Let’s analyze the example below to see why.</p><p>Each year, triathlon races are held in many countries. Triathletes from around the world can sign up for one of 4 distances.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2307a88aae111c7ac14998354daa75e1/href">https://medium.com/media/2307a88aae111c7ac14998354daa75e1/href</a></iframe><p>Notice that every time someone tries to register, we have to fetch the participant count. We also have to use a lock to prevent race conditions. This can be slow when many people are trying to register at the same time.</p><p>To solve that, we decide to add the <strong>entries_left</strong> field.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/035944489c12deeeb1124ef2e336a6ba/href">https://medium.com/media/035944489c12deeeb1124ef2e336a6ba/href</a></iframe><p>While signing up for a race, we can check left entries based on the <strong>triathlon_races</strong> table. When a user successfully registers, the number of available entries decreases.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69ddc0eac04b97b37ccd50d5725fc930/href">https://medium.com/media/69ddc0eac04b97b37ccd50d5725fc930/href</a></iframe><p>We made the <strong>Race</strong> and <strong>Participant</strong> modules less coupled in terms of operational and temporal coupling<a href="http://coupling.it/">.</a> What’s more, it’s worth noting that available entries are the crucial part of the business logic. That’s why it’s good to keep them in the <strong>Race</strong> module.</p><h3>Time to deploy!</h3><p>What will happen if we deploy both the migration and the new code?</p><p>Once it is deployed to the production, our server runs the new code with the new migration which is run in a transaction. It means that until the entire process of the migration is finished, the changes entered by the migration are not visible by the system.</p><p>What if the table that we want to update contains thousands of records? This process can take several minutes then. Since the migration does <strong>UPDATE</strong> on the <strong>triathlon_races</strong> table and the <strong>fetch_exclusively/1</strong> function does <strong>LOCK</strong> <strong>FOR</strong> <strong>UPDATE</strong>, the registration process will wait until the lock can be acquired. It means that users won’t be able to register for a race at the time.</p><h3>How to guarantee a zero downtime deployment?</h3><p>One way is to implement this process in four steps. Deploying each of them separately to production:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QVW0yA48U2bO332sPIKVSw.png" /></figure><ol><li>Migration which adds the <strong>entries_left</strong> field.</li><li>Add code which:</li></ol><ul><li>sets the <strong>entries_left</strong> field while organzing a race.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/30994d314bf96c7f77e088400c7e1794/href">https://medium.com/media/30994d314bf96c7f77e088400c7e1794/href</a></iframe><ul><li>during the process of signing up, updates the <strong>entries_left</strong> field only for the newly created races. The old races will have this field <strong>nil</strong> so we can skip all such cases.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20a7e3dc6689ddcfa104dab17804789d/href">https://medium.com/media/20a7e3dc6689ddcfa104dab17804789d/href</a></iframe><p>3. Run code which updates the <strong>entries_left</strong> field in the old records.</p><p>4. After performing points 2 and 3 the data are up-to-date and we can already use them in our logic. So we can replace the old code with the one that uses the <strong>entries_left</strong> field instead of fetching participants number from the database each time.</p><h3>Do we really need step 2?</h3><p>Without this step we assume that none would register between the process of updating the <strong>entries_left</strong> field (see step 3) and replacing the old code with the new one (see step 4).</p><p>If someone signed up in that specific moment, we would have a race with the <strong>entries_left</strong> field equal <strong>nil</strong>. It means that <strong>any_entry_available?/1</strong> function would return <strong>nil</strong> for this race, although the system wouldn’t know how many available entries there are.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/371/1*v9ZtyTsoTXnlQGOghBoh5Q.png" /></figure><p>Such a scenario silently destroys our data consistency, because a user can register for a race which has all entries sold out.</p><h3>SUMMING UP</h3><p>Seemingly simple migrations are dangerous and when executed incorrectly can lead to errors and downtimes. Each should be planned and carried out in such a way that the system is available all the time. What’s more, run migrations should be reversible. It means that, if there appears any errors, we should be able to rollback these changes, restoring system stability.</p><p><em>Originally published at </em><a href="https://patrykbak.com/2020/05/28/how-to-migrate-live-production-data.html"><em>https://patrykbak.com</em></a><em> on May 28, 2020.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=baa762cb8a80" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Should you use database transactions for data consistency?]]></title>
            <link>https://patrykbak.medium.com/should-you-use-database-transactions-for-data-consistency-7029ab6d301d?source=rss-b7e28ccb88fb------2</link>
            <guid isPermaLink="false">https://medium.com/p/7029ab6d301d</guid>
            <category><![CDATA[database-transaction]]></category>
            <category><![CDATA[phoenix-framework]]></category>
            <category><![CDATA[data-consistency]]></category>
            <category><![CDATA[eventual-consistency]]></category>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[Patryk Bąk]]></dc:creator>
            <pubDate>Wed, 15 Apr 2020 20:49:45 GMT</pubDate>
            <atom:updated>2020-07-01T11:07:46.128Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ap7z9Hh63wbqATUUGkRrSw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/0LU4vO5iFpM">Greg Rakozy</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption></figure><p>In our systems, most business actions require changes across multiple contexts. How can we keep these contexts consistent?</p><p>Let’s look at an example and analyze potential problems and solutions.</p><p>Imagine a logistics application, where drivers can earn money by completing jobs. We’ll focus on two modules: <strong>Documents</strong> and <strong>Payment</strong>. For every job, the system requires two documents to be uploaded and then accepted. After both documents have been accepted, a driver receives compensation for their work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HFzdIA3sKOgUSkacR9Csbg.png" /></figure><p>Accepting a document looks like this:</p><pre>defmodule Documents do<br>  def accept(document, job_id) do<br>    Repo.transaction(fn -&gt;<br>      {:ok, _} =<br>        document<br>        |&gt; Ecto.Changeset.change(accepted: true)<br>        |&gt; Repo.update()<br> <br>      :ok = Payment.handle_document_accepted(job_id)<br><br>      # a bunch of other logic<br>    end)<br>  end<br>end<br><br>defmodule Payment do<br>  def handle_document_accepted(job_id) do<br>    if Documents.both_accepted?(job_id) &amp;&amp; not_paid_yet?(job_id) do<br>      :ok = pay_the_driver(job_id)<br>    end<br>  end<br>end</pre><p>We can see that it is in a transaction that guarantees data consistency. The functions are idempotent so double-clicking and triggering this interface twice for the same document doesn’t cause any problems.</p><h3>What’s wrong?</h3><p>Although it looks correct at a first glance, there are some hard-to-see problems with this code.</p><p>The diagram below shows the scenario which may occur when a user clicks the acceptance button one by one, initializing two transactions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n6zlRQnQhJc0rN7u2vwhpQ.png" /></figure><p>The problem occurs when someone clicks the acceptance button one time after another within a very short period of time. In the beginning, both transactions see the same database state. While the first document is being accepted and then verified if both are accepted, there is only one confirmed document. This is fine because that’s a document which has just been accepted in this transaction. Let’s look at the second transaction at the moment <strong>T1</strong> (it is marked on the diagram above) when the code checks whether both documents are accepted. When looking at the timeline, we can notice that at that moment both documents should be accepted. Unfortunately, the first transaction hasn’t been committed yet, so the changes in the database are not visible in the second transaction. <strong>Documents.both_accepted?/1</strong> function returns false and the driver doesn’t get paid for their job. After all, when both transactions finish, the state of the database indicates that both documents are accepted.</p><h3>How to solve this?</h3><p>Let’s try to think about the real world for a second. In the real world, employees don’t get their compensation instantly. A driver finishes their work, uploads documents and then a shipper accepts these papers. From a driver’s point of view, this isn’t a synchronous process. Thus, we can imagine that after the finished job, this information is delivered to the payment department which, in a specific moment, pays the compensation.</p><p>The one way to avoid this inconsistency is to, firstly, create a function, which moves accepted documents to the place where they’ll be awaiting payment:</p><pre>defmodule Documents.Accepted do<br>   use Ecto.Schema<br>   import Ecto.Query<br><br>   schema &quot;accepted_documents&quot; do<br>      field(:job_id, :binary_id)<br>      field(:document_id, :binary_id)<br>   end<br><br>   def handle_document_accepted(job_id, document_id) do<br>     %Accepted{<br>       job_id: job_id,<br>       document_id: document_id<br>     } |&gt; Repo.insert()<br>   end<br>end</pre><p>So now, our <strong>Documents</strong> module looks as follows:</p><pre>defmodule Documents do<br>  def accept(document, job_id) do<br>    Repo.transaction(fn -&gt;<br>      {:ok, _} =    <br>        document<br>        |&gt; Ecto.Changeset.change(accepted: true)<br>        |&gt; Repo.update()<br><br>      {:ok, _} = Documents.Accepted.handle_document_accepted(job_id, document.id)<br><br>      # bunch of other logic<br>    end)<br>  end<br>end</pre><p>As we have all the accepted documents kept in one place, we can create now a worker which will fetch all these documents and pay drivers:</p><pre>defmodule Payment.Worker do<br>  use GenServer</pre><pre>  @interval 10 * 6000</pre><pre>  def start_link() do<br>    ...<br>  end</pre><pre>  def init() do<br>    Process.send_after(self(), :work, <a href="http://twitter.com/interval">@interval</a>)<br>    {:ok, %{interval: <a href="http://twitter.com/interval">@interval</a>}}<br>  end</pre><pre>  def handle_info(:work, state) do<br>    Repo.transaction(fn -&gt;<br>      Documents.Accepted.fetch()<br>      |&gt; Enum.map(fn %{job_id: job_id} -&gt;<br>        :ok = pay_the_driver(job_id)<br>        <br>        job_id<br>      end)<br>      |&gt; Documents.Accepted.delete_paid()<br>    end)<br>    <br>    Process.send_after(self(), :work, state.interval)<br>    <br>    {:noreply, state}<br>  end<br>end</pre><p>Worker is run asynchronously every 60 seconds and fetches <strong>job_ids</strong> if both documents are accepted. If all the drivers from those jobs are paid, accepted documents are deleted as they are kept there only for payment purposes. It is worth noting that function <strong>pay_the_driver/1</strong> is idempotent to make sure that no driver is paid twice for the same job.</p><p>This makes the modules less coupled in terms of operational and temporal coupling. So far, functions from <strong>Documents</strong> and <strong>Payment</strong> modules have had to be executed in a certain order and at an appropriate time. By making the <strong>Documents</strong> module decoupled from the side effects in other contexts, we make it far more extensible and easier to work with.</p><p>The result? Less error-prone code. More understandable. Less coupled. More coherent.</p><p>The lesson we can see here is that when modeling our code, we should follow the way the real world works. In the real world, we are used to eventual consistency, so we shouldn’t be afraid of it in our software.</p><p>Originally published at <a href="https://patrykbak.com/2020/04/15/should-you-use-database-transactions-for-data-consistency.html">https://patrykbak.com</a> on Apr 15, 2020.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7029ab6d301d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>