Skip to content
tezvyn:
Menu

Embed or reference likes in a document database?

Source: interviewMediumHow cards are made

Summary

document modeling driven by access patterns and growth.

Key points

embedding is fast for small bounded lists but unbounded likes hit document size limits; referencing scales for high-cardinality, write-heavy likes.

What's really being asked

This evaluates whether you can choose between embedding and referencing in a document store based on cardinality, growth, and access patterns, which is the central skill of NoSQL data modeling.

The full answer

Embedding the liker IDs in the post document gives excellent read locality: one fetch returns the post and its likes, and updates can be atomic within a single document. This works only when the array is small and bounded. Likes on a popular post are unbounded and can reach millions, which collides with the per-document size limit and causes the whole document to be rewritten and reindexed on every like, creating write contention on viral posts. Referencing solves growth by storing each like as its own record in a separate collection keyed by post and user, scaling to any cardinality and spreading writes, at the cost of an extra query or aggregation to assemble counts and membership. A common hybrid stores a denormalized like count on the post for fast display and a separate likes collection for membership checks and analytics.

The mistakes people make

Defaulting to embed because it is the document way, without considering that likes are unbounded. Ignoring the document size ceiling and the cost of rewriting a large document on each like. Forgetting the duplicate-like uniqueness constraint, which a separate collection enforces cleanly with a compound unique index.

What usually comes next

How do you prevent a user from liking twice? How do you keep the cached count accurate under concurrency? How would the answer change for comments versus likes? How do you paginate likers?

A concrete example

For a celebrity post with two million likes, embedding fails immediately on the size limit. A likes collection with a unique index on the post and user pair enforces single likes, supports paginated liker lists, and a counter on the post serves the displayed total without scanning.

Interview question

Why is embedding the full list of liking user IDs inside each post document a poor choice for a popular social platform?

  • a.Referencing is impossible without joins, which document stores lack
  • b.The liker array is unbounded, so it can exceed the document size limit and forces rewriting the whole document on each likeCorrect
  • c.Embedding always violates ACID guarantees
  • d.Document databases cannot store arrays at all
Why?

Unbounded growth collides with the document size cap and causes costly full-document rewrites and contention per like. Document stores do support arrays and offer lookups, so the other options are false.

Just read this? Test yourself on what you have been reading.

Read the original → mongodb.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles