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.
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
)
;| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.HOOK_WINDOW.AMOUNT | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_WINDOW.EVENT_TIME | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_WINDOW.MERCHANT_ID | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_WINDOW.ON_PAYMENT | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_WINDOW.PAYMENTS | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_WINDOW.PAYMENT_ID | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_WINDOW.PAY_INDEX | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_WINDOW.PAY_PS | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_WINDOW.PAY_ROW | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_WINDOW.STREAM | CREATED | Feature created as not exists |
Read it from the top:
STREAMdeclares the broker and the format of its records, asSOURCE_DB()declares a database.PAYMENTSdeclares the record's fields and their types, from the topictut_hook_window. A record is never looked up, so it has noBIND TO.PAY_PSis an ordinary prepared statement. It only shapes the payment: the fields the window needs, nothing read from elsewhere.PAY_INDEXis the sink.MODE = 'append'withSHAPE = 'sorted_set'makes one sorted set per merchant, each member scored by itsevent_time.UNIQUE ON payment_idmakes a record with a known payment ID replace its member instead of adding a second one.ON_PAYMENTis the hook.ON PAYMENTS AS pfires it on each record,USING (...)passes the record's fields to the statement's inputs, andTO PAY_INDEXwrites the result.
The Hooks and Sinks and reading back references have every clause.
The read
The index is read like any key-value source:
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))
;| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.HOOK_WINDOW.AT | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_WINDOW.KV | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_WINDOW.PAYS | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_WINDOW.PAYS_10M | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_WINDOW.WINDOW_10M | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_WINDOW.WINDOW_PS | CREATED | Feature created as not exists |
PAYSdeclares 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 asARRAY<ROW<...>>.PAYS_10Mkeeps the members of the ten minutes beforeAT. This filter onevent_timebecomes the sorted set's score range, so only the window's members are fetched.WINDOW_PStakes the merchant and the time of the window. In production, bind the time withAT := 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:
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:
FM.TUTORIALS.HOOK_WINDOW.WINDOW_PS{"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 BIGINT | AT VARCHAR | WINDOW_PS VARCHAR |
|---|---|---|
| 7 | 2025-11-24 15:08:00 | {n: 2, total: 14.0} |
| 7 | 2025-11-24 15:13:00 | {n: 2, total: 5.0} |
| 9 | 2025-11-24 15:13:00 | {n: 1, total: 5.0} |
| 9 | 2025-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:
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:
FM.TUTORIALS.HOOK_WINDOW.WINDOW_PS{"input_1": [[7, "2025-11-24 15:08:00"], [7, "2025-11-24 15:13:00"]]} | merchant_id BIGINT | at VARCHAR | window_ps VARCHAR |
|---|---|---|
| 7 | 2025-11-24 15:08:00 | {n: 3, total: 16.0} |
| 7 | 2025-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:
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"}}FM.TUTORIALS.HOOK_WINDOW.WINDOW_PS{"input_1": [[7, "2025-11-24 15:13:00"]]} | merchant_id BIGINT | at VARCHAR | window_ps VARCHAR |
|---|---|---|
| 7 | 2025-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:
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"}}FM.TUTORIALS.HOOK_WINDOW.WINDOW_PS{"input_1": [[7, "2025-11-24 15:13:00"]]} | merchant_id BIGINT | at VARCHAR | window_ps VARCHAR |
|---|---|---|
| 7 | 2025-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
appendsink withSHAPE = '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
- Scoring each event stores a value per payment, and then a decision per payment over this index.
- Choosing a pattern says when a window read at request time is enough, and when to read buckets.
- Late, corrected and missing events covers what a window can miss.
- Size
RETENTION,EXPIREand memory sizes the index for the window. - Start, backfill and replay explains why a new hook starts with an empty index.