LESSON 5

Database and Data Architecture

Design the data model, choose storage, decide on tenancy, indexing, consistency, retention, and deletion.

Introduction

Code can be rewritten; data is migrated, retained, audited, and sometimes legally required to be deleted. Data decisions therefore tend to outlive and constrain almost everything else. A weak data design causes slow queries, awkward features, privacy failures, and painful migrations.

This lesson covers choosing stores, modeling data, isolating tenants, handling consistency, and meeting retention and deletion requirements.

Learning Objectives

  • Choose data stores based on access patterns and requirements.
  • Create a logical data model with ownership and relationships.
  • Compare tenancy models and their isolation trade-offs.
  • Design indexing, consistency, and migration strategies.
  • Address privacy, retention, backup, and deletion in the design.
  • Use AI to review a data model without trusting it blindly.

1. Start With Access Patterns, Not Tables

Ask first: What does the system read and write, how often, and how fast?

Access pattern (SignalDesk) Characteristics
Insert feedback items in batches during import Bursty writes, up to thousands per job
List items of a theme with evidence Frequent reads, relational joins
Full-text search across a workspace Text relevance, filtering, 1 s target
Generate a summary from a theme Moderately heavy read across several entities
Delete all data of a workspace Rare, but strict deadline, must be complete

These patterns decide the store. Most business applications fit a relational database well, because they need transactions, relationships, and flexible queries.

2. Store Choices

Store type Strength Typical use
Relational Transactions, constraints, joins, mature tooling System of record for core entities
Document Flexible schema, aggregate-oriented Content with variable structure
Key-value / cache Very fast lookups Sessions, caches, rate limits
Search engine Text relevance, faceting Search over text; derived from primary data
Object storage Cheap storage of large files Uploaded CSVs, exports
Message queue Decoupling, buffering Jobs and events
Analytical / columnar Large-scale aggregations Reporting at scale

A sound default is one relational database as the system of record, plus specialized stores only where a driver demands it. Every additional store adds operations, failure modes, and consistency questions. Justify each one.

For SignalDesk: relational database for core data; object storage for uploaded files; a search index as derived data; a queue for jobs. Check whether the relational database’s built-in full-text search meets the search target before introducing a separate search engine, since it removes a whole system to operate. This is a hypothesis to test with a measured prototype, not a conclusion.

3. The Logical Data Model

Identify entities, attributes, relationships, and ownership. A simplified SignalDesk model:

1
2
3
4
5
6
7
8
Workspace 1---* Membership *---1 User
Workspace 1---* FeedbackItem
Workspace 1---* Theme
Theme *---* FeedbackItem          (via ThemeAssignment)
Theme 1---* Summary
Workspace 1---* ImportJob 1---* ImportRowError
FeedbackItem *---1 Source
Workspace 1---* AuditEntry

For each entity note: owner module, key fields, required constraints, and sensitivity.

Principles:

  • Every table that holds tenant data carries a workspace identifier.
  • Use stable, non-guessable identifiers (such as UUIDs) for public references.
  • Model what the business needs to distinguish, such as the difference between an item’s original text and its redacted text.
  • Prefer constraints in the database (unique, foreign key, not null) over validation only in code.
  • Normalize first; denormalize deliberately, with measurements, when a read pattern requires it.

4. Tenancy Models

How tenants’ data is separated is among the most consequential decisions.

Model Description Isolation Cost / complexity
Shared tables, tenant column All tenants in the same tables, with a workspace_id on each row Logical, enforced in code and database policies Lowest cost; easiest operations; isolation relies on discipline
Schema per tenant Separate schema for each tenant in one database Stronger More migrations to run, scaling limits at many tenants
Database per tenant Separate database for each tenant Strongest High operational cost; suits few, large or regulated tenants

For small-business SaaS with many small tenants, shared tables with a tenant column are common. To make isolation reliable, enforce it centrally: for example database row-level security or a mandatory tenant-scoped data access layer, plus automated tests that attempt cross-tenant access. Match the choice to the SRS isolation requirement and to a tenant’s contractual expectations.

