Skip to content

Senior follow-ups

You drew a design and said why. The interviewer now asks “why did you choose that?”, “what happens when it is ten times bigger?” or “is it consistent?”. These follow-ups are not a second examination of what you know. They test whether you knew what your choice cost when you made it. A candidate who chose well and cannot say what it costs looks the same as one who chose by habit.

Mental model. Every choice is a trade, and a follow-up asks for the other side of the trade. If you stated the cost when you made the choice, the follow-up is a repeat of what you said. If you did not, it is the first time you have thought about it, out loud, on a clock.

The Arena scripts this. After each stage it asks up to 2 follow-ups, some of which depend on a choice you made in that stage or an earlier one, and each needs at least 20 characters of your own answer before its guidance is shown. This lesson is the method for answering them.

The course uses one framework for every comparison: the same seven steps in the same order, in every trade-off explorer. In an interview it is a way to get from “I chose X” to a complete answer without forgetting a side. You do not recite all seven. You walk them, and each takes a sentence or two.

Step What you say
Decision What you chose, in one sentence, so that the answer has a subject.
Why? The requirement or number that the choice answers. A choice without a reason is a habit.
Benefits What it gives you, as something you could measure.
Costs What you pay for it every day: money, latency, complexity, a limit on what you can ask.
Failure modes How it breaks, and what the user sees when it does.
Scaling limitations The load at which it stops being the right choice, as a number if you can.
Alternative What you would choose instead, and the condition under which you would.

Here it is for a cache in front of the URL shortener’s database. The numbers are the ones from the first lesson: a peak of 10,000 requests per second, and a database that answers a lookup in 5 ms, 4 at a time, which is 800 per second. The 95% hit ratio is an assumption.

  • Decision. A cache in front of the database for redirects.
  • Why? 10,000 requests per second against 800 is 12.5 times what the database can serve, and the load is reads.
  • Benefits. With a hit ratio of 0.95 the database sees 10,000 × 0.05, which is 500 requests per second, 63% of its capacity.
  • Costs. A second system to run, and a copy that can be out of date until it is refreshed or expires. The cache-aside pattern describes both.
  • Failure modes. If the cache is lost, all 10,000 requests per second reach a database that does 800: 12.5 times its capacity. The lesson on cache failure follows that through.
  • Scaling limitations. The cache holds only what fits in its memory, and the hit ratio falls if the hot set outgrows it.
  • Alternative. Read replicas, if the hit ratio would be low, for example when the reads are spread evenly over a huge set of keys.

That is eight lines, and a spoken version takes about a minute. Notice that two of the seven steps, the failure mode and the limit, are where the numbers are. They are also what the next follow-up asks about, and you have already answered it.

The explorer below applies the same framework to five data models. It is the quickest way to practise the “alternative” step, because every option names the choice you would make instead, and when.

Data models

Which data model fits the way your application reads, writes and connects its data, and how far must it scale?

Every option follows the same framework: decision, why, benefits, costs, failure modes, scaling limitations, alternative.

Choosing options and requirements needs JavaScript. All the options are shown below.

Options to compare

Show or hide options. At least one stays on.

What does your system need?

Tick the requirements that apply. Each option is then measured against them.

Relational

Decision

Data lives in tables of rows with declared, typed columns. Rows refer to one another through keys, and SQL filters, joins and aggregates tables in one query. PostgreSQL is the example used here.

Why?

A declared schema, constraints and transactions let the database protect the rules of the data itself, and joins let it answer questions the application did not plan for.

Benefits
  • A transaction bundles several steps into one all-or-nothing operation, and other sessions do not see the intermediate states.
  • One query can join several tables, and aggregate functions summarise the result.
  • The database enforces rules itself: unique, not-null, check and foreign key constraints, so a reference cannot point at a row that does not exist.
Costs
  • The columns are declared before data is stored, so changing them is a migration. Adding a column is quick when its default is constant or NULL, but a volatile default rewrites the whole table and its indexes.
  • A business object that is normalised over several tables takes a join to read and several rows to write.
  • Free-form fields fit awkwardly. A jsonb column can hold them, but PostgreSQL’s documentation advises a somewhat fixed structure so that queries stay easy to write.
Failure modes
  • A recursive query over cyclic data never ends. PostgreSQL’s own graph-search example loops if the links contain cycles, and needs cycle detection (the CYCLE clause).
  • If the primary fails, committed transactions can be lost. PostgreSQL streaming replication is asynchronous by default, so a transaction that the primary committed but had not yet sent to the standby is lost if the primary fails catastrophically.
