HOP and TUMBLE, we compute aggregates for a window once the watermark passes. This is a powerful model, as allows the streaming system to signal completeness to its consumers, so they don’t need to reason about when they are able to trust the results. However, the requirement that all computations are windowed can be limiting.
Updating semantics, on the other hand, allow for more flexibility in the types of queries that can be expressed, including most of normal batch SQL. It works by treating the input stream as a table that is constantly being updated, and the output as updates on a materialized view of the query.
When writing to a sink, the output is a stream of updates (in Debezium format), representing additions, updates, and deletes to the materialized view.
Reading from updating sources
Source connectors such as Kafka can specify the format as'debezium_json' to read Debezium formatted messages.
Updating sources need at least one primary key, which tells Ingest Engine which rows are logically the same. The primary key is specified in the DDL, like this:
Writing to updating sinks
Updating queries can be written to sinks with thedebezium_json format. This output can then be consumed by the Debezium sink connector to write to a RDBMS like MySQL or Postgres.
For a complete example of this, see the PostgreSQL CDC to Iceberg example.
TTLs
The base semantics of updating tables require that, for any event that comes in, we must be able to update the state the output. However doing this with complete correctness would require storing data for all time. This is generally intractable in a streaming system without blowing up our state. Therefore, updating states have a time-to-live (TTL) associated with them. This TTL is the maximum amount of time we will store a key after we last saw an event for it. (A key might be something like a user id or a transaction id; generally this is the thing being grouped by in an aggregation or joined on.) By default, the TTL is 1 day, but it can be configured with theSET updating_ttl command, which takes a SQL interval. For example, to set the TTL to 1 hour:
TTL eviction example
If TTL is set to 1 hour and a customer hasn’t placed an order in over 1 hour, their aggregation state is dropped. The next order from that customer starts a fresh aggregation. Consider aSUM(amount) grouped by customer_id with updating_ttl = '1 hour':
- Customer
c1orders at 10:00 (amount=50), 10:15 (amount=60), 10:30 (amount=40) - running total = 150.00 - No orders from
c1for 1.5 hours (exceeds the 1 hour TTL) - State for
c1is evicted - Customer
c1orders at 12:00 (amount=40.00) - treated as a new group, total = 40.00 (not 190.00)
Updating aggregates
Aggregating data without a window will result in an updating output. This will emit an insert the first time data is processed for a group and subsequent data will retract the prior value and then insert the new value. Aggregates are buffered in the operator, occasionally flushing. By default flushing happens every 1 second, but can be overridden with thepipeline.update-aggregate-flush-interval config.
For instance, the following query
For examples of manipulating updating data, see the Debezium documentation.
Worked example: aggregating orders by customer
Given an input streamorders:
And this query:
"c"= create - emitted the first time a group key is seen (first order for that customer)."u"= update - emitted when a group’s aggregate changes. For each update, the old value is retracted and the new value is inserted.