Serving errors and fallbacks
A serving call reads live sources: Redis, a database, an HTTP service. Any of them can be slow or down. This page shows what a call does when a read fails, how a feature writes its own fallback, and what the caller and the logs are told.
The examples use two Redis sources: UP answers, DOWN points at a closed port. Key 1 holds a tier, key 2 holds nothing. RT reads the tier from DOWN; DECISION and OUTCOME are explained below:
CREATE OR REPLACE FEATURES IN FM.DOCS_SERVING_ERRORS AS
PERSIST
UP := SOURCE_KV('redis://host.docker.internal:26379' WITH (timeout='500ms')),
DOWN := SOURCE_KV('redis://host.docker.internal:1' WITH (timeout='300ms')),
K := INPUT(BIGINT),
RT := EXTERNAL_KV(KEY 'serving_slt:docs_errors:tier:' || K::VARCHAR FROM DOWN),
TIER := COALESCE(RT[value], 'none'),
DECISION := CASE WHEN RT[source_status] IN ('OK', 'MISS') THEN TIER ELSE 'REVIEW' END,
OUTCOME := ROW(DECISION AS decision, RT[source_status] AS read_status)
;| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.DOCS_SERVING_ERRORS.DECISION | CREATED | Feature created as not exists |
| FM.DOCS_SERVING_ERRORS.DOWN | CREATED | Feature created as not exists |
| FM.DOCS_SERVING_ERRORS.K | CREATED | Feature created as not exists |
| FM.DOCS_SERVING_ERRORS.OUTCOME | CREATED | Feature created as not exists |
| FM.DOCS_SERVING_ERRORS.RT | CREATED | Feature created as not exists |
| FM.DOCS_SERVING_ERRORS.TIER | CREATED | Feature created as not exists |
| FM.DOCS_SERVING_ERRORS.UP | CREATED | Feature created as not exists |
A failed read fails the call
By default, a failed read fails the call: the source was unreachable, timed out, rejected the request, or sent a value that does not decode. The call returns no rows, and the error names the source and what went wrong:
| Code | Cause |
|---|---|
SERVING-SOURCE-TIMEOUT | The source did not answer within its timeout |
SERVING-SOURCE-UNAVAILABLE | The source could not be reached |
SERVING-SOURCE-ERROR | The source rejected the read, or its value does not decode |
After RECOMPILE SERVING (shown below ), a read of DOWN fails:
DEFINE
k := INPUT(BIGINT),
rt := EXTERNAL_KV(
KEY 'serving_slt:docs_errors:tier:' || k::VARCHAR
FROM fm.docs_serving_errors.down
)
RETURN
k,
tier := rt[value]
FOR
k := BIND_VALUES(ARRAY(1))
;A missing answer is never turned into NULL silently: a NULL from a failed read would look like a key without a value, and the call would compute a wrong answer with no sign of it.
Read status: source_status
Every source read row has a reserved field, source_status. It is never declared. It exists on every read:
- a table read:
EXTERNAL_COLUMNS(... FROM TABLE(src[table]))of aSOURCE_DB(); - a key-value read:
EXTERNAL_KV(); - an HTTP read:
EXTERNAL_HTTP().
EXTERNAL_KV() and EXTERNAL_HTTP() reads are rows too. A value that is not a ROW comes as ROW(value T): read it with KV[value], and its status with KV[source_status].
source_status | Meaning |
|---|---|
OK | The read returned a value |
MISS | The source answered, but has no value for the key. The fields are NULL |
TIMEOUT | The source did not answer in time |
UNAVAILABLE | The source could not be reached |
ERROR | The source rejected the read, or the value does not decode |
A history (a Redis sorted set) is never MISS: a key with no member is an empty history, OK with [].
With a source that answers, key 1 is OK and key 2 is MISS:
DEFINE
k := INPUT(BIGINT),
rt := EXTERNAL_KV(
KEY 'serving_slt:docs_errors:tier:' || k::VARCHAR
FROM fm.docs_serving_errors.up
)
RETURN
k,
tier := rt[value],
read_status := rt[source_status]
FOR
k := BIND_VALUES(ARRAY(1, 2))
;| K BIGINT | TIER VARCHAR | READ_STATUS VARCHAR |
|---|---|---|
| 1 | gold | OK |
| 2 | NULL | MISS |
Reading the status guards the read
Reading source_status changes what a failure does. The read is guarded: on a failed key, the fields are NULL, the status says why, and the call succeeds. The same read of DOWN as above, now with its status:
DEFINE
k := INPUT(BIGINT),
rt := EXTERNAL_KV(
KEY 'serving_slt:docs_errors:tier:' || k::VARCHAR
FROM fm.docs_serving_errors.down
)
RETURN
k,
tier := rt[value],
read_status := rt[source_status],
decision := CASE
WHEN rt[source_status] IN ('OK', 'MISS') THEN COALESCE(
rt[value],
'none'
)
ELSE 'REVIEW'
END
FOR
k := BIND_VALUES(ARRAY(1, 2))
;| K BIGINT | TIER VARCHAR | READ_STATUS VARCHAR | DECISION VARCHAR |
|---|---|---|---|
| 1 | NULL | UNAVAILABLE | REVIEW |
| 2 | NULL | UNAVAILABLE | REVIEW |
The fallback is written in the feature. A common form keeps the normal answer when the source answered, with or without a value, and returns a safe default otherwise:
DECISION := CASE WHEN RT[source_status] IN ('OK', 'MISS') THEN TIER ELSE 'REVIEW' END
A table read works the same way:
ORD := EXTERNAL_COLUMNS(order_id BIGINT BIND TO ORDER_ID, amount DOUBLE FROM TABLE(PG[orders])),
AMOUNT_OR_FALLBACK := CASE WHEN ORD[source_status] IN ('OK', 'MISS') THEN ORD[amount] ELSE -1.0::DOUBLE END
Which reference guards a read
In a free-form query, any reference to a read's source_status guards that read.
In a prepared statement, only the returned feature counts: a read is guarded when the returned feature, directly or through the features it depends on, reads that source's source_status. Each source read is guarded or not on its own.
- A
source_statusread only in aCAPTUREdoes not guard a read the returned feature uses. If that read fails, the call fails. When the call succeeds, the captured status isOKorMISS. - A read reached only through captures or loggers is guarded without asking: logging never fails a call. A failed read there leaves
NULLin the logged row.
Passing the outcome to the caller
A caller that reads only values does not see source_status. To tell it that the answer is a fallback, return a ROW that carries the status with the decision. OUTCOME does that, and its statement reads the status, so its read of DOWN is guarded:
CREATE OR REPLACE FEATURE FM.DOCS_SERVING_ERRORS.OUTCOME_PS AS
PREPARED_STATEMENT(FM.DOCS_SERVING_ERRORS.OUTCOME USING FM.DOCS_SERVING_ERRORS.K);| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.DOCS_SERVING_ERRORS.OUTCOME_PS | CREATED | Feature created as not exists |
The call succeeds, and each row says why it fell back:
FM.DOCS_SERVING_ERRORS.OUTCOME_PS{"input_1": [[1], [2]]} | K BIGINT | OUTCOME_PS ROW |
|---|---|
| 1 | {decision: REVIEW, read_status: UNAVAILABLE} |
| 2 | {decision: REVIEW, read_status: UNAVAILABLE} |
A statement that returns the tier alone does not read the status. Its read is not guarded, so the same outage fails the call:
CREATE OR REPLACE FEATURE FM.DOCS_SERVING_ERRORS.TIER_PS AS
PREPARED_STATEMENT(FM.DOCS_SERVING_ERRORS.TIER USING FM.DOCS_SERVING_ERRORS.K);| feature_name VARCHAR | status VARCHAR | message VARCHAR |
|---|---|---|
| FM.DOCS_SERVING_ERRORS.TIER_PS | CREATED | Feature created as not exists |
FM.DOCS_SERVING_ERRORS.TIER_PS{"input_1": [[1]]} | K BIGINT | TIER_PS VARCHAR |
|---|---|
| ERROR SERVING-SOURCE-UNAVAILABLE |
Batch has no read status: a batch query reads its warehouse, and fails when the warehouse fails. Reading source_status in batch is refused with UE/SOURCE-STATUS-SERVING-ONLY.
Call status
Every call has a status:
| Status | Meaning |
|---|---|
OK | No read failed |
PARTIAL | Only guarded reads failed. The call returned its rows, with the fallbacks |
ERROR | The call failed |
The response carries the status and one entry per failed source and kind: the source, the kind (TIMEOUT, UNAVAILABLE, ERROR), the first message, the failed and requested key counts, and the CALL_INDEX of the failed rows. Over HTTP the body is {data, status, failures, errors}; over gRPC the same fields are in QueryMetadata. The engine's message is never a value of a feature.
Serving logs record the same in CALL_DETAILS : status, and sources, one entry per failed source and kind with source, kind, message, features (the features that read it), failed_keys, requested_keys, call_indexes and guarded.
A source can also fail when the engine loads it. RECOMPILE SERVING and SHOW SERVING STATUS then report each prepared statement that reads it as PARTIAL, naming the source. The statement is still served: its calls follow the rules above. Here DOWN cannot be reached, so OUTCOME_PS is PARTIAL:
RECOMPILE SERVING;| FEATURE VARCHAR | KIND VARCHAR | STATUS VARCHAR | MESSAGE VARCHAR | SINCE VARCHAR |
|---|---|---|---|---|
| FM.DOCS_SERVING_ERRORS.OUTCOME_PS | PREPARED_STATEMENT | PARTIAL |
Timeouts
A source's timeout is the deadline for its whole read in a call, connection included. A read that misses it is not retried. A source declared without timeout gets a short default for its kind:
| Source | Default timeout |
|---|---|
SOURCE_KV() | 250 ms |
SOURCE_DB() | 1 s |
SOURCE_HTTP() | 2 s |
Sources are read in parallel, and a failed read does not stop the others. A source read in several parts (one request per batch of keys, for example) marks only the keys of the part that failed.
Runtime errors in a prepared statement
A read failure is about the source. A runtime error is about the formula: a division by zero, a non-finite result, a cast that fails, a named runtime error such as an invalid number for FROM_BASE(). In a call, it works as in batch: the call is all or nothing, so a row that raises fails the whole call. There is no per-row error, so serving and batch return the same values and fail on the same inputs.
A prepared statement finds these errors when it is created, before a call meets them. Every operation in the statement's graph that can raise (in the returned feature, its captures, its loggers, persisted features included) gives the warning PREPARED-STATEMENT-UNHANDLED-RUNTIME-ERROR, naming the feature and the operation. The warning fails the creation, unless the operation is handled or acknowledged.
Handled means the formula shows that the error cannot happen:
TRYbefore the call:TRY DIVIDE(a, b),TRY LN(x). The error becomesNULL.- A non-zero literal divisor:
a / 100. NULLIF(d, 0)as the divisor:a / NULLIF(d, 0).- A cast that cannot fail, such as
INTEGERtoBIGINT.
A CASE guard does not count: CASE WHEN d <> 0 THEN a / d END is still reported, because an engine may evaluate the branch it does not take. Write a / NULLIF(d, 0) instead. See Absence and errors for TRY.
Acknowledged means you accept that the error fails the call. Put the warning's token in a comment of the CREATE statement, as for any warning (see Tools in the query ). The acknowledgment is stored with the statement. This is how a feature written for batch is served unchanged; the other way is a VARIANT() that replaces the operation with its TRY form.
For example, with AVG_BASKET := TOTAL / N_ORDERS where N_ORDERS comes from a source, the statement below is refused with the warning, and passes once the token it prints is in a comment:
-- ACK-XXXX
CREATE OR REPLACE FEATURE FM.SHOP.AVG_BASKET_PS AS
PREPARED_STATEMENT(FM.SHOP.AVG_BASKET USING FM.SHOP.CUSTOMER_ID);
Writing AVG_BASKET := TOTAL / NULLIF(N_ORDERS, 0) handles it instead: a customer with no order gets NULL.
Two cases return NULL without an error, in serving and in batch:
NULL / 0isNULL(also for//and%): aNULLoperand givesNULLbefore the zero check. A non-NULLvalue divided by zero still raises.ARRAY_JOIN([])isNULL, likeSTRING_AGG()over no rows.
Integer overflow is not part of the check: a bare integer +, - or * that leaves the BIGINT range is undefined, as in batch.
Related pages
- Prepared statements : the response of a call.
- Feature logging :
CALL_DETAILSand captures. - Key-value sources , Database sources , HTTP data sources : declaring
timeout.