Precomputing features
This section is the reference for precompute: streams, hooks, sinks and delivery. How featuremeshd runs hooks, its consumer groups and replicas, is in the Platform guides ( Configure featuremeshd ). Why features are precomputed, and how it fits with serving, is in Real-time serving . This page says how the parts fit together: the kinds of hook, queues, the times a value carries, how results stay correct, and the limits.
The pages
| Page | Covers |
|---|---|
| Streams | Kafka topics, record decoding, reserved fields, connections |
| Hooks | the three kinds, their clauses, WITH, queues and drains, chains, checks at registration |
| Sinks and reading back | modes, which write lands, properties, queues, the write layout, readers |
| Delivery and failures | at least once, the fence, what a call sees, failures, tombstones, leases, shutdown |
Materialized views are scheduled queries, not hooks: see Materialized views . Hooks and views in a Python process are in Hooks and views in Python . What they do not do in this version is in Limits .
Three kinds of hook
| Event hook | Hook on a statement | Scheduled hook | |
|---|---|---|---|
| Declared as | HOOK(… ON <stream record>) | HOOK(… ON <statement>) | HOOK(… ON <statement> AT …) |
| Fired by | each record of a Kafka topic | each successful call of its statement by another hook | an entry of its queue falling due |
REQUEST_TIME() | the record's Kafka timestamp | its trigger's | the drain's time |
The full comparison is in Hooks .
A hook runs a prepared statement you already serve, unchanged, so a feature tested in serving runs as a hook without a second definition. The hook says what fires it (ON), which values the call takes (USING) and where the result goes (TO).
An event hook typically writes the event index, a key's recent events in a Redis sorted set or a table. Serving reads the index as a history. A hook on a statement can then read the index the event hook has just written, such as a decision per payment.
Scheduled hooks and queues
A scheduled hook runs for a reference, a row such as a merchant and a minute. Its queue keeps one entry per reference, with a due time, in a store. A drain runs the entries that fell due, under a lease that lets one featuremeshd drain a queue at a time. Because the queue keeps one entry per reference, ten thousand payments for one merchant in one minute schedule one computation of that minute's bucket.
The queue can also wait for quiet. A heavy profile can be computed once after a burst of activity, rather than once per payment ( A heavy profile ). Its cost then follows the merchants that were active, one computation each per burst. Each payment still costs a read, an index write and a queue write.
Reading results back
Hooks keep no events in memory. What a hook needs from past events, it reads from a sink, usually the event index. Hooks only write. To read a result back, serving uses an ordinary feature over an ordinary source, called a reader ( Sinks and reading back ).
Hooks run in featuremeshd when its configuration enables them. They also run in a Python process with embedded serving, on the same engine, where your code polls the stream and calls the engine ( Hooks and views in Python ).
Four times
Precompute deals with four times, which are easy to mix up:
- A payload field, such as
event_time, is when the event happened, as your producer says. It comes from your record, and a statement reads it like any other field. - The Kafka timestamp is the producer's time, or the broker's for a topic with
LogAppendTime. A record without one gets the consumer's clock when it is read. - A due time is when a queued reference may run. The hook's
ATcomputes it. - The drain's time is when a drain ran the entry, on the queue store's clock, in whole seconds.
Every call has one request_time, the value of REQUEST_TIME(), and every value the call writes carries it:
- a serving call: the time the server received it;
- an event hook: the record's Kafka timestamp, not a field of its payload;
- a hook on a statement: its trigger's
request_time; - a scheduled hook: the drain's time, between the due time and the due time plus
EXPIRE. The bucket's own minute or hour is in its key. Its value is as of the drain, so a later drain reads more of the index; - a view: its slot, the scheduled time the run stands for.
A view's slot is not a snapshot of its sources. They are read as they are when the run executes. Updates since the slot are seen, two sources are not read at one instant, and a retry can read different values. A reproducible view reads sources that keep their past, and filters them on the slot.
How results stay correct
The exact rules are in Delivery and failures . In words, five ideas carry them.
At least once. An event hook commits its place in the topic only after it has written its results. A drain deletes its entries only after they ran. A crash in between runs the same records or entries again, so a value can be written twice.
The latest fence. A latest sink keeps one value per key. A value lands only over an older or equal request_time, so a record arriving out of order, or written a second time, never brings back an older value. Between equal times, the last write wins. The fence has three escapes:
- It lasts as long as the stored row. Once a value expires under
RETENTION, or is deleted, any record lands, an older one included. - An event index has no fence. A member with the same identity is replaced whatever its time. That is why a correction is sent as a new event, a reversal, rather than as an edited one ( Late, corrected and missing events ).
- A record without a Kafka timestamp takes the consumer's clock. A replay gives it a later
request_time, which wins over the values stored since.
Tombstones. A call that raises, a division by zero or a failed cast, replaces its key's latest value with a tombstone, unless a newer value is stored. Serving then reads a miss, not a value that the record should have changed. The tombstone fences like a value.
Retries read their sources again. A call that failed on an unavailable source is computed again, whole. It reads its sources again, so the value it writes can differ from the first attempt's. A value is exact for its record and for what the sources held when it was computed. A guarded read that answers with its fallback in serving is retried in a hook instead, so a stored value never hides an outage.
Calls of one batch also do not see each other's writes. A payment is in the index only once its hook wrote it, so a window read in the statement that writes the index misses that payment and its batch-mates. The window is read in a hook on that statement, or at serving.
When a value is final. A late event queues its bucket again, and the bucket is computed again whole, as long as its due time plus EXPIRE has not passed. After that, the entry is dropped and counted. A bucket is final once the clock passes its due time plus EXPIRE, plus the drain's period and lag. A consumer that needs final values reads buckets older than that ( Buckets per key ).
Changing what is served
A served statement, its hooks and their sinks are features in the registry, so they follow its rules for change. Lifecycle states (DEV, BETA, ACTIVE, DEPRECATED) keep experimental edits from breaking active endpoints.
Limits
Hooks bring streams into FeatureMesh's model, where a value is computed when it is asked for. They keep the indexes, buckets and profiles serving reads, with little to run. They are not a stream processor or a connector platform:
- Formats and inputs. Hooks read JSON records from Kafka topics. Avro, Protobuf, other brokers and change data capture from a database reach a hook through a topic that another tool writes.
- Outputs. Hooks write Redis, PostgreSQL and MySQL or MariaDB. Writing to Kafka or to other systems, exactly once, dead letters and the pace of a replay stay with your pipeline tools.
- State. Hooks hold no window in memory, and a run cannot schedule itself again. A value that changes with time alone, such as a count falling as events leave a window, is read at request time from the index.
- Triggers. A serving call does not fire a hook. Hooks fire on records, on drains and on other hooks' runs.
- Warehouses. A view does not run in a warehouse. There, the warehouse's own materialized views or scheduled queries do the work, and what serving needs is loaded into PostgreSQL or MySQL.
Where a limit is what a pipeline tool does well, the workaround is FeatureQL inside that tool: embedded serving in a Python consumer, or the pipeline calling a prepared statement over featuremeshd's HTTP API. Such a call returns its value only: it writes no sink and fires no hook, so the pipeline writes what it keeps ( Feed hooks from your own pipeline ).
What hooks do not do in this version, each with its workaround. The limits of a deployment are in Configure featuremeshd , and what a Python process does less in Differences from featuremeshd .
| Limit | Workaround |
|---|---|
| Serving calls do not fire hooks. A hook fires on a stream's records, on a queue's drains and on other hooks' runs | Log the calls and forward them to a topic a hook consumes ( Write results to Kafka ) |
No sink writes to Kafka or other systems. A hook writes Redis, PostgreSQL and MySQL/MariaDB; a logger writes files, object stores and stdout:// | A logger and a log collector forward values to a topic, best effort ( Write results to Kafka ) |
| Stream records are JSON. Avro and Protobuf are not decoded | Write them again as JSON to a topic a hook consumes; or call the prepared statement over HTTP and write the outputs yourself; or run embedded serving on the records you decoded |
| Streams are Kafka topics. Change data capture from PostgreSQL or MySQL, and other brokers, are not sources | A pipeline tool brings them into a topic |
| Queues live in PostgreSQL, MySQL/MariaDB or Redis. MySQL-compatible sinks and queues are tested on MariaDB, not on MySQL itself | Test on MySQL before relying on it |
POLICY is 'earliest' or 'latest'. A cooldown, POLICY = 'first', is refused | A debounce under 'latest' runs once after a burst ( A heavy profile ) |
| A recompile that moves a queue drops its entries ( Queues and drains ) | Empty the queue first ( Move a queue ) |
| A queue entry is its reference and its due time. No payload is stored with it | The run reads what it needs from the index or a source |
| A hook does not re-arm itself. A run cannot set its own reference's next due time | A value that should change with time alone, such as a count dropping as events leave a window, is read at request time from the index ( A rolling window ) |
| One drainer per queue. A rollup cannot be split over several queues, because its shards would write one sink | Split by hand: several hooks, each with a WHERE on the key, its own queue and its own table, read together |
One writer per sink, so one topic per index. A second hook on the same index is refused (UE/SINK-SHARED) | Merge the topics upstream, or keep two indexes and read both |
The one-writer check is on the sink feature, in one registry. Two sink features naming the same table, Redis key prefix or WATERMARK_KEY are not caught. Nor are two environments whose hooks share a broker ( Consumer groups ) | Give each writer its own table, key prefix and watermark |
No RECOMPUTE and no seek. A hook's consumer resumes from its group's offsets | Drop the hook, reset its group's offsets, create it again ( Start, backfill and replay ) |
No exactly once. Writes are at least once: a retry rewrites a latest key under the fence, and an append member of the same identity with no fence | A pipeline with transactional outputs runs embedded serving and writes its own outputs |
| No rate setting. Writes go as fast as records arrive and drains run: a replay after a long stop writes at the store's full speed, the store serving reads included. A batch spans every partition a replica holds, so a stalled batch holds them all | Size the store for peak ( Prepare the stores ). A pipeline tool with backpressure calls the statement over HTTP at its own pace |
| No dead letters. A data error is counted and kept among the last writes in featuremeshd's memory, then skipped; a restart forgets the sample | A logger on the hook's statement logs failed calls with their error code, best effort. A record that makes no call is logged nowhere. A pipeline tool with dead-letter queues can call the statement over HTTP and route its failures |
| Status is per hook, not per record | A logger on the hook's statement keeps every call it delivers, best effort |
Tutorials
The serving tutorials build each pattern on a stream of payments: A rolling window , Scoring each event , Buckets per key , Long horizons , A heavy profile , A materialized view and Precompute in a Python process . Choosing a pattern says which one fits a feature.
A demo in the FeatureMesh demos repository runs one pipeline end to end on Redpanda, Redis and PostgreSQL in Docker: 100,000 payments go through an event hook writing an index and each merchant's latest payment, a minute rollup per merchant and a materialized view of every merchant's totals, then a serving call reads each merchant back and checks it against the generated data. It runs once on featuremeshd and once in a Python process ( Hooks and views in Python ).