liveBook · Manning

Self-reference relationship

As explained in API Design Patterns

A self-reference relationship is a relationship in which entities of one resource type reference other entities of the same type.

A self-reference relationship occurs when a resource points to another resource of the same type. The reference targets the resource type, not the resource itself. This pattern commonly represents trees, hierarchies, and directed graphs. section 4.1.1

Company directory

An Employee resource can reference other Employee resources as its manager or assistant. For example, one employee may report to another, while also identifying that person as an assistant or another special relationship. section 4.1.1

Following users

A User resource can reference many other User resources that it follows, while each user can have many followers. This many-to-many self-reference models a social network but can become expensive at large scale. section 4.2.1

Design self-reference relationships deliberately:

  • Use them when the relationship is purposeful and fundamental to the API. section 4.2.1
  • Estimate long-term operational cost before adding dense webs of references. section 4.2.1
  • Account for cascading effects: deleting a highly connected user may require updating or removing millions of related records. section 4.2.1
Figure 4.4 An employee resource points at other employee resources as managers and assistants.

Visualizes an Employee resource pointing to other Employee resources as manager and assistant. section 4.1.1

How API Design Patterns covers it

How deep it goes
Defines the pattern and illustrates uses, then stops short of explaining relationship mechanics or mitigation techniques.
Examples
Uses employee managers and assistants, plus users following users, to demonstrate hierarchical and many-to-many self-references.
What you can do after
Evaluate API relationships by identifying same-type links and weighing their long-term performance and maintenance costs.
How it connects
Connects resource references with hierarchical relationships, directed graphs, many-to-many relationships, and social networks.

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.