Scaling limitations
  • To spread data over servers, PostgreSQL’s documentation describes data sets that are each modified by only one server; an application that needs two sets queries both servers.
  • Standby servers answer read-only queries, so they add read capacity, not write capacity. PostgreSQL does not offer synchronous multimaster replication, where every server accepts writes.
Alternative

Choose Document when a business object is almost always read and written whole, records of one kind differ in shape, and data must spread over several machines.

Document

Decision

Each record is a document with named fields, which can hold nested documents and arrays. Data that is used together is stored together in one document. MongoDB is the example used here.

Why?

A core principle is to store together the data that is accessed together, so one read returns a whole business object and one write to a document is atomic. Documents of one collection may have different fields.

Benefits
  • Embedded documents and arrays hold related data in one document, and an operation on a single document is atomic, so many changes need no transaction.
  • Documents in a collection need not have the same fields, and validation rules can be added only to the parts that need stricter control.
  • Sharding is built in: the shard key distributes a collection’s documents across machines, to support very large data sets and high throughput.
Costs
  • Embedding can mean duplicating data. MongoDB recommends references when the child side has high cardinality, when duplication is too complicated to manage, or when embedded data grows without bounds.
  • A change across several documents needs a multi-document transaction. MongoDB supports them, including on sharded clusters, but they usually cost more than single-document writes and should not replace good schema design.
  • Relating documents is the application’s job: MongoDB tells you to add application logic to keep references consistent. A join across collections is a $lookup stage, a left outer join in an aggregation pipeline.
Failure modes
  • A document that keeps growing. An embedded array with no natural bound grows until the document reaches the maximum size of 16 MiB.
  • Copies that disagree. When the same data is embedded in many documents, an update that stops half-way leaves the copies different, unless a transaction covers it.
  • A poorly chosen shard key spreads data unevenly, which can negate some benefits of sharding or cause performance bottlenecks.
Scaling limitations
  • The shard key sets the trade-off. Hashed sharding spreads data evenly, even for keys that increase steadily, but range queries on the key are more likely to reach every shard. Ranged sharding lets mongos send an operation only to the shards that hold the data, but its efficiency depends on the key chosen.
  • Transactions across documents cost more than single-document writes, so a request that touches one document is the cheapest to serve.
Alternative

Choose Relational when you must join and aggregate across many kinds of records in ways you cannot predict, or when transactions across many records are routine.

Key-value

Decision

Reads and writes address a value by its key. In Dynamo, the published design used here, the value is opaque bytes that the store does not interpret. Redis, the other example, adds data structures such as hashes and sorted sets, still addressed by key.

Why?

When the access pattern really is to fetch the object stored under a key, the store can stay simple, spread keys over nodes by hashing them, and add capacity one node at a time.

Benefits
  • Keys are spread over nodes by hashing: Dynamo uses consistent hashing, and Redis Cluster maps each key to one of 16,384 hash slots.
  • Dynamo aims to stay writable: its design goal is a store where no update is rejected because of failures or concurrent writes.
  • Any value shape fits. Dynamo treats keys and values as opaque arrays of bytes, so the store needs no change when the structure of a value changes.
Costs
  • Everything goes through the key. Dynamo’s query model is a read or write of one item identified by its key, with no operation spanning several items and no relational schema.
  • Searching by contents needs an index you maintain or an add-on query engine. Redis, for example, offers Redis Search as a secondary index and query engine.
  • Transactions are weak. Dynamo gives no isolation and permits only single-key updates, and Redis does not roll back a transaction when one of its commands fails.
Failure modes
  • Acknowledged writes are lost. Redis Cluster replicates asynchronously, so an acknowledged write is lost if its master fails before the write reaches a replica.
  • Conflicting versions. Dynamo accepts concurrent writes, so a read can return several versions of an object, and the application, or a simple rule such as last write wins, must reconcile them.
  • A hot key. The key alone decides which nodes hold a value (Dynamo hashes it, Redis Cluster takes its slot from it), so one very popular key cannot be split: its traffic stays on the nodes that hold it.
Scaling limitations
  • Redis Cluster has 16,384 hash slots, which caps the cluster at 16,384 master nodes, and its documentation suggests staying on the order of 1,000 nodes.
  • Operations across several keys are limited. In Redis Cluster, set unions and intersections work only when all the keys hash to the same slot, hash tags can force that, and such operations can be unavailable during resharding.
Alternative

Choose Wide-column when each key should hold many rows that are read back in order, such as one device’s readings between two times.

Wide-column

Decision