5. Indexing and Performance

Indexes speed reads and slow writes. Design them from the access patterns:

  • Index foreign keys and the columns used in frequent filters, usually starting with the workspace identifier.
  • Use composite indexes in the order of the filter and sort.
  • Avoid indexing everything; measure with realistic data volumes.
  • Test with the SRS scale: for example 50,000 items per workspace and a total of several million rows.

Query performance claims must come from measurement using representative data, not from intuition or generated advice.

6. Consistency and Transactions

  • Use database transactions for changes that must succeed or fail together, such as assigning an item to a theme and recording the audit entry.
  • Accept eventual consistency only where it is harmless and declare it: for instance, the search index may lag the database by a few seconds, and the interface should not promise instant search results after an import.
  • Make background operations idempotent: running an import step twice must not create duplicates. Unique constraints and idempotency keys help.
  • Use the outbox pattern if a change must reliably trigger an event or index update.

7. Migrations and Evolution

Schemas change. Plan for it:

  • Version-control all schema changes as migrations.
  • Prefer backward-compatible changes (add a column before using it; remove it later).
  • Test migrations on production-like data volumes.
  • Keep rollback or forward-fix procedures documented.

8. Privacy, Retention, Backup, and Deletion

These are architectural, not afterthoughts:

Requirement Design implication
Data stays in the EU Choose hosting regions for database, object storage, backups, and any third-party processor
Deletion within 30 days Know every place data lives: tables, search index, object storage, caches, logs, backups; design a deletion job that covers them and a purge schedule for backups
Redaction of personal data Store original and redacted text appropriately; ensure derived data (search index, summaries) is updated
Audit Append-only audit trail with restricted access
Backup and restore Defined recovery point and time objectives, with restore tested
Encryption In transit and at rest; manage keys carefully

Create a data inventory: each data type, where it is stored, sensitivity, retention, and how it is deleted. Do not rely on a statement from AI about a legal obligation; confirm with qualified advisers.

9. Use AI to Review the Data Design

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Act as a database architect.

Context: access patterns, scale assumptions, tenancy requirement,
retention and deletion requirements, and the proposed logical model.
[Paste.]

Tasks:
1. Check the model against each access pattern and point out queries
   that will be expensive or awkward.
2. Identify missing constraints and indexes, and explain why each
   is needed.
3. Identify places where tenant isolation could be bypassed.
4. List every location where personal data may remain after a
   deletion request.
5. Identify risks in migrations as the schema evolves.

Cite entities and queries. Do not claim performance numbers; state
what should be measured instead.

Confirm each point with a prototype or a query plan. AI-generated SQL and schemas often look right but miss constraints, tenant filters, or index order.

10. Practical Exercise

  1. Write an access-pattern table with frequency and performance needs.
  2. Produce a logical data model with entities, relationships, key attributes, and owner module.
  3. Choose and justify the stores and the tenancy model. Record alternatives considered.
  4. Define indexes for the top five queries, and the test data volume you will use to check them.
  5. Create the data inventory with retention and deletion method for each item.
  6. Run the AI review and verify each finding.

Verification

You are ready to continue when:

  • Every store has a justification tied to an access pattern or requirement.
  • Tenant isolation is enforced by a central mechanism, with a test plan.
  • Deletion and residency requirements are addressed in the data inventory.
  • Eventual consistency is declared wherever it exists.
  • Performance claims are marked as hypotheses until measured.

Key Takeaways

  • Start from access patterns; use specialized stores only when required.
  • Tenancy and deletion are architectural decisions, not features to add later.
  • Declare consistency expectations and make background work idempotent.
  • AI can critique a data model, but only measurement confirms performance.

What’s Next?

Data and components need a way to communicate with clients and other systems. Next, we design APIs: styles, resource modeling, errors, versioning, and security.