A rolling window

This tutorial builds a count and a sum of a merchant's payments over the last ten minutes, read at the moment of the request. Nothing is computed ahead. An event hook keeps each merchant's recent payments in Redis, and serving reads the window from there when asked.

You need a test featuremeshd, Kafka and Redis. Tutorial settings lists what to run, and explains the settings that exist only because the records are dated in the past.

The index

Payments arrive on a Kafka topic. An event hook runs a prepared statement on each record of the topic, and writes the result to a sink. Here the result is the payment itself, and the sink is the merchant's event index: one Redis sorted set per merchant, with one member per payment.

FeatureQL
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.HOOK_WINDOW AS
PERSIST
    stream := SOURCE_STREAM(
        'kafka://host.docker.internal:29093'
        WITH (
            FORMAT = 'json'
        )
    ),
    payments := EXTERNAL_COLUMNS(
        payment_id BIGINT,
        merchant_id BIGINT,
        amount DOUBLE,
        event_time TIMESTAMP(3)
        FROM TABLE(stream[tut_hook_window])
    ),
    payment_id := INPUT(BIGINT),
    merchant_id := INPUT(BIGINT),
    amount := INPUT(DOUBLE),
    event_time := INPUT(TIMESTAMP(3)),
    pay_row := ROW(
        event_time AS event_time,
        payment_id AS payment_id,
        amount AS amount
    ),
    pay_ps := PREPARED_STATEMENT(
        pay_row
        USING (payment_id, merchant_id, amount, event_time)
    ),
    pay_index := SINK(
        'redis://host.docker.internal:26379'
        KEY 'tut_hook_window:pay:{' || merchant_id::VARCHAR || '}'
        INDEXED ON event_time UNIQUE ON payment_id
        WITH (
            FORMAT = 'msgpack',
            MODE = 'append',
            SHAPE = 'sorted_set',
            RETENTION = INTERVAL '36500 DAY'
        )
    ),
    on_payment := HOOK(
        RUN pay_ps
        ON payments AS p
        USING (p[payment_id], p[merchant_id], p[amount], p[event_time])
        TO pay_index
    )
;
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.HOOK_WINDOW.AMOUNTCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_WINDOW.EVENT_TIMECREATEDFeature created as not exists
FM.TUTORIALS.HOOK_WINDOW.MERCHANT_IDCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_WINDOW.ON_PAYMENTCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_WINDOW.PAYMENTSCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_WINDOW.PAYMENT_IDCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_WINDOW.PAY_INDEXCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_WINDOW.PAY_PSCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_WINDOW.PAY_ROWCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_WINDOW.STREAMCREATEDFeature created as not exists

Read it from the top:

  • STREAM declares the broker and the format of its records, as SOURCE_DB() declares a database.
  • PAYMENTS declares the record's fields and their types, from the topic tut_hook_window. A record is never looked up, so it has no BIND TO.
  • PAY_PS is an ordinary prepared statement. It only shapes the payment: the fields the window needs, nothing read from elsewhere.
  • PAY_INDEX is the sink. MODE = 'append' with SHAPE = 'sorted_set' makes one sorted set per merchant, each member scored by its event_time. UNIQUE ON payment_id makes a record with a known payment ID replace its member instead of adding a second one.
  • ON_PAYMENT is the hook. ON PAYMENTS AS p fires it on each record, USING (...) passes the record's fields to the statement's inputs, and TO PAY_INDEX writes the result.

The Hooks and Sinks and reading back references have every clause.

The read

The index is read like any key-value source:

FeatureQL
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.HOOK_WINDOW AS
PERSIST
    kv := SOURCE_KV('redis://host.docker.internal:26379'),
    at := INPUT(TIMESTAMP(3)),
    pays := EXTERNAL_KV(
        KEY 'tut_hook_window:pay:{' || merchant_id::VARCHAR || '}'
        FROM kv
        AS ARRAY<
            ROW<
                event_time TIMESTAMP(3),
                payment_id BIGINT,
                amount DOUBLE,
                request_time TIMESTAMP(6),
                compute_time TIMESTAMP(6),
                datakey VARCHAR,
                partition INT,
                offset BIGINT,
                drain BIGINT
            >
        > INDEXED ON event_time
        WITH (
            FORMAT = 'msgpack'
        )
    )[value],
    pays_10m := pays.CARRY(at AS t).TRANSFORM(
        RETURN amount
        WHERE event_time > t - INTERVAL '10 MINUTE' AND event_time <= t
    ),
    window_10m := ROW(
        ARRAY_COUNT(pays_10m) AS n,
        COALESCE(ARRAY_SUM(pays_10m[amount]), 0e0) AS total
    ),
    window_ps := PREPARED_STATEMENT(window_10m USING (merchant_id, at))
;
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.HOOK_WINDOW.ATCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_WINDOW.KVCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_WINDOW.PAYSCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_WINDOW.PAYS_10MCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_WINDOW.WINDOW_10MCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_WINDOW.WINDOW_PSCREATEDFeature created as not exists

  • PAYS declares the members of the merchant's sorted set: the payment's fields, then the layout columns every stored value has (request_time, compute_time, datakey, partition, offset, drain), in that order. ARRAY(ROW(...)) is the same type as ARRAY<ROW<...>>.
  • PAYS_10M keeps the members of the ten minutes before AT. This filter on event_time becomes the sorted set's score range, so only the window's members are fetched.
  • WINDOW_PS takes the merchant and the time of the window. In production, bind the time with AT := REQUEST_TIME() so that the window ends at each call.