Data is addressed by a row key (Bigtable) or a partition key (Cassandra). Bigtable keeps rows in order by row key, and Cassandra sorts the rows of a partition by their clustering keys, so a range can be read together. A row can hold many columns, and tables are designed around the queries they must answer. Bigtable and Cassandra are the examples used here.

Why?

A key that partitions the data spreads writes and volume over many machines, the write path only appends, and rows that are read together sit together, in order.

Benefits
  • The write path only appends. Cassandra writes to a commit log and a memtable, then flushes to immutable SSTables, and its documentation calls the write path highly optimised.
  • Reading a range is efficient. Bigtable keeps rows in order by row key, so reads of short row ranges involve only a small number of machines, and Cassandra sorts the rows of a partition by clustering keys.
  • There is no single primary. Cassandra replicates every partition to many nodes, and every replica can accept writes (multi-master replication with tunable consistency).
Costs
  • No joins. Cassandra’s documentation says you cannot perform joins: you do the work on the client side or create a denormalised second table. You start from the queries, then design a table for each.
  • Only queries on the key are cheap. Only equality is allowed on a partition key, and filtering on another column needs a secondary index or ALLOW FILTERING, which runs a full scan.
  • Compaction rewrites data in the background, so each write is rewritten several times (write amplification), which adds background I/O.
Failure modes
  • A query the key does not serve. ALLOW FILTERING scans every partition, so latency grows with the amount of data and performance is unpredictable.
  • Copies out of step. The same data is stored in several tables, one per query, and each write must reach all of them. A logged batch makes the updates complete eventually or not at all, but isolation covers only updates that share a partition key, so a reader can see one table updated before another.
  • Replicas that disagree. Cassandra is eventually consistent: divergent versions of the same data can exist temporarily before they are reconciled.
Scaling limitations
  • A partition lives on one set of replicas, so it must stay small and evenly spread. Cassandra’s documentation gives a general guideline of fewer than 100,000 values and less than 100 MB per partition, and says the partition key must distribute data evenly across the cluster.
  • The unit of atomic change is small. Bigtable guarantees atomicity for one row and has no general transactions across row keys, and Cassandra isolates a batch only within a partition key.
Alternative

Choose Relational when the queries are not known in advance, or you need joins, constraints and transactions across many rows.

Graph

Decision

Data is stored as nodes, which are entities, and relationships, which are directed, typed connections between two nodes. Both can carry properties. A query describes a pattern of connections and the database finds the matches. Neo4j is the example used here.

Why?

When the questions are about how things connect, relationships are first-class, so following them is a pattern in the query rather than a chain of joins.

Benefits
  • Connections are written as patterns. Neo4j’s documentation says there is no need to JOIN tables, and variable-length patterns match paths of unknown length.
  • Indexes and constraints are optional: Neo4j is described as schema optional, and properties are key-value pairs on nodes and relationships.
  • Transactions are ACID: operations that access the graph, indexes or schema run in a transaction.
Costs
  • A business object is made of nodes, relationships and properties rather than stored as one record, so reading it back is a pattern query, not a single lookup.
  • Splitting a graph over several databases is a design decision. Behind a composite database, queries written for a single database must be modified according to the sharding rules.
  • Open-ended traversals have to be bounded. Neo4j’s manual recommends inline predicates that say precisely which nodes and relationships to include, so that unwanted results are pruned as the graph is traversed.
Failure modes
  • A pattern that matches far too much. Quantified path patterns can match very large numbers of paths, resulting in slow queries, especially with a large maximum length or a pattern that is too general.
  • Losing the write majority. A database remains available for writes as long as a simple majority of its primaries is working.
  • A write that spans two shards. A composite database allows only transactions that write to a single graph, so a change that must touch two shards cannot be one transaction.
Scaling limitations
  • One database has one writer. It pushes writes synchronously to other primaries and completes a commit only when enough members confirm, and secondaries are there to scale reads.
  • Beyond one database, Neo4j’s documentation lists composite databases over shards (an Enterprise Edition feature, with write concurrency scaling horizontally depending on the graph model) and property sharding (the structure stays in a single graph shard, with the write concurrency of a single instance).
Alternative

Choose Relational when the connections you follow are shallow, a join or two, and most questions are about the rows themselves.

Nothing is ticked yet. Tick a requirement to see how each option measures up.

References

