<?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 Ahmed Galal on Medium]]></title>
        <description><![CDATA[Stories by Ahmed Galal on Medium]]></description>
        <link>https://medium.com/@ahmed1g1hamed?source=rss-2619d078d975------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bNEeQxSsYAX476DxiHiFAA.jpeg</url>
            <title>Stories by Ahmed Galal on Medium</title>
            <link>https://medium.com/@ahmed1g1hamed?source=rss-2619d078d975------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 05:55:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ahmed1g1hamed/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[How databases store data? (well, it is mostly done using B-Trees)]]></title>
            <link>https://medium.com/@ahmed1g1hamed/how-databases-store-data-well-it-is-mostly-done-using-b-trees-613150e611a9?source=rss-2619d078d975------2</link>
            <guid isPermaLink="false">https://medium.com/p/613150e611a9</guid>
            <category><![CDATA[database-internals]]></category>
            <category><![CDATA[ssd-vs-hdd]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[b-tree]]></category>
            <dc:creator><![CDATA[Ahmed Galal]]></dc:creator>
            <pubDate>Fri, 13 Nov 2026 10:27:12 GMT</pubDate>
            <atom:updated>2026-11-13T10:37:30.171Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the fundamental concepts to help us understand databases and how they work internally is knowing the data structure it uses to store its data. Spoiler alert, they mostly use B-Trees. But why not other data structures? we will find out.</p><h3>Taking A Step Back To Memory</h3><p>Let’s begin by taking a step back to the realm we easily understand: The Volatile Memory. What if we need a data structure that balances reads &amp; writes overheads? We have two good choices here:</p><ol><li><strong>A Hash table:</strong> it offers <strong>O(1)</strong> for insertions and <strong>O(1)</strong> for lookups [ignoring the cases of collisions].</li><li><strong>A BST: </strong>it offers O(log(n)) on average for insertions and O(log(n)) for lookups.</li></ol><h3>Adapting Our Data Structures for Disk Storage</h3><p>But before we do that, there is something worth noting: disks are fundamentally different from memory.</p><h4>A. How Disks Are Different?</h4><ol><li>HDDs consist of sectors, and positioning the read/write head over the correct track and sector is expensive.</li><li>SSDs are split into pages which form block and blocks form planes which form a die. On read operations we have to read entire pages (so when reading a single record we will fetch other data with it too) and on delete operations we have to delete an entire block.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*3Hgwb7UJ4wV5SdKcWYUWDg.png" /></figure><h4>B. Can we use Hash tables &amp; BSTs here?</h4><ol><li><strong>Hash tables Case:</strong> it is not worth using it as the main data structure as in HDDs the lookup and insertion operations will require to move the head of the arm per operation which can be a nightmare for queries that are dealing with ranges (imagine we are filtering on <em>age &gt; 25</em> this will search the entire table) and if we are using SSDs things are not better at all as lookup for N records will result in N*M data records fetched (where M is the number of the records in the page).</li><li><strong>BST Case: </strong>Things are better here as in case of HDDs moving 1 level down the tree will require the positioning of the head of the arm so lookups and insertion will have O(log(n)) on average head positionings. However, the insertion can be O(n) if the BST is not balanced and trying to balance it frequently is expensive on the disk. In the case of SSDs, we have the problem of reading in pages and removing in blocks so fetching N sequential records will fetch N*M records her too.</li></ol><h4>C. Let’s Improve on BST to Better Suit Disks</h4><p><em>first of all, let’s draw the BST slightly differently from what we are used to (this will help us later)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jhiN7nYy5EyeVhVVSb10Dg.png" /><figcaption>BST</figcaption></figure><p><strong>We will make the following tweaks:</strong></p><ol><li><strong>Increase the number of children per node (aka fan-out)</strong> which will reduce the number of disk seeks as increasing the number of children will reduce the height of the tree (improvement for HDD case).</li><li><strong>Make each node fit into page</strong> so, one page read now fetches an entire batch of sequential records (improvement for SSD case).</li><li><strong>Decrease the number of balancing/splitting operations</strong> by storing values on the leaf nodes only and leave other nodes with keys only which will result in updates altering the last level only for most of the time (This structure is called a B+ Tree, a commonly used variant of B-Trees.).</li></ol><p>The resulting structure resembles a B-Tree<em>:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*AcgA1o0V1jkwF5hHjwtBtw.png" /><figcaption>B-tree</figcaption></figure><h3>Summary</h3><p>we started by picking the best data structures for volatile memory and the differences between the memory and the disk which made us think about BSTs for disks but with tweaks.</p><p>There are many different types of data structures that are used in databases such as LSM-Trees and there are many variations of the B-Tree, and I would recommend Database Internals by Alex Petrov for further information on this topic.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=613150e611a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[When Throwing Away Legacy Code Is Better Than Refactoring It]]></title>
            <link>https://medium.com/@ahmed1g1hamed/when-throwing-away-legacy-code-is-better-than-refactoring-it-6c2fe393e016?source=rss-2619d078d975------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c2fe393e016</guid>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[dotnet-core]]></category>
            <category><![CDATA[legacy-code]]></category>
            <category><![CDATA[web-forms]]></category>
            <category><![CDATA[dotnet]]></category>
            <dc:creator><![CDATA[Ahmed Galal]]></dc:creator>
            <pubDate>Tue, 21 Jul 2026 08:24:22 GMT</pubDate>
            <atom:updated>2026-07-21T08:30:08.330Z</atom:updated>
            <content:encoded><![CDATA[<p>The standard advice for legacy code is simple: introduce unit tests, then refactor. In one project, we didn’t do that and it was the right decision.</p><p>One of the main techniques for refactoring that you can find in books like “<em>Working Effectively with Legacy Code”</em> is to use unit tests in refactoring legacy code. <strong>This idea is actually useful for a number of reasons:</strong></p><ul><li>Unit tests serve as documentation of behavior.</li><li>Introducing tests often encourages separating business logic from infrastructure dependencies.</li><li>Changes are made with a reference point, so the quality of the code will increase over time.</li></ul><h3>A Case To Study</h3><p>Consider this case: A system written in WebForms where all developers are familiar with modern languages and frameworks like .NET Core, Java Spring, etc. This is in addition to the problematic structure of WebForms, which mixes UI with logic in one big fat file. And, of course, there is no documentation just like any amazing legacy code.</p><p>Every change consisted of reading unfamiliar code, then trying to make the smallest possible modification while hoping nothing broke in the process.</p><p>No, we are not done with the disaster yet. The WebForms system is a subsystem of a bigger system that manages data for 1M+ users, and it periodically copies data from the main DB into a separate DB while introducing new tables for itself.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/521/1*f1puo-p3zAn5aGNRZ8Moew.jpeg" /></figure><h3>Solution</h3><p><strong>Well, in this particular case, investing in unit tests would have been a poor use of our limited time for a number of reasons:</strong></p><ul><li>The system structure is a mess, and rebuilding one with the correct structure will be faster. Plus, it was a business decision to get rid of it over time anyway.</li><li>Making devs learn WebForms to make changes will take more time than skimming the code, understanding the logic, and rebuilding it in a modern language.</li><li>Not getting rid of it will require the onboarding process for each new employee to include understanding WebForms.</li></ul><p>We needed to get rid of this disaster as fast as possible, but we needed documentation too. So, here is what was done.</p><p>Let’s break our problem into two separate problems.</p><p><strong>First: The DB Problem<br></strong>1. During a weekend, the system was shut down.<br>2. A script was run copying any unique data from the isolated DB into the main DB.<br>3. The script steps were documented so anything like overwriting data that was corrupt can be seen as to why it happened.<br>4. Made the system reference the same DB, not the isolated one.</p><p>Now we have achieved a single source of truth.</p><p><strong>Second: The System Itself<br></strong>1. The system was mainly a dashboard.<br>2. Each dashboard page was rebuilt in .NET Core.<br>3. Every migrated page included a short document describing the original behavior, edge cases, and assumptions we made.<br>4. Made the dashboard redirect to the new pages one by one.<br>5. Instead of unit testing every little change, we relied on validation from the employees who used these dashboards every day on a sandbox database before going into production.</p><h3>Conclusion</h3><p>Sometimes using conservative ways like unit testing as a way of refactoring is not the optimal path, as sometimes the cost of throwing away legacy systems is just cheaper.</p><p>Before you ask “How should we refactor this?”, ask “Should we refactor it at all?”</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c2fe393e016" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>