Payments in, windows out

Merchant 7 pays at 15:00, 15:06 and 15:12. Merchant 9 pays once, at 15:05:

FeatureQL
TEST SERVING PRODUCE 'FM.TUTORIALS.HOOK_WINDOW.PAYMENTS'
{"key": "7", "time": "2025-11-24T15:00:00", "partition": 0, "value": {"payment_id": 1, "merchant_id": 7, "amount": 10.0, "event_time": "2025-11-24T15:00:00"}}
{"key": "9", "time": "2025-11-24T15:05:00", "partition": 0, "value": {"payment_id": 2, "merchant_id": 9, "amount": 5.0, "event_time": "2025-11-24T15:05:00"}}
{"key": "7", "time": "2025-11-24T15:06:00", "partition": 0, "value": {"payment_id": 3, "merchant_id": 7, "amount": 4.0, "event_time": "2025-11-24T15:06:00"}}
{"key": "7", "time": "2025-11-24T15:12:00", "partition": 0, "value": {"payment_id": 4, "merchant_id": 7, "amount": 1.0, "event_time": "2025-11-24T15:12:00"}}

Read each merchant's window at a few times:

Prepared
FM.TUTORIALS.HOOK_WINDOW.WINDOW_PS
Call 1
{"input_1": [[7, "2025-11-24 15:08:00"], [7, "2025-11-24 15:13:00"], [9, "2025-11-24 15:13:00"], [9, "2025-11-24 15:16:00"]]}
MERCHANT_ID BIGINTAT VARCHARWINDOW_PS VARCHAR
72025-11-24 15:08:00{n: 2, total: 14.0}
72025-11-24 15:13:00{n: 2, total: 5.0}
92025-11-24 15:13:00{n: 1, total: 5.0}
92025-11-24 15:16:00{n: 0, total: 0.0}

The window is computed from the members present when the call runs. There is no bucket to close. At 15:13 the window is (15:03, 15:13]: the payment of 15:00 has left it, and a payment at exactly 15:03 would have too.

A late payment

A record can reach the topic long after the payment. Here merchant 7 paid 2.0 at 15:02, and the record arrives at 15:20:

FeatureQL
TEST SERVING PRODUCE 'fm.tutorials.hook_window.payments'
{"key": "7", "time": "2025-11-24T15:20:00", "partition": 0, "value": {"payment_id": 5, "merchant_id": 7, "amount": 2.0, "event_time": "2025-11-24T15:02:00"}}

Read the same two windows again:

Prepared
FM.TUTORIALS.HOOK_WINDOW.WINDOW_PS
Call 1
{"input_1": [[7, "2025-11-24 15:08:00"], [7, "2025-11-24 15:13:00"]]}
merchant_id BIGINTat VARCHARwindow_ps VARCHAR
72025-11-24 15:08:00{n: 3, total: 16.0}
72025-11-24 15:13:00{n: 2, total: 5.0}

The window at 15:08 now counts the late payment: it holds three payments, 16.0. The window at 15:13 starts after 15:03, so the payment of 15:02 is outside it, and nothing changes there. A window holds what the hook has indexed when the read runs. A read at 15:08, made before the record arrived, returned two payments.

A correction, then a replay

A payment can be corrected. Here payment 4 was 6.0, not 1.0, and the correction is sent under the same payment ID:

FeatureQL
TEST SERVING PRODUCE 'fm.tutorials.hook_window.payments'
{"key": "7", "time": "2025-11-24T15:30:00", "partition": 0, "value": {"payment_id": 4, "merchant_id": 7, "amount": 6.0, "event_time": "2025-11-24T15:12:00"}}

Prepared
FM.TUTORIALS.HOOK_WINDOW.WINDOW_PS
Call 1
{"input_1": [[7, "2025-11-24 15:13:00"]]}
merchant_id BIGINTat VARCHARwindow_ps VARCHAR
72025-11-24 15:13:00{n: 2, total: 10.0}

The corrected record replaced its member, so the window sums 4.0 and 6.0. Now the original record of payment 4 is read again, as a replay of the topic would read it:

FeatureQL
TEST SERVING PRODUCE 'fm.tutorials.hook_window.payments'
{"key": "7", "time": "2025-11-24T15:12:00", "partition": 0, "value": {"payment_id": 4, "merchant_id": 7, "amount": 1.0, "event_time": "2025-11-24T15:12:00"}}

Prepared
FM.TUTORIALS.HOOK_WINDOW.WINDOW_PS
Call 1
{"input_1": [[7, "2025-11-24 15:13:00"]]}
merchant_id BIGINTat VARCHARwindow_ps VARCHAR
72025-11-24 15:13:00{n: 2, total: 5.0}

The old record replaced the correction. The index takes events as immutable: a record with a known payment ID replaces its member, whatever its time. To correct a payment, send new events with their own IDs instead: a reversal and the corrected payment ( Late, corrected and missing events ).

What you learned

  • An event hook runs a prepared statement on each record of a topic, and writes the result to a sink.
  • An append sink with SHAPE = 'sorted_set' keeps each key's events, one member per identity.
  • Serving reads a window from the index at request time, and fetches only the window's members.
  • A window counts what is indexed when it is read: a late payment changes the windows that hold its time.
  • A record with a known identity replaces its member, so a replayed old record undoes a correction.

Next