Three exercises, for a chat store that holds messages by conversation:

  1. Know what you write. Tick Writes and data volume exceed one machine, and reads are mostly a lookup by key. The document, key-value and wide-column options meet it. The relational option meets it only partly: its documentation says standby servers answer read-only queries, and that spreading data over servers means data sets that are each modified by only one server. This is the honest form of “SQL does not scale”: it scales its reads easily, and its writes by splitting the data yourself.
  2. Ask a question you did not plan for. Add Next month’s questions are not known yet: you must filter, join and aggregate across kinds of records without redesigning storage. The wide-column option fails it, and so does the key-value option. This is what the Arena means when it says a wide-column store gives up joins and queries on other columns. If the product team will ask unplanned questions about messages, the choice has a cost you must say.
  3. Ask for both. Untick everything, then tick One change touches several records and must apply entirely or not at all. and Writes and data volume exceed one machine, and reads are mostly a lookup by key. No option meets both. Whichever you pick, the follow-up is about the one you gave up, and you now know that before you are asked.

A follow-up chain: PostgreSQL for 5 billion messages a day

Section titled “A follow-up chain: PostgreSQL for 5 billion messages a day”

The Arena’s global messaging scenario has 100 million users, 20 million daily users and 5 billion messages a day. In its architecture stage you choose a store: PostgreSQL, a wide-column store or another. If you choose PostgreSQL, the scripted interviewer asks:

Why PostgreSQL for 5 billion messages a day? What happens when one primary can’t absorb the write rate?

Write your own answer first. Then compare it with the shape below, which is the framework applied to this branch.

Start from the numbers, not from a feeling. 5 billion a day over 86,400 seconds is 5,000,000,000 ÷ 86,400, about 57,870 inserts per second on average, and with a peak at 3 times the average about 173,611, which you round to 175,000. At 200 bytes a message, 5 billion messages are 1 TB of new rows a day, and a year is 365 TB for one copy.

Say whether one primary absorbs that, and how you would know. It depends on row size, indexes, hardware and tuning, so the strong answer says “I would measure it” and does not recite a figure. For the arithmetic, use the model’s store node from the chat case study, which serves 8 requests at a time at 3 ms each: 8 ÷ 0.003, about 2,667 per second. That is an illustrative size, not a measurement of PostgreSQL. At 173,611 writes per second you would need 173,611 × 0.003 ÷ 8, which is 65.1, so 66 such nodes. The point is the method: divide the peak by what one primary does, and the quotient tells you whether you are batching, splitting or both.

Give the way out, in order, with the cost of each step.

  1. Batch the inserts, so that one round trip carries many rows.
  2. Keep the recent, hot data apart from the old.
  3. Split the table across several primaries by conversation id, so that a conversation stays on one shard and keeps its order. PostgreSQL’s documentation describes data sets that are each modified by only one server, which is this. The costs are routing every query to the right shard, queries and transactions that cross shards (see cross-shard queries and choosing a shard key), and moving data when you add shards.

Then say when you would choose otherwise. If every operation is an append to one conversation or a read of its latest messages, a store that partitions by key does the splitting for you. That is the alternative step, and it is the answer to the next follow-up the Arena has for the other branch: if you had chosen a wide-column store, it would ask what you give up and how you read one conversation in order. The answer is the second exercise above: joins, queries on other columns and multi-row transactions are gone, the order of a conversation still comes from its numbering, and partitions must stay bounded, for example with a time bucket in the key.

Neither branch is the right one. Both are complete when they name the number, the way out and its price, and the condition that would flip the choice.

“Is it consistent?” cannot be answered yes or no, and answering it that way is the usual mistake. Ask three things back, out loud.

  1. Which data? A bank balance, a chat message and a “last seen” time do not need the same promise.
  2. Under which failure? A healthy network and a cut link are different questions. CAP is about the cut: while it lasts, a system either refuses some requests or answers with something that may be old. Abadi’s PACELC adds the other half: with no cut, the choice is between latency and consistency, and every request pays it.
  3. What does the user see? Say it as a sentence about a person: “the sender sees their own message at once, and the recipient may see it a moment later”.

Then name the weakest model that keeps the feature correct, because each step up costs waiting and availability. The consistency models lesson lists five. For a chat they read like this: read-your-writes, so that you always see the message you just sent; causal consistency, so that nobody sees a reply before the question; and one total order per conversation if every member must see the same sequence. Jepsen describes the first two as sticky available, and it describes linearizability and sequential consistency as not totally or sticky available.

Now price it. The global messaging case study gets one order per conversation from one writer, the conversation’s home region. The price is a round trip for members elsewhere: in the lab, the slowest 5% of messages take 28 ms with a relay log and no home, and 145 ms when messages go through the home. That is the PACELC trade in numbers, paid on every message, not only in a failure. If the home is unreachable, members in other regions cannot get a message numbered until another region takes over with a lease and a fencing token.

