Lesson 4 of 9 · 27m
Data Models and API Contracts Drive the Architecture
Model entities, ownership, access patterns, invariants, and API semantics before debating databases or service boundaries.
Most architecture diagrams are database diagrams wearing a costume. The hard questions are not "SQL or NoSQL?" They are: what is the entity, who owns it, which fields are authoritative, what invariants must survive concurrency, and which access patterns dominate? Choose the wrong shape and every downstream component becomes compensation.
Start with entities and ownership
A compact entity model beats a giant diagram:
Workspace id, owner_id, plan, created_at, deleted_at
Member workspace_id, user_id, role, invited_at, accepted_at
Document id, workspace_id, title, current_version, created_by
Edit document_id, version, actor_id, operation, created_atOwnership means both business ownership and write authority. Who may mutate role? Which service owns current_version? Can two writers update the same row? If two services can write the same field, you have not finished the model.
Access patterns come before storage engines
List the queries the product actually performs:
find membership for user+workspace very frequent
list documents in workspace, ordered frequent
load document plus recent edits frequent
append edit at next version frequent
find workspace billing owner occasional
scan deleted workspaces past retention backgroundNow storage choices become concrete. A relational database handles membership and document metadata well. Append-only edit history may fit the same database initially, then move when write volume or retention forces it. Search is a derived index, not the source of truth.
Preserve invariants explicitly
An invariant is a rule that must survive concurrency and partial failure:
a user cannot accept an invitation after revocation
document version numbers never regress
only one owner can be primary for a workspace
a deleted workspace cannot gain new membersWrite down where each invariant is enforced: database constraint, transaction, single-writer queue, or application convention. "The service checks it" is not enough when retries and concurrent requests exist.
APIs are behavioral contracts
Endpoint shapes influence coupling and failure modes:
POST /v1/workspaces
POST /v1/workspaces/{id}/invitations
POST /v1/invitations/{token}/accept
GET /v1/documents/{id}?since_version=812Prefer idempotency keys for client-visible create operations:
POST /v1/documents
Idempotency-Key: 01J9M2KQZK2M5QW4Y0XV4V9D5MRetries are guaranteed in distributed systems. If retrying creates duplicates, the client, server, network, and queue all become part of your correctness bug.
Derived data must be labeled as derived
Search indexes, denormalized counts, analytics tables, cache entries, and read models are not authoritative. Record how they are rebuilt and how stale they may become. The dangerous design is one where nobody knows which copy is truth after an incident.
Field notes
- Composite keys and foreign keys are not old-fashioned; they are how invariants survive tired engineers.
- Do not model every relationship as a graph because one query is graph-shaped.
- Soft deletion changes every query. If deleted rows remain visible to joins or unique constraints, state the rule.
- Public APIs freeze mistakes. Internal APIs can evolve; external contracts need versioning and compatibility discipline.
Was this lesson helpful?