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:

FeatureQL
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)
;
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.DOCS_SERVING_ERRORS.DECISIONCREATEDFeature created as not exists
FM.DOCS_SERVING_ERRORS.DOWNCREATEDFeature created as not exists
FM.DOCS_SERVING_ERRORS.KCREATEDFeature created as not exists
FM.DOCS_SERVING_ERRORS.OUTCOMECREATEDFeature created as not exists
FM.DOCS_SERVING_ERRORS.RTCREATEDFeature created as not exists
FM.DOCS_SERVING_ERRORS.TIERCREATEDFeature created as not exists
FM.DOCS_SERVING_ERRORS.UPCREATEDFeature 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:

CodeCause
SERVING-SOURCE-TIMEOUTThe source did not answer within its timeout
SERVING-SOURCE-UNAVAILABLEThe source could not be reached
SERVING-SOURCE-ERRORThe source rejected the read, or its value does not decode

After RECOMPILE SERVING (shown below ), a read of DOWN fails:

FeatureQL
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))
;
Result
ERROR SERVING-SOURCE-UNAVAILABLE

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:

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_statusMeaning
OKThe read returned a value
MISSThe source answered, but has no value for the key. The fields are NULL
TIMEOUTThe source did not answer in time
UNAVAILABLEThe source could not be reached
ERRORThe 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:

FeatureQL
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))
;
Result
K BIGINTTIER VARCHARREAD_STATUS VARCHAR
1goldOK
2NULLMISS

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:

FeatureQL
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))
;
Result
K BIGINTTIER VARCHARREAD_STATUS VARCHARDECISION VARCHAR
1NULLUNAVAILABLEREVIEW
2NULLUNAVAILABLEREVIEW

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_status read only in a CAPTURE does not guard a read the returned feature uses. If that read fails, the call fails. When the call succeeds, the captured status is OK or MISS.
  • A read reached only through captures or loggers is guarded without asking: logging never fails a call. A failed read there leaves NULL in 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:

FeatureQL
CREATE OR REPLACE FEATURE FM.DOCS_SERVING_ERRORS.OUTCOME_PS AS
PREPARED_STATEMENT(FM.DOCS_SERVING_ERRORS.OUTCOME USING FM.DOCS_SERVING_ERRORS.K);
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.DOCS_SERVING_ERRORS.OUTCOME_PSCREATEDFeature created as not exists

The call succeeds, and each row says why it fell back:

Prepared
FM.DOCS_SERVING_ERRORS.OUTCOME_PS
Call 1
{"input_1": [[1], [2]]}
K BIGINTOUTCOME_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:

FeatureQL
CREATE OR REPLACE FEATURE FM.DOCS_SERVING_ERRORS.TIER_PS AS
PREPARED_STATEMENT(FM.DOCS_SERVING_ERRORS.TIER USING FM.DOCS_SERVING_ERRORS.K);
Result
feature_name VARCHARstatus VARCHARmessage VARCHAR
FM.DOCS_SERVING_ERRORS.TIER_PSCREATEDFeature created as not exists

Prepared
FM.DOCS_SERVING_ERRORS.TIER_PS
Call 1
{"input_1": [[1]]}
K BIGINTTIER_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:

StatusMeaning
OKNo read failed
PARTIALOnly guarded reads failed. The call returned its rows, with the fallbacks
ERRORThe 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:

FeatureQL
RECOMPILE SERVING;
Result
FEATURE VARCHARKIND VARCHARSTATUS VARCHARMESSAGE VARCHARSINCE VARCHAR
FM.DOCS_SERVING_ERRORS.OUTCOME_PSPREPARED_STATEMENTPARTIAL

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:

SourceDefault 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:

  • TRY before the call: TRY DIVIDE(a, b), TRY LN(x). The error becomes NULL.
  • A non-zero literal divisor: a / 100.
  • NULLIF(d, 0) as the divisor: a / NULLIF(d, 0).
  • A cast that cannot fail, such as INTEGER to BIGINT.

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 / 0 is NULL (also for // and %): a NULL operand gives NULL before the zero check. A non-NULL value divided by zero still raises.
  • ARRAY_JOIN([]) is NULL, like STRING_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