title: Is Clickhouse Buffer Table appropriate for realtime ingestion of many small inserts?
body: <p>I am writing an application that plots financial data and interacts with a realtime feed of such data. Due to the nature of the task, live market data may be received very frequently in one-trade-at-a-time fashion. I am using the database locally and I am the only user. There is only one program (my middleware) that will be inserting data to the db. My primary concern is latency - I want to minimize it as much as possible. For that reason, I would like to avoid having a queue (in a sense, I want the Buffer Table to fulfill that role). A lot of the analytics Clickhouse calculates for me are expected to be realtime (as much as possible) as well. I have three questions:</p>
  <ol>
  <li>Clarify some limitations/caveats from the Buffer Table documentation</li>
  <li>Clarify how querying works (regular queries + materialized views)</li>
  <li>What happens when I query the db when data is being flushed</li>
  </ol>
  <p><strong>Question 1)</strong> Clarify some limitations/caveats from the Buffer Table documentation</p>
  <p>Based on Clickhouse documentation, I understand that many small INSERTs are sub-optimal to say the least. While researching the topic I found that the Buffer Engine [1] could be used as a solution. It made sense to me, however when I read Buffer's documentation I found some caveats:</p>
  <blockquote>
  <p>Note that it does not make sense to insert data one row at a time, even for Buffer tables. This will only produce a speed of a few thousand rows per second, while inserting larger blocks of data can produce over a million rows per second (see the section “Performance”).</p>
  </blockquote>
  <p>A few thousand rows per second is absolutely fine for me, however I am concerned about other performance considerations - if I do commit data to the buffer table one row at a time, should I expect spikes in CPU/memory? If I understand correctly, committing one row at a time to a MergeTree table would cause a lot of additional work for the merging job, but it should not be a problem if Buffer Table is used, correct?</p>
  <blockquote>
  <p>If the server is restarted abnormally, the data in the buffer is lost.</p>
  </blockquote>
  <p>I understand that this refers to things like power outage or computer crashing. If I shutdown the computer normally or stop the clickhouse server normally, can I expect the buffer to flush data to the target table?</p>
  <p><strong>Question 2)</strong> Clarify how querying works (regular queries + materialized views)</p>
  <blockquote>
  <p>When reading from a Buffer table, data is processed both from the buffer and from the destination table (if there is one).
  Note that the Buffer tables does not support an index. In other words, data in the buffer is fully scanned, which might be slow for large buffers. (For data in a subordinate table, the index that it supports will be used.)</p>
  </blockquote>
  <p>Does that mean I can use queries against the target table and expect Buffer Table data to be included automatically? Or is it the other way around - I query the buffer table and the target table is included in the background? If either is true (and I don't need to aggregate both tables manually), does that also mean Materialized Views would be populated? Which table should trigger the materialized view - the on-disk table or the buffer table? Or both, in some way?</p>
  <p>I rely on Materialized Views a lot and need them updated in realtime (or as close as possible). What would be the best strategy to accomplish that goal?</p>
  <p><strong>Question 3)</strong> What happens when I query the db when data is being flushed?</p>
  <p>My two main concerns here are with regards to:</p>
  <ol>
  <li>Running a query at the exact time flushing occurs - is there a risk of duplicated records or omitted records?</li>
  <li>At which point are Materialized Views of the target table populated (I suppose it depends on whether it's the target table or the buffer table that triggers the MV)? Is flushing the buffer important in how I structure the MV?</li>
  </ol>
  <p>Thank you for your time.</p>
  <p>[1] <a href="https://clickhouse.tech/docs/en/engines/table-engines/special/buffer/" rel="nofollow noreferrer">https://clickhouse.tech/docs/en/engines/table-engines/special/buffer/</a></p>