liveBook · Manning

Referential integrity

As explained in API Design Patterns

Referential integrity is the property that references remain valid and continue to identify the intended existing resources or data.

Referential integrity keeps references valid and connected to the intended resources. APIs must define what happens when a referenced resource is deleted, moved, copied, or soft-deleted; otherwise clients face inconsistent behavior and dangling pointers. section 13.0 section 25.3.6

Choose an explicit deletion policy for resources that other resources reference:

  • Cascade deletes every resource pointing to the deleted resource, but can trigger an avalanche of deletions. section 14.3.6
  • Restrict prevents deletion while references exist; an API can return HTTP 412 Precondition Failed. section 14.3.6
  • Set null clears all pointers, but can create a stampede of writes across large numbers of resources. section 14.3.6
  • Do nothing permits deletion and leaves dangling references for consumers to handle. section 14.3.6

Association resource deletion

Suppose MessageReviewReport contains messageId pointing to Message. Deleting that Message should generally either be rejected while the report exists or proceed while leaving the report's pointer invalid. Cascading or nulling references may produce unbounded deletion or update workloads. section 14.3.6

Moving resources is harder than deleting them: every resource referring to the moved target, including descendants moved indirectly with a parent, may require updates. This complexity makes renaming or relocating resources undesirable. Soft deletion should preserve the existing reference policy rather than silently changing it. section 17.3.3 section 25.3.6

Table 14.3 Summary of referential integrity behavior (view table figure)

Option

Behavior

Cascade

Delete all other resources that point to the deleted resource.

Restrict

Prevent deleting any resources that are still pointed to by others.

Set null

Any pointers to the resource being deleted are set to null.

Do nothing

Deleting resources does nothing to the pointers and leaves them as invalid.

Compare cascade, restrict, set-null, and do-nothing policies before choosing deletion behavior. section 14.3.6

How API Design Patterns covers it

How deep it goes
Compares four deletion policies and explains write avalanches, deletion cascades, dangling pointers, moves, and soft-deletion consistency.
Examples
Uses MessageReviewReport.messageId, moved Message and ChatRoom resources, and four standardized deletion behaviors.
What it assumes
Assumes resources can reference one another and introduces deletion consequences through association-resource examples.
What you can do after
Evaluate deletion and movement policies, then configure API behavior that either restricts references or deliberately leaves them for consumers.
How it connects
Connects association resources, soft deletion, resource moves, relational databases, and HTTP precondition failures.

Also explained in

Compare how 4 books explain referential integrity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of API Design Patterns. The section links point to the parts of the book it draws on.