<?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 Bernardo Batista on Medium]]></title>
        <description><![CDATA[Stories by Bernardo Batista on Medium]]></description>
        <link>https://medium.com/@bdcsbatista?source=rss-da15d94df5e9------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9FxJ0nG_vyxJuCztalBXqg.jpeg</url>
            <title>Stories by Bernardo Batista on Medium</title>
            <link>https://medium.com/@bdcsbatista?source=rss-da15d94df5e9------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 00:15:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bdcsbatista/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[Data Archiving and Recovery in OutSystems with Supabase: Best Practices for Efficiency and…]]></title>
            <link>https://medium.com/@bdcsbatista/data-archiving-and-recovery-in-outsystems-with-supabase-best-practices-for-efficiency-and-f80f423f3ae8?source=rss-da15d94df5e9------2</link>
            <guid isPermaLink="false">https://medium.com/p/f80f423f3ae8</guid>
            <category><![CDATA[low-code]]></category>
            <category><![CDATA[outsystems-development]]></category>
            <category><![CDATA[low-code-development]]></category>
            <category><![CDATA[outsystems]]></category>
            <category><![CDATA[data-archiving]]></category>
            <dc:creator><![CDATA[Bernardo Batista]]></dc:creator>
            <pubDate>Tue, 08 Apr 2025 09:39:33 GMT</pubDate>
            <atom:updated>2025-04-08T09:39:33.524Z</atom:updated>
            <content:encoded><![CDATA[<h3>Data Archiving and Recovery in OutSystems with Supabase: Best Practices for Efficiency and Reliability</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y-ZrPuj3_z3Zs0AJKmVTkA.jpeg" /></figure><h3>Introduction</h3><p>In today’s fast-moving digital environment, applications must handle large volumes of data without sacrificing performance. As databases expand, maintaining speed, scalability, and efficiency becomes increasingly challenging. This is where data archiving plays a crucial role.</p><p>Data archiving allows applications to differentiate between frequently accessed “active” data and older, less-used records. By doing so, it helps maintain performance while still enabling the retrieval of historical data when needed.</p><p>In OutSystems, implementing a comprehensive data archiving and recovery strategy is essential to ensure applications remain streamlined, responsive, and scalable.</p><p>In this article, we will walk through the processes of archiving and recovering data to and from an archive database. Along the way, we’ll address common challenges — such as handling errors during these processes — and implement solutions to ensure data integrity.</p><h3>Understanding Data Archiving and Recovery</h3><p><strong>Data archiving</strong> is the process of moving inactive data to a separate storage location, helping to improve application performance by reducing the volume of data in the active database.</p><p><strong>Data recovery</strong> is the process of restoring archived data back to the active database when needed, ensuring that valuable historical information remains accessible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*scFC1Bmv5AH2p2EPkCgrZA.png" /><figcaption>Overview of the Recovery and Archive processes.</figcaption></figure><p>For example, imagine the following scenarios:</p><ul><li><strong>Scenario</strong>: A telecom company archives support tickets that are older than 6 months or have been marked as resolved.</li><li><strong>Why it makes sense</strong>: This reduces the load on the active database, but archived tickets can still be retrieved if needed for auditing, reporting, or customer reference.</li></ul><p>OR</p><ul><li><strong>Scenario</strong>: A retail business archives sales transactions that are older than 2 years.</li><li><strong>Why it makes sense</strong>: This helps maintain fast query performance on recent transactions while ensuring that older sales data remains accessible for financial reports or tax audits.</li></ul><h3>Storing Active vs. Archived Data: OutSystems and Supabase Integration</h3><p>In any application, it’s essential for <strong>active data</strong> — information that is frequently accessed and updated — to be readily available in a high-performance database.</p><p>For this reason, our application’s active data will be stored in the <strong>OutSystems database</strong>. However, over time, not all records need to remain in the active database. Some information, such as <strong>historical data</strong>, <strong>infrequently accessed records</strong>, or entries in a <strong>specific state </strong>(e.g., completed, canceled, or inactive), can be archived to reduce the load on the main database while still remaining accessible if necessary.</p><p>To efficiently manage archived data, we’ll use a <strong>PostgreSQL database provided by </strong><a href="https://www.supabase.com/"><strong>Supabase</strong></a><strong> </strong>as our archive database. In a follow-up article, I’ll dive deeper into Supabase integration and how to set it up. For now, all we need to know is that <strong>Supabase will act as our archive database</strong>.</p><p><strong>Note:</strong> All communication with the archive database (e.g., creating or fetching records) is done via <strong>HTTPS requests</strong>.</p><h3>What are we archiving?</h3><p>For the purpose of this article, we will be archiving data from an entity called <strong>Request</strong>. This table will exist in both databases, with some minor differences. Since the specific attributes are not crucial to our discussion, we’ll keep it simple:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*6nc1vBFwsu5ky12mcSx5OA.png" /><figcaption>Request and RequestArchive tables.</figcaption></figure><p>The table in the archive database — <strong>RequestArchive</strong> — has a few differences: it omits the <strong>createdAt</strong> and <strong>IsArchived</strong> attributes and introduces an <strong>ArchiveJobUUID</strong> identifier.</p><p><strong>Note:</strong> The <strong>RequestArchive</strong> table is represented in the image as an OutSystems Entity for simplicity. However, it will exist only in the <strong>Archive Database</strong> — in our case, <strong>Supabase</strong>.</p><h3>Archive Process</h3><p>Here’s what the archive process will look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ki8lGEUlJ__Jd4xgkgsoEw.png" /><figcaption>Overview of the archive process.</figcaption></figure><ol><li>The records to be archived are first written to the <strong>archive database</strong>.</li><li>Once this process is successful, the records are <strong>purged from the main database</strong>.</li></ol><p>To support the archiving functionality, we need to create a few additional <strong>Entities</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/1*RWdaMuMREkwaWX_s7alM3w.png" /><figcaption>More information table attributes can be found <a href="https://gist.github.com/atbernardodev/542db9294f7c09ed03d88037fd5c16fc"><strong>here</strong></a>.</figcaption></figure><p>Here’s a breakdown of the <strong>archive process</strong>:</p><ul><li>At a predefined <strong>off-peak time</strong>, an asynchronous job runs in the OutSystems environment. The execution frequency — whether daily, weekly, monthly, or another schedule — depends on<strong> business needs and requirements</strong>. Running it during off-peak hours <strong>ensures minimal impact on system performance</strong>.</li><li>This timer will first check if there are any records that match the criteria for the archival — In our case, it will be all records created more than <strong>X days ago</strong> (based on the business logic defined in a <strong>site property</strong>).</li><li>If so, we start the archive process. The archive process will be separated into chunks and we’ll implement a soft time out mechanism to prevent any unwanted abortions. To check the best practices regarding this type of mechanism, I recommend reading <a href="https://itnext.io/how-to-manage-heavy-timers-in-outsystems-6c954789783e">How to Manage Heavy Timers in OutSystems | by Ricardo Pereira | ITNEXT</a>.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*zLIJk2yOKqlZgqJ5YgQGcg.png" /><figcaption>Archive Process Timer.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*xlO-qntjgRS9c9wHkaPjeA.png" /><figcaption>Process Archive Server Action.</figcaption></figure><ul><li>An entry is created in the <strong>ArchiveJob entity</strong>. The <strong>GUID</strong> of this record is used to track the archived records in the <strong>archive database</strong>, allowing for a rollback if needed.</li><li>The identified records are then <strong>sent to the archive database</strong> via a <strong>REST API call</strong> and inserted into the <strong>archive table</strong>.</li><li>If the records are successfully stored in the <strong>archive database</strong>, the IsArchived flag of the record is set to <strong>true</strong>. This operation is performed by the <strong>ArchiveRecordsFromMainCatalog</strong> server action, that calls the following SQL statement:</li></ul><pre>WITH CTE(RequestId) AS (<br>  SELECT TOP (@NrOfRecordsToArchive) {Request}.[Id]<br>  FROM {Request}<br>  WHERE DATEDIFF(DAY, CAST({Request}.[CreatedAt] AS DATE), @CurrDate) &gt; @DiffDaysDateThreshold<br>    AND {Request}.[IsArquived] = 0<br>  ORDER BY {Request}.[Id] ASC<br>)<br><br>UPDATE {Request}<br>SET {Request}.[IsArquived] = 1<br>FROM {Request}<br>INNER JOIN CTE ON {Request}.[Id] = CTE.RequestId</pre><ul><li>If, for some reason, an error occurs, the archive process stops and the information about the failure can be later revised by an admin in a back office screen.</li></ul><h4>Purging Records From the Active Database</h4><p>Storing inactive or outdated data in the system takes up extra storage space and demands more resources, such as memory and processing power, to maintain performance. It also makes database backups and maintenance more complex, potentially leading to costly and avoidable hardware upgrades.</p><p>Once the data is successfully archived, it can be <strong>removed from the active database</strong>. To achieve this, we need a <strong>timer</strong> that periodically checks for records ready to be <strong>archived</strong>. If such records exist, it triggers a <strong>bulk delete SQL statement</strong> to remove them from the active database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7QbQ6DQ2SMR293Iv8H1mlg.png" /><figcaption>Purge Records Timer.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*RdHOREXMCwRyOCrp91hTPg.png" /><figcaption>Purge Records Server Action.</figcaption></figure><p><strong>Notes:</strong></p><p><strong>You should also consider the scenario where the bulk statement errors for some reason. For simplicity, that scenario is not covered in this article.</strong></p><h4>Error Handling</h4><p>Although this may seem like a straightforward process, we need to consider a few potential scenarios:</p><ol><li>The <strong>POST</strong> call to the database can fail for various reasons (e.g., <strong>server unavailability</strong>).</li><li>The <strong>POST</strong> call succeeds, but the <strong>bulk update</strong> of the records in the active database fails (e.g., raises a <strong>database exception</strong>). How should we handle this?</li></ol><h4>Handling the First Scenario:</h4><p>For the first scenario, we can implement a simple <strong>retry mechanism</strong> that attempts to hit the API endpoint three times before ending execution. On failure, the <strong>Archive Job </strong>HasRanSuccessfuly flag is set to <strong>false</strong>, and the ArchiveJobErrorTypeIdattribute is set to <strong>Entities.ArchiveJobErrorType.MainDatabase</strong>. This information can later be displayed on a back-office dashboard where the admin can review failed archive jobs and the reasons for failure.</p><h4>Handling the Second Scenario:</h4><p>The second scenario is more complex since the records from the active database have already been written to the archive database. How to overcome this depends on your scenario:</p><ul><li><strong>Option 1:</strong> You could wrap both the archive and insert operations within a <strong>single transaction</strong>. This ensures that either both operations succeed or neither does, preserving data atomicity. However, this becomes tricky when dealing with two separate databases, as <strong>distributed transactions</strong> (2-phase commits) can be complex and are not natively supported in every setup.</li><li><strong>Option 2:</strong> You could design a <strong>Compensating Transaction</strong>, commonly referred to as a <strong>Rollback</strong>, to remove the archived records from the archive database.</li><li><strong>Option 3:</strong> You could mark the records that failed and implement a <strong>“Retry Job”</strong> to periodically check for these failed operations and retry them.</li></ul><p>In our case, we will implement <strong>Option 2</strong> — the <strong>Rollback</strong>.</p><h4>Rollback mechanism</h4><p>To implement the <strong>Rollback</strong> mechanism, we will design a <strong>Timer</strong> that fetches all the <strong>Archive Jobs</strong> that failed. We’ll then process each archive job sequentially to rollback the records that have been written in the archive database. How?</p><p>One of the attributes we introduced in the <strong>archive database</strong> is the <strong>archive_job_UUID</strong>. So, for each failed archive job, all we need to do is delete the records in the archive database where archive_job_uuid = CurrentArchiveJob.Id.</p><p>To achieve this, we will call a function that we will define in the second part of this article:<br><strong>rollback_added_records(arc_job_uuid UUID)</strong>. This function essentially deletes all the records associated with the given archive job identifier from the archive database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/513/1*AY_TYUfzw1fKcz5FwoaPoA.png" /><figcaption>Rollback Timer.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KrMCMp-aoY6X1gFJZPoDuw.png" /><figcaption>RollBackArchiveJob Server Action.</figcaption></figure><p>Similarly to failed attempts to archive data, failed rollbacks can later be displayed in the <strong>back office</strong> for the admin to analyze.</p><h3>Recovery Process Overview</h3><p>One of the reasons we archive data is that, even though it may not be relevant today, we never know when we might need to recover and use it again. This section of the article focuses on the recovery mechanism from the archive database to the active database.</p><p>Here’s what the archive recovery process will look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F1L65uQ-LyDjIXhy9-tTCg.png" /><figcaption>Overview of the recover process.</figcaption></figure><ol><li>The archived records will be fetched from the archive database and written to the OutSystems database.</li><li>If this operation is successful, the recovered records are then deleted from the archive database.</li></ol><p>To support the recovery functionality, we need to create additional entities:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*K55juiVEzzekoycxKXMadg.png" /><figcaption>More information table attributes can be found <a href="https://gist.github.com/atbernardodev/c3d0df9b4e09fd98cd3a4d7ef2f6519e">here.</a></figcaption></figure><ul><li><strong>RecoverJob</strong>: Stores information about a data recovery job.</li><li><strong>RecoverJobBucket</strong>: Stores information about the buckets created for each recovery job.</li><li><strong>RecoverJobBucketRecord</strong>: Stores information about the records in a bucket that failed to be recovered.</li></ul><p>Here’s what the data recovery process looks like:</p><ul><li>A REST call is made to the archive database to fetch the records based on selected filters.</li><li>Once received, the records are serialized into a JSON object and then converted into binary.</li><li>Next, an entry is created in the <strong>RecoverJob</strong> entity, and the <strong>Timer_ProcessRecoveryRequest</strong> is triggered</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tltoX6s4RLJjM0LLMJ1LlA.png" /><figcaption>RecoverArchivedRequests Server Action.</figcaption></figure><p>To process the data more efficiently, we will leverage the use of light BPTs by dividing the data into buckets and processing each bucket individually. The <strong>Timer_ProcessRecoveryRequest</strong> handles this logic:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1007/1*GVD1jJw6n8KgE2V7qFRq0A.png" /><figcaption>Timer to process recover request.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/782/1*TMZ_lDn5GU-Op3wqZdz-Kg.png" /><figcaption>Process Recover Request Server Action.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/422/1*MnZpxwNpqcwKD-tRdVTH6A.png" /><figcaption>CreateRecoverBucket Server Action.</figcaption></figure><p>The <strong>Create_Bucket</strong> action will trigger a process that recovers the bucket’s records back into the active database:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/1*nqgFKTE1N4MPzKOwG-YK_A.jpeg" /><figcaption>Create_Bucket Server Action.</figcaption></figure><p>If a bucket’s record fails to be recovered, an entry will be created in the <strong>RecoverJobBucketRecord</strong> entity. These records can later be displayed in a back office for the user to analyze.<br>At this point, the data has been successfully recovered from the archive database into the active database.</p><h4>Now, we face another important decision: Do we keep the records we just recovered in the archive database or not?</h4><p>This decision depends on several factors related to your application’s requirements, retention policies, and business needs. You might want to keep the data for purposes such as Audit Trail, Legal Compliance, or Business Continuity. Alternatively, you might choose to delete the data to avoid Data Redundancy or if your organization has strict data retention policies requiring data to be deleted from the archive database once it is recovered.</p><p>For the purpose of this article, we will delete the data from the archive database.</p><p>To do this, we will implement a timer that runs periodically to fetch the Recover Jobs that have been processed. Then, for each Recover Job, we will retrieve the associated Buckets and, for each bucket, run a process responsible for deleting the respective records from the archive database.</p><p>It’s important to note that, for each bucket, some records may have been successfully recovered while others may have errored. We will only delete the records that were successfully recovered from the archive database, as the others will require further analysis to determine the cause of failure and the next steps.</p><p>In Part 2, we will create the function responsible for this process: <strong>delete_records(successful_ids text, unsuccessful_ids text)</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M1ELZBQ0M6rzWxQ-t1-XTA.png" /><figcaption>DeleteRecoverRecords Server Action.</figcaption></figure><h3>Final Considerations</h3><p>Before wrapping things up, there are a few final points I’d like to leave you with. These last technical considerations will help you fine-tune your data archiving solution, ensuring it’s efficient, scalable, and well-suited to your business needs as it grows. Let’s take a quick look at some things you might want to keep in mind.</p><h4><strong>Database Indexes</strong></h4><p>In a <strong>data archiving process</strong>, the primary goal is typically to <strong>optimize write performance</strong> rather than <strong>read performance</strong>. This is especially important when dealing with large volumes of data. Archiving data often involves <strong>bulk insertions</strong> or <strong>deletions</strong> of records, which are typically more resource-intensive than querying. By replicating the original database indexes on the archived table, you’re adding unnecessary overhead to <strong>write operations</strong> — which can slow down the process. For that reason, it is important to focus on optimizing the process for bulk inserts and deletes, and reserve indexing for only the <strong>key columns</strong> that may occasionally be used for specific queries.</p><h4><strong>Why you might want to opt for archiving data outside OutSystems</strong></h4><p>Archiving data outside of OutSystems databases can significantly enhance <strong>performance</strong> by easing the strain on the core system, allowing it to focus on real-time operations. It also provides <strong>cost efficiencies</strong>, as external storage options can be more affordable and scalable compared to traditional databases. By offloading historical or less-accessed data, you simplify <strong>database management</strong>, making tasks like backups and indexing faster and more efficient. Additionally, external archiving solutions can help meet <strong>compliance requirements</strong>, with built-in retention policies and better security measures. They also offer improved <strong>disaster recovery</strong> options, with features like geographically distributed backups. Ultimately, choosing external storage for archived data helps ensure a more <strong>optimized</strong>, <strong>flexible</strong>, and <strong>scalable</strong> solution as your business grows.</p><h3>Thanks for Making It to the End!</h3><p>If you enjoyed this article and found it helpful, feel free to share it with others or reach out here or via <a href="https://www.linkedin.com/in/atbernardo/">linkedin</a> if you have any questions. Your feedback is always appreciated.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f80f423f3ae8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>