<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Alexander Novikov</title>
    <description>The latest articles on DEV Community by Alexander Novikov (@alnovis).</description>
    <link>https://dev.to/alnovis</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2229822%2Fe05bd1cb-55bc-45b9-ad8d-f5fe3b1ad69f.jpg</url>
      <title>DEV Community: Alexander Novikov</title>
      <link>https://dev.to/alnovis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alnovis"/>
    <language>en</language>
    <item>
      <title>Phoenix vs ClickHouse: Why the Argument Keeps Coming Back</title>
      <dc:creator>Alexander Novikov</dc:creator>
      <pubDate>Wed, 11 Nov 2026 12:54:14 +0000</pubDate>
      <link>https://dev.to/alnovis/phoenix-vs-clickhouse-why-the-argument-keeps-coming-back-2h4a</link>
      <guid>https://dev.to/alnovis/phoenix-vs-clickhouse-why-the-argument-keeps-coming-back-2h4a</guid>
      <description>&lt;h2&gt;
  
  
  The Argument
&lt;/h2&gt;

&lt;p&gt;The system I work on keeps its sales documents in two databases, and every few weeks the same argument comes back to our standup.&lt;/p&gt;

&lt;p&gt;One side: "The data is already in Phoenix. Let's build the monthly report there. Why copy anything?" The report is a &lt;code&gt;GROUP BY&lt;/code&gt; over a few months of documents. It runs for minutes, loads the region servers, and the dashboard times out first.&lt;/p&gt;

&lt;p&gt;The other side: "It's one document with the wrong status. Just fix it in ClickHouse." The fix is an &lt;code&gt;ALTER TABLE ... UPDATE&lt;/code&gt;. It changes one row and rewrites far more than one row, and a day of such fixes leaves a queue of mutations behind it.&lt;/p&gt;

&lt;p&gt;I have argued both sides, sometimes in the same week.&lt;/p&gt;

&lt;p&gt;The usual way to settle it is a label. Phoenix is OLTP, ClickHouse is OLAP; one stores rows, the other stores columns; one is an LSM tree, the other is not. The labels are not exactly wrong, but none of them says what a given query will cost, so the argument comes back.&lt;/p&gt;

&lt;p&gt;This post compares what both engines read and write, using the same ten million documents in each on a test stand. The differences come down to four knobs. Each one comes with a small formula that predicts which workload it makes expensive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Phoenix Is a Layer over HBase
&lt;/h2&gt;

&lt;p&gt;Phoenix is a SQL layer over HBase. Its tables and indexes are HBase tables, and it compiles SQL into HBase operations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A table becomes an HBase table. The primary key columns are joined into the row key; every other column becomes a cell in a column family, under a short encoded qualifier instead of the column name.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;SELECT&lt;/code&gt; by the full primary key becomes a lookup of one row key. A range or a full &lt;code&gt;SELECT&lt;/code&gt; becomes scans, at least one per region, which the client runs in parallel.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;GROUP BY&lt;/code&gt; runs in two steps: region servers compute partial aggregates in coprocessors, next to the data, and the client merges them.&lt;/li&gt;
&lt;li&gt;A secondary index is another HBase table, and every upsert writes to it as well.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4d3j69s4r6yk2w2fvo6d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4d3j69s4r6yk2w2fvo6d.png" alt="A GROUP BY query goes to the Phoenix client, which plans one scan per region. Four regions on two region servers each scan their rows and compute partial sums. The partial sums converge back to the Phoenix client, which merges them into the result." width="800" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Phoenix decides where the work runs. What each region reads from disk is up to HBase.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Phoenix's main optimization is summing next to the data: only partial sums cross the network, not rows. It can also add work: an index write for every upsert. What it cannot change is what a region server has to read from disk to answer a query. That is up to HBase, block by block.&lt;/p&gt;

&lt;p&gt;So the argument is about Phoenix, but the storage comparison is HBase against ClickHouse's MergeTree. Counting Phoenix's overhead against HBase, or HBase's storage costs against Phoenix, would distort every comparison. From here on, the measurements use HBase directly, through its native client (Get, Put, Scan), and Phoenix comes back only where it adds or removes a cost.&lt;/p&gt;

&lt;h2&gt;
  
  
  Both Are Log-Structured
&lt;/h2&gt;

&lt;p&gt;The textbook frame says that an OLTP store is a B-tree that updates its pages in place, and an OLAP store is something else entirely. Neither engine here updates anything in place. Their write paths have the same shape:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F47hph8b8ylcoc25u3a98.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F47hph8b8ylcoc25u3a98.png" alt="Three write paths in four columns. HBase: a Put goes to the WAL and the memstore, is flushed to an HFile and merged later by compaction. ClickHouse: an INSERT block is sorted in memory, written as a part and merged later in the background. Cassandra: a write goes to the commit log and the memtable, is flushed to an SSTable and merged later by compaction." width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;All three write paths buffer writes, write an immutable sorted file, and merge the files later.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;An HBase Put goes to the write-ahead log and into the memstore, a sorted buffer in memory; at 128 MB the memstore is flushed into an HFile, and compactions later merge HFiles into bigger ones. A ClickHouse INSERT block is sorted in memory and written as a new part, a directory of column files; background merges later combine parts into bigger ones.&lt;/p&gt;

&lt;p&gt;Both are log-structured merge designs, so "LSM vs B-tree" does not describe this comparison.&lt;/p&gt;

