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
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'
)
)
;| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.HOOK_PROFILE.AMOUNT | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.EVENT_TIME | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.KV | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.MERCHANT_ID | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.M_PAYS | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.ON_PAYMENT | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.PAYMENTS | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.PAYMENT_ID | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.PAY_INDEX | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.PAY_PS | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.PAY_ROW | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.PROFILE | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.PROFILE_DUE | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.PROFILE_LAST | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.PROFILE_PS | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.PROFILE_QUEUE | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.PROFILE_ROW | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.REQ_TS | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.STREAM | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.WINDOW_PAYS | CREATED | Feature created as not exists |
PROFILE_PSis a statement you could serve as it is. It reads the merchant's index up toREQ_TS, bound toREQUEST_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 itsrequest_time.- Each payment captures
PROFILE_DUE, 5 seconds after the payment's ownevent_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_QUEUEkeeps the queue in Redis, next to the profiles. ItsKEYis 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:
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"}}TEST SERVING DRAIN 'FM.TUTORIALS.HOOK_PROFILE.PROFILE';| HOOK VARCHAR | DRAINS BIGINT | FIRED BIGINT | EXPIRED BIGINT | DELETED BIGINT | DATA_ERRORS BIGINT |
|---|---|---|---|---|---|
| FM.TUTORIALS.HOOK_PROFILE.PROFILE | 1 | 2 | 0 | 2 | 0 |
Four payments made two profile runs, one per merchant. Merchant 7's profile covers its three payments:
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}| N BIGINT | TOTAL VARCHAR | LARGEST VARCHAR | STAMPED VARCHAR |
|---|---|---|---|
| 3 | 17.0 | 10.0 | true |
'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.
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'
)
)
;| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.TUTORIALS.HOOK_PROFILE.PROFILE_EARLY | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.PROFILE_EARLY_LAST | CREATED | Feature created as not exists |
| FM.TUTORIALS.HOOK_PROFILE.PROFILE_EARLY_QUEUE | CREATED | Feature created as not exists |
Merchant 8 pays three times, two seconds apart:
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:
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| latest VARCHAR | earliest VARCHAR |
|---|---|
| 1 entry, due 15:10:09 | 1 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:
TEST SERVING DRAIN 'FM.TUTORIALS.HOOK_PROFILE.PROFILE';| HOOK VARCHAR | DRAINS BIGINT | FIRED BIGINT | EXPIRED BIGINT | DELETED BIGINT | DATA_ERRORS BIGINT |
|---|---|---|---|---|---|
| FM.TUTORIALS.HOOK_PROFILE.PROFILE | 1 | 1 | 0 | 1 | 0 |
TEST SERVING DRAIN 'FM.TUTORIALS.HOOK_PROFILE.PROFILE_EARLY';| HOOK VARCHAR | DRAINS BIGINT | FIRED BIGINT | EXPIRED BIGINT | DELETED BIGINT | DATA_ERRORS BIGINT |
|---|---|---|---|---|---|
| FM.TUTORIALS.HOOK_PROFILE.PROFILE_EARLY | 1 | 1 | 0 | 1 | 0 |
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, :04 | one entry, due :05 | one entry, due :05 | one entry, due :09 |
| :06 | the :05 drain ran: a new entry, due :11 | joins the entry, still due :05 | joins it, now due :11 |
| runs | at :05 over three payments, then at the first drain at or after :11 over four | at :08 over four | at :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
- A materialized view refreshes every key on a schedule, not after activity.
- Choosing a pattern says which policy fits, and when a profile goes stale.
- An entry pinned far in the future clears an entry pinned by a payment dated in the future.
- Size
RETENTION,EXPIREand memory sizes drain batches for heavy calls. - Hooks has the rules for an entry queued while it runs.