Caching

Last updated 27 days ago

j17 caches aggregate state to reduce repeated work on reads. Caching is enabled by default for every aggregate type.

Read freshness

Default aggregate reads include writes committed before the read. You do not need ?synchronous=true for a read immediately after a successful write.

This freshness contract applies to aggregate reads such as GET /order/:id. Projection queries have a separate consistency contract and may take time to include a new write.

Configuration

Most applications can leave caching enabled. To exclude specific types, use modules.never_cache in the spec:

{
  "aggregate_types": {
    "user": { "events": { "...": {} } },
    "order": { "events": { "...": {} } },
    "audit_log": { "events": { "...": {} } }
  },
  "modules": {
    "never_cache": ["audit_log"]
  }
}

Types in never_cache are computed from events on every read. This changes the work required to serve a read, not its freshness guarantee.

Reading aggregates

Use the default read for normal application requests:

curl https://myapp.j17.dev/user/$USER_ID \
  -H "Authorization: Bearer $API_KEY"

For verification, request computation from event data instead of relying on cached aggregate state:

curl "https://myapp.j17.dev/user/$USER_ID?synchronous=true" \
  -H "Authorization: Bearer $API_KEY"

Both forms provide current aggregate state. Synchronous reads are useful when checking a derived result, and can be more expensive for long histories.

Replay limits

A request that requires replaying more than 32 MB of event history is refused with HTTP 422. This can affect synchronous reads, uncached types, and default reads when a usable cached state is unavailable. A default read is not an unlimited-replay alternative.

Concurrency control

The default aggregate response includes metadata.length. Use it as metadata.previous_length in a later write when that write depends on the state you read. If another write changes the stream first, the guarded write returns 409.

Synchronous reads do not make a read followed by a write atomic. The write precondition protects that interval with either read form.

Optimistic UI updates

After confirming a successful event write, your application can update its local view immediately. If it needs the resulting aggregate, a default read is sufficient.

Handle rejected writes before confirming the UI change. A transport error can leave the write outcome uncertain; retry with the same operation-specific idempotency key and appropriate concurrency precondition.

Spec changes and application caches

Aggregate reads use the deployed spec. Cached aggregate state does not require manual invalidation after a spec update.

Any response caching in your own application has its own freshness and access rules; it is separate from j17's aggregate-read contract.

See also

Can't find what you need? support@j17.app