Interview question

Writing and reviewing an answer needs JavaScript.

The interviewer asks

Your company runs one monolith, built by 40 engineers in six teams. The last three releases were late because of a broken change from another team, and the CTO wants to rewrite it as microservices. What do you say?

Assume one PostgreSQL database, a pipeline of 45 minutes, basic monitoring and no distributed tracing, and about 3,000 requests a second at peak.

Write your answer first. The strong answer unlocks once you have written a real attempt.

0 of 80 characters written to unlock the strong answer

The question above is not about consistency, but its strong answer uses the same moves: it asks what problem the choice is meant to solve, prices the alternative, offers cheaper steps first, and says when the answer would become yes.

“It depends” is the right answer to many design questions and a poor one when it ends the sentence. A useful “it depends” has four parts: the variable, the threshold, the default and the cost of being wrong.

Take the cache again. Does a cache save the database? It depends on the hit ratio. The database serves 800 per second against a peak of 10,000, so the hit ratio must be above 1 − 800 ÷ 10,000, which is 92%, just for the database to stay under its capacity at the peak. At 92% it would run at 100%, so in practice you want more. Now the answer has all four parts: “It depends on the hit ratio. Above about 92% the database survives the peak, and I would plan for 95%. I would start with a cache because redirects repeat. If the hit ratio were below that, I would add replicas instead, and I would measure it in the first week.”

The same shape works for the monolith question: it depends on whether a part must scale or deploy on its own, and whether the team has the operational prerequisites. See the monolith and modular monolith lesson. If you can state the variable and the threshold, you have shown that you know what the decision hangs on, and that is most of what the follow-up is looking for.

Some follow-ups are not questions but facts: “messages now carry a preview and are 2,000 bytes, not 200”. This is step 10 of the ten steps, defending the design, and the strong response is to revise, not to defend. Recompute only what depends on the new number, and say what does not change.

In the global messaging example the message rates stay at 57,870 and 175,000 per second, because the count of messages did not change. What changes is the bytes. The traffic between regions, 26,250 messages per second at 15% of the peak, goes from 5.25 MB/s to 26,250 × 2,000, which is 52.5 MB/s, or 420 Mbit/s, which starts to matter on a link between continents. A year for one copy goes from 365 TB to 3.65 PB. The limit moved from the message rate to the byte rate, and you can say so in two sentences. Changing your design when given a fact is not losing the argument. Holding on to the old design when the numbers have moved is.

What happens if you defend a choice with only its benefits?

Section titled “What happens if you defend a choice with only its benefits?”

A candidate says “MongoDB, because NoSQL scales”. The interviewer asks: “the payment between two accounts must apply entirely or not at all. What now?” The one-sided answer has no cost to point to, so it has to be improvised.

The framework answer already has it. In the explorer, the document option meets the requirement One change touches several records and must apply entirely or not at all. only partly: a single document is atomic, and a change across documents needs a multi-document transaction, which MongoDB supports, including on sharded clusters, but which usually costs more than a single-document write. The relational option meets it fully. And the opening claim was wrong in the other direction too: PostgreSQL’s documentation describes how to spread data over servers, so “SQL does not scale” is not a reason to pick a document store either.

The cost of the one-sided answer is not the wrong database. It is that the interviewer now knows you did not look for the cost, and will look for it in everything else you have said. Stating the cost first removes the question.

  • A follow-up asks for the other side of your choice. The seven steps give it in order: decision, why, benefits, costs, failure modes, scaling limitations, alternative.
  • Start every follow-up from a number. For the cache, 10,000 requests per second against 800 is 12.5 times, and a hit ratio of 0.95 leaves the database 500.
  • For 5 billion messages a day, the average is about 57,870 writes per second, the peak about 173,611 (rounded to 175,000), and a year is 365 TB for one copy. Give the way out in order: batch, separate hot from old, shard by conversation, and say when a store that partitions by key would be simpler.
  • Answer the consistency question with three questions: which data, which failure and what the user sees. Name the weakest model that keeps the feature correct, and its price: in the lab, 28 ms becomes 145 ms for the slowest 5% when messages go through a home region.
  • Say it depends with a variable, a threshold and a default. The cache needs a hit ratio above 92% to keep the database under its capacity at the peak.
  • Given a new fact, recompute only what depends on it. At 2,000 bytes, the inter-region traffic is 52.5 MB/s (420 Mbit/s) and a year is 3.65 PB.
  • A benefit stated without its cost is the first thing a follow-up finds.

Next: Recap and quiz, the ten steps against the eight stages and a checklist for the interview.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.