KatInterview

Multi-Tenant Labeling Service: API Contract and Storage Design

System DesignModel answer
MediumModel answerSystem Designrest-apidata-modelingidempotency

Last updated

Design a workspace-scoped labeling service with REST endpoints, a schema, a search strategy for AND/OR filtering, and handling of concurrency, idempotency and pagination.

You are building a labeling service for a multi-tenant collaboration product. Users create labels (tags) in a workspace and apply them to items such as pages or issues. Those items are owned by other services, so your service only stores references to them.

Operations to support

  • Create a label.
  • Rename a label.
  • Delete a label.
  • Apply or remove one or more labels on an item.
  • List the labels on a given item.
  • Find items by one or more labels, with both "has all of these" (AND) and "has any of these" (OR) semantics.

Rules and quality requirements

  • Label names are unique within a workspace.
  • Applying and removing labels must behave sensibly under concurrent requests and client retries (idempotency).
  • Every list and search endpoint must be paginated.

What to deliver

  1. A set of REST endpoints with example requests and responses.
  2. A data model or database schema.
  3. An indexing and query strategy for label-based search.
  4. How you handle edge cases, at minimum: deleting a label that is still applied to items, renaming a label to a name that already exists, and bulk operations.

State your scale and consistency assumptions explicitly, and explain the trade-offs behind your choices.

Hints

Hint 1

Give labels a stable surrogate ID and use that ID in attachment URLs, so renames never touch the attachment data. Decide how to normalize names (case, whitespace, Unicode) for the uniqueness check.

Hint 2

Model applying a label as a sub-resource of the item and use PUT/DELETE so they are naturally idempotent. Reserve idempotency keys for POST-style calls such as batch changes. Use a version or ETag for concurrent renames.

Hint 3

A composite primary key on (workspace, item type, item id, label id) answers 'labels of an item'. A second index led by (workspace, label id) answers 'items with a label'. AND search is a count-and-compare over the requested labels, with keyset pagination.

Hint 4

Think about when relational queries stop being enough (huge labels, combined text or status filters) and what a search index plus change-data-capture would cost in staleness and permissions.