AlgoViz

Document database

easy 25 min

A document database stores each thing as one self-describing document, such as a JSON object with nested objects and lists.

See it run

Follow one document in, find it again, change it, summarise many, and decide what belongs inside.

Operations

Insert

A new user arrives: identifier, rules, document, index, replica, answer.

Step 1 of 6

Receive the document

Our systemWeb appinsertOneMongoDB primaryEmail indexUsers on diskMongoDB replica

{ name, email }

The application sends a JSON-like object with a name and an email.

Step 1: Receive the document

def insert(coll, doc):    doc.setdefault("_id", new_object_id())    if not coll.rules_accept(doc):        raise ValidationError    coll.write(doc)    for index in coll.indexes:        index.add(doc)    coll.replicate(doc)    return doc["_id"]
Documents touched
1 document
Atomic?
Yes
Four tables
several rows, one transaction

Each index is one more thing to write. Ten indexes make inserts clearly slower.

In the real world

The same document database doing real jobs. Pick one and step through it.

A post with every comment inside, at 200 bytes a comment (an assumption).

Step 1 of 4

Day one

commentssizea read carries
102 KBeverything

Fast

Ten comments inside the post. One read shows the page.

Step 1: Day one

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • Embedding an unbounded list

    critical

    The document grows until reads are slow and writes hit the size limit.

    Wrong
    posts.update(id, {"$push": {"comments": c}})
    Right
    comments.insert({"postId": id, **c})posts.update(id, {"$push": {"latest": {    "$each": [c], "$slice": -10}}})
  • Reading, changing and writing back

    critical

    Two writers read the same version. The second write silently removes the first change.

    Wrong
    doc = users.find_one(id)doc["logins"] += 1users.replace(id, doc)
    Right
    users.update(id, {"$inc": {"logins": 1}})
  • Two conditions on a list without an element match

    major

    Each condition can be met by a different element, so wrong documents match.

    Wrong
    {"items.qty": {"$gt": 5}, "items.price": {"$lt": 10}}
    Right
    {"items": {"$elemMatch": {    "qty": {"$gt": 5}, "price": {"$lt": 10}}}}
  • Believing there is no schema

    major

    A misspelt field is stored without a word and the bug appears far away.

    Wrong
    users.insert({"emial": "a@example.com"})
    Right
    users.set_rule(required=["email"])users.insert({"email": "a@example.com"})

Interview tips

  • How to explain it

    • One self-describing object per document.
    • Collections group documents that may differ.
    • One lookup, one atomic write.
  • Memory trick: FLEX

    • Flexible schema
    • Layered, nested structures
    • Expressive queries
    • eXtensible over time
  • Embed when

    • One to one or one to few.
    • Always read together.
    • The child data is bounded.
  • Reference when

    • One to many or many to many.
    • Read on its own.
    • Unbounded, large or often changing.