Skip to content
Tutorials Center
My learning

Topics

Lesson 40 of 77 ยท Databases and Data Interview Questions

Databases: NoSQL, Caching and Scaling

Prepare NoSQL and scaling interview answers: CAP theorem, replication, sharding, cache-aside versus write-through, eviction policies and quorums, with runnable models.

Once a system outgrows one database server, interviewers ask how you would scale it. The answers revolve around a handful of ideas: replicate for availability and read capacity, shard for write capacity and size, cache to avoid repeating work, and accept that distributed systems force trade-offs (the CAP theorem). "SQL or NoSQL?" is the opening question, and a good answer is never a slogan.

The real systems (MongoDB, Cassandra, Redis, DynamoDB, PostgreSQL replicas) cannot run on this site, so their commands and configuration appear in read-only blocks. The runnable parts are small Node.js stand-ins (Node 24 on the site's runner) that model the algorithms those systems use: modulo versus consistent-hash sharding, an LRU cache, cache strategies and quorum reads. They are simplified teaching models, not the source of any real product. For SQL basics see indexes and transactions.

Scaling a data tier

Replicate Copies for reads and failover.
Shard Split data across machines.
Cache Keep hot data in memory.

Each move buys capacity and costs consistency or simplicity.

1 / 2

SQL or NoSQL?

Short answer: "NoSQL" is a family of databases that give up some relational features (joins, rigid schemas, sometimes strong consistency) in return for flexible data models or easier horizontal scaling. Neither is better in general.

Choose a relational database when you have structured data with relationships, need joins, constraints and multi-row transactions, and your scale fits a well-tuned primary with replicas. That covers most applications, and a PostgreSQL or MySQL server goes surprisingly far.

Consider NoSQL when the access pattern is simple and known (look up by key, append events), the schema varies per record (documents), write volume or data size exceeds what one node handles, or latency at huge scale matters more than ad-hoc querying. In NoSQL you typically design the data model around your queries, and you denormalize: store what you read together, together.

JavaScript
// Read-only: a document database stores nested data in one record
{
  "_id": "order-1001",
  "customer": { "id": 7, "name": "Ana" },
  "items": [
    { "sku": "KB-1", "qty": 1, "price": 50 },
    { "sku": "MS-2", "qty": 2, "price": 20 }
  ],
  "status": "paid"
}
// One read returns the whole order. A relational design would join three tables.

The CAP theorem

In a distributed data store, when a network partition splits the nodes so they cannot talk to each other, you must choose between:

  • Consistency (C): every read sees the latest write (or an error).
  • Availability (A): every request gets a non-error response, even if it might be stale.

Partition tolerance (P) is not optional; networks do fail. So the real choice is CP or AP during a partition. A CP system (a quorum-based store, ZooKeeper-style coordination) refuses some requests rather than return stale data. An AP system (Cassandra-style, Dynamo-style stores) keeps answering and reconciles later, which is called eventual consistency. When there is no partition you can have both, but there is still a trade-off between consistency and latency (this is the PACELC refinement, a good thing to mention briefly).

Common mistakes to avoid: saying "pick any two of three" as if P could be dropped, and saying a database "is" CP or AP without noting that many are configurable per query.

Replication

Replication keeps copies of data on several nodes.

  • Leader/follower (primary/replica): writes go to the leader, which streams changes to followers. Followers serve reads and take over if the leader fails. Replication lag means a follower read can be stale. A user who writes and immediately reads from a follower may not see their own write (read-your-writes problem); route that user's reads to the leader for a short time.
  • Multi-leader / leaderless: several nodes accept writes, so you need a conflict-resolution rule (last write wins, version vectors, merge functions).

Quorums

In leaderless stores each key lives on N replicas. A write waits for W acknowledgements; a read asks R replicas and takes the newest answer. If R + W > N, every read set overlaps every write set, so a read sees the latest acknowledged write. Smaller values are faster and more available but may return stale data. This stand-in makes the overlap visible:

Node.js Stand-in: quorum reads and writes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Sharding

Replication copies the same data; sharding (partitioning) splits different data across nodes so each holds a slice. It scales storage and writes. The central question is how to map a key to a shard.

  • Range sharding: shard by key ranges (Aโ€“F, Gโ€“M...). Range scans are easy, but hot spots appear (new users all hit the last shard).
  • Hash sharding: hash(key) mod N. Even distribution, but adding a shard changes N and almost every key moves.
  • Consistent hashing: place nodes and keys on a ring; a key belongs to the next node clockwise. Adding or removing a node moves only about 1/N of the keys. Real systems add many virtual nodes per server to even out the load.

Compare the two hashing approaches by counting how many keys move when we add a fourth node:

Node.js Stand-in: modulo versus consistent hashing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Sharding has costs: cross-shard joins and transactions are hard or impossible, resharding is operationally heavy, and a poor shard key creates hot spots. Say "avoid sharding until you must" and you will sound experienced.

Caching strategies

A cache stores copies of expensive-to-get data in fast memory. Interviewers want the strategy names and their failure modes.

  • Cache-aside (lazy loading): the application checks the cache; on a miss, it reads the database and stores the result. Simple and resilient, but the first request is slow and the cache can go stale.
  • Read-through: the cache itself loads missing data from the database.
  • Write-through: every write goes to the cache and the database together. Reads are fresh; writes are slower.
  • Write-back (write-behind): write to the cache and flush to the database later. Fast, but data can be lost if the cache dies before flushing.

Invalidation is the hard part: use a TTL (time to live) as a safety net, and delete or update the entry when the source changes. Known failure modes: cache stampede (a popular key expires and many requests hit the database at once; fix with request coalescing, jittered TTLs or early refresh), cache penetration (requests for keys that do not exist always miss; cache the "not found" for a short time) and stale data.

JavaScript
// Read-only: cache-aside against Redis (the commands are real; not runnable here)
const cached = await redis.get(`user:${id}`);
if (cached) return JSON.parse(cached);
const user = await db.query("SELECT * FROM users WHERE id = ?", [id]);
await redis.set(`user:${id}`, JSON.stringify(user), { EX: 300 });  // TTL 300 s
return user;
Node.js Stand-in: cache-aside with TTL and invalidation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Eviction policies

A cache has limited memory, so it must evict. Common policies:

  • LRU (least recently used): drop the entry untouched the longest. A good default.
  • LFU (least frequently used): drop the entry used the fewest times; resists scans but adapts slowly.
  • FIFO and random: simple, rarely best.
  • TTL-based: drop expired entries first.

An LRU cache needs O(1) get and put. In languages without an ordered map, the classic design is a hash map plus a doubly linked list. JavaScript's Map remembers insertion order, so deleting and re-inserting a key on each access gives LRU in a few lines:

Node.js Stand-in: an LRU cache with Map

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Honest summary of the stand-ins: the quorum, hashing, cache-aside and LRU examples model the core algorithms. Real systems add networking, persistence, failure detection and many more details (for example Redis approximates LRU by sampling). The real products are shown only as read-only snippets.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the CAP theorem say you must choose between during a network partition?

    Partitions happen, so a distributed store must either refuse some requests to stay consistent (CP) or answer possibly stale data to stay available (AP).
  2. 2. With N = 5 replicas and W = 3, which read quorum R guarantees seeing the latest write?

    R + W must exceed N: 3 + 3 = 6 > 5. With R = 2, 2 + 3 = 5 is not greater than 5, so a read might miss the newest replica.
  3. 3. Why is consistent hashing preferred over hash(key) % N when nodes are added?

    Changing N in the modulo formula remaps most keys. On a ring, a new node only takes over the keys between it and its predecessor.
  4. 4. Which cache strategy risks losing acknowledged writes if the cache crashes?

    Write-back acknowledges after writing to the cache and flushes to the database later, so unflushed data can be lost.
  5. 5. A very popular cache key expires and thousands of requests hit the database at once. What is this called?

    It is also called the thundering herd. Mitigations include request coalescing, jittered expirations and refreshing before expiry.

Summary

  • Choose a database by access pattern and consistency needs; relational is the strong default, NoSQL fits simple, large-scale or flexible-schema workloads.
  • CAP: during a partition, choose consistency (reject) or availability (maybe stale).
  • Replication gives availability and read scale but introduces lag; quorums with R + W > N give fresh reads.
  • Sharding splits data for write scale; consistent hashing minimizes data movement when nodes change.
  • Cache-aside is the common pattern; know write-through, write-back, TTLs, stampedes and invalidation.
  • LRU evicts the least recently used entry; a map plus ordering gives O(1) operations.

Next: Data Science and pandas Interview Questions.