A heavy profile

This tutorial keeps a per-merchant profile too heavy to compute on every request or on every payment: many reads, a long history, a model. It is computed again a few seconds after a merchant's last payment, once per burst of payments. You then send one burst to two hooks, one per queue policy, and compare when each would run.

You need a test featuremeshd, Kafka and Redis ( Tutorial settings ). Complete Buckets per key first.

The hook

FeatureQL
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.HOOK_PROFILE 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_profile])
    ),
    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
    ),
    profile_due := DATE_TRUNC(event_time + INTERVAL '5 SECOND', 'second')::TIMESTAMP(0),
    pay_ps := PREPARED_STATEMENT(
        pay_row
        USING (payment_id, merchant_id, amount, event_time)
        CAPTURE (profile_due)
    ),
    pay_index := SINK(
        'redis://host.docker.internal:26379'
        KEY 'tut_hook_profile: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
    ),
    kv := SOURCE_KV('redis://host.docker.internal:26379'),
    m_pays := EXTERNAL_KV(
        KEY 'tut_hook_profile: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],
    req_ts := INPUT(TIMESTAMP),
    window_pays := m_pays.CARRY(req_ts AS t).TRANSFORM(
        RETURN amount
        WHERE event_time > t - INTERVAL '36500 DAY' AND event_time <= t
    ),
    profile_row := ROW(
        ARRAY_COUNT(window_pays) AS n,
        ARRAY_SUM(window_pays[amount]) AS total,
        ARRAY_MAX(window_pays[amount]) AS largest
    ),
    profile_ps := PREPARED_STATEMENT(
        profile_row
        USING (merchant_id, req_ts := REQUEST_TIME())
    ),
    profile_last := SINK(
        'redis://host.docker.internal:26379'
        KEY 'tut_hook_profile:last:{' || merchant_id::VARCHAR || '}'
        WITH (
            FORMAT = 'json',
            MODE = 'latest',
            RETENTION = INTERVAL '36500 DAY'
        )
    ),
    profile_queue := SINK(
        'redis://host.docker.internal:26379'
        KEY 'tut_hook_profile:queue'
    ),
    profile := HOOK(
        RUN profile_ps
        ON pay_ps
        AT profile_due
        USING (merchant_id)
        TO profile_last
        QUEUE profile_queue
        WITH (
            POLICY = 'latest',
            EVERY = INTERVAL '1 HOUR',
            EXPIRE = INTERVAL '36500 DAY'
        )
    )
;
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.HOOK_PROFILE.AMOUNTCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.EVENT_TIMECREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.KVCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.MERCHANT_IDCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.M_PAYSCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.ON_PAYMENTCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.PAYMENTSCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.PAYMENT_IDCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.PAY_INDEXCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.PAY_PSCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.PAY_ROWCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.PROFILECREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.PROFILE_DUECREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.PROFILE_LASTCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.PROFILE_PSCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.PROFILE_QUEUECREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.PROFILE_ROWCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.REQ_TSCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.STREAMCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.WINDOW_PAYSCREATEDFeature created as not exists

  • PROFILE_PS is a statement you could serve as it is. It reads the merchant's index up to REQ_TS, bound to REQUEST_TIME(). In a scheduled hook, REQUEST_TIME() is the drain's time, so the profile is as of when it ran, and carries that time as its request_time.
  • Each payment captures PROFILE_DUE, 5 seconds after the payment's own event_time, in whole seconds. ON PAY_PS AT PROFILE_DUE USING (MERCHANT_ID) queues one entry per merchant.
  • POLICY = 'latest' debounces. When a payment queues the merchant again before its entry runs, the entry keeps the later of the two due times. A burst keeps pushing the run back, and the profile runs once, 5 seconds after the burst.
  • QUEUE PROFILE_QUEUE keeps the queue in Redis, next to the profiles. Its KEY is a plain prefix: FeatureMesh adds the braces of a Redis Cluster hash tag itself ( Queues ).

Payments in, profiles out

Merchant 7 pays three times. Merchant 9 pays once:

FeatureQL
TEST SERVING PRODUCE 'FM.TUTORIALS.HOOK_PROFILE.PAYMENTS'
{"key": "7", "time": "2025-11-24T15:00:10", "partition": 0, "value": {"payment_id": 1, "merchant_id": 7, "amount": 10.0, "event_time": "2025-11-24T15:00:10"}}
{"key": "9", "time": "2025-11-24T15:00:30", "partition": 0, "value": {"payment_id": 4, "merchant_id": 9, "amount": 5.0, "event_time": "2025-11-24T15:00:30"}}
{"key": "7", "time": "2025-11-24T15:00:40", "partition": 0, "value": {"payment_id": 2, "merchant_id": 7, "amount": 3.0, "event_time": "2025-11-24T15:00:40"}}
{"key": "7", "time": "2025-11-24T15:01:20", "partition": 0, "value": {"payment_id": 3, "merchant_id": 7, "amount": 4.0, "event_time": "2025-11-24T15:01:20"}}

FeatureQL
TEST SERVING DRAIN 'FM.TUTORIALS.HOOK_PROFILE.PROFILE';
Result
HOOK VARCHARDRAINS BIGINTFIRED BIGINTEXPIRED BIGINTDELETED BIGINTDATA_ERRORS BIGINT
FM.TUTORIALS.HOOK_PROFILE.PROFILE12020

Four payments made two profile runs, one per merchant. Merchant 7's profile covers its three payments:

Redis
EVAL "local v = cjson.decode(redis.call('GET', KEYS[1])) return {v.n, string.format('%.1f', v.total), string.format('%.1f', v.largest), tostring(v.request_time ~= nil)}" 1 tut_hook_profile:last:{7}
Result
N BIGINTTOTAL VARCHARLARGEST VARCHARSTAMPED VARCHAR
317.010.0true

'earliest' against 'latest'

Add a second hook on the same payments, with POLICY = 'earliest', its own queue and its own sink. 'earliest' is the default: the entry keeps the earlier due time, so a bucket's close does not move.

FeatureQL
CREATE OR REPLACE FEATURES IN FM.TUTORIALS.HOOK_PROFILE AS
PERSIST
    profile_early_last := SINK(
        'redis://host.docker.internal:26379'
        KEY 'tut_hook_profile:early:{' || merchant_id::VARCHAR || '}'
        WITH (
            FORMAT = 'json',
            MODE = 'latest',
            RETENTION = INTERVAL '36500 DAY'
        )
    ),
    profile_early_queue := SINK(
        'redis://host.docker.internal:26379'
        KEY 'tut_hook_profile:queue_early'
    ),
    profile_early := HOOK(
        RUN profile_ps
        ON pay_ps
        AT profile_due
        USING (merchant_id)
        TO profile_early_last
        QUEUE profile_early_queue
        WITH (
            POLICY = 'earliest',
            EVERY = INTERVAL '1 HOUR',
            EXPIRE = INTERVAL '36500 DAY'
        )
    )
;
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.TUTORIALS.HOOK_PROFILE.PROFILE_EARLYCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.PROFILE_EARLY_LASTCREATEDFeature created as not exists
FM.TUTORIALS.HOOK_PROFILE.PROFILE_EARLY_QUEUECREATEDFeature created as not exists

Merchant 8 pays three times, two seconds apart:

FeatureQL
TEST SERVING PRODUCE 'fm.tutorials.hook_profile.payments'
{"key": "8", "time": "2025-11-24T15:10:00", "partition": 0, "value": {"payment_id": 11, "merchant_id": 8, "amount": 1.0, "event_time": "2025-11-24T15:10:00"}}
{"key": "8", "time": "2025-11-24T15:10:02", "partition": 0, "value": {"payment_id": 12, "merchant_id": 8, "amount": 2.0, "event_time": "2025-11-24T15:10:02"}}
{"key": "8", "time": "2025-11-24T15:10:04", "partition": 0, "value": {"payment_id": 13, "merchant_id": 8, "amount": 3.0, "event_time": "2025-11-24T15:10:04"}}

Each queue now holds one entry for merchant 8. Read its due time from the queue's sorted set:

Redis
EVAL "local out = {} for i = 1, 2 do local z = redis.call('ZRANGE', KEYS[i], 0, -1, 'WITHSCORES') local s = tonumber(z[2]) % 86400 out[i] = string.format('%d entry, due %02d:%02d:%02d', #z / 2, math.floor(s / 3600), math.floor(s % 3600 / 60), s % 60) end return out" 2 {tut_hook_profile:queue}:due {tut_hook_profile:queue_early}:due
Result
latest VARCHARearliest VARCHAR
1 entry, due 15:10:091 entry, due 15:10:05

'latest' kept the last payment's due time, 15:10:09. 'earliest' kept the first's, 15:10:05. Both times have passed by now, so each hook runs once, over the three payments:

FeatureQL
TEST SERVING DRAIN 'FM.TUTORIALS.HOOK_PROFILE.PROFILE';
Result
HOOK VARCHARDRAINS BIGINTFIRED BIGINTEXPIRED BIGINTDELETED BIGINTDATA_ERRORS BIGINT
FM.TUTORIALS.HOOK_PROFILE.PROFILE11010

FeatureQL
TEST SERVING DRAIN 'FM.TUTORIALS.HOOK_PROFILE.PROFILE_EARLY';
Result
HOOK VARCHARDRAINS BIGINTFIRED BIGINTEXPIRED BIGINTDELETED BIGINTDATA_ERRORS BIGINT
FM.TUTORIALS.HOOK_PROFILE.PROFILE_EARLY11010

In live operation, the drain runs every few seconds, and the two due times make the difference. Payments at 10:00:00, 10:00:02 and 10:00:04, then 10:00:06, each due 5 seconds after it:

'earliest', drains at :05 and :10'earliest', first drain at :08'latest', first drain at :12
:00, :02, :04one entry, due :05one entry, due :05one entry, due :09
:06the :05 drain ran: a new entry, due :11joins the entry, still due :05joins it, now due :11
runsat :05 over three payments, then at the first drain at or after :11 over fourat :08 over fourat :12 over four

'latest' computes less, once per burst. A merchant that never stops paying is never profiled under 'latest', since each payment pushes its entry past the next drain. 'earliest' bounds the wait.

What you learned

  • A scheduled hook keyed by merchant computes a heavy value once per burst, not once per payment.
  • In a scheduled hook, REQUEST_TIME() is the drain's time.
  • POLICY = 'latest' keeps the latest due time and debounces; 'earliest' keeps the first and bounds the wait.

Next