KatInterview

Build a Multi-Tenant Tag Index That Scales to Billions

System DesignModel answer
HardModel answerSystem Designsystem-designdistributed-systemsindexing

Last updated

Design a tagging system supporting tag lifecycle, boolean item queries, autocomplete, and high read/write throughput across thousands of tenants.

Scenario

Your company offers a SaaS platform where each tenant's workspace contains millions of resources — documents, tasks, tickets, or similar entities. Product wants to ship a tagging feature: any resource can carry multiple tags, users can search by tag, and the feature must hold up at scale.

You are asked to design this system from scratch. Walk through your reasoning at each step.


What the system must do

  1. Tag lifecycle — Create a new tag or return an existing one by name. Attach one or more tags to a resource, remove them, and make sure the same tag cannot be attached to the same resource twice.

  2. Single-tag lookup — Given a tag, return all resources that carry it. Support pagination and ordering (for example, most recently tagged first).

  3. Boolean tag queries — Return resources matching a set of tags combined with AND (intersection) or OR (union), or a mix of both.

  4. Top-N retrieval — Efficiently surface the top N resources for a tag, ranked by recency or a relevance score.

  5. Tag counts — Return how many resources carry each tag, cheaply and at high request rates.

  6. Autocomplete and suggestions — Offer prefix-based tag name completion as the user types. Optionally surface tags that frequently appear alongside a queried tag.

  7. Data model and storage — Define the schema and choose the right storage technology for tags, resources, and the assignments that link them.

  8. Indexing — Describe your forward index (resource → tags), inverted index (tag → resources), and how you handle very large or heavily skewed posting lists.

  9. Caching — What you cache, how you expire or invalidate it, and how you handle hot keys.

  10. Sharding and multi-tenancy — How you partition data across nodes, isolate tenants, and protect against hot-tag hotspots.

  11. Consistency — Choose and justify a consistency model. Explain how you support read-your-writes after a tag is attached.

  12. Tag de-duplication, rename, and merge — What happens when the same concept exists under two tag names, when a tag is renamed, and when two tags are merged into one.

  13. Access control — How tenant data is isolated and how per-resource ACL filtering fits into query results.

  14. Pagination — Design a stable, scalable pagination strategy that holds up under concurrent writes.

  15. Backfill and cleanup — How you reindex existing data, and how you clean up orphan tags and counters that have drifted.

  16. Capacity, observability, and SLOs — Rough order-of-magnitude sizing, the metrics you would instrument, and the latency and availability targets you would propose.


What is expected

This is an open-ended design question. A strong answer reasons about trade-offs rather than listing technologies. Cover scale assumptions upfront, justify your storage and indexing choices, and explain where you accept eventual consistency and why. Concrete numbers and back-of-envelope math are expected. Mention failure modes and how you mitigate them.

Hints

Hint 1

Start with scale: estimate the number of tenants, resources, tags, and assignments before picking any technology. The resulting data volume will drive every storage and sharding decision that follows.

Hint 2

Separate your source of truth (the OLTP store that enforces uniqueness and handles writes) from your derived read views (inverted index, search cluster, counters). Use a CDC pipeline to propagate changes rather than dual-writing from the application layer.

Hint 3

Think about skew early. A tag used on 50 million resources needs a different read path than one used on 50. Consider bucket-partitioning the inverted index for oversized posting lists and keeping a capped in-memory sorted set for the top-N hot-tag case.