Read Models
Read models are the query-side of Causet’s CQRS split. They live in a separate PostgreSQL database (projections), are maintained by the projection worker, and are queried by the query service. You never write to them directly — they are derived entirely from the event ledger.
The CQRS split
WRITE SIDE READ SIDE
────────────────────────────── ─────────────────────────────────
causet-runtime-service causet-projection-worker
intents → rules → events Kafka consumer → UPSERTs
causet (Postgres) projections (Postgres)
ledger_events per-tenant schema tables
entity_snapshots {platformId}_{appId}_{forkId}
intent_status
│ ▲
└── Kafka ────────────────────►┘The write database (causet) and the read database (projections) are separate PostgreSQL instances (or at minimum, separate databases). They are never joined by the application. The only data flow between them is through Kafka.
This separation means:
- Read queries do not contend with write transactions.
- The read schema can be tuned for query patterns independently of the write schema.
- The read side can be scaled independently.
Tenant isolation
Each fork gets its own PostgreSQL schema in the projections database:
{platformId}_{applicationId}_{forkId}For example, a platform acme, application concert-app, fork main gets schema:
acme_concert_app_mainAll projection tables for that fork live in this schema:
acme_concert_app_main.user_following
acme_concert_app_main.artist_show_directory
acme_concert_app_main.show_attendanceThis means tenants (forks) are isolated at the PostgreSQL schema level. A query against one fork cannot accidentally read another fork’s data due to schema separation.
IR-driven DDL
Projection table schemas are not managed by migration files. You do not write Flyway migrations or Liquibase changesets. The table schema is derived from the IR (causet.projections.json) and applied by the projection worker at deploy time.
When you deploy a new release:
- The new
causet.projections.jsonis loaded by the projection worker. - The worker compares the declared projection schema against the actual database schema.
- Missing tables are created. Missing columns are added.
- The worker does not drop columns or tables — schema changes are additive in the forward direction.
Note: If you change a column’s SQL type (e.g. from
TEXTtoBIGINT), the worker cannot apply this automatically — it requires a manual DDL step followed by a projection rebuild. See Schema Versioning.
Active IR version per fork
The query service does not use a static compiled artifact. At runtime, it resolves the active IR version for a given fork from causet-saas-cloud and loads the corresponding compiled SQL templates from S3.
This means:
- Different forks can be on different IR versions simultaneously.
- Rolling out a new release to a fork updates the IR version it uses.
- Rolling back a release reverts the IR version — the query service immediately uses the previous compiled queries.
Redis result cache
The query service supports a Redis result cache with TTL, declared per-query in the DSL:
queries:
artist_leaderboard:
from: artist_leaderboard
order_by:
attend_count: desc
limit: 20
cache_ttl_seconds: 30Cache keys are composed of the query name and the serialized input parameters. Results are stored as JSON in Redis.
The effective staleness of a cached result is cache_ttl_seconds + projection lag. For counters and leaderboards where slight staleness is acceptable, this trade-off is usually worth it.
Stale reads
Read models are eventually consistent with the event ledger. Two sources of staleness:
-
Projection lag. The Kafka consumer group for the projection worker processes events asynchronously. Under normal load this is milliseconds. After a restart, rebuild, or high backlog, it may be longer.
-
Redis TTL. If a query uses a Redis cache, results are stale by up to the TTL duration.
Design your application to tolerate this. Common patterns:
- Show the user the intent result optimistically in the UI while the projection catches up.
- Include
event.ts-derived timestamps in projections and filter on the client if staleness is critical. - For financial or inventory-critical reads, disable caching and accept the projection lag only.
Rebuilding read models
To rebuild a projection table from the beginning of history:
- Reset the Kafka consumer group offset for the projection worker to the earliest available offset on
causet.projection-events.v1. - The worker re-processes all events and applies the projection handler.
- Existing rows are overwritten via UPSERT semantics — rebuilds are idempotent.
You can also rebuild from a specific checkpoint by setting the offset to a known event sequence number.
Reasons you might need to rebuild:
- You added a new projection against historical events.
- You fixed a bug in a projection handler.
- A column type changed and required a DDL migration.
- The projection table was dropped or corrupted.
See Replay for the full rebuild workflow.
No direct writes
Projection tables are owned by the projection worker. Application code must never write to them directly. If you need to correct projection data, the correct approach is:
- Fix the projection handler in the DSL.
- Recompile and deploy the new IR.
- Rebuild the projection from the event ledger.
Writing to projection tables directly bypasses the event history and creates a permanent inconsistency between the ledger and the read model.
Production notes
Schema inspection. Connect to the projections database and inspect the tenant schema to verify DDL was applied correctly after a deploy:
SELECT table_name, column_name, data_type
FROM information_schema.columns
WHERE table_schema = 'acme_concert_app_main'
ORDER BY table_name, ordinal_position;Consumer group lag. Monitor the Kafka consumer group lag for the projection worker. High sustained lag means the read side is falling behind the write side. Scale the projection worker or investigate slow handlers.
PostgreSQL connection pooling. The projection worker uses a connection pool for batch UPSERTs. Under high event volume, tune pool size and batch size together for throughput. Projections that fan out to many small UPSERTs are less efficient than projections that batch naturally by primary key.