Are updates to materialized view done synchronously with updates to base table?

Are updates to materialized view done synchronously with updates to base table? A write request to base table will update base table and won’t return success to client until MV is also updated, or is update to MV done async?

In general, the answer is no, updates to the views are asynchronous: A write to the base table waits only until CL (consistency level) replicas of the base table are updated, but doesn’t wait for the write to the view table.

So for example, if you write to a table with CL=QUORUM and then read from the same table with CL=QUORUM, you are guaranteed to see the value you wrote; But if you read from the view you are not guaranteed to see the new value. The new value will be readable in the view eventually, i.e., eventual consistency, but not immediately.

As an extension over Cassandra, Scylla allows marking a materialized view as WITH synchronous_updates = true; (see explanation and syntax in ScyllaDB CQL Extensions | ScyllaDB Docs). When a view has synchronous updates enabled, the write only succeeds once CL copies were written in both base table and view. In this mode, the new data will be immediately readable from the view. The downside of synchronous-updates mode is higher latency for the writes, as well as the risk of unavailability - a write can fail despite being able to reach the desired consistency level in the base table, because of an inability to reach enough view replicas.

In some cases, Scylla may do synchronous view updates even without this being explicitly requested - however, the user should not rely on this (and in Cassandra, those updates are always asynchronous). The aforementioned documentation explains:

Even in an asynchronous view, some view updates may be done synchronously. This happens when the materialized-view replica is on the same node as the base-table replica. This happens, for example, in tables using vnodes where the base table and the view have the same partition key; But is not the case if the table uses tablets: With tablets, the base and view tablets may migrate to different nodes. In general, users should not, and cannot, rely on these serendipitous synchronous view updates; If synchronous view updates are important, mark the view explicitly with synchronous_updates = true.

1 Like