<?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 Amol S Deshmukh on Medium]]></title>
        <description><![CDATA[Stories by Amol S Deshmukh on Medium]]></description>
        <link>https://medium.com/@amol.deshmukh_97340?source=rss-6916dadcc362------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_AOJEfGwmQVmPYdkTb87Ig.jpeg</url>
            <title>Stories by Amol S Deshmukh on Medium</title>
            <link>https://medium.com/@amol.deshmukh_97340?source=rss-6916dadcc362------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 07:39:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amol.deshmukh_97340/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[Safe data-fix pattern for RDBMS]]></title>
            <link>https://medium.com/@amol.deshmukh_97340/safe-data-fix-pattern-for-rdbms-117d7fc2eb00?source=rss-6916dadcc362------2</link>
            <guid isPermaLink="false">https://medium.com/p/117d7fc2eb00</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[relational-databases]]></category>
            <category><![CDATA[database-administration]]></category>
            <dc:creator><![CDATA[Amol S Deshmukh]]></dc:creator>
            <pubDate>Mon, 05 Feb 2024 04:41:15 GMT</pubDate>
            <atom:updated>2024-02-05T04:57:52.415Z</atom:updated>
            <content:encoded><![CDATA[<h3>Safe data-fix pattern for relational databases</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nBzNhIAK1DfeuZcU2kkKaQ.jpeg" /><figcaption><em>Photo by </em><a href="https://unsplash.com/@jankolar?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash"><em>Jan Antonin Kolar</em></a><em> on </em><a href="https://unsplash.com/photos/brown-wooden-drawer-lRoX0shwjUQ?utm_content=creditCopyText&amp;utm_medium=referral&amp;utm_source=unsplash"><em>Unsplash</em></a></figcaption></figure><p>As developers, we often encounter scenarios that necessitate data-fixes directly within our relational databases. Whether it’s rectifying inconsistencies, updating erroneous entries, or handling unexpected data issues, the need to perform these fixes is sometimes inevitable.</p><p>This post discusses a light-weight and safe approach for managing data-fixes to relational databases. For an alternate approach that discusses a more managed setup for carrying out data-fixes, take a look at <a href="https://mikebolshakov.medium.com/how-to-survive-fixing-data-on-a-prod-in-a-micro-service-environment-4e1cb45b5984">Survive fixing data on PROD (Golang)</a>.</p><h4>Navigating the pitfalls</h4><p>Data-fixes are a break-glass procedure that need to be carried out with utmost caution. A well-written application can ensure that all data changes meet the business constraints that may apply in addition to the database-level integrity constraints. An obvious concern that arises when modifying data directly, outside the application, is that these changes lack the additional consistency safeguards that are enforced by the application.</p><h4>Going beyond version control, code reviews, and testing</h4><p>Data-fixes need to be applied precisely. And should there be a need to troubleshoot a problem after the data fix, it should be possible to easily see the effect of the data fix after the fact.</p><p>Standard best practices like version-controlling the data-fix code, ensuring code reviews of the same &amp; testing the premise behind the data-fix must be followed. However, despite these efforts, it may not always be possible to guarantee that a data-fix will not lead to unintended consequences that may be discovered only after the fix has been applied. The <strong><em>safe data-fix pattern</em></strong> provides mitigation for dealing with such scenarios.</p><h3>The safe data-fix pattern</h3><p>The general approach with the <strong><em>safe data-fix pattern</em></strong> is to back up the rows that will be affected by the data-fix. This enables inspecting the state of the affected rows as it was prior to applying the data-fix. This also enables the possibility of restoring the rows to that prior state, if necessary.</p><p><em>NOTE: The following examples assume that the tables in question all have a single primary key column named </em><em>id, however the scheme is relevant even for cases where the tables have a composite primary key.</em></p><h4>Safe datafix pattern for DELETE statement</h4><pre>-- Backup the rows to be deleted.<br>CREATE TABLE my_item_datafix00001 AS<br>SELECT * FROM my_item<br>WHERE ... ;<br><br>-- Delete only the backed up rows by joining on the primary key field.<br>DELETE FROM my_item target<br> INNER JOIN my_item_datafix00001 source on source.id = target.id;</pre><p>By applying the WHERE clause to the backup table creation instead of the DELETE statement, we have ensured that we maintain a copy of the affected rows prior to deletion.</p><h4>Safe datafix pattern for UPDATE statement</h4><pre>-- Backup the rows to be updated.<br>CREATE TABLE my_item_datafix00001 AS<br>SELECT * FROM my_item<br>WHERE ... ;<br><br>-- Update only the backed up rows by joining on the primary key field.<br>UPDATE target<br>   SET target.column_a = &#39;some new value&#39;<br>     , target.column_b = &#39;another new value&#39;<br>  FROM my_item target<br> INNER JOIN my_item_datafix00001 source on source.id = target.id;</pre><p>Just like in the case with the DELETE statement, we ensure that only the rows that were backed up are updated.</p><h4>Safe datafix pattern for INSERT statement</h4><pre>-- Insert in a separate datafix table.<br>CREATE TABLE my_item_datafix00001 LIKE my_item;<br><br>-- Insert into the datafix table first.<br>INSERT INTO my_item_datafix00001 VALUES (<br>  ...<br>);<br><br>-- Copy from the datafix table into the  main table.<br>INSERT INTO my_item SELECT * FROM my_item_datafix00001;</pre><p>It should be noted that for INSERT statements, creating the datafix table is not so much about backing up existing data (since we are adding new rows). But in this case, we ensure that a record of the rows inserted by the data-fix is maintained. This is helpful if these newly inserted rows get updated/deleted in the future and we need to backtrack and find out what was the initial state at insertion.</p><h3>Conclusion</h3><p>This post discussed an approach for safely carrying out data-fixes. By ensuring that we only update rows that are already backed up, we introduce a mitigation for dealing with the unanticipated issues resulting from the data-fix. This approach is light-weight since it does not assume any other supporting system and is compatible with almost all of the commonly used relational database systems.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=117d7fc2eb00" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Running Custom Workloads with Sysbench]]></title>
            <link>https://medium.com/@amol.deshmukh_97340/running-custom-workloads-with-sysbench-c6d5338a503b?source=rss-6916dadcc362------2</link>
            <guid isPermaLink="false">https://medium.com/p/c6d5338a503b</guid>
            <category><![CDATA[sysbench]]></category>
            <category><![CDATA[performance-monitoring]]></category>
            <dc:creator><![CDATA[Amol S Deshmukh]]></dc:creator>
            <pubDate>Thu, 20 Aug 2020 06:53:41 GMT</pubDate>
            <atom:updated>2020-08-20T06:53:41.997Z</atom:updated>
            <content:encoded><![CDATA[<p>Sysbench is a popular open-source benchmarking tool for Linux that comes bundled with a suite for benchmarking CPU, memory, file I/O, mutex, and even MySQL. Ease of installation/use and the multitude of options that allow tweaking the execution and reporting of results are the main reasons for its popularity. Instructions for installing from binary packages and basic usage are available in the official documentation at <a href="https://github.com/akopytov/sysbench#installing-from-binary-packages.">https://github.com/akopytov/sysbench#installing-from-binary-packages.</a></p><p>Aside from the out-of-the-box support for various benchmarks, sysbench supports execution of custom workloads via it’s support for executing arbitrary Lua scripts.</p><p>The rest of this post is focussed on the customizability that is built into sysbench to allow execution of arbitrary workloads.</p><h3>Custom workload execution</h3><p>Sysbench can execute custom workloads by accepting the path to a Lua script on the command line as illustrated below:</p><pre>$ sysbench [STD_OPTS] [CUSTOM_OPTS] path/to/script.lua [COMMAND]</pre><p>A list of standard options (STD_OPTS) and their description/usage can be obtained by runningsysbench — help. We will not discuss these in great detail in this post. Instead, let’s focus on the 3 mechanisms offered by sysbench to aid in authoring custom workload definitions.</p><h4>Built-in commands</h4><p>Sysbench supports 4 built-in commands, viz. help, prepare, cleanup, and run. Of these the first 3 built-in commands, (i.e. help, prepare, cleanup) are simply names of global Lua functions that are recognized by sysbench; they don’t come with any useful predefined behavior. Implementing support for these is quite straightforward as illustrated below:</p><pre>$ cat &gt;builtin_prepare.lua &lt;&lt;EOF</pre><pre>function prepare()<br>  print(&quot;**** prepare() was called.&quot;)<br>end</pre><pre>EOF</pre><p>Let’s test this:</p><pre>$ sysbench ./builtin_prepare.lua prepare</pre><pre>sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)<br>**** prepare() was called.</pre><p>Similar results may be obtained by replacing prepare with cleanup or help. The only distinction between these 3 built-in commands is the semantics of the command name itself. i.e. If the underlying behavior is to render a message to indicate usage, it would be best implemented as the help built-in command, whereas thecleanup command may seem more appropriate for behavior that clears state left behind after a test execution.</p><p>Implementing support for the run built-in command is a bit different from the other 3 commands, since the run command has an associated lifecycle that is illustrated below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*bP4s53RMJgTtS0VoAH9Bbg.png" /><figcaption>Lifecycle of the run() built-in command, illustrated here with 3 events across 2 threads.</figcaption></figure><p>The custom Lua script is <em>required</em> to provide an implementation for the event() function, while the other lifecycle functions are optional. Depending on the standard command-line options that specify the number of events (either directly using the--events option, or indirectly by specifying the --time option) and the number of threads, the event() function may be called multiple number of times across the various threads.</p><p>Let’s see the lifecycle of the run() command in action with an example:</p><pre>$ cat &gt;builtin_run.lua &lt;&lt;EOF</pre><pre>function init(thread_id)<br>  print(&quot;**** init(&quot; .. (thread_id or &quot;main&quot;) .. &quot;) called&quot;)<br>end</pre><pre>function thread_init(thread_id)<br>  print(&quot;**** thread_init(&quot; .. thread_id .. &quot;) called: &quot;)<br>end</pre><pre>function event(thread_id)<br> print(&quot;**** event(&quot; .. thread_id .. &quot;) called&quot;)<br>end</pre><pre>function thread_done(thread_id)<br>  print(&quot;**** thread_done(&quot; .. thread_id .. &quot;) called&quot;)<br>end</pre><pre>function done(thread_id)<br>  print(&quot;**** done(&quot; .. (thread_id or &quot;main&quot;) .. &quot;) called&quot;)<br>end</pre><pre>EOF</pre><p>Running this script reveals the sequence in which the lifecycle functions are called:</p><pre>$ sysbench - events=3 - threads=2 ./builtin_run.lua run</pre><pre>sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)<br>**** init(main) called<br>Running the test with following options:<br>Number of threads: 2<br>Initializing random number generator from current time<br>Initializing worker threads…<br>**** thread_init(0) called:<br>**** thread_init(1) called:<br>Threads started!<br>**** event(1) called<br>**** event(0) called<br>**** thread_done(0) called<br>**** event(1) called<br>**** thread_done(1) called<br>General statistics:<br>total time: 0.0002s<br>total number of events: 3<br>Latency (ms):<br>min: 0.00<br>avg: 0.01<br>max: 0.03<br>95th percentile: 0.03<br>sum: 0.04<br>Threads fairness:<br>events (avg/stddev): 1.5000/0.50<br>execution time (avg/stddev): 0.0000/0.00<br>**** done(main) called</pre><p>Note that init() and done() are called by the main thread, while thread_init(), event(), and thread_done() are invoked by individual threads.</p><h4>Custom options</h4><p>The implementation of the built-in commands can be supplemented by specifying custom options for these commands. Let’s take a look at an example that specifies 2 custom options, viz. test_data_csv &amp; num_retries:</p><pre>$ cat &gt;custom_options.lua &lt;&lt;EOF</pre><pre>sysbench.cmdline.options = {<br>  test_data_csv = {<br>    &#39;File to load the test data from.&#39;,<br>    &#39;fixture.csv&#39;,<br>    sysbench.cmdline.ARG_STRING<br>  },<br>  num_retries = {<br>    &#39;Number of retries on failure.&#39;,<br>    3,<br>    sysbench.cmdline.ARG_INT<br>  },<br>}</pre><pre>function help()<br>  print(&quot;Options:&quot;)<br>  sysbench.cmdline.print_test_options()<br>end</pre><pre>EOF</pre><p>Note the built-in support for rendering a useful help command. Let’s put this to test:</p><pre>$ sysbench ./custom_command.lua help<br>sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)<br>Options:<br> - num_retries=N Number of retries on failure. [3]<br> - test_data_csv=STRING File to load the test data from. [fixture.csv]</pre><p>The option values can be accessed as illustrated below (following command appends to the Lua script that was created in the earlier step):</p><pre>$ cat &gt;&gt;custom_options.lua &lt;&lt;EOF</pre><pre>function prepare()<br>  print(&quot;**** test_data_csv: &quot; .. sysbench.opt.test_data_csv)<br>  print(&quot;**** num_retries: &quot; .. sysbench.opt.num_retries)<br>end</pre><pre>EOF</pre><p>Let’s test this:</p><pre>$ sysbench - num_retries=10 ./custom_command.lua prepare</pre><pre>sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)<br>**** test_data_csv: fixture.csv<br>**** num_retries: 10</pre><p>Note that num_retries gets the overridden value from the command line, where as test_data_csv retains the default value specified in the script.</p><h4>Custom commands</h4><p>If the support for built-in commands with custom options is not adequate for any reason, sysbench also provides a way to specify custom commands. Let’s take a look at an example:</p><pre>$ cat &gt;custom_command.lua &lt;&lt;EOF</pre><pre>function cmd_generate_test_data()<br>  print(&quot;**** cmd_generate_test_data() called.&quot;)<br>end</pre><pre>sysbench.cmdline.commands = {<br>  generate_test_data = {<br>    cmd_generate_test_data,<br>    sysbench.cmdline.PARALLEL_COMMAND<br>  },<br>}</pre><pre>EOF</pre><p>Once again, a quick test shows this in action:</p><pre>$ sysbench ./custom_command.lua generate_test_data</pre><pre>sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2)</pre><pre>**** cmd_generate_test_data() called.</pre><h3>In Conclusion…</h3><p>While sysbench is popular for it’s built-in support of various benchmarks, it is also quite customizable through its support for Lua scripting. This post discussed 3 different customization mechanisms: built-in commands, custom options, and custom commands. Complex use cases may find it useful to leverage all of the 3 customization mechanisms.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6d5338a503b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>