<?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 Etti Gur on Medium]]></title>
        <description><![CDATA[Stories by Etti Gur on Medium]]></description>
        <link>https://medium.com/@etti.gur_39776?source=rss-f7d429ca9768------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*oAtMKlugf7HgOrsJexvDAA.jpeg</url>
            <title>Stories by Etti Gur on Medium</title>
            <link>https://medium.com/@etti.gur_39776?source=rss-f7d429ca9768------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 07:05:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@etti.gur_39776/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[Selective data deletion in Druid]]></title>
            <link>https://medium.com/@etti.gur_39776/selective-data-deletion-in-druid-7f5c663c8ead?source=rss-f7d429ca9768------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f5c663c8ead</guid>
            <category><![CDATA[retention-strategies]]></category>
            <category><![CDATA[druid]]></category>
            <category><![CDATA[big-data]]></category>
            <category><![CDATA[data-ingestion]]></category>
            <category><![CDATA[data-retention]]></category>
            <dc:creator><![CDATA[Etti Gur]]></dc:creator>
            <pubDate>Fri, 28 Jan 2022 14:04:46 GMT</pubDate>
            <atom:updated>2022-02-10T13:07:23.530Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YAAX8Z8Tp7Cm0fIRdFSEWQ.png" /></figure><p>In 2019, my colleagues (<a href="https://www.linkedin.com/in/assadana/">Dana Assa</a> and<a href="https://www.linkedin.com/in/itaiy/"> Itai Yaffe</a>) wrote a detailed <a href="https://medium.com/nmc-techblog/data-retention-and-deletion-in-apache-druid-74ffd12398a8">blog post</a> about Data retention and deletion in Druid, specifically about “Dimension-based TTL.”</p><p>In this article, I will discuss a use case where <strong>“Dimension-based TTL” </strong>was<strong> needed </strong>but<strong> did not work </strong>as expected and<strong> </strong>how we managed to<strong> make it work</strong>.</p><h3>What is “Dimension-based TTL”?</h3><p>In the blog post I mentioned, Dana and Itai present a unique option of <strong>using TTL</strong> not based on the timestamp of the data but instead on the<strong> value</strong> (or values) of one of the<strong> dimensions</strong> in the Druid datasource.</p><p>To quickly recap, the post shows an example of how we can keep all the rows of <strong>the latest day</strong> for dimension value “AAA” and all the rows of <strong>all days</strong> for dimension value “BBB” by specifying <strong>a filter within the ingestion spec</strong> of your indexing task.</p><p>Consider the example given in the post, which showed the aggregated data collected for impressions of 2 advertising campaigns (“Bebsi” and “Boba Cola”):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*e46oWW1uGKC3fpor" /></figure><p>The goal was to retain all rows with “campaign_name=Boba Cola” for one day and “campaign_name=Bebsi” for 30 days.</p><p>The solution was to <strong>ingest the</strong> <strong>latest day</strong> of data for <strong>both campaigns</strong> and <strong>combine it with the existing data in Druid</strong> while filtering in only existing rows belonging to <strong>Bebsi</strong> from the last 30 days.</p><p>This way:</p><ol><li>We drop all<strong> </strong>older rows of <strong>Boba Cola</strong> that existed in Druid before the ingestion</li><li>We drop all rows of <strong>Bebsi</strong> older than 30 days as well</li></ol><p>Leaving us with only new rows (from the latest day) of <strong>Boba Cola</strong> and 30 days (previous 29 days plus the latest day) of <strong>Bebsi</strong>.</p><p>A snippet from our<a href="https://druid.apache.org/docs/latest/ingestion/hadoop.html"> Hadoop-based Batch Ingestion</a> task:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*a6qSqdyHDVNZE0hs2Tj7gA.png" /></figure><h3>New requirement</h3><p>A couple of months ago, we got <strong>a new requirement</strong>:</p><p>To store the latest day of each of the two campaigns in Druid, <strong>the data for each campaign</strong> will be loaded from a different source, and therefore in two different ETL pipelines, meaning — <strong>separate ingestion tasks</strong>.</p><p>On top of that, we still need to <strong>keep the data for both campaigns in the same datasource</strong> since clients query this data is in a unified manner, treating this data as one table.</p><p>You might think: “Well, this simplifies it all! We can just set a retention period of one day on the datasource, and that’s it!”</p><p>Well — not quite.</p><p>Yes, we run the two ingestion tasks daily, and yes — we usually succeed, and the latest day of each campaign will be, in fact, yesterday (as it should be, since every day we process and ingest the data of the previous day).</p><p>But we have to be ready for the scenario where one or even both ingestion tasks would fail. In that case, we’d want to keep the data we already have in Druid (i.e., from the last successful execution of ingestion) and <strong>not drop it</strong> (which is what would’ve happened if we used the timestamp-based TTL).</p><p>For example, if we have a failure in, say, <strong>Bebsi</strong> data ingestion, and the <strong>Boba Cola</strong> data succeeded — we’d expect the Druid datasource to have <strong>Boba Cola</strong> data for yesterday and <strong>Bebsi</strong> data for the day before yesterday, like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dM39rEWl7wrbbv2M" /></figure><p>Timestamp-based TTL would drop the segments of the day before yesterday:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*siDx_okCuobV6PvY" /></figure><p>which is <strong>not</strong> what we want.</p><h3>What was missing?</h3><p>Let’s see through an example.<br>If we have data for the latest day for both campaigns in Druid:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ltohURVDPRzL9W8r" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nujnWk4uU_6FM-rl" /></figure><p>Data of campaigns on Druid — note the y-axis is simplified to small numbers to show the existence of data and not the actual relative values.</p><p>Running the ingestion task for <strong>Bebsi</strong> data for 2021–09–02 with a <strong>filter</strong> of only <strong>Boba Cola</strong> data would leave us with:</p><ol><li><strong>Boba Cola</strong> data for 2021–09–01</li><li><strong>Bebsi</strong> data for 2021–09–02</li></ol><p>Therefore the older data for <strong>Bebsi</strong> was dropped as expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9ePS0RKm7N_DTU8e" /></figure><p>But once we would run the second ingestion task, this time for <strong>Boba Cola </strong>(i.e. ingesting <strong>Boba Cola</strong> data of 2021–09–02 with <strong>filtering in</strong> only <strong>Bebsi</strong> data) we would <strong>expect</strong> to see the older data for the <strong>Boba Cola</strong> on 2021–09–01 dropped, and have only 2021–09–02 for both campaigns, having no data whatsoever on 2021–09–01. But, <strong>the actual result</strong> was we’d see the data for <strong>both</strong> campaigns for 2021–09–02, but also we still see the data for <strong>Boba Cola</strong> for 2021–09–01 (it wasn’t dropped 😔 )</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*etFqIEEhh4OFa7pf" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*p2V20dwdn4zfRpK4" /></figure><p>This result made us suspect that deleting data from a Druid segment will only work when the segment that contains data to be deleted has some <strong>other data </strong>which needs to be retained after the ingestion. Otherwise, if the data that should be deleted happens to be the only data in this segment — the result will be that this segment will not be touched and remain undeleted.</p><p>In our example, the segment of <strong>2021–09–01</strong> has<strong> no data left </strong>in it after we apply the <strong>filter</strong> in the ingestion task. In this case, we <strong>filter in</strong> only data for <strong>Bebsi, </strong>and since we do not have any data of Bebsi on this date — no data will be filtered in. The segment <strong>will not be included in the ingestion process; therefore</strong>, it <strong>will not be modified</strong>, leaving us with older data for <strong>Boba Cola</strong> (that should have been otherwise deleted).</p><h3>The twist!</h3><p>To have each segment included in the ingestion, regardless of our filters, we added each segment with one dummy row that will be left in after the filter is applied:</p><p><strong>Before ingestion</strong> — in the upstream process that prepares the <strong>Bebsi</strong> campaign data for ingestion (Apache Spark, in our case), create <strong>one dummy row</strong>:</p><p><strong>Timestamp</strong> = same as the other <strong>Bebsi</strong> data in this execution cycle,</p><p><strong>Campaign_name</strong> = “Other”,</p><p><strong>Impressions</strong> = 1</p><p>For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2WImsVqwTh0nZ8td" /><figcaption>Dummy row example</figcaption></figure><p>Add this row to the rest of the <strong>Bebsi</strong> campaign data being processed.</p><p>Then,<strong> </strong>ingest the complete dataset into Druid, which would result in <strong>Bebsi</strong> rows plus one <strong>Other</strong> (dummy) row in that segment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JpjLcfHmd_XgRpTP" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/0*xdJqvAVE0Gy-ImX0" /></figure><p><strong>Next</strong>, once we ingest the <strong>Boba Cola </strong>campaign data — it will be added to that segment of the date.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*xzSzUrkxtLBkdrG8" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/526/0*ooIm8G9A-kMW8JNT" /></figure><p><strong>Every time</strong> we run an ingestion task for one of the campaigns, we would filter out the existing rows of the campaign we are <strong>currently ingesting</strong>:</p><p>E.g., ingest <strong>Boba Cola</strong> with filter “campaign != Boba Cola”, which essentially means filter-in existing rows from all other campaigns (“<strong>Bebsi</strong>,” “<strong>Other</strong>”) and drop all existing “<strong>Boba Cola</strong>” rows (excluding the newly-ingested rows).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/509/0*5aF65-HeOH7YQ1cN" /></figure><p>Same for <strong>Bebsi</strong>: ingest <strong>Bebsi</strong> with a filter campaign != Bebsi which essentially means filter-in existing rows from all other campaigns (“<strong>Boba Cola</strong>,” “<strong>Other</strong>”) and drop all existing “<strong>Bebsi</strong>” rows (excluding the newly-ingested rows).</p><p>That one dummy row allows the ingestion to include the relevant segment even after filtering. This approach guarantees we always keep one day of data from each campaign, regardless of whether or not the most recent ingestion task was successful.</p><p>If we continue ingesting the next day (2021–09–02) — we first ingest <strong>Bebsi</strong> campaign data for 2021–09–02 (remember — it also includes the dummy row, where campaign_name = Other for the same date) and we <strong>filter in</strong> the existing data where campaign_name != Bebsi, we would end up with the following (notice the older data of <strong>Bebsi </strong>for<strong> </strong>2021–09–01 is now gone):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7_v4XajGY9nsVoTD" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/0*8m6XOtAQ8kr7Jd7L" /></figure><p>When we ingest the <strong>Boba Cola</strong> campaign data for the same date (2021–09–02), and we <strong>filter in</strong> the existing data where “campaign_name != <strong>Boba Cola</strong>”, we would end up with the following (notice the older data of <strong>Boba Cola </strong>for<strong> </strong>2021–09–01 is also gone):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zvgq7o__d4twQQWZ" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/524/0*eFtJIjm9JqiGVdar" /></figure><p>This means we added new data for both campaigns, and we dropped all previously-existing data of 2021–09–01, leaving just the dummy rows (which have zero impact on the queries executed on this datasource).</p><p>Below you can see how the segments in our production Druid datasource look like on a regular day after successfully ingesting both datasets. The tall bar is the data for the latest day for <strong>Boba Cola</strong> and <strong>Bebsi,</strong> and the low tail is the dummy segment we keep to make the drop of the old data possible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BrVC-fiEFvW15ebh" /></figure><p>Druid on production — the data spread over the date segments on a regular day</p><h3>Summary</h3><p>The challenge was — how can we <strong>delete data selectively based on dimension value</strong>? (as Delete FROM datasource WHERE dimension == ‘val&#39;)</p><p>We understand that:</p><ul><li>Load/drop rules are not useful for selective data deletion.</li><li>Reindex on ingestion — with filter specified — will only work when the segment that contains data to be deleted has some <strong>other data </strong>which needs to be retained <strong>after</strong> the ingestion. Otherwise, if the data that should be deleted happens to be the only data in this segment — the result will be that <strong>this segment will not be touched</strong> and remain undeleted.</li><li>To overcome this behavior, we need to artificially keep data (one dummy row) that will always persist on all our segments — this data will remain after deletion and will allow the segment to be involved (and therefore changed) in all ingestion deletion.</li></ul><p>By leveraging the technique of adding one dummy row to each date segment, we deleted data based on dimension value(s), despite the restriction we found in Druid.</p><p>I hope you’ll find this blog post useful 🙂</p><p>Big thanks to <a href="https://medium.com/u/db3ddcbe6de9">Itai Yaffe</a> for the technical advice. <br>Please feel free to comment below or reach out to me via<a href="https://www.linkedin.com/in/etti-gur-7537bb40/"> my Linkedin</a> — your feedback is welcome!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f5c663c8ead" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>