GuidesQueriesRead consistency

Read consistency

The entity snapshot and a named query are different reads.

The snapshot is the state committed with the intent. A named query reads a projection table. The projection worker updates that table after the commit.

A consistency token asks one follow-up query for that entity to include the cursor from the commit. The behavior below is what the query service implements. Current support is listed in What runs today?.


Two reads

ReadWhat it returnsWhen it moves
Entity snapshotState committed with the intentIn the runtime commit
Named query, no tokenProjection rows, possibly from RedisAfter the worker applies the event, and after any cache TTL
Named query, token, flag onThat entity’s projection row once its watermark has reached the token cursorSame worker apply. A miss is 409 and is not cached
Named query, token, flag offThe ordinary query, including cacheThe header does not change the read

consistency: read_your_writes on the query only matters when the flag is on. Without it, a token on a list or a join is rejected.


Token from a sync write

POST /v1/platforms/{platform}/applications/{application}/intents/execute with "mode": "sync".

When the intent is COMMITTED and the cursor is greater than 0, the SaaS response includes cursor and consistencyToken. An async accept has no cursor yet, so it has no token. A rejected intent has no token. Side-effect, fraud, and moderation intents are separate cursors and are not inside this token.

Send the token as X-Causet-Consistency on:

  • GET /v1/platforms/{platform}/applications/{application}/forks/{fork}/q/{queryName}
  • POST .../forks/{fork}/reference/queries/{queryName}/run

A bare X-Causet-Min-Cursor header is 400. The cursor has to come from the signed token.


The query

queries:
  get_counter:
    from: counter_view
    consistency: read_your_writes
    input:
      counter_id: { type: string, required: true }
      stream_id: { type: string, required: true }
    where:
      counter_id: { eq: input.counter_id }
      stream_id: { eq: input.stream_id }
    limit: 1

read_your_writes and entity_local both mean one entity and no join. The inputs must contain the token’s stream id and entity id as values. The stream id on the token is the bare state name (counter), not counter_stream.


When the flag is on

The worker writes projection_entity_watermarks in the same database transaction as the projection change. The stored cursor is the greatest cursor applied for that stream, entity, projection, and version. An older apply cannot move it backward. The gate reads that row. It does not wait inside the query. A miss returns immediately.

StatuserrorWhen
200Watermark is READY, the projection version matches, and ledger_cursor is at least the token cursor
400VALIDATION_ERRORBare min cursor, malformed token, join or non-entity-local query, or inputs that are not the token’s stream and entity
401UNAUTHORIZEDSignature failed, or the token is for another platform, application, or fork
409PROJECTION_NOT_VISIBLENo ready watermark, version mismatch, or the watermark cursor is still behind

The 409 body includes cursor and Projection is not visible through cursor N. Retry the same request. When you stop retrying, show that error. Do not substitute the untokened query. That call can still be cached.

There is no server timeout that turns a miss into 200. A statement timeout elsewhere is still a 500, and that is a different failure.


Scope

  • The token applies to one entity query. A list, a join, or a different entity returns 400.
  • A 200 means the watermark cursor is at least the token cursor.
  • Saga prepare, commit apply, genesis import, fork clone, and surgical replay advance cursors outside the allocation row the recovery scan uses.
  • If the projection event for an allocated cursor never arrives, the query stays at 409.
  • With min-cursor reads disabled, the header is ignored and the ordinary query path runs, including cache.

What the counter example showed

examples/read-consistency in the Causet repo was run against a dev stack where the min-cursor flag was on.

CheckWhat happened
Multi-entityThe token’s counter returned 200. The same token on another id returned 400. list_counters returned 400.
One-shot readThe row was already visible and matched the commit. That run did not observe a 409.
Later cursorCursor 2 became visible at value 1, then cursor 3 at value 2.
Newer tokenAfter two adds, the later token returned the later value.

Those checks did not crash the worker, and they did not exercise the recovery scan. A cursor that never gets applied stays 409.


Next steps