Out-of-Order Event Streams: Calculating Time Deltas with grouping by topic

We are processing a stream of events on different topics. We want to compute, for each event, how much time has passed since the previous event on the same topic. The issue is that the events do not appear in the order of their timestamps at input! With Pathway there is no need to worry about that!

First we use Debezium to synchronize the input data from a database table with the following columns:

  • timestamp - logical date-time when the event happened
  • topic_id - topic in question
  • message - message content.
import pathway as pw

events =
        "": "$GROUP_NAME",
        "bootstrap.servers": "",
        "": "6000",

Then we sort the events from the table. Pathway provides a sort function to sort a table according to its key column: in this case we are going to sort according to the timestamps of the events. In addition, each topic is mapped to an instance field, which allows us to work on different streams simultaneously. The prev and next pointers are automatically extracted.

sorted_events = events.sort(key=events.timestamp, instance=events.topic_id)

Finally, we process events in order of their timestamps at input.

events_with_prev = events.with_columns(
    prev_timestamp=events.ix(sorted_events.prev, optional=True).timestamp

differences = - pw.this.prev_timestamp)
        "host": "localhost",
        "port": "5432",
        "dbname": "transactions",
        "user": "pathway",
        "password": "my_password",