&lt;p&gt;They also fail the same way. The first load on the stand wrote ten million rows into HBase in key order, so at any moment every write went to the one region whose range was being filled. Flushes produced files faster than compaction merged them. At 17 files in the region, one over &lt;code&gt;hbase.hstore.blockingStoreFiles&lt;/code&gt;, HBase began delaying each flush by up to 90 seconds. The memstore kept growing to its blocking size, flush size times the block multiplier, 128 MB x 4 = 512 MB, and then the region refused writes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RegionTooBusyException: Over memstore limit=512.0 M
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It kept refusing them until a compaction caught up. ClickHouse has the same failure under another name. When inserts create parts faster than merges combine them, it first slows inserts down (&lt;code&gt;parts_to_delay_insert&lt;/code&gt;) and then rejects them with "Too many parts" (&lt;code&gt;parts_to_throw_insert&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Cassandra, the third engine on the stand, belongs to the same family: commit log and memtable, flushed into SSTables, merged by compaction. It is the write path of Google's Bigtable, which HBase copied as well. Its storage choices sit close to HBase's, so it gets no separate pass through the first three knobs. It differs on the fourth, who owns a key, and returns in that section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Four Knobs
&lt;/h2&gt;

&lt;p&gt;Both engines append and merge, but they differ in four storage decisions that every log-structured engine has to make somewhere:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;knob&lt;/th&gt;
&lt;th&gt;HBase&lt;/th&gt;
&lt;th&gt;ClickHouse&lt;/th&gt;
&lt;th&gt;Cassandra&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1. rows behind one index entry&lt;/td&gt;
&lt;td&gt;a block: 64&amp;nbsp;KB of whole rows&lt;/td&gt;
&lt;td&gt;a granule: up to 8,192 rows&lt;/td&gt;
&lt;td&gt;two levels: every partition, then 64&amp;nbsp;KB inside one&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2. what one unit of read holds&lt;/td&gt;
&lt;td&gt;whole rows, every column of the family&lt;/td&gt;
&lt;td&gt;one column, compressed&lt;/td&gt;
&lt;td&gt;like HBase: whole rows&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3. what a change rewrites&lt;/td&gt;
&lt;td&gt;nothing now: a new cell version, merged later&lt;/td&gt;
&lt;td&gt;the changed columns of every part with a matching row&lt;/td&gt;
&lt;td&gt;like HBase&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4. who owns a key on many nodes&lt;/td&gt;
&lt;td&gt;one server per key range, storage shared&lt;/td&gt;
&lt;td&gt;the shard the sharding key picks, own disks&lt;/td&gt;
&lt;td&gt;a hash ring, any node coordinates&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Every setting makes one cost lower and another higher. These costs have a name, the RUM triangle: the cost of &lt;strong&gt;R&lt;/strong&gt;eading, of &lt;strong&gt;U&lt;/strong&gt;pdating, and of the &lt;strong&gt;M&lt;/strong&gt;emory or space the data takes. The next four sections measure what each knob costs, and the triangle at the end puts them together.&lt;/p&gt;
&lt;h2&gt;
  
  
  Knob 1: How Many Rows Stand Behind an Index Entry
&lt;/h2&gt;

&lt;p&gt;Neither engine indexes every row. Both keep a sparse index: the rows are sorted by key and cut into chunks, and the index remembers where each chunk starts. To find one row, the engine finds its chunk and processes the entire chunk.&lt;/p&gt;

&lt;p&gt;The size of the chunk is the first knob. Call it &lt;em&gt;g&lt;/em&gt;, the rows behind one index entry:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz0f78rsuaj5gnvwymcsv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz0f78rsuaj5gnvwymcsv.png" alt="E equals N over g: index entries for N rows with g rows behind each. Rows processed by a point lookup are about g, depending on the plan. g equals N over E: for a fixed N, every entry cut from the index is rows added to every point read." width="800" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ClickHouse sets &lt;em&gt;g&lt;/em&gt; = 8,192 rows and calls the chunk a granule. The test table has 10 million rows and a primary index of 1,224 marks: 4.9 KB, always in memory (&lt;code&gt;marks&lt;/code&gt; and &lt;code&gt;primary_key_bytes_in_memory&lt;/code&gt; in &lt;code&gt;system.parts&lt;/code&gt;). At the same &lt;em&gt;g&lt;/em&gt;, a billion rows would need about 122 thousand marks, which is still negligible. The cost is on the other side of the formula: to return one row, ClickHouse processes its whole granule, 8,192 rows of every column the query asks for.&lt;/p&gt;

&lt;p&gt;HBase cuts by bytes, not by rows. A block closes at 64 KB of uncompressed cells, which for these 1.2 KB rows is about 58 rows. Its &lt;em&gt;g&lt;/em&gt; is 140 times smaller, so its index has 140 times as many entries: 172,472 for the same table, one per data block (the blocks a full scan counts in HBase's server-side scan metrics). HBase does not keep such an index in memory as one array: it stores it in blocks too, read through the block cache, alongside a bloom filter for each file that answers the question: "Is this key in this file at all?"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy15wzbn0cgjb3drjjej9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy15wzbn0cgjb3drjjej9.png" alt="Schematic. ClickHouse: six large granules, an index mark at the start of each; to return one row, the whole granule is processed, 8,192 rows of every column asked for. HBase: forty small blocks, an index entry at the start of each; to return one row, one 64 KB block of about 58 rows is read, plus a bloom filter chunk and an index block." width="800" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Schematic. On the stand ClickHouse has 1,224 granules and HBase 172,472 blocks for the same 10 million rows.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One point read of a whole row, caches warm, median of 2,000 random keys:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;em&gt;g&lt;/em&gt;&lt;/th&gt;
&lt;th&gt;index&lt;/th&gt;
&lt;th&gt;to return one row&lt;/th&gt;
&lt;th&gt;median&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ClickHouse&lt;/td&gt;
&lt;td&gt;8,192&amp;nbsp;rows&lt;/td&gt;
&lt;td&gt;1,224 marks, 4.9&amp;nbsp;KB&lt;/td&gt;
&lt;td&gt;one granule processed, 8,192 rows&lt;/td&gt;
&lt;td&gt;3.47&amp;nbsp;ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HBase&lt;/td&gt;
&lt;td&gt;~58&amp;nbsp;rows&lt;/td&gt;
&lt;td&gt;172,472 entries&lt;/td&gt;
&lt;td&gt;one 64&amp;nbsp;KB data block accessed, plus 256&amp;nbsp;KB of bloom and index blocks, all in the block cache&lt;/td&gt;
&lt;td&gt;0.15&amp;nbsp;ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;"One granule" counts rows, not bytes, and the bytes actually read differ. A separate run with ClickHouse's own counters, per point read:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ClickHouse, one row&lt;/th&gt;
&lt;th&gt;processed&lt;/th&gt;
&lt;th&gt;decompressed&lt;/th&gt;
&lt;th&gt;compressed bytes read (page cache)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;whole row, 30 columns&lt;/td&gt;
&lt;td&gt;1.70&amp;nbsp;MB&lt;/td&gt;
&lt;td&gt;2.82&amp;nbsp;MB in 38 blocks&lt;/td&gt;
&lt;td&gt;525&amp;nbsp;KB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3 columns&lt;/td&gt;
&lt;td&gt;180&amp;nbsp;KB&lt;/td&gt;
&lt;td&gt;328&amp;nbsp;KB in 5 blocks&lt;/td&gt;
&lt;td&gt;90&amp;nbsp;KB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;More is decompressed than processed because a compressed block holds at least 64 KB of uncompressed data. A one-byte column such as &lt;code&gt;status&lt;/code&gt; takes 8 KB per granule, so one of its blocks covers eight granules, and reading one granule means decompressing all eight. The three-column read is five blocks of exactly 64 KB: the three columns and the two key columns the filter needs. (The volumes come from that separate run, the latency from the timed one.)&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;SELECT 1&lt;/code&gt; through the same HTTP client takes 0.6 ms: the baseline of any query on this stand, parsing, planning and the round trip included. Above that baseline, three columns cost 0.8 ms and all thirty columns cost 2.9 ms. That is the part that grows with the columns asked for; decoding the granule is in it, and so is opening one column file per column. The timing alone does not split the two.&lt;/p&gt;

&lt;p&gt;HBase pays for its small &lt;em&gt;g&lt;/em&gt; in a way the formula does not show. Of the ~320 KB a Get touches, 64 KB is the data block; the rest is one 128 KB chunk of the bloom filter and one leaf block of the index, up to 128 KB. All of it comes from the block cache here, so it costs microseconds, but it is three blocks per Get, not one. The card in the next section counts data blocks only, and for one file per region it says one; the index and the filter are the reads it leaves out.&lt;/p&gt;

&lt;p&gt;For a single row ClickHouse is 24 times slower, and not because it is badly built: it chose a &lt;em&gt;g&lt;/em&gt; that keeps its index tiny, and a point read pays for that choice.&lt;/p&gt;
&lt;h3&gt;
  
  
  Cassandra Lets the Schema Choose &lt;em&gt;g&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;With the stock configuration on this stand, Cassandra's index has two levels. The partition index has an entry for every partition; a sample of it, one entry in 128, sits in memory, next to a bloom filter on the partition key. Inside a partition the rows are sorted by the clustering key, and a partition bigger than 64 KB gets an index of its own, an entry per 64 KB.&lt;/p&gt;

&lt;p&gt;So in Cassandra &lt;em&gt;g&lt;/em&gt; is a schema decision, and the stand can vary it. We loaded the same ten million rows twice: once with a partition per register, a thousand documents ordered by number inside it, and once with a partition per document. Totals across all three nodes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;partition per&lt;/th&gt;
&lt;th&gt;partitions&lt;/th&gt;
&lt;th&gt;index on disk&lt;/th&gt;
&lt;th&gt;bloom filter&lt;/th&gt;
&lt;th&gt;space&lt;/th&gt;
&lt;th&gt;point read&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;register&lt;/td&gt;
&lt;td&gt;10,000&lt;/td&gt;
&lt;td&gt;1.45&amp;nbsp;MB&lt;/td&gt;
&lt;td&gt;12.6&amp;nbsp;KB&lt;/td&gt;
&lt;td&gt;684&amp;nbsp;MiB&lt;/td&gt;
&lt;td&gt;223-231&amp;nbsp;µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;document&lt;/td&gt;
&lt;td&gt;10,045,874&lt;/td&gt;
&lt;td&gt;257&amp;nbsp;MB&lt;/td&gt;
&lt;td&gt;12.6&amp;nbsp;MB&lt;/td&gt;
&lt;td&gt;1,564&amp;nbsp;MiB&lt;/td&gt;
&lt;td&gt;206-216&amp;nbsp;µs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The card holds for a third engine too. A thousand times as many entries require a bloom filter a thousand times larger (ten bits per partition in both tables) and 177 times the index on disk. The point read got 15 microseconds faster, and the table more than doubled. Of the extra 880 MiB, 245 MiB is index; the rest is most likely per-partition overhead and the cost of the row order. With a partition per document the rows lie in hash order, so the rows of one register, whose constant columns compress well side by side, are scattered across the file. That part is an interpretation, not a measurement.&lt;/p&gt;

&lt;p&gt;What the small partitions did buy is a bloom filter that works:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuzx8851a3vnlc0ohos9d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fuzx8851a3vnlc0ohos9d.png" alt="Four SSTables on one node. With a partition per register, every SSTable holds a slice of register 42; the bloom filter is keyed by the partition, so all four are opened, and only one holds document 4242. With a partition per document, only one SSTable holds document 4242; the bloom filter skips the other three and one file is opened." width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Reading document 4242 of register 42. Measured: a partition per register, 3.3 files opened per read (2-4 SSTables per node); a partition per document, exactly 1 (5-6 per node).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With a partition per register, every file holds a slice of every register, and the filter lets every file through. With a partition per document, each document lives in exactly one file, and the read traces show the filter skipping 4.3 files per read. "Exactly one" holds for this table, loaded once and never updated: an update writes a newer piece of the partition into a newer file, and from then on the filter lets two through. That is the &lt;em&gt;p&lt;/em&gt; (&lt;em&gt;h&lt;/em&gt; - 1) term of the next section's card: a filter only helps when its key is as fine-grained as the read.&lt;/p&gt;
&lt;h2&gt;
  
  
  Knob 2: What One Read Brings Back
&lt;/h2&gt;

&lt;p&gt;The second knob is what sits inside the chunk. An HBase block holds sorted cells: all the cells of about 58 rows, every column of the column family, one row after another. A ClickHouse granule is not one chunk but one per column, each compressed on its own. A query that needs three columns opens three files and never touches the other twenty-seven.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbneyds3a6tqgh17abyty.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fbneyds3a6tqgh17abyty.png" alt="Left, HBase: a block holds the sorted cells of about 58 rows, 30 cells each; the three columns a query needs are in accent, the other 27 in amber; narrow or wide, the scan goes through blocks that contain all columns, 11.3 GB on the uncompressed table. Right, ClickHouse: separate column streams in wide parts; only the three accent columns are read, 100 MB." width="800" height="319"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The query is sum(amount) WHERE status = 1 GROUP BY payment_type: three columns of thirty. All thirty HBase columns are in one column family.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For a scan the layout alone decides how many bytes are read, and it fits a formula. For a point read, the same layout works the other way round:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvgn8tzotkgd0ch6uncev.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fvgn8tzotkgd0ch6uncev.png" alt="B row equals N times the sum over all columns C of the family of w of c plus k. B col equals N times the sum over the query's columns Q of w of c. Both are uncompressed bytes. A point read in HBase reads, in a simple model, 1 plus p times h minus 1 data blocks. A point read in ClickHouse reads at least Q blocks, one per column read." width="799" height="311"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;ClickHouse read 100 MB for the narrow scan: status 10 MB, payment_type 10 MB, amount 80 MB, uncompressed by its own counter. That matches &lt;em&gt;B&lt;/em&gt;col exactly. Compressed, it read 52.6 MB, all from the page cache on this stand: the three column files and nothing else. The wide scan read 315 MB compressed, the whole table. HBase read 172,472 blocks for the narrow scan and the same 172,472 for the wide one, 11.3 GB from the file system either way on the uncompressed table. Asking HBase for 3 columns did not change what it read, only what crossed the network.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;k&lt;/em&gt; term is large. In the uncompressed HBase table about 80% of the bytes are keys: every cell carries its row key, family, qualifier and timestamp, about 36 bytes (inferred from the table's size, not read out of an HFile), next to 8 bytes of value on average. Prefix encoding and LZ4 bring the table from 11,180 MiB down to 792 MiB. ClickHouse keeps the same rows in 299 MiB.&lt;/p&gt;

&lt;p&gt;For point reads the result is reversed. Both tables compressed, medians:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8zdzfu1svdnfdlunbupm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8zdzfu1svdnfdlunbupm.png" alt="Median time, log scale. Scan of 3 of 30 columns: HBase 24.7 s, ClickHouse 0.03 s. Scan of all 30 columns: HBase 26.9 s, ClickHouse 0.35 s. Point read of a whole row: HBase 170 microseconds, ClickHouse 3.47 ms. Point read of 3 columns: HBase 197 microseconds, ClickHouse 1.44 ms. The ClickHouse floor for any query over HTTP is 0.6 ms." width="800" height="298"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The layout that is 77 to 820 times faster on a scan is 7 to 20 times slower on a point read.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  The same numbers as a table
  &lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;HBase (FAST_DIFF + LZ4)&lt;/th&gt;
&lt;th&gt;ClickHouse (LZ4 + delta codecs)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;scan, 3 of 30 columns&lt;/td&gt;
&lt;td&gt;24.7&amp;nbsp;s&lt;/td&gt;
&lt;td&gt;0.03&amp;nbsp;s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;scan, all 30 columns&lt;/td&gt;
&lt;td&gt;26.9&amp;nbsp;s&lt;/td&gt;
&lt;td&gt;0.35&amp;nbsp;s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;point read, whole row&lt;/td&gt;
&lt;td&gt;170&amp;nbsp;µs&lt;/td&gt;
&lt;td&gt;3.47&amp;nbsp;ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;point read, 3 columns&lt;/td&gt;
&lt;td&gt;197&amp;nbsp;µs&lt;/td&gt;
&lt;td&gt;1.44&amp;nbsp;ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;on disk&lt;/td&gt;
&lt;td&gt;792&amp;nbsp;MiB&lt;/td&gt;
&lt;td&gt;299&amp;nbsp;MiB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;br&gt;
&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The point reads show one term of the card per engine. ClickHouse's whole row costs 2.4 times its three columns: one block per requested column, the |&lt;em&gt;Q&lt;/em&gt;| term. HBase's whole row costs about the same as three columns: the block holds the row either way, and fewer columns only save serialization.&lt;/p&gt;

&lt;p&gt;Two caveats, both against HBase. Its LZ4 runs in Java on this stand (the native Hadoop libraries are not loaded), and the aggregation runs in the bench client. Neither explains the gap: the wide scan moves nine times more cells to the client than the narrow one and takes only 1.1 times longer. The time goes to reading blocks. Phoenix's coprocessors would save the transfer, not the blocks.&lt;/p&gt;

&lt;p&gt;HBase has its own way to separate columns: column families are stored apart, so putting the few hot columns into a family of their own is manual column storage. It works for the columns you knew about in advance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Knob 3: What a Change Rewrites
&lt;/h2&gt;

&lt;p&gt;Now the fix from the introduction: one document with the wrong status. Both engines keep their files immutable, so neither of them can change a value in place. What differs is what each of them writes instead, and when.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcq1o7x8qw77d4o6gxysu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcq1o7x8qw77d4o6gxysu.png" alt="An HBase Put writes the size of the new cells; compactions merge files later. A ClickHouse mutation reads the WHERE columns W of every part P it has to check and the columns being set S of every part T with a matching row, and writes the columns S of every part in T whole." width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An HBase Put writes one new cell with a newer timestamp and leaves the old one where it is. A read merges the memstore with the files and the newest timestamp wins. The old cell stays on disk until a compaction merges its file with a newer one. A minor compaction merges a selection of files; a major compaction rewrites every file of a column family's store, whatever changed. The test table has one column family, so a major compaction there rewrites every cell of the region.&lt;/p&gt;

&lt;p&gt;A ClickHouse &lt;code&gt;ALTER TABLE ... UPDATE&lt;/code&gt; is a mutation: it finds the rows, then writes a new version of every part that has one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fi7r7ytmnkzasljei3w8b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fi7r7ytmnkzasljei3w8b.png" alt="HBase: the memstore holds the new cell status 2 at timestamp 2, the HFile still holds status 1 at timestamp 1; a Get merges both and the newest timestamp wins. The old cell stays until a compaction merges its file. ClickHouse: ALTER UPDATE reads the two WHERE columns and the status column of the part and writes a new part in which the status column is rewritten whole, 1.39 MB to change 1 byte, while the other 29 columns of this wide part are hard-linked." width="799" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;One status changed. HBase adds a cell and defers the rewrite; ClickHouse rewrites a column of the part now.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;P&lt;/em&gt; is the set of parts the mutation has to check; on a real table the partition key and the primary index can rule many of them out. On the stand the table is one part of 10 million rows, so there is nothing to rule out: &lt;em&gt;P&lt;/em&gt; and &lt;em&gt;T&lt;/em&gt; are the same single part, &lt;em&gt;W&lt;/em&gt; is the key (&lt;code&gt;register_id&lt;/code&gt;, &lt;code&gt;doc_id&lt;/code&gt;) and &lt;em&gt;S&lt;/em&gt; is &lt;code&gt;status&lt;/code&gt;. One update read 130 MB, register_id 40 MB plus doc_id 80 MB plus status 10 MB, uncompressed, exactly what the card predicts. It wrote the status column of the part, 1.39 MB compressed, to change one byte.&lt;/p&gt;

&lt;p&gt;The card also says the cost is set by the touched parts and columns, not by the number of rows changed. On this stand, updating more rows in the same part barely changes the cost:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;rows changed at once&lt;/th&gt;
&lt;th&gt;1&lt;/th&gt;
&lt;th&gt;1,003&lt;/th&gt;
&lt;th&gt;100,000&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ClickHouse &lt;code&gt;ALTER UPDATE&lt;/code&gt;, wait for the mutation&lt;/td&gt;
&lt;td&gt;157&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;139&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;138&amp;nbsp;ms&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HBase Puts, one batch&lt;/td&gt;
&lt;td&gt;(warm-up)&lt;/td&gt;
&lt;td&gt;310&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;850&amp;nbsp;ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;(The single-row HBase batch measured the client's first region lookup, not the Put.)&lt;/p&gt;

&lt;p&gt;The gap is widest for a stream of single fixes. A hundred single-row updates, one after another, medians: an HBase Put 472 µs, a ClickHouse mutation 81.7 ms. That is 173 times; at those medians the same hundred statuses take 0.05 s on HBase and 8 s on ClickHouse.&lt;/p&gt;
&lt;h3&gt;
  
  
  ClickHouse Updates by Appending Versions
&lt;/h3&gt;

&lt;p&gt;There is a known workaround: do not change the value, append a new version and let reads pick the newest. HBase does that natively. ClickHouse has done it for years with &lt;code&gt;ReplacingMergeTree&lt;/code&gt; and &lt;code&gt;argMax&lt;/code&gt;, which is the trick &lt;a href="https://alnovis.io/blog/aggregate-is-a-function" rel="noopener noreferrer"&gt;an earlier post&lt;/a&gt; is built on. And ClickHouse 26.3 does it inside the engine, as a beta: a lightweight &lt;code&gt;UPDATE&lt;/code&gt; writes a small patch part with only the changed rows, readers apply the patches on the fly, and a merge or &lt;code&gt;APPLY PATCHES&lt;/code&gt; folds them into the data later.&lt;/p&gt;

&lt;p&gt;The same hundred single-row updates as a lightweight &lt;code&gt;UPDATE&lt;/code&gt;: 3.12 ms each, 26 times faster than the mutation and still 6.6 times slower than HBase. The cost moved to readers and to a later rewrite:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqszg6qv465q4jtfuw69n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fqszg6qv465q4jtfuw69n.png" alt="Who pays for one updated value, and when. HBase Put: 472 microseconds now; readers merge memstore and files on every read, which is the normal path; later a major compaction rewrites the whole store, here the whole region, 52 seconds per region. ClickHouse lightweight UPDATE: 3.1 ms now, a patch part; readers pay 8 bytes more per row read while a patch is pending; later APPLY PATCHES, 103 ms once for all 100 updates. ClickHouse ALTER UPDATE: 81.7 ms now, the status column rewritten; nothing for readers and nothing later." width="799" height="292"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;A cheaper write moves more of the cost to readers and to a later rewrite.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The read overhead is large relative to the size of the patch. With one patch part pending, a part of 100 rows and 1,900 bytes, the narrow scan read 180 MB instead of 100 and took twice as long. On this stand the pending patch added roughly 8 bytes per scanned row, though only 100 of the 10 million rows had changed. &lt;code&gt;APPLY PATCHES&lt;/code&gt; rewrote the status column once, in 103 ms, and the scans went back to 100 MB. (Measured on one part with one patch part, which the background merge had already made out of the hundred; many unmerged patches were not measured.)&lt;/p&gt;

&lt;p&gt;"ClickHouse cannot update" is wrong. It can update the way HBase does: write a small delta now, merge it on every read, fold it into the data later. A change can impose costs at three points: when it is written, while it is read, and when the data is rewritten later. Each engine distributes these costs differently, and even different update methods within the same engine make different trade-offs. A plain &lt;code&gt;ALTER UPDATE&lt;/code&gt; pays the cost up front. Cassandra writes a new cell like HBase, and its later rewrite, the compaction strategy, is a knob of its own, the one measured in the triangle section at the end.&lt;/p&gt;
&lt;h2&gt;
  
  
  Knob 4: Who Owns a Key When There Are Many Nodes
&lt;/h2&gt;

&lt;p&gt;Everything so far ran on one server. Big data rarely does, and the fourth knob only exists on many nodes: what the unit of placement is, and who moves it. The three engines give three different answers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6zee94uteqtb38fp6xsm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6zee94uteqtb38fp6xsm.png" alt="Three ways to own a key. HBase: key ranges over a shared store; the client caches who owns which range and goes to the owning server in one hop, and a move hands over files, not bytes. ClickHouse: shards chosen by a sharding key, each with its own disk; a query goes to an initiator that plans it, sends it to the shards and merges the answers, and a row's shard is fixed at insert. Cassandra: a hash ring with no leader; a token-aware client goes straight to the replica, any other node forwards the request to it." width="800" height="374"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;One point read, three answers to "who owns this key". Highlighted: the path that goes straight to the owner.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First, a limitation of the stand. All the pods run on one machine with six physical cores, so adding a second node adds another CPU quota, not another physical machine. Each half of the wide scan, alone on its shard, took 176 ms; both halves at once took 340 ms each. Speedup cannot be measured here. What can be measured is where the work goes and how many nodes a request has to touch, and that is what this section is about.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdntovxj0pwi08n7me205.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdntovxj0pwi08n7me205.png" alt="Nodes a point read asks: 1 when the key decides the owner, S otherwise. Work on the initiator for an aggregation is of the order of S times G, the partial states of G groups from S shards. Share of the data that moves when a cluster grows from S to S plus 1 nodes: S over S plus 1 with a modulo hash, about 1 over S plus 1 with a ring, and nothing at once for a range split, whose bytes compaction rewrites later." width="800" height="381"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  One Read, One Node, or a Detour
&lt;/h3&gt;

&lt;p&gt;An HBase read has no coordinator at all. The client reads the region locations once from the &lt;code&gt;hbase:meta&lt;/code&gt; table, caches them, and sends every Get straight to the server that owns the key. With two region servers, both answered a whole-row Get in about 250 µs, and no other node was involved.&lt;/p&gt;

&lt;p&gt;ClickHouse puts an initiator in front of the shards: the server that receives the query plans it again, sends it to each shard over TCP (on this stand its own shard too) and merges what comes back. For one row that detour cost more than the read itself:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F84msvwa0ph5t9rkrb1wa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F84msvwa0ph5t9rkrb1wa.png" alt="Point read of a whole row, median, log scale. HBase with two region servers, straight to the owner: 250 microseconds, no coordinator. Cassandra with three nodes: 230 microseconds token-aware, 363 microseconds when a coordinator forwards. ClickHouse with two shards: 3.5 ms on one server, 8.3 ms through the Distributed table." width="800" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The extra latency is what the coordinator does on the way: a Cassandra node forwards, a ClickHouse initiator plans, sends and merges a query.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The owning shard did exactly the work a single server does, a granule in 4 ms. The initiator added 4 to 5 ms of its own on top, a fixed cost per query that is invisible under a 0.3 s scan and doubles a point read. Without the sharding key in the &lt;code&gt;WHERE&lt;/code&gt;, it also sent the query to the shard that does not have the row, and that shard on average read a granule of its own to find nothing: 17.3 thousand rows processed per lookup instead of 8.2 thousand. With &lt;code&gt;optimize_skip_unused_shards&lt;/code&gt; the query goes to the owner only. That halves the cluster's work but saves just 0.5 ms of latency, because the two shards were asked in parallel.&lt;/p&gt;

&lt;p&gt;In Cassandra the detour is the client's choice. Every node can coordinate. A token-aware driver computes the partition's token from the bound key and sends the request to a replica, like the HBase client. If the client sends the request to another node, that node forwards it, much like a ClickHouse initiator. One table, both paths, measured inside the same run: 245 µs when the coordinator was the replica, 363 µs when it forwarded. On this read at &lt;code&gt;ONE&lt;/code&gt;, the extra coordination cost was about 120 µs, largely consistent with one more network hop. A coordinator also enforces the consistency level and collects and reconciles the answers; at &lt;code&gt;ONE&lt;/code&gt; there is little of that to do.&lt;/p&gt;
&lt;h3&gt;
  
  
  Where New Rows Go
&lt;/h3&gt;

&lt;p&gt;Ownership by key range has a well-known weakness: a key that only grows. Two million new documents written in &lt;code&gt;doc_id&lt;/code&gt; order into an HBase table with eight regions on two servers:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frpdu57o22guscctv0e25.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Frpdu57o22guscctv0e25.png" alt="Two million new documents in key order. With the key equal to doc_id, all two million rows went to server 1 and none to server 2, at 39.8 thousand rows per second; a range of 1,000 new documents is one scan, 11.3 ms. With a salt byte in front of doc_id, each server got one million rows, at 59.1 thousand rows per second; the same range is 8 scans in parallel, 4.1 ms on an idle cluster." width="799" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;With a key that only grows, every new row lands in the last region. A salt byte spreads the writes and splits every range read eight ways.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With the plain key, every new row went to the last region and so to one server, while the other server and seven of the eight regions sat idle. Pre-splitting the existing key range does not help: the new writes still land beyond the last split point. Salting, what Phoenix calls &lt;code&gt;SALT_BUCKETS&lt;/code&gt;, spread the same rows exactly evenly and wrote 1.5 times faster from the same client. Its price is on the read side: a range of new documents is now eight scans instead of one. On an idle cluster the eight parallel scans were even faster, 4.1 ms against 11.3. On a loaded cluster, they still generate eight times as many requests; we did not measure the impact.&lt;/p&gt;

&lt;p&gt;With a hash-based sharding expression, ClickHouse makes the same trade; the user chooses the expression, and it need not hash at all. Cassandra's partitioner makes it by default. A hash spreads monotonic keys by itself, and in exchange a range that is not inside one shard or partition goes to every node. Cassandra goes further: there is no order across partitions at all, only inside one.&lt;/p&gt;
&lt;h3&gt;
  
  
  What Moving Costs
&lt;/h3&gt;

&lt;p&gt;When ownership changes, the knob decides whether bytes move. On the stand, in HBase:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;HBase, two region servers&lt;/th&gt;
&lt;th&gt;took&lt;/th&gt;
&lt;th&gt;readers saw&lt;/th&gt;
&lt;th&gt;bytes rewritten&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;move a region to the other server&lt;/td&gt;
&lt;td&gt;1.0&amp;nbsp;s&lt;/td&gt;
&lt;td&gt;one refused Get, retried in 138&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;none: the new owner opens the same files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;split a 99&amp;nbsp;MiB region&lt;/td&gt;
&lt;td&gt;1.1&amp;nbsp;s&lt;/td&gt;
&lt;td&gt;one pause of 757&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;none at once: both daughters start as references to the parent's file, and rewrite their halves in a compaction within a minute&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;merge the daughters back&lt;/td&gt;
&lt;td&gt;0.6&amp;nbsp;s&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;none at once, references again&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Ownership in HBase is metadata, because the storage is shared; the bytes move later, during compaction. A ClickHouse shard owns its rows on its own disks, so there is nothing to hand over: moving rows means copying them, and a new shard only changes where new rows go. In Cassandra a new node takes over a slice of the ring and streams its data from the others, about 1 / (&lt;em&gt;S&lt;/em&gt; + 1) of it; that was not measured here. Its placement needed no pre-split at all: with 16 virtual nodes per server, the three nodes got 35.7, 32.7 and 31.6% of the rows on their own.&lt;/p&gt;
&lt;h3&gt;
  
  
  Coordinator Work Grows With the Number of Groups
&lt;/h3&gt;

&lt;p&gt;A scan's partial results have to be merged somewhere, and for a &lt;code&gt;GROUP BY&lt;/code&gt; that is the initiator. Its merge work grows with the number of groups, not with the number of rows. Ten million rows on two shards:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;10M groups&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;wall&lt;/th&gt;
&lt;th&gt;initiator CPU&lt;/th&gt;
&lt;th&gt;initiator receives&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GROUP BY doc_id&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;one server&lt;/td&gt;
&lt;td&gt;261&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;two shards, initiator merges&lt;/td&gt;
&lt;td&gt;896&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;1,360&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;92&amp;nbsp;MB of partial states&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;two shards, told no group crosses shards&lt;/td&gt;
&lt;td&gt;229&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;5&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;almost nothing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GROUP BY register_id, doc_id&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;two shards, initiator merges&lt;/td&gt;
&lt;td&gt;1,017&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;2,041&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;124&amp;nbsp;MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;two shards, sharding key recognised&lt;/td&gt;
&lt;td&gt;293&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;6&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;almost nothing&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With ten million groups the initiator received every shard's partial states and spent as much CPU merging them as one server needs for the whole query. The two-shard cluster was 3.4 times slower than one server. When ClickHouse may treat the groups as disjoint, the initiator does nothing. You can tell it so (&lt;code&gt;distributed_group_by_no_merge = 2&lt;/code&gt;, correct here only because each &lt;code&gt;doc_id&lt;/code&gt; lives on one shard), or it can work that out itself when the sharding key is in the &lt;code&gt;GROUP BY&lt;/code&gt;. On 26.3 the second needed both &lt;code&gt;optimize_distributed_group_by_sharding_key&lt;/code&gt; and &lt;code&gt;optimize_skip_unused_shards&lt;/code&gt;; the first alone did nothing. For about a million groups the cost was small: 132 ms of initiator CPU, and 113 ms of wall time against 75 on one server. The sharding key, chosen for writes, also determines this read cost.&lt;/p&gt;
&lt;h3&gt;
  
  
  Replicas, and What "Acknowledged" Means
&lt;/h3&gt;

&lt;p&gt;Only Cassandra in this post keeps replicas of its own: HBase leaves them to HDFS, and the ClickHouse cluster here had none. With three replicas, every request picks how many of them must answer. A point read, token-aware, warm medians:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;consistency level&lt;/th&gt;
&lt;th&gt;replicas that answer&lt;/th&gt;
&lt;th&gt;median&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ONE&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;264-280&amp;nbsp;µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;QUORUM&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;362-385&amp;nbsp;µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ALL&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;400-439&amp;nbsp;µs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The coordinator asks the replicas in parallel; what changes with the level is how many answers it waits for. &lt;code&gt;QUORUM&lt;/code&gt; waits for two replica responses instead of one, and on this stand that added roughly 100 µs, comparable to the forwarding penalty measured above. &lt;code&gt;ALL&lt;/code&gt; waits for the slower of the two remote replicas as well.&lt;/p&gt;

&lt;p&gt;The ten million rows were loaded at the driver's default, &lt;code&gt;LOCAL_ONE&lt;/code&gt;: a write is acknowledged when one replica has it. Under that load the replicas reported 116 thousand dropped mutations, timing out on six shared cores, and the client saw no error. Hinted handoff handled the catch-up: after the load had ended, the hint dispatchers on all three nodes kept completing tasks, the replicas' local write counts kept rising, by 565 thousand in half an hour, and then no node had a pending hint left. The counters do not match one to one, and the number of hints written was not checked: a hint is stored for a missing answer, not for a missing write, so the replayed writes, 1.8 million over the 30 million expected, include writes that had landed already. After compaction the three copies were exactly the same size, 717,670,958 bytes each; their contents were not compared. "Acknowledged" had meant one replica; the other two caught up in the background, without a repair, at least by size.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Triangle
&lt;/h2&gt;

&lt;p&gt;In 2016 Athanassoulis and co-authors put a name to what every section above kept running into: the RUM conjecture. An access method can keep any two of three overheads low, the cost of &lt;strong&gt;R&lt;/strong&gt;eading, of &lt;strong&gt;U&lt;/strong&gt;pdating and of the &lt;strong&gt;M&lt;/strong&gt;emory or space the data takes, but not all three. Each knob is a way to move cost from one corner to another:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Knob 1&lt;/strong&gt;, a bigger &lt;em&gt;g&lt;/em&gt;: the index shrinks (M), and every point read processes more rows (R).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Knob 2&lt;/strong&gt;, a column per unit of read: a scan reads only its columns and they compress well (R for scans, M), while a point read opens a block per column (R for point reads). Row storage makes the opposite trade.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Knob 3&lt;/strong&gt;, append a version instead of rewriting: cheap now (U), paid by readers who merge (R) and by old versions that take space (M) until the next rewrite.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Knob 4&lt;/strong&gt; is not on the triangle. It multiplies whatever the other three cost by the number of nodes a request has to touch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3j9qo9kf9p2gxf1ybgy8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3j9qo9kf9p2gxf1ybgy8.png" alt="Schematic RUM triangle with corners R, cheap reads, at the top, U, cheap updates, at the bottom left, and M, little space, at the bottom right. HBase sits on the R-U edge: cheap Puts and Gets, 792 MiB on disk. ClickHouse sits on the R-M edge: cheap scans in 299 MiB, 82 ms per update. A lightweight UPDATE moves ClickHouse toward U, and readers pay for it. Cassandra's compaction strategy is a measured segment parallel to the R-U edge, from UCS T4 near U to L10 near R." width="800" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Schematic, no common scale: each engine sits where its own kind of read is cheap. Only the Cassandra segment is measured, below.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Both engines claim the R corner, each for its own kind of read: HBase for a point read, ClickHouse for a scan. The positions are not coordinates measured on a common scale: each engine optimizes a different kind of read, and the RUM conjecture itself is about access methods, not whole databases. Changing a setting moves cost between corners; it does not make all three low. Phoenix is not a point on it at all: a layer moves work between the client and the servers, not between the corners.&lt;/p&gt;
&lt;h3&gt;
  
  
  Cassandra: Compaction Strategy as a Table Option
&lt;/h3&gt;

&lt;p&gt;In HBase and ClickHouse the position is built into the engine. In Cassandra one part of it is a table option: the compaction strategy decides how often data is rewritten (U) against how many files a read has to open (R). Cassandra 5.0 folds the classic strategies into one, the Unified Compaction Strategy, with a single parameter that slides from tiered (&lt;code&gt;T4&lt;/code&gt;) through &lt;code&gt;N&lt;/code&gt; to leveled (&lt;code&gt;L10&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;We wrote the same stream into four tables that differed only in that option: ten million rows, then five million updates of two columns at random rows. Then we waited until compaction had nothing left to do:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9inbrv9jjrqe1mswbw1s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F9inbrv9jjrqe1mswbw1s.png" alt="Rewrites per byte against SSTables opened per read, after compaction settled, the same stream into four tables. UCS T4: 2.84 rewrites, 2.92 files per read. UCS L10: 8.49 rewrites, 1.66 files per read. STCS, the table default: 3.75 rewrites, 3.27 files. UCS N: 5.54 rewrites, 3.65 files. T4 and L10 trade one for the other; STCS and N are worse than T4 on both." width="799" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;T4 and L10 show the expected trade; the default and the middle setting rewrote more than T4 and opened more files per read. Latency is discussed below.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  The same numbers as a table
  &lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;strategy&lt;/th&gt;
&lt;th&gt;rewrites per byte&lt;/th&gt;
&lt;th&gt;SSTables opened per read&lt;/th&gt;
&lt;th&gt;space over the floor&lt;/th&gt;
&lt;th&gt;point read, median&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;STCS, the table default&lt;/td&gt;
&lt;td&gt;3.75&lt;/td&gt;
&lt;td&gt;3.27&lt;/td&gt;
&lt;td&gt;+4.9%&lt;/td&gt;
&lt;td&gt;293-329&amp;nbsp;µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UCS T4&lt;/td&gt;
&lt;td&gt;2.84&lt;/td&gt;
&lt;td&gt;2.92&lt;/td&gt;
&lt;td&gt;+3.5%&lt;/td&gt;
&lt;td&gt;355-398&amp;nbsp;µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UCS N&lt;/td&gt;
&lt;td&gt;5.54&lt;/td&gt;
&lt;td&gt;3.65&lt;/td&gt;
&lt;td&gt;+5.0%&lt;/td&gt;
&lt;td&gt;272-297&amp;nbsp;µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UCS L10&lt;/td&gt;
&lt;td&gt;8.49&lt;/td&gt;
&lt;td&gt;1.66&lt;/td&gt;
&lt;td&gt;+1.3%&lt;/td&gt;
&lt;td&gt;237-251&amp;nbsp;µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;any, after a major compaction&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;1.00&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;223-299&amp;nbsp;µs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;br&gt;
&lt;p&gt;&lt;/p&gt;

&lt;p&gt;"Rewrites per byte" is what compaction wrote, divided by the size of the same table after a major compaction; the flush writes everything once more on top. SSTables per read are counted from read traces, one event per file a read opens.&lt;/p&gt;

&lt;p&gt;From T4 to L10 the rewrites triple and the files a read opens nearly halve. Once settled, L10 reads as fast as a fully compacted table. That matches the textbook trade-off.&lt;/p&gt;

&lt;p&gt;The middle setting does not. N rewrote twice as much as T4 and still left reads opening the most files. An interpretation, based on UCS's design rather than its source code: with flushes of a few megabytes and about 260 MB per node, a node holds some 65 flushes of data. That is about three levels at a fanout of 4, six at a fanout of 2 (N), two at a fanout of 10, and each level is a rewrite. On this workload, N was not a midpoint between T4 and L10. The default STCS was also behind T4 on both axes in this run. One stream and one stopping point, though: which levels happen to be full when the stream ends can move the count per read by a whole file.&lt;/p&gt;

&lt;p&gt;Latency does not follow the file count. N opened the most files and still answered faster than T4: 272-297 µs against 355-398. L10, with the fewest files, was the fastest, so the count is not noise; but with every file in the page cache and the key cache warm, an extra file cost tens of microseconds, and something else, not traced here, outweighed it between N and T4. So the chart shows the trade between rewriting and files touched per read. The number of files is a useful measure of read cost and a poor predictor of latency on a warm cache; on a cold disk each extra file would cost more.&lt;/p&gt;

&lt;p&gt;Space barely moved: it stayed within 5% of the floor, because the updates touched two columns of thirty and the old versions were small. The bloom filter helped none of the strategies: with a partition per register every file holds a slice of every partition. The same rows with a partition per document read exactly one file of five or six without any help from compaction (knob 1 again). In Cassandra the schema moves the R corner as much as the compaction setting does.&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing an Engine
&lt;/h2&gt;

&lt;p&gt;Back to the standup. The two cases from the introduction, with the knob that decides each of them and what it cost on the stand:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdcp2sbww6gczc5t1nno7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdcp2sbww6gczc5t1nno7.png" alt="The two standup scenes revisited. The monthly report on Phoenix is decided by knob 2, whole rows per block: HBase took 24.7 s because every column is read, ClickHouse 0.03 s reading 3 columns of 30. Fixing one status in ClickHouse is decided by knob 3, a whole column rewritten: ClickHouse took 81.7 ms, rewriting 1.39 MB for one byte, HBase 472 microseconds, writing one new cell." width="800" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In each case the workload went to the engine that makes its main operation expensive.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Neither engine was inherently the wrong choice. In each case the workload's dominant cost was one that the other engine handles cheaply. The report is one scan over a few columns, and HBase reads whole rows. The fix is one value in one part, and ClickHouse rewrites the column. Both engines did exactly what their knobs say, and were slower by factors of about 800 on the report's scan and 170 on the fix.&lt;/p&gt;

&lt;p&gt;So the choice can come from the knobs rather than from taste:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Point reads versus scans, and how many columns a query touches&lt;/strong&gt;, decide knobs 1 and 2. A workload of point reads by key wants a small &lt;em&gt;g&lt;/em&gt; and whole rows. A workload of scans over a few of many columns wants a granule and a file per column.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;How often rows change, and how scattered the changes are&lt;/strong&gt;, decide knob 3. A stream of single-row fixes is a stream of column rewrites for a mutation, Puts of under half a millisecond each for HBase, and something in between for a lightweight &lt;code&gt;UPDATE&lt;/code&gt; whose cost goes to the readers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The key decides knob 4.&lt;/strong&gt; Route by it and a point read touches one node. Group by it and the coordinator has nothing to merge. Make it grow monotonically under a range partitioning, and one server takes every write.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The SQL layer is a separate decision.&lt;/strong&gt; Phoenix on HBase buys SQL, secondary indexes and partial aggregation on the region servers. It does not change the storage.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some workloads need both: a hot table of changing documents and a cold history for scans. For them, one answer is two systems connected by change data capture, with its own cost in lag and operations. Or one engine and the append-a-version trick, which is what &lt;a href="https://alnovis.io/blog/aggregate-is-a-function" rel="noopener noreferrer"&gt;the cells in the earlier post&lt;/a&gt; do inside ClickHouse. Either way the cost of the other corner remains; the design only decides where and when it is paid.&lt;/p&gt;

&lt;p&gt;The next time someone says "the data is already in Phoenix", the first thing to check is which knob the query's largest cost term depends on. I still argue both sides, but now I know which side I am on before I start.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  Every number in this post, and the stand they come from
  &lt;p&gt;One k3s node, an AMD Ryzen 5 5600X with 6 physical cores and 12 hardware threads, shared by every pod. ClickHouse 26.3.9.8, 5 CPU and 12 GiB per server. HBase 2.5.16, standalone with 5 CPU, 12 GiB and an 8 GiB heap, or two region servers with 5 CPU, 6 GiB and a 4 GiB heap each over one shared volume. Cassandra 5.0.9, three nodes with 4 CPU, 5 GiB and a 2 GiB heap each. One synthetic table of 10 million documents, 30 columns, the same rows in every engine. Latencies are medians of warm runs.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;HBase&lt;/th&gt;
&lt;th&gt;ClickHouse&lt;/th&gt;
&lt;th&gt;Cassandra&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;on disk, 10M rows&lt;/td&gt;
&lt;td&gt;792&amp;nbsp;MiB (FAST_DIFF + LZ4)&lt;/td&gt;
&lt;td&gt;299&amp;nbsp;MiB (LZ4 + delta codecs)&lt;/td&gt;
&lt;td&gt;684&amp;nbsp;MiB (LZ4, a partition per register)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;point read, whole row&lt;/td&gt;
&lt;td&gt;170&amp;nbsp;µs; 250&amp;nbsp;µs on two servers&lt;/td&gt;
&lt;td&gt;3.47&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;216-243&amp;nbsp;µs, token-aware&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;point read, 3 columns&lt;/td&gt;
&lt;td&gt;197&amp;nbsp;µs&lt;/td&gt;
&lt;td&gt;1.44&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;point read through a coordinator&lt;/td&gt;
&lt;td&gt;none&lt;/td&gt;
&lt;td&gt;8.3&amp;nbsp;ms (Distributed)&lt;/td&gt;
&lt;td&gt;362-364&amp;nbsp;µs (forwarded)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;scan, 3 of 30 columns&lt;/td&gt;
&lt;td&gt;24.7&amp;nbsp;s&lt;/td&gt;
&lt;td&gt;0.03&amp;nbsp;s&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;scan, all 30 columns&lt;/td&gt;
&lt;td&gt;26.9&amp;nbsp;s&lt;/td&gt;
&lt;td&gt;0.35&amp;nbsp;s&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;one single-row update&lt;/td&gt;
&lt;td&gt;472&amp;nbsp;µs (Put)&lt;/td&gt;
&lt;td&gt;81.7&amp;nbsp;ms (&lt;code&gt;ALTER UPDATE&lt;/code&gt;), 3.12&amp;nbsp;ms (lightweight)&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;100,000 rows changed at once&lt;/td&gt;
&lt;td&gt;850&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;138&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;GROUP BY&lt;/code&gt; of 10M groups, one node and two&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;261&amp;nbsp;ms and 896&amp;nbsp;ms&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;a region moved, split, merged&lt;/td&gt;
&lt;td&gt;1.0&amp;nbsp;s, 1.1&amp;nbsp;s, 0.6&amp;nbsp;s&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;a monotonic key, rows per server&lt;/td&gt;
&lt;td&gt;2,000,000 and 0; salted 1,000,000 each&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;read at &lt;code&gt;QUORUM&lt;/code&gt; of 3 replicas&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;362-385&amp;nbsp;µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;compaction rewrites per byte, T4 to L10&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;2.84 to 8.49&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;p&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Prior Art
&lt;/h2&gt;

&lt;p&gt;None of the four knobs is new, and neither is the triangle. The log-structured merge tree comes from O'Neil, Cheng, Gawlick and O'Neil, "The Log-Structured Merge-Tree (LSM-Tree)", Acta Informatica, 1996. HBase and Cassandra both store data the way Google's "Bigtable: A Distributed Storage System for Structured Data" (Chang et al., OSDI 2006) describes, and Cassandra places it the way Amazon's "Dynamo" (DeCandia et al., SOSP 2007) does. The column store half of the story is in "C-Store: A Column-oriented DBMS" (Stonebraker et al., VLDB 2005) and in Abadi, Madden and Hachem, "Column-Stores vs. Row-Stores: How Different Are They Really?" (SIGMOD 2008). The triangle is "Designing Access Methods: The RUM Conjecture" (Athanassoulis et al., EDBT 2016). Chapter 3 of Martin Kleppmann's &lt;em&gt;Designing Data-Intensive Applications&lt;/em&gt; tells the same story at book length.&lt;/p&gt;

&lt;p&gt;Our own contribution is only the stand: the same ten million documents in three engines, the formula cards checked against the engines' own counters, and a note every time a number did not fit. Several did not: a point read decompresses more than its granule. A pending patch taxes every row of a scan. Replicas left behind by writes acknowledged at &lt;code&gt;LOCAL_ONE&lt;/code&gt; caught up by themselves, minutes later, at least by size. And the middle setting of a compaction slider was not in the middle.&lt;/p&gt;

</description>
      <category>hbase</category>
      <category>clickhouse</category>
      <category>cassandra</category>
      <category>database</category>
    </item>
    <item>
      <title>An Aggregate Is a Function, Not a Sum</title>
      <dc:creator>Alexander Novikov</dc:creator>
      <pubDate>Tue, 10 Nov 2026 10:15:14 +0000</pubDate>
      <link>https://dev.to/alnovis/an-aggregate-is-a-function-not-a-sum-15ci</link>
      <guid>https://dev.to/alnovis/an-aggregate-is-a-function-not-a-sum-15ci</guid>
      <description>&lt;h2&gt;
  
  
  Reports Over a Stream of Receipts
&lt;/h2&gt;

&lt;p&gt;The system I work on stores sales receipts. Receipts from every cash register arrive through Kafka, and ClickHouse stores them as documents.&lt;/p&gt;

&lt;p&gt;On top of the raw documents sit the reports people actually look at: totals per cashier shift, totals per hour and per day for a store, revenue grouped by a custom field the merchant puts on the receipt.&lt;/p&gt;

&lt;p&gt;It looks like a standard ClickHouse use case: insert rows, then aggregate them. It took us four attempts.&lt;/p&gt;

&lt;p&gt;What makes it hard is not the volume but three properties of the stream:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2bi0j64ahmtqziz8vuj1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F2bi0j64ahmtqziz8vuj1.png" alt="Each receipt is a line from the moment it was printed at the register to the moment it was delivered through Kafka, on one shared time axis. Most lines are short and almost vertical. One receipt is delivered twice, one is delivered a week late, and aggregation has a gap while it is down." width="800" height="345"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Three properties of the stream that a running sum does not handle.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Documents come twice.&lt;/strong&gt; Kafka delivery is at-least-once. A batch fails halfway, the consumer retries, the same receipt is inserted again. Any total that is "the sum of what was inserted" is now wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Documents come late.&lt;/strong&gt; Most receipts arrive within seconds, but a register that was offline for a week sends its backlog when it reconnects. A small but steady share of documents arrive more than a day late, and some a month late. A report for last Tuesday is never final.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The pipeline stops.&lt;/strong&gt; Whatever computes the aggregates will sometimes be down: a deploy, a node restart, a bug. When it comes back, it must not lose anything.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The reports also have latency budgets: an interactive call should answer in a few seconds, a heavy report over hundreds of registers in under thirty seconds.&lt;/p&gt;

&lt;h2&gt;
  
  
  Four Attempts
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;#&lt;/th&gt;
&lt;th&gt;The idea&lt;/th&gt;
&lt;th&gt;What broke&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Add it up on insert&lt;/td&gt;
&lt;td&gt;a duplicate is counted twice, forever&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Recompute from the wide table&lt;/td&gt;
&lt;td&gt;read 46 million rows to use 508 thousand&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Remember every key&lt;/td&gt;
&lt;td&gt;the key set did not fit in memory&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Don't store anything&lt;/td&gt;
&lt;td&gt;3.5 billion rows for one report, on the test cluster&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  1. Add It Up on Insert
&lt;/h3&gt;

&lt;p&gt;The first version was the classic one: materialized views on insert, writing partial sums into a &lt;code&gt;SummingMergeTree&lt;/code&gt;. Each insert adds its contribution, background merges fold the rows together, and reports read pre-summed rows.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz7nr3xk4batkt7o1dax6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fz7nr3xk4batkt7o1dax6.png" alt="Kafka sends a batch with receipt 1234 for 500, the consumer inserts it and the hour total becomes 500. The batch fails halfway and is retried; the same batch is inserted again and the hour total becomes 1000." width="800" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The second insert adds to the total instead of replacing it. Nothing in the total says which 500 came twice.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It is fast, but a re-delivered receipt adds its sum a second time, and once two numbers have been added there is no way to subtract the right one later. The aggregate does not record which documents contributed to it.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Recompute From the Wide Table
&lt;/h3&gt;

&lt;p&gt;The second version fixed the double counting with recomputation: a refreshable materialized view periodically rebuilt the totals of recently touched shifts and hours from the documents themselves, with duplicates collapsed by document key.&lt;/p&gt;

&lt;p&gt;This version was correct. But it read the main documents table, which is wide (payload, statuses, dozens of columns) and sorted by register and document key, not by time.&lt;/p&gt;

&lt;p&gt;The recompute filtered by date, and in ClickHouse a date filter on a table not sorted by date cuts away whole partitions, but not granules (more on granules in a moment). One pass read 46 million rows to use 508 thousand:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fesprsa7a2pbh0z2y4wyz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fesprsa7a2pbh0z2y4wyz.png" alt="An older month partition is skipped whole. In the current month the table is sorted by register, so each granule holds weeks of one register's rows, oldest to newest. The date filter falls inside every granule, just before its newest rows, so all six granules are read whole for a few rows each." width="800" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Schematic. The date cuts between months, but inside a month it cuts through every granule.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In production the passes started hitting the per-query memory limit and failed repeatedly.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. Remember Every Key
&lt;/h3&gt;

&lt;p&gt;The third version went back to adding up on insert and tried to make duplicates impossible: before adding a receipt, check whether its key was seen before. The key lookup was implemented as a ClickHouse dictionary over a table of recent keys.&lt;/p&gt;

&lt;p&gt;A dictionary lives in memory, and a key in it is not small: 161 bytes on our test cluster. A month of keys for every register did not fit in production. The dictionary failed to load. After that, every insert failed:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnixm9q47wuo7yoyo2dn4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnixm9q47wuo7yoyo2dn4.png" alt="A month of keys does not fit, the dictionary fails to load, inserts write their rows and then fail, the consumer retries, and every retry stores another copy of the same receipt." width="800" height="239"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Every retry wrote the rows before failing, so every retry stored one more copy.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Documents started landing several times each, so the deduplication itself was producing duplicates.&lt;/p&gt;
&lt;h3&gt;
  
  
  4. Don't Store Anything
&lt;/h3&gt;

&lt;p&gt;The fourth version gave up on stored aggregates altogether: compute every report from the documents at read time, collapsing duplicates with a &lt;code&gt;GROUP BY&lt;/code&gt; on the document key. There is no state to get out of sync, and re-deliveries and late documents are handled for free.&lt;/p&gt;

&lt;p&gt;It was correct, and it was slow in a way no tuning could fix. A report over 600 shifts read about 3.5 billion rows. On our test cluster, at the 99th percentile:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Report&lt;/th&gt;
&lt;th&gt;1 client&lt;/th&gt;
&lt;th&gt;4 clients&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;600 shifts by key&lt;/td&gt;
&lt;td&gt;23.4 s&lt;/td&gt;
&lt;td&gt;119 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;hourly retail, 150 registers, a month&lt;/td&gt;
&lt;td&gt;17.0 s&lt;/td&gt;
&lt;td&gt;failed on memory and time limits&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;daily retail, 150 registers, a month&lt;/td&gt;
&lt;td&gt;14.3 s&lt;/td&gt;
&lt;td&gt;timed out on every call&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  What the Failures Had in Common
&lt;/h3&gt;

&lt;p&gt;None of the three production failures was caused by storing an aggregate:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnxxl6hczfjz8ui0bf7ab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fnxxl6hczfjz8ui0bf7ab.png" alt="A grid of the four attempts against four properties. Attempts 1 to 3 store an aggregate; each fails in a different column: a sum that cannot be corrected, reading too much, remembering every key. Attempt 4 stores nothing and fails on reading too much. The goal row stores an aggregate and fails on none." width="799" height="309"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Each production failure sits in its own column, and none of them in the first. The last row is the design we were looking for.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Attempt 4 removed storage and paid for it on every read, which led to the same failure as attempt 2.&lt;/p&gt;

&lt;p&gt;So the question became: can an aggregate be stored, survive duplicates without remembering keys, and be recomputed without reading the whole documents table?&lt;/p&gt;
&lt;h2&gt;
  
  
  A Short Detour: Granules
&lt;/h2&gt;

&lt;p&gt;To see why the read volume matters so much, it helps to know how ClickHouse reads.&lt;/p&gt;

&lt;p&gt;A MergeTree table is split into parts, and each part is split into granules of 8,192 rows by default. The primary index is sparse: it stores the first key of every granule, not of every row.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fioyvdjtm411mypv89r5l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fioyvdjtm411mypv89r5l.png" alt="A part of five granules of 8192 rows. The sparse primary index above them holds only the first key of each granule. A filter on register B can only rule out granules 1 and 5, so granules 2 to 4 are read whole." width="800" height="258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The index knows where each granule starts, not what is inside it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When a query filters by the primary key, ClickHouse finds the granules that may contain matching rows and reads them whole. Skip indexes (for example &lt;code&gt;minmax&lt;/code&gt; over a date column) can throw away more granules, but only if the values inside a granule are close to each other.&lt;/p&gt;

&lt;p&gt;So what a query reads depends far more on how rows are laid out than on how many rows it needs. If the rows you want are scattered across the table, you read the granules around each of them:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fckgt3k3u4hyvfai5lbul.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fckgt3k3u4hyvfai5lbul.png" alt="Two layouts of the same table, eight granules each. Sorted by hour, the rows a query needs are spread over all eight granules, and all eight are read. Sorted by register, the same rows sit together in two granules, and only those two are read." width="800" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The same twelve rows in both layouts. Only the sort order differs.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In our compute-at-read attempt, the narrow retail table was sorted by hour first. Within an hour, receipts of thousands of registers were interleaved, so a report for 150 registers touched nearly every granule of the month.&lt;/p&gt;

&lt;p&gt;We even had to rewrite our own acceptance criteria: a limit on "rows read" was impossible to meet with any query shape, because the boundary granules always contain someone else's rows.&lt;/p&gt;

&lt;p&gt;The reverse also holds: if the rows of one unit of work sit next to each other, reading them costs a handful of granules. The final design relies on this.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Idea: An Aggregate Is a Function
&lt;/h2&gt;

&lt;p&gt;In the design we kept, an aggregate is not a running sum of what was inserted but a function of its documents.&lt;/p&gt;

&lt;p&gt;Take the smallest useful unit of aggregation, a cell:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CellKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RegisterId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hour&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Instant&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// hour in UTC&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Every document belongs to exactly one cell, and the link is not stored anywhere. It is computed from three columns of the document:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcxr61pcxak4r0fnrkeq2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcxr61pcxak4r0fnrkeq2.png" alt="key of d is register, shift and hour of d. D of k is the set of documents d with key d equal to k. L of k is the latest version of each document in D of k. V of k sums the measures of the documents in L of k." width="799" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same in code:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="kd"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CellKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nc"&gt;CellKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;register&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;truncatedTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;HOURS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;docs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CellKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Seq&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;allDocuments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;key&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CellKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Measures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nf"&gt;docs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;groupBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;values&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;maxBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;writtenAt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;// a re-delivered document collapses here&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;foldLeft&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Measures&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// sums, counts, min, max&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The value of a cell can be computed again at any moment, because the documents are still there and the rule is the same. A new computation is written as a new row, versioned by the time of recomputation, and supersedes the old one in a &lt;code&gt;ReplacingMergeTree&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This gives three properties:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;A re-delivered document changes nothing.&lt;/strong&gt; During recomputation the rows of one document are collapsed by document key (&lt;code&gt;argMax&lt;/code&gt; by write time). Two deliveries become one row; the cell value stays the same. No dictionary, no key table, no memory of what was seen.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recomputing twice is harmless.&lt;/strong&gt; Recomputing a cell with no new documents produces the same value. On one node, two recomputations running at once both write a version, and the one that started later has seen at least as much, so it wins. Replicas are a different story (see What It Costs).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A late document is not lost.&lt;/strong&gt; A receipt that is a month late marks its month-old cell as dirty now, and that cell is recomputed in the next pass.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The second half of the idea is that measures merge across cells. Every measure in a cell is a sum, a count, a &lt;code&gt;min&lt;/code&gt; or a &lt;code&gt;max&lt;/code&gt;. Every document is in exactly one cell. So any report over any set of cells is just the combination of their values, without double counting:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkceaqtkjtoz2ftn8mzyd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fkceaqtkjtoz2ftn8mzyd.png" alt="A grid of cells, two registers by four hours, with a revenue number in each. The 14:00 column adds up to the store's hour, 130 plus 150 is 280. Register A's row is its shift 75, 120 plus 80 plus 200 plus 150 is 550. Hours add up to a day, days to a month." width="800" height="286"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The same cells are summed along different axes. A week, a quarter or any set of stores would be the same addition.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;None of these needs a table of its own: a new report is a new query over the same cells. Even time zones should work, as long as the offset from UTC is a fixed whole number of hours, since a local day boundary is then still an hour boundary.&lt;/p&gt;

&lt;p&gt;We checked this the boring way: hours, days and shifts assembled from cells matched the same reports computed directly from documents on all 209 measures.&lt;/p&gt;
&lt;h3&gt;
  
  
  Not Only Receipts
&lt;/h3&gt;

&lt;p&gt;The pattern is not specific to registers and receipts:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;In this article&lt;/th&gt;
&lt;th&gt;In general&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;a register&lt;/td&gt;
&lt;td&gt;a source of events: a device, an account, a sensor&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;a receipt, stored as a document&lt;/td&gt;
&lt;td&gt;an event with a stable key of its own&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;a shift, an hour&lt;/td&gt;
&lt;td&gt;natural windows the events fall into&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;sums, counts, &lt;code&gt;min&lt;/code&gt;, &lt;code&gt;max&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;measures that merge&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;It needs three things: every event carries everything needed to compute its cell key, that key does not change when the event is delivered again, and the measures add up, or at least merge. Where those hold, the aggregate can be a function of its events instead of a running sum.&lt;/p&gt;
&lt;h2&gt;
  
  
  Making It Work in ClickHouse
&lt;/h2&gt;

&lt;p&gt;Most of the work was in the ClickHouse mechanics.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3px0f2wlkgwx4zldqe75.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F3px0f2wlkgwx4zldqe75.png" alt="Kafka feeds the consumer, which inserts a batch into the documents table on the register's shard. In the same insert, views write a narrow copy of the documents and a dirty mark for each touched cell. Every 30 seconds a refreshable view recomputes the marked cells from the narrow table; another assembles shifts from cells. Reports read cells and shifts." width="800" height="602"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The document, its narrow copy and its dirty mark are written by one insert. Everything after that is recomputation.&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  A Narrow Table With the Right Order
&lt;/h3&gt;

&lt;p&gt;Recomputation never reads the wide documents table. A materialized view copies every document row into a narrow table with only the columns the measures need, sorted by &lt;code&gt;(register, document number)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There is no time column in that key, and it does not need one. A register numbers its documents in the order it issues them, so the documents of one register in one hour sit next to each other in every part. Finding the documents of a cell is a short range scan. The densest cell in our test history, a little over 5,000 documents, reads 20 granules and finishes in 7 ms. Compared to reading the same minute of traffic from the wide table, the narrow one reads 30 to 37 times fewer rows.&lt;/p&gt;
&lt;h3&gt;
  
  
  A Journal Written in the Same Insert
&lt;/h3&gt;

&lt;p&gt;A second materialized view on the same insert writes a dirty mark for every touched cell: the cell key and the time of the mark. No sums, just "this cell needs recomputing".&lt;/p&gt;

&lt;p&gt;Both views run as part of the same insert as the document itself, so our code has no separate step between "document stored" and "cell marked". That is not a transaction, though. ClickHouse writes the source table and its views one after another, and if a view fails after the document is stored, the insert reports an error with the document already in place. Our consumer then retries the batch, which normally writes the mark again, and a repeated document is harmless here. We have not checked how that retry meets insert deduplication on replicated tables: if a repeated block is dropped before its views run, the mark is lost and the cell is not recomputed.&lt;/p&gt;

&lt;p&gt;Status updates for documents arrive as separate rows without a date and shift, and the views simply skip them, so a status never triggers a recompute. No measure in a cell depends on a status.&lt;/p&gt;

&lt;p&gt;The journal is a &lt;code&gt;ReplacingMergeTree&lt;/code&gt; keyed by the cell, so repeated marks of one cell collapse into one row with the latest time. Small inserts produced about 14 marks per cell per hour; after collapsing it was 1.16, and the main recompute pass became five times cheaper.&lt;/p&gt;
&lt;h3&gt;
  
  
  "Recomputed" Lives in the Data
&lt;/h3&gt;

&lt;p&gt;A refreshable materialized view runs every 30 seconds and recomputes the cells that need it. The obvious way to track progress is a cursor, "processed up to time T". We did not keep one.&lt;/p&gt;

&lt;p&gt;Instead, each pass compares two times taken from the data itself:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsuft0p6cmy18k5h2p77m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fsuft0p6cmy18k5h2p77m.png" alt="M of k is the latest mark time of cell k in the journal. R of k is the latest recomputation time of cell k, or zero if it was never computed. dirty is the set of cells with M greater than R minus 60 seconds. A pass writes a new version for the 50 000 oldest dirty cells." width="799" height="260"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The same in code:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="kd"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lastMark&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CellKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Instant&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;journal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;marks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;max&lt;/span&gt;

&lt;span class="kd"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;lastRecompute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CellKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Instant&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;cells&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;versions&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;maxOption&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getOrElse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Instant&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;EPOCH&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// never computed yet&lt;/span&gt;

&lt;span class="kd"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;dirty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CellKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nf"&gt;lastMark&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isAfter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;lastRecompute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;minusSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Seq&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;Cell&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="n"&gt;journal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dirty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sortBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lastMark&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;take&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;50_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Cell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;recomputedAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Instant&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;A cell that was never computed has no version yet, so it counts as computed at the epoch and turns dirty with its first mark.&lt;/p&gt;

&lt;p&gt;A cursor has to be moved atomically together with the write, and it breaks under retries, parallel runs and restarts. The comparison is not affected by any of these. After an outage of up to a day, the next pass finds every cell that was marked after its last recompute. The 60-second overlap covers the fact that parts of one insert become visible at slightly different moments, so a mark can appear just before its document does. The price is an occasional extra recompute.&lt;/p&gt;

&lt;p&gt;A pass reads only the documents of the cells it recomputes.&lt;/p&gt;
&lt;h3&gt;
  
  
  Two Windows That Must Not Overlap
&lt;/h3&gt;

&lt;p&gt;The main pass looks at marks from the last two hours. A second, catch-up view runs every ten minutes and looks at marks between two hours and one day old. It exists for outages longer than two hours.&lt;/p&gt;

&lt;p&gt;The windows must not overlap. In an early version they did, and after an outage both views grabbed the same cells at the same time: 26 seconds and 2 x 2.8 GB of memory on one node for work that should have been done once.&lt;/p&gt;
&lt;h3&gt;
  
  
  Version by Snapshot Time
&lt;/h3&gt;

&lt;p&gt;The version of a cell row is the time its recompute query started, not the latest write time of its documents. We tried the latter first. With many documents written in the same second, and recomputations running close together, the order of versions became undefined. A later snapshot always sees at least as many documents as an earlier one, so snapshot time is a version that is always correct.&lt;/p&gt;

&lt;p&gt;That "at least as many" holds for snapshots taken on the same node. Across replicas it does not, and that is the weakest point of the whole design.&lt;/p&gt;
&lt;h3&gt;
  
  
  Shifts From Cells, Not From Documents
&lt;/h3&gt;

&lt;p&gt;A shift is assembled from its cells by a second refreshable view: take the latest row of every cell of the shift, sum the measures, take &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; of numbers and dates. A busy register can have 142,000 documents in a shift, and some shifts stay open for months. Rebuilding a shift from documents on every new receipt would reread it all; from cells it reads one row per hour of the shift.&lt;/p&gt;
&lt;h3&gt;
  
  
  Reading Without FINAL
&lt;/h3&gt;

&lt;p&gt;Readers take the latest row of each key with &lt;code&gt;LIMIT 1 BY&lt;/code&gt; over the key, ordered by recomputation time, and run that subquery on every shard over its local tables. Everything belonging to one register (documents, marks, cells, shifts) lives on the same shard, so "the latest row" is decided correctly locally and nobody waits for background merges.&lt;/p&gt;
&lt;h2&gt;
  
  
  Things That Bit Us
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Backfill Floods the Shift View
&lt;/h3&gt;

&lt;p&gt;When we filled cells for 77 days of history in one go, every filled cell looked "recently recomputed" to the shift view. It then spent two hours regrouping millions of cells and writing nothing new.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F950w059gf3l9g6w6xwwk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F950w059gf3l9g6w6xwwk.png" alt="Two versions of the same backfill. In the first, filled cells get recomputed_at = now; the shift view sees them as recomputed in the last two hours and regroups every one, 88 million rows and half a core per pass. In the fixed one, they get recomputed_at = now minus 2 days; the shift view skips them, 4 million rows per pass and almost no CPU." width="799" height="243"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The shift view only looks at cells recomputed in the last 2 hours, its catch-up at the last day. Two days old is outside both; a new mark still gets a backfilled cell recomputed.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Backfilled rows now get a recomputation time two days in the past, outside both windows. The shift view went from 88 million rows and half a core per pass to 4 million rows and almost nothing.&lt;/p&gt;
&lt;h3&gt;
  
  
  Filter in a Subquery Inside Insert Views
&lt;/h3&gt;

&lt;p&gt;On ClickHouse 26.3 we saw an insert view write column values shifted. Three things came together: output aliases that matched source column names, the filter in the same query, and blocks that mixed documents with status rows.&lt;/p&gt;

&lt;p&gt;Moving the filter into a subquery fixed it, and we have a test for it:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- values came out shifted&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;MATERIALIZED&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;docs_mv&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;docs&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;register_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;doc_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;ifNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;transactions&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- fixed&lt;/span&gt;
&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;MATERIALIZED&lt;/span&gt; &lt;span class="k"&gt;VIEW&lt;/span&gt; &lt;span class="n"&gt;docs_mv&lt;/span&gt; &lt;span class="k"&gt;TO&lt;/span&gt; &lt;span class="n"&gt;docs&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;register_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;doc_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;ifNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;transactions&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Granule Size Matters for Wide Rows
&lt;/h3&gt;

&lt;p&gt;A cell has about 250 columns, and the retail report reads 189 of them. With the default granule of 8,192 rows, a query for 150 registers decompressed about 1.1 GB to use 22,000 cells.&lt;/p&gt;

&lt;p&gt;Smaller granules and smaller compressed blocks on the cell table:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="no"&gt;SETTINGS&lt;/span&gt; &lt;span class="n"&gt;index_granularity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="n"&gt;min_compress_block_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8192&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="n"&gt;max_compress_block_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;65536&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The same query decompressed about 0.3 GB, and CPU dropped by about 30% for 9% more disk.&lt;/p&gt;
&lt;h3&gt;
  
  
  One Aggregate State per Document, Not per Column
&lt;/h3&gt;

&lt;p&gt;Collapsing a document with a separate &lt;code&gt;argMax&lt;/code&gt; per column builds dozens of aggregate states per document. One &lt;code&gt;argMax&lt;/code&gt; over a tuple of the columns builds one:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- one state per column&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;doc_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;argMax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;updated_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;argMax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;updated_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;argMax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;updated_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;docs&lt;/span&gt; &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;doc_key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- one state per document&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;doc_key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
       &lt;span class="nf"&gt;argMax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;updated_at&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;docs&lt;/span&gt; &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;doc_key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  The Numbers
&lt;/h2&gt;

&lt;p&gt;The test cluster is small: two shards on one machine with six physical cores, a five-CPU limit per ClickHouse node, tens of thousands of registers, 77 days of history. Report latency, 99th percentile:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F83pcpiv7qzdocmx49410.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F83pcpiv7qzdocmx49410.png" alt="p99 response time of four reports, computed from documents versus read from cells, at 1 and 4 clients, log scale. 1 client: 600 shifts 23.4 s vs 2.1 s; hourly retail 17.0 vs 2.5; daily retail 14.3 vs 1.3; revenue by field 7.9 vs 0.6. 4 clients: 600 shifts 119 s vs 2.7 s; hourly retail failed vs 6.1; daily retail timed out vs 3.7; revenue by field 100 vs 1.6. The report budget is 30 s." width="800" height="321"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Every report from cells sits left of the budget line, under load as well. From documents, four clients were enough to push every report past it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  The same numbers as a table
  &lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Report&lt;/th&gt;
&lt;th&gt;From documents, 1 client&lt;/th&gt;
&lt;th&gt;Cells, 1 client&lt;/th&gt;
&lt;th&gt;From documents, 4 clients&lt;/th&gt;
&lt;th&gt;Cells, 4 clients&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;600 shifts by key&lt;/td&gt;
&lt;td&gt;23.4 s&lt;/td&gt;
&lt;td&gt;2.1 s&lt;/td&gt;
&lt;td&gt;119 s&lt;/td&gt;
&lt;td&gt;2.7 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;hourly retail, 150 registers, a month&lt;/td&gt;
&lt;td&gt;17.0 s&lt;/td&gt;
&lt;td&gt;2.5 s&lt;/td&gt;
&lt;td&gt;failed&lt;/td&gt;
&lt;td&gt;6.1 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;daily retail, 150 registers, a month&lt;/td&gt;
&lt;td&gt;14.3 s&lt;/td&gt;
&lt;td&gt;1.3 s&lt;/td&gt;
&lt;td&gt;timed out&lt;/td&gt;
&lt;td&gt;3.7 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;revenue by custom field, 50 registers&lt;/td&gt;
&lt;td&gt;7.9 s&lt;/td&gt;
&lt;td&gt;0.6 s&lt;/td&gt;
&lt;td&gt;100 s&lt;/td&gt;
&lt;td&gt;1.6 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;br&gt;
&lt;p&gt;&lt;/p&gt;

&lt;p&gt;The 600-shift report reads up to 360 thousand rows instead of 3.5 billion. Every report call on the cells stays within twice the theoretical granule bound for the rows it needs.&lt;/p&gt;

&lt;p&gt;Correctness was checked against an independent reference: an expected value for every day, shift, hour and month of 1,000 registers, computed by the load generator without the service.&lt;/p&gt;

&lt;p&gt;There were zero mismatches across 31 checks, after three hours of live traffic in steps of increasing load, the last at three times the first.&lt;/p&gt;

&lt;h2&gt;
  
  
  What It Costs
&lt;/h2&gt;

&lt;p&gt;These are the trade-offs of the design and the gaps we know about.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr1ters4tzhhx0y0teks6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fr1ters4tzhhx0y0teks6.png" alt="What happens to a document by how long recomputation has been down. With no outage a report sees it in 75 to 115 seconds. Down under 2 hours, the main view catches up; between 2 hours and a day, the catch-up view does. Between 1 and 7 days a manual pass is needed while the marks are still kept. Past 7 days the marks have expired." width="799" height="196"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The windows bound how long an outage can be, not how late a document can be.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Lag.&lt;/strong&gt; A report sees a new document one or two passes later, usually 75 to 115 seconds after insert. We decided not to compute the pending tail at read time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Long outages need a hand.&lt;/strong&gt; Marks older than a day are not picked up automatically. The journal keeps marks for seven days, so there is time to run a manual pass.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Replicas recompute on their own, and that is a hole.&lt;/strong&gt; Refreshable views run on each replica of a shard, and each replica sees its own copy of the documents and of the journal, which replicate separately. A replica can see a mark before the document behind it, recompute the cell without that document, and stamp the result with a later time than the correct version from the other replica. The later stamp wins. If the gap outlasts the 60-second overlap, nothing marks the cell again until its next document, and for a late document that may be never. Our test cluster had one replica per shard, so it never showed up there; we found this one on paper, while writing this post. The fixes we see: let one replica per shard recompute, derive the version from the documents rather than the clock, or sweep yesterday's cells once a day regardless of their stamps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A retried insert may lose its mark.&lt;/strong&gt; If a view fails after its document is stored, the batch is retried, and on replicated tables insert deduplication can drop the repeated block before its views run. Then nothing marks the cell until its next document. We have not checked this yet.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Only mergeable measures.&lt;/strong&gt; Sums, counts, &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; merge across cells. Exact distinct counts and exact medians do not add up as plain numbers. ClickHouse can keep a mergeable aggregate state in a cell instead of a number: &lt;code&gt;uniqState&lt;/code&gt; and &lt;code&gt;quantileState&lt;/code&gt; combine across cells with &lt;code&gt;uniqMerge&lt;/code&gt; and &lt;code&gt;quantileMerge&lt;/code&gt;, approximately, with relatively little storage. The exact variants merge too, but carry every value. Our reports need none of this today; if one does, a cell can hold such states.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backfill is a procedure.&lt;/strong&gt; Cells for existing history are built by a separate script, about an hour and a half for our 77 test days.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Closing
&lt;/h2&gt;

&lt;p&gt;Each attempt fixed the previous failure and ran into the next problem: duplicates, scattered reads, memory for deduplication, and finally the cost of reading everything on every request.&lt;/p&gt;

&lt;p&gt;None of it is new, and we could have known that sooner. Databases call it incremental view maintenance: record which parts of a materialized result a change touched, recompute only those parts, replace the old values. Oracle's &lt;a href="https://docs.oracle.com/en/database/oracle/oracle-database/21/dwhsg/refreshing-materialized-views.html" rel="noopener noreferrer"&gt;partition change tracking&lt;/a&gt; does exactly that for materialized views, and a &lt;a href="http://sites.computer.org/debull/95JUN-CD.pdf" rel="noopener noreferrer"&gt;survey from 1995&lt;/a&gt; had already catalogued the techniques. Our own part is only the choice of the cell, and keeping the mark, the version and the "recomputed" flag inside ClickHouse tables themselves.&lt;/p&gt;

&lt;p&gt;What finally worked was not a new ClickHouse feature but a different meaning of an aggregate. As long as an aggregate is a sum of inserts, every duplicate and every late document is a correction you have to remember. When it is a function of its documents, a duplicate or a late document needs no correction: the cell is computed again.&lt;/p&gt;

</description>
      <category>clickhouse</category>
      <category>dataengineering</category>
      <category>distributedsystems</category>
      <category>kafka</category>
    </item>
    <item>
      <title>MLIR, Nanopass, and Green-Red Trees: Compiler Ideas That Make Code Generators Better</title>
      <dc:creator>Alexander Novikov</dc:creator>
      <pubDate>Mon, 23 Mar 2026 11:08:51 +0000</pubDate>
      <link>https://dev.to/alnovis/mlir-nanopass-and-green-red-trees-compiler-ideas-that-make-code-generators-better-1p9n</link>
      <guid>https://dev.to/alnovis/mlir-nanopass-and-green-red-trees-compiler-ideas-that-make-code-generators-better-1p9n</guid>
      <description>&lt;p&gt;Most code generators are monolithic. A single class reads some input, makes a bunch of decisions, and spits out source files. It works until it doesn't — until you need to support another target language, until you need incremental builds, until a 2000-line generator method becomes everyone's least favorite file to touch.&lt;/p&gt;

&lt;p&gt;I hit this wall while building &lt;a href="https://github.com/alnovis/proto-wrapper-plugin" rel="noopener noreferrer"&gt;Proto Wrapper Plugin&lt;/a&gt;, a tool that generates version-agnostic Java wrappers from multiple protobuf schemas. The generator worked fine, but every new feature meant wrestling with a monolith. Adding Kotlin support would have meant duplicating the entire thing.&lt;/p&gt;

&lt;p&gt;So I went looking for better ideas. I found three — from compiler design, of all places — and they changed how I think about code generation entirely.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; These ideas have since been implemented as &lt;a href="https://github.com/alnovis/ircraft" rel="noopener noreferrer"&gt;IRCraft&lt;/a&gt; — a standalone Scala 3 library for multi-language code generation with MLIR-inspired dialects, nanopass pipelines, and immutable green trees.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The Three Concepts
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Origin&lt;/th&gt;
&lt;th&gt;What it gives you&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;MLIR Dialects&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;LLVM/MLIR project&lt;/td&gt;
&lt;td&gt;Structure — multiple abstraction levels with typed operations&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Nanopass&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Academic compiler research&lt;/td&gt;
&lt;td&gt;Modularity — many small composable transformations&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Green-Red Trees&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Roslyn (C# compiler)&lt;/td&gt;
&lt;td&gt;Immutability — safe, cacheable, incrementally updatable AST&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Each one is powerful on its own. Together, they form an architecture that's extensible, testable, and surprisingly elegant.&lt;/p&gt;

&lt;p&gt;Let's look at each one.&lt;/p&gt;




&lt;h2&gt;
  
  
  MLIR: Stop Mixing Abstraction Levels
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The Idea
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://mlir.llvm.org/" rel="noopener noreferrer"&gt;MLIR&lt;/a&gt; (Multi-Level Intermediate Representation) is an LLVM project that structures compiler IR into &lt;em&gt;dialects&lt;/em&gt; — groups of operations at a specific abstraction level. Instead of one giant IR that represents everything from high-level constructs to machine instructions, MLIR lets you define separate dialects and &lt;em&gt;lower&lt;/em&gt; between them progressively.&lt;/p&gt;

&lt;p&gt;The key insight: &lt;strong&gt;different stages of compilation think in different abstractions.&lt;/strong&gt; Forcing everything into one representation creates a mess.&lt;/p&gt;

&lt;h3&gt;
  
  
  How This Applies to Code Generation
&lt;/h3&gt;

&lt;p&gt;A typical code generator goes straight from input to output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Finpmmx7dqnfpf9uxgz56.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Finpmmx7dqnfpf9uxgz56.png" alt="Simple flow: Protobuf descriptors → JavaPoet types → .java files" width="615" height="70"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Everything happens at once. The code that understands protobuf semantics is tangled with the code that knows Java syntax. Want to add Kotlin output? You'd need to rewrite everything.&lt;/p&gt;

&lt;p&gt;With dialects, you get clean separation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1lzq0ttqjygkbx3ks278.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1lzq0ttqjygkbx3ks278.png" alt="Dialect levels: Proto → Semantic → Java/Kotlin Code Dialects" width="586" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each dialect defines its own &lt;em&gt;operations&lt;/em&gt; — typed IR nodes that belong to that abstraction level. A &lt;code&gt;MessageOp&lt;/code&gt; belongs to the Proto Dialect. A &lt;code&gt;ClassOp&lt;/code&gt; belongs to the Semantic Dialect. You can't accidentally mix them.&lt;/p&gt;

&lt;p&gt;The critical piece is the &lt;strong&gt;Semantic Dialect&lt;/strong&gt; — it captures OOP concepts (classes, interfaces, methods, fields) without any language-specific details. This layer is shared. Adding Kotlin support means writing a new Code Dialect and an emission pass. The entire Proto → Semantic pipeline stays untouched.&lt;/p&gt;

&lt;h3&gt;
  
  
  Beyond Code Generators
&lt;/h3&gt;

&lt;p&gt;The dialect idea applies anywhere you have multiple abstraction levels:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;API gateways&lt;/strong&gt; — represent requests at the protocol level (HTTP, gRPC), the domain level (business operations), and the backend level (database queries, service calls)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Configuration management&lt;/strong&gt; — high-level intent ("deploy this service with 3 replicas"), mid-level plan ("create these Kubernetes resources"), low-level actions ("apply these YAML files")&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data pipelines&lt;/strong&gt; — logical plan (what to compute), physical plan (how to compute it), execution plan (where to run it)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The pattern is always the same: define clean abstraction boundaries, type the operations at each level, and lower progressively.&lt;/p&gt;




&lt;h2&gt;
  
  
  Nanopass: Small Passes Beat Big Passes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The Idea
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://nanopass.org/" rel="noopener noreferrer"&gt;Nanopass Framework&lt;/a&gt; comes from academic compiler research. Its premise is simple: instead of a few large compiler passes that each do many things, write many small passes that each do one thing.&lt;/p&gt;

&lt;p&gt;A traditional compiler might have a "type checking" pass that also resolves overloads, infers types, checks visibility, and validates constraints. A nanopass compiler splits that into five separate passes, each focused on a single concern.&lt;/p&gt;

&lt;h3&gt;
  
  
  How This Applies to Code Generation
&lt;/h3&gt;

&lt;p&gt;My original generator was one method that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read protobuf descriptors&lt;/li&gt;
&lt;li&gt;Decided which messages to include&lt;/li&gt;
&lt;li&gt;Resolved type references&lt;/li&gt;
&lt;li&gt;Detected type conflicts between versions&lt;/li&gt;
&lt;li&gt;Generated interfaces&lt;/li&gt;
&lt;li&gt;Generated abstract classes&lt;/li&gt;
&lt;li&gt;Generated implementations&lt;/li&gt;
&lt;li&gt;Generated builders (conditionally)&lt;/li&gt;
&lt;li&gt;Generated VersionContext factory&lt;/li&gt;
&lt;li&gt;Wrote files&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's 10 responsibilities in one place. Testing any of them in isolation was nearly impossible.&lt;/p&gt;

&lt;p&gt;With nanopass, each becomes a standalone transformation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4fakzzwh79ei2mtdo6tx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4fakzzwh79ei2mtdo6tx.png" alt="Nanopass pipeline: Proto → Semantic → Emission passes" width="327" height="1272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each pass is a pure function: immutable input → immutable output. Each can be tested with a 10-line unit test. Each can be conditionally enabled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// AddBuildersPass only runs when the user configured generateBuilders=true&lt;/span&gt;
&lt;span class="nd"&gt;@Override&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;isEnabled&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PassContext&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getConfig&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"generateBuilders"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Composition Problem
&lt;/h3&gt;

&lt;p&gt;Small passes need a way to compose. In our case, it's a simple pipeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Pipeline&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ProtoFileInput&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;JavaFile&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;pipeline&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Pipeline&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;from&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ProtoFileInput&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;then&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ParseProtoPass&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;then&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ResolveTypesPass&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;then&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FilterMessagesPass&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;then&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ValidateProtoPass&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
    &lt;span class="c1"&gt;// ... more passes&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;JavaFile&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pipeline&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;execute&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pipeline handles execution order, skips disabled passes, and logs timing. Passes communicate through &lt;code&gt;PassContext&lt;/code&gt; — a typed key-value store for shared data (e.g., a type registry built by one pass and consumed by another).&lt;/p&gt;

&lt;h3&gt;
  
  
  Beyond Code Generators
&lt;/h3&gt;

&lt;p&gt;Nanopass is just the &lt;a href="https://en.wikipedia.org/wiki/Single-responsibility_principle" rel="noopener noreferrer"&gt;Single Responsibility Principle&lt;/a&gt; applied to data transformations. It works anywhere you have a processing pipeline:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Image processing&lt;/strong&gt; — resize, crop, watermark, compress, optimize — each as a separate filter&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Request validation&lt;/strong&gt; — authenticate, authorize, validate schema, check rate limits, sanitize input — each as middleware&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data migration&lt;/strong&gt; — extract, clean, transform, validate, load — each as a named step with independent rollback&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Document processing&lt;/strong&gt; — parse, normalize, enrich, validate, render — each as a pipeline stage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The pattern gives you testability (test each step alone), composability (add/remove/reorder steps), and observability (log/time each step independently).&lt;/p&gt;




&lt;h2&gt;
  
  
  Green-Red Trees: Immutability That Pays for Itself
&lt;/h2&gt;

&lt;h3&gt;
  
  
  The Idea
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://ericlippert.com/2012/06/08/red-green-trees/" rel="noopener noreferrer"&gt;Green-Red Trees&lt;/a&gt; come from Roslyn, Microsoft's C# and VB.NET compiler. The problem Roslyn solves: an IDE needs to re-parse code on every keystroke, but re-creating the entire syntax tree each time is expensive.&lt;/p&gt;

&lt;p&gt;The solution splits the tree into two layers:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx6kyz5j84jfq6p08k8dd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx6kyz5j84jfq6p08k8dd.png" alt="Green-Red Trees: Red facade wraps immutable Green nodes" width="800" height="329"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Green tree&lt;/strong&gt; (the data):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Immutable&lt;/li&gt;
&lt;li&gt;Content-addressable (identity based on content, not object reference)&lt;/li&gt;
&lt;li&gt;No parent references&lt;/li&gt;
&lt;li&gt;Relative positions (width), not absolute&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Red tree&lt;/strong&gt; (the navigation):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lightweight facade over green nodes&lt;/li&gt;
&lt;li&gt;Adds parent references and absolute positions&lt;/li&gt;
&lt;li&gt;Created lazily during traversal&lt;/li&gt;
&lt;li&gt;Thrown away entirely on any change&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The insight: &lt;strong&gt;most of the tree doesn't change between edits.&lt;/strong&gt; If you insert a character on line 50, every node on lines 1-49 is structurally identical. With content-based identity, you can reuse those green nodes directly — no copying, no diffing.&lt;/p&gt;

&lt;h3&gt;
  
  
  How This Applies to Code Generation
&lt;/h3&gt;

&lt;p&gt;All our IR nodes are immutable case classes. Each computes a &lt;code&gt;contentHash&lt;/code&gt; from its content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MessageOp&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;presentInVersions&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;regions&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Vector&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Region&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt;
    &lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AttributeMap&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;span&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Span&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Operation&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;

  &lt;span class="kt"&gt;lazy&lt;/span&gt; &lt;span class="kt"&gt;val&lt;/span&gt; &lt;span class="kt"&gt;contentHash:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nv"&gt;ContentHash&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;combine&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
      &lt;span class="nv"&gt;ContentHash&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;ofString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
      &lt;span class="nv"&gt;ContentHash&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;ofSet&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;presentInVersions&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
      &lt;span class="nv"&gt;ContentHash&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;ofList&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;fields&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;toList&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="nv"&gt;Operation&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;operationHashable&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two &lt;code&gt;MessageOp&lt;/code&gt; nodes with the same content have the same hash. This gives us:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Change detection.&lt;/strong&gt; After running a pass, compare output hash to input hash. Same hash? Nothing changed — skip downstream passes for this node.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caching.&lt;/strong&gt; Store &lt;code&gt;contentHash → generated output&lt;/code&gt; mapping. If a proto file hasn't changed, reuse the previous output without running the pipeline.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Incremental builds.&lt;/strong&gt; In a project with 50 proto files, changing one file triggers regeneration for only that file's IR subtree. Everything else is a cache hit.&lt;/p&gt;

&lt;p&gt;The "red tree" layer (parent references, absolute positions) is planned for IDE integration — enabling "go to definition" and "find usages" across proto schemas and generated code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Immutability Matters for Passes
&lt;/h3&gt;

&lt;p&gt;Nanopass transformations are pure functions: &lt;code&gt;GreenNode(v1) → GreenNode(v2)&lt;/code&gt;. The input is never modified. This means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No defensive copying.&lt;/strong&gt; You know the input won't change under you.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Safe parallelism.&lt;/strong&gt; Multiple passes can read the same input concurrently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Time travel.&lt;/strong&gt; Keep the input around for debugging — it's immutable, it won't change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Structural sharing.&lt;/strong&gt; If a pass only modifies 2 out of 20 fields in a message, the unmodified field ops are shared between input and output trees.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Beyond Code Generators
&lt;/h3&gt;

&lt;p&gt;Immutable content-addressed trees appear in many systems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Git&lt;/strong&gt; — the entire object model (blobs, trees, commits) is content-addressed and immutable. Every &lt;code&gt;git commit&lt;/code&gt; creates new tree objects while sharing unchanged blobs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;React&lt;/strong&gt; — virtual DOM diffing works because components produce immutable trees that can be compared efficiently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nix&lt;/strong&gt; — the package manager uses content-addressed derivations. Same inputs always produce the same output hash.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Persistent data structures&lt;/strong&gt; — Clojure's vectors, maps, and sets use structural sharing to create "modified" copies in O(log n) time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event sourcing&lt;/strong&gt; — events are immutable facts. State is derived by replaying events, with snapshots as caching.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The principle is the same: make data immutable, identify it by content, share structure where possible.&lt;/p&gt;




&lt;h2&gt;
  
  
  How They Work Together
&lt;/h2&gt;

&lt;p&gt;The three concepts are complementary:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7n5hg3qw1g4p10d4d6v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc7n5hg3qw1g4p10d4d6v.png" alt="How they work together: Dialects → Nanopass → Green-Red Trees → Dialects" width="369" height="1325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Dialects&lt;/strong&gt; define the abstraction levels (where)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Passes&lt;/strong&gt; define the transformations (how)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Green nodes&lt;/strong&gt; define the data model (what)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each pass is a pure function: &lt;code&gt;GreenNode(v1) → GreenNode(v2)&lt;/code&gt;. Input is never modified. This means no defensive copying, safe parallelism, and the ability to keep previous IR versions around for debugging.&lt;/p&gt;




&lt;h2&gt;
  
  
  When to Use These Ideas
&lt;/h2&gt;

&lt;p&gt;You don't need all three concepts, and you don't need to build a full compiler framework. Here's when each one pays off:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MLIR Dialects&lt;/strong&gt; — when your system has multiple abstraction levels and you're fighting the urge to put everything in one data model. If you find yourself adding "kind" or "type" fields to distinguish between fundamentally different operations, you probably want dialects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nanopass&lt;/strong&gt; — when you have a processing pipeline with more than 3-4 concerns. If your &lt;code&gt;generate()&lt;/code&gt; or &lt;code&gt;process()&lt;/code&gt; method is over 200 lines and growing, break it into passes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Green-Red Trees&lt;/strong&gt; — when you need change detection, caching, or incremental processing. If your data is already immutable (or should be), content hashing is a natural extension.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Advice
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Start with nanopass.&lt;/strong&gt; It's the easiest to adopt and gives the most immediate benefit. Take your monolithic processor, identify the distinct responsibilities, and extract them into passes. You'll immediately gain testability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add content hashing when you need caching.&lt;/strong&gt; If your pipeline runs on every build and takes noticeable time, add &lt;code&gt;contentHash()&lt;/code&gt; to your data types. Use it for short-circuiting ("nothing changed, skip this pass") and caching ("same input, return cached output").&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Introduce dialects when you add a second target.&lt;/strong&gt; If you only generate Java, one abstraction level might be fine. The moment you need Kotlin, TypeScript, or any second output format, the dialect split pays for itself instantly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don't over-abstract.&lt;/strong&gt; These are tools, not rules. A 50-line code generator doesn't need an IR framework. Use the ideas when the complexity justifies them — and not a moment sooner.&lt;/p&gt;




&lt;h2&gt;
  
  
  Further Reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://arxiv.org/abs/2002.11054" rel="noopener noreferrer"&gt;MLIR: A Compiler Infrastructure for the End of Moore's Law&lt;/a&gt; — the original MLIR paper&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://mlir.llvm.org/docs/Tutorials/Toy/" rel="noopener noreferrer"&gt;MLIR Tutorial&lt;/a&gt; — LLVM's hands-on tutorial&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://legacy.cs.indiana.edu/~dyb/pubs/nano-jfp.pdf" rel="noopener noreferrer"&gt;A Nanopass Framework for Compiler Education&lt;/a&gt; — the foundational nanopass paper&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://andykeep.com/pubs/np-preprint.pdf" rel="noopener noreferrer"&gt;Nanopass Framework for Commercial Compiler Development&lt;/a&gt; — nanopass in practice&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://ericlippert.com/2012/06/08/red-green-trees/" rel="noopener noreferrer"&gt;Persistence, Facades, and Roslyn's Red-Green Trees&lt;/a&gt; — Eric Lippert's original blog post&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://willspeak.me/2021/11/24/red-green-syntax-trees-an-overview.html" rel="noopener noreferrer"&gt;Red-Green Syntax Trees: An Overview&lt;/a&gt; — accessible summary with examples&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/alnovis/proto-wrapper-plugin" rel="noopener noreferrer"&gt;Proto Wrapper Plugin&lt;/a&gt; — where we're applying all of this&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/alnovis/ircraft" rel="noopener noreferrer"&gt;IRCraft&lt;/a&gt; — the standalone Scala 3 implementation of these ideas&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://alnovis.io/blog/compiler-ideas-for-code-generation" rel="noopener noreferrer"&gt;alnovis.io&lt;/a&gt; on Februari 5, 2026.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>scala</category>
      <category>compilers</category>
      <category>architecture</category>
      <category>codegen</category>
    </item>
    <item>
      <title>Do You Still Need Computer Science in the Age of AI</title>
      <dc:creator>Alexander Novikov</dc:creator>
      <pubDate>Fri, 20 Mar 2026 23:10:21 +0000</pubDate>
      <link>https://dev.to/alnovis/do-you-still-need-computer-science-in-the-age-of-ai-2mc1</link>
      <guid>https://dev.to/alnovis/do-you-still-need-computer-science-in-the-age-of-ai-2mc1</guid>
      <description>&lt;h2&gt;
  
  
  "Maybe I Should Become a Welder"
&lt;/h2&gt;

&lt;p&gt;A colleague recently told me about a conversation with someone at Amazon. The person described how the company is aggressively pushing AI tools for code generation — management pressure to move faster, ship more, spend less. My colleague, twenty years in the industry, went quiet afterward. "Maybe I should learn welding," he joked. It was only half a joke.&lt;/p&gt;

&lt;p&gt;I get it. When you've spent twenty years in this profession and keep hearing that any intern with Copilot will soon do the same thing — it hits not your skills, but your identity. The sense that everything you've learned actually mattered.&lt;/p&gt;

&lt;p&gt;Amazon, like any large company, has plenty of strong engineers who've given years to the craft and understand exactly what's happening. But popular opinion doesn't make distinctions: why learn algorithms if AI will write them? Why bother with design patterns if Claude or Copilot can generate a working service in an hour? Why understand how a hash table works when the library gives you one out of the box? It seems like you just need to ask — "build me a service" — and it all works out.&lt;/p&gt;

&lt;p&gt;I disagree. Not because I'm defending my profession. But because I can see where mindless use of a tool leads.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Tool, Not a Replacement
&lt;/h2&gt;

&lt;p&gt;There was a time when we wrote code in terminal editors. Vim, Emacs — rough days. Then came Eclipse, Visual Studio, IntelliJ IDEA. It was a quantum leap in productivity: autocomplete, refactoring, debugger, VCS integration. Nobody said IDEs replaced programmers. IDEs made programmers faster.&lt;/p&gt;

&lt;p&gt;But using even an IDE mindlessly comes at a cost. I once came across job postings — from different companies, so this wasn't a one-off — that listed as a requirement: "ability to write and run a Java program outside of IntelliJ IDEA." I laughed for a while, then stopped to think. People had become so dependent on the tool that they couldn't perform a basic task without it. They didn't understand what was happening under the hood.&lt;/p&gt;

&lt;p&gt;AI is the next step in that same evolution. A more powerful tool. But still a tool. And depending on it without understanding the fundamentals leads to the same place — only the stakes are higher.&lt;/p&gt;

&lt;h2&gt;
  
  
  What AI Actually Automated
&lt;/h2&gt;

&lt;p&gt;Let's be honest: what does AI do well?&lt;/p&gt;

&lt;p&gt;It's excellent at generating boilerplate code. CRUD services, data mapping, standard integrations — everything an experienced developer writes on autopilot, AI writes faster. That's real value, and arguing against it is pointless.&lt;/p&gt;

&lt;p&gt;But AI doesn't understand your business context. It doesn't know why you chose eventual consistency over strong consistency. It can't see that your current load will grow tenfold in six months. It doesn't account for the fact that this service will be maintained by a team of three, not twenty. AI doesn't make architectural decisions — it generates code within the decisions that you (or nobody) made for it.&lt;/p&gt;

&lt;p&gt;And that "or nobody" is key. If you don't make a decision deliberately, AI will make it for you. Quietly, confidently, and unconsciously.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two Price Tags
&lt;/h2&gt;

&lt;p&gt;Every piece of software has two costs: the cost of writing it and the cost of maintaining it. The second is almost always several times higher.&lt;/p&gt;

&lt;p&gt;AI has radically reduced the first cost. Generating code is fast, cheap, practically free. But it hasn't reduced the cost of maintenance. If anything, it's made it worse: when code is generated ten times faster, it needs to be reviewed ten times more. And if there's no review, or the reviewer doesn't understand what they're looking at — the cost of maintenance grows exponentially.&lt;/p&gt;

&lt;p&gt;Code that's easy to write and expensive to maintain isn't a savings. It's technical debt taken out at a high interest rate.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Happens Without Oversight
&lt;/h2&gt;

&lt;p&gt;This isn't theory. In March 2026, Amazon faced a series of incidents tied to AI-generated code. On March 2nd — 120,000 lost orders and 1.6 million website errors. On March 5th — a 99% drop in orders across North America: 6.3 million lost orders in a single day. In a separate case, the AI agent Kiro, attempting to fix an issue with a cost calculation system, decided to delete and recreate the environment from scratch. Thirteen hours of downtime.&lt;/p&gt;

&lt;p&gt;Amazon's response is telling. Not abandoning AI. Tightening the requirements for reviewer qualifications. Now any AI-assisted code must be approved by a senior engineer before deployment. Amazon essentially confirmed: the problem isn't the tool — it's who's using it.&lt;/p&gt;

&lt;p&gt;And this isn't just Amazon. According to CodeRabbit, AI-generated code contains 1.7x more bugs than human-written code. 1.75x more logic errors. 1.57x more security issues. And eight times more unnecessary I/O operations. In 2025, pull requests per developer increased by 20%, while incidents per PR rose by 23.5%. More code — more problems.&lt;/p&gt;

&lt;p&gt;A telling case: in February 2026, AI generated code using &lt;code&gt;Promise.all&lt;/code&gt; that fired 4,200 simultaneous requests for user data synchronization. The connection pool couldn't handle it — twenty-two minutes of downtime. Anyone who understands concurrency and backpressure would have spotted the problem in seconds. But whoever accepted this code either wasn't taught that, or decided it no longer mattered.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Language of Task Definition
&lt;/h2&gt;

&lt;p&gt;And this brings us to the main point. Why do you need Computer Science if the machine writes the code?&lt;/p&gt;

&lt;p&gt;Not to write a sorting algorithm on a whiteboard during an interview. Not to memorize the pseudocode for depth-first search. That really is routine work, and yes, the machine can reproduce it.&lt;/p&gt;

&lt;p&gt;The value of Computer Science is in the conceptual framework. In the ability to think systematically, decompose problems, distinguish between abstractions, understand constraints and trade-offs. In the ability to verify what AI just generated for you.&lt;/p&gt;

&lt;p&gt;Without terminology, a person says: "rewrite this better," "something's off here," "make it look nice," "speed it up," "fix the architecture." Fix it how? Speed up in what direction? What does "better" mean?&lt;/p&gt;

&lt;p&gt;With terminology, a person says: isolate the domain from infrastructure, wire dependencies through IoC, implement an adapter for the legacy API integration, replace branching with the strategy pattern, reduce coupling, separate component responsibilities, fix the abstraction leak, break circular dependencies, extract object creation into a factory. Use a doubly linked list instead of an array, optimize hash table collisions, reduce unnecessary allocations. Protect the module from race conditions, add backpressure, use a circuit breaker. Don't mix application service with domain service, preserve aggregate invariants, make the operation idempotent.&lt;/p&gt;

&lt;p&gt;The first person asks AI to guess what they mean. The second precisely defines the task. And more importantly, the second one can verify the result.&lt;/p&gt;

&lt;h2&gt;
  
  
  CS as a Frame of Reference
&lt;/h2&gt;

&lt;p&gt;I'm not saying you need to implement a hash table from scratch with your eyes closed. I'm saying you need to understand how it works — so you can notice when AI suggests the wrong data structure. You don't need to remember Dijkstra's implementation — but you need to understand algorithmic complexity to see when AI produces O(n²) where O(n log n) would do.&lt;/p&gt;

&lt;p&gt;Though it's still better to both understand and know how to implement. Or at least strive for it.&lt;/p&gt;

&lt;p&gt;Computer Science isn't a set of skills you need to reproduce by hand. It's a frame of reference in which you think about software. IoC, SOLID, GRASP, DDD, Clean Architecture, CQRS, Event Sourcing, CAP theorem, ACID, consistency models, memory model, cache locality, idempotency, fault tolerance — these aren't for exams. They're a working vocabulary that lets you define tasks precisely, review results meaningfully, and tell working architecture from a convincing-looking hallucination.&lt;/p&gt;

&lt;h2&gt;
  
  
  Not About Fear
&lt;/h2&gt;

&lt;p&gt;I'm not trying to scare anyone. AI is a breakthrough, and I use it every day. It makes me more productive. But it doesn't make me unnecessary — just as IntelliJ didn't make Java developers unnecessary.&lt;/p&gt;

&lt;p&gt;To that colleague who joked about becoming a welder, I'd say: your twenty years of experience haven't lost their value. They've become more valuable. Because now you're needed not to write code, but to understand what's been written. To control quality. To make architectural decisions. To spot design flaws before they become production incidents.&lt;/p&gt;

&lt;p&gt;Computer Science matters. Maybe more than ever. Just not the kind they ask about in interviews. The kind that lets you think.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://alnovis.io/blog/do-you-need-cs-in-ai-era" rel="noopener noreferrer"&gt;alnovis.io&lt;/a&gt; on March 20, 2026.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>opinion</category>
      <category>ai</category>
      <category>computerscience</category>
      <category>career</category>
    </item>
    <item>
      <title>Proto Wrapper Plugin: How I Stopped Fighting Protobuf Versioning</title>
      <dc:creator>Alexander Novikov</dc:creator>
      <pubDate>Wed, 04 Mar 2026 12:03:59 +0000</pubDate>
      <link>https://dev.to/alnovis/proto-wrapper-plugin-how-i-stopped-fighting-protobuf-versioning-4jd3</link>
      <guid>https://dev.to/alnovis/proto-wrapper-plugin-how-i-stopped-fighting-protobuf-versioning-4jd3</guid>
      <description>&lt;p&gt;If you've ever worked on a system that processes protobuf messages from multiple protocol versions, you know the pain. You add a new field, change a type from &lt;code&gt;int32&lt;/code&gt; to &lt;code&gt;enum&lt;/code&gt;, and suddenly your codebase is littered with &lt;code&gt;if (version == 1) ... else if (version == 2)&lt;/code&gt; blocks.&lt;/p&gt;

&lt;p&gt;I built Proto Wrapper Plugin to solve this problem once and for all.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Pain Point
&lt;/h2&gt;

&lt;p&gt;Here's a typical scenario. You have a payment processing system. Version 1 of your protocol uses an integer for payment type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight protobuf"&gt;&lt;code&gt;&lt;span class="c1"&gt;// v1/payment.proto&lt;/span&gt;
&lt;span class="kd"&gt;message&lt;/span&gt; &lt;span class="nc"&gt;Payment&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kt"&gt;int32&lt;/span&gt; &lt;span class="na"&gt;payment_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// 1=cash, 2=card, 3=transfer&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then someone decides (rightfully so) that enums are better:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight protobuf"&gt;&lt;code&gt;&lt;span class="c1"&gt;// v2/payment.proto&lt;/span&gt;
&lt;span class="kd"&gt;message&lt;/span&gt; &lt;span class="nc"&gt;Payment&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;PaymentType&lt;/span&gt; &lt;span class="na"&gt;payment_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;enum&lt;/span&gt; &lt;span class="n"&gt;PaymentType&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;CASH&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="na"&gt;CARD&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="na"&gt;TRANSFER&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now your code looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This is everywhere in your codebase&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;protocolVersion&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;paymentV1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPaymentType&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;processPayment&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;protocolVersion&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;PaymentType&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;paymentV2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPaymentType&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;processPayment&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getNumber&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Multiply this by dozens of messages, and you have a maintenance nightmare.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution: One Interface to Rule Them All
&lt;/h2&gt;

&lt;p&gt;Proto Wrapper generates a unified Java API that works across all your protocol versions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Single code path. Works with v1, v2, v3... whatever.&lt;/span&gt;
&lt;span class="nc"&gt;Payment&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;versionContext&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;wrapPayment&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;anyProto&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPaymentType&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Always returns int&lt;/span&gt;
&lt;span class="nc"&gt;PaymentType&lt;/span&gt; &lt;span class="n"&gt;typeEnum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getPaymentTypeEnum&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Returns enum when available&lt;/span&gt;

&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;bytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toBytes&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Serializes back to original version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The plugin analyzes your proto schemas, detects conflicts, and generates appropriate accessors. You write version-agnostic code, and the generated wrappers handle the details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;Add the plugin to your Maven build:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;io.alnovis&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;proto-wrapper-maven-plugin&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&lt;/span&gt;2.3.2&lt;span class="nt"&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;basePackage&amp;gt;&lt;/span&gt;com.example.model&lt;span class="nt"&gt;&amp;lt;/basePackage&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;protoRoot&amp;gt;&lt;/span&gt;${basedir}/proto&lt;span class="nt"&gt;&amp;lt;/protoRoot&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;versions&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&amp;lt;protoDir&amp;gt;&lt;/span&gt;v1&lt;span class="nt"&gt;&amp;lt;/protoDir&amp;gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;version&amp;gt;&amp;lt;protoDir&amp;gt;&lt;/span&gt;v2&lt;span class="nt"&gt;&amp;lt;/protoDir&amp;gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/versions&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;execution&amp;gt;&amp;lt;goals&amp;gt;&amp;lt;goal&amp;gt;&lt;/span&gt;generate&lt;span class="nt"&gt;&amp;lt;/goal&amp;gt;&amp;lt;/goals&amp;gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or if you prefer Gradle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;plugins&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"io.alnovis.proto-wrapper"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="s"&gt;"2.3.2"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;protoWrapper&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;basePackage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"com.example.model"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;protoRoot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"proto"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="nf"&gt;versions&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;version&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"v1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;version&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"v2"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;mvn generate-sources&lt;/code&gt; or &lt;code&gt;./gradlew generateProtoWrapper&lt;/code&gt;, and you get a clean API.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Conflicts Can It Handle?
&lt;/h2&gt;

&lt;p&gt;Over time, I've added support for pretty much every type conflict I've encountered in production:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;INT_ENUM&lt;/strong&gt; — The most common one. Field starts as &lt;code&gt;int32&lt;/code&gt;, becomes &lt;code&gt;enum&lt;/code&gt;. You get both &lt;code&gt;getField()&lt;/code&gt; (returns int) and &lt;code&gt;getFieldEnum()&lt;/code&gt; (returns enum).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ENUM_ENUM&lt;/strong&gt; — Two different enums across versions. Unified as int with enum conversion methods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WIDENING&lt;/strong&gt; — &lt;code&gt;int32&lt;/code&gt; grows to &lt;code&gt;int64&lt;/code&gt;. The wrapper uses &lt;code&gt;long&lt;/code&gt; everywhere, with runtime validation for versions that need the narrower type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FLOAT_DOUBLE&lt;/strong&gt; — Precision change from &lt;code&gt;float&lt;/code&gt; to &lt;code&gt;double&lt;/code&gt;. Unified as &lt;code&gt;double&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;SIGNED_UNSIGNED&lt;/strong&gt; — &lt;code&gt;int32&lt;/code&gt; vs &lt;code&gt;uint32&lt;/code&gt;, &lt;code&gt;sint32&lt;/code&gt; vs &lt;code&gt;int32&lt;/code&gt;. Unified as &lt;code&gt;long&lt;/code&gt; with proper range validation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PRIMITIVE_MESSAGE&lt;/strong&gt; — A simple &lt;code&gt;int64 total&lt;/code&gt; becomes &lt;code&gt;Money total&lt;/code&gt; with amount and currency. You get &lt;code&gt;getTotal()&lt;/code&gt; for primitive versions and &lt;code&gt;getTotalMessage()&lt;/code&gt; for message versions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STRING_BYTES&lt;/strong&gt; — Someone decides &lt;code&gt;string&lt;/code&gt; should be &lt;code&gt;bytes&lt;/code&gt;. Dual accessors handle the conversion.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;REPEATED_SINGLE&lt;/strong&gt; — Field changes from singular to repeated or vice versa. Returns &lt;code&gt;List&lt;/code&gt; in all cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FIELD_RENUMBER&lt;/strong&gt; — Sometimes field numbers change between versions (legacy systems, don't ask). You can explicitly map them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;fieldMappings&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;fieldMapping&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;message&amp;gt;&lt;/span&gt;Order&lt;span class="nt"&gt;&amp;lt;/message&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;fieldName&amp;gt;&lt;/span&gt;parent_ref&lt;span class="nt"&gt;&amp;lt;/fieldName&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;versionNumbers&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;v1&amp;gt;&lt;/span&gt;3&lt;span class="nt"&gt;&amp;lt;/v1&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;v2&amp;gt;&lt;/span&gt;5&lt;span class="nt"&gt;&amp;lt;/v2&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/versionNumbers&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/fieldMapping&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/fieldMappings&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The diff tool can even detect suspected renumbering and suggest the configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Batteries Included
&lt;/h2&gt;

&lt;p&gt;A few things that make life easier:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No protoc installation required.&lt;/strong&gt; The plugin downloads the right protoc binary for your platform automatically. Just works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Incremental builds.&lt;/strong&gt; Changed one proto file? Only affected wrappers regenerate. Saves 50%+ build time on large projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Well-known types conversion.&lt;/strong&gt; &lt;code&gt;google.protobuf.Timestamp&lt;/code&gt; becomes &lt;code&gt;java.time.Instant&lt;/code&gt;. &lt;code&gt;Duration&lt;/code&gt; becomes &lt;code&gt;java.time.Duration&lt;/code&gt;. No more manual conversions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Builder pattern.&lt;/strong&gt; Full support for creating and modifying messages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Payment&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Payment&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newBuilder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setPaymentType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PaymentType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;CARD&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setAmount&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Money&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;newBuilder&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setValue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setCurrency&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"USD"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Spring Boot Starter.&lt;/strong&gt; For Spring Boot 3+ projects, there's an auto-configuration that handles version context per HTTP request.&lt;/p&gt;

&lt;h2&gt;
  
  
  Schema Diff Tool
&lt;/h2&gt;

&lt;p&gt;Before deploying a new protocol version, you probably want to know what changed. The built-in diff tool helps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mvn proto-wrapper:diff &lt;span class="nt"&gt;-Dv1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;proto/v1 &lt;span class="nt"&gt;-Dv2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;proto/v2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It shows added/removed fields, type changes, and flags breaking changes. You can integrate it into CI to catch compatibility issues before they hit production:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mvn proto-wrapper:diff &lt;span class="nt"&gt;-Dv1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;proto/production &lt;span class="nt"&gt;-Dv2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;proto/development &lt;span class="nt"&gt;-DfailOnBreaking&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output formats include text, JSON, and Markdown for reports.&lt;/p&gt;

&lt;h2&gt;
  
  
  Version Constants
&lt;/h2&gt;

&lt;p&gt;No more magic strings. The plugin generates a &lt;code&gt;ProtocolVersions&lt;/code&gt; class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Compile-time constants&lt;/span&gt;
&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ProtocolVersions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;V1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Runtime validation&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ProtocolVersions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isSupported&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;versionId&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;VersionContext&lt;/span&gt; &lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;VersionContext&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;forVersionId&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;versionId&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Fail fast on unknown versions&lt;/span&gt;
&lt;span class="nc"&gt;ProtocolVersions&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;requireSupported&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;versionId&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Generated Code
&lt;/h2&gt;

&lt;p&gt;Here's what the structure looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;com/example/model/
├── api/
│   ├── Payment.java           # Interface
│   ├── PaymentType.java       # Unified enum
│   ├── VersionContext.java    # Factory
│   ├── ProtocolVersions.java  # Constants
│   └── impl/
│       └── AbstractPayment.java
├── v1/
│   ├── PaymentV1.java
│   └── VersionContextV1.java
└── v2/
    ├── PaymentV2.java
    └── VersionContextV2.java
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The generated code is plain Java. No runtime dependencies beyond protobuf itself. You can read it, debug it, understand it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://github.com/alnovis/proto-wrapper-plugin" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://central.sonatype.com/artifact/io.alnovis/proto-wrapper-core" rel="noopener noreferrer"&gt;Maven Central&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/alnovis/proto-wrapper-plugin/tree/main/docs" rel="noopener noreferrer"&gt;Full Documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The plugin is open source (Apache 2.0) and actively maintained. If you're dealing with protobuf versioning headaches, give it a try.&lt;/p&gt;

</description>
      <category>java</category>
      <category>protobuf</category>
      <category>grpc</category>
      <category>codegen</category>
    </item>
  </channel>
</rss>
