1. We are building a marketplace with users, listings, orders and payments. Would you use a SQL or a NoSQL database?
Seniordata-modellingsqlnosql
What the interviewer is really testing
Whether you choose a store from the data, the queries and the numbers instead of from fashion, and whether you can say what each choice gives up. Interviewers also watch for candidates who treat it as one decision for the whole system.
Show the model answer, follow-ups and checklist
Model answer
I would not choose one database for the whole product. I would choose per dataset, from the queries and from what must never be wrong.
The core is relational. Placing an order touches the buyer, the listing’s stock and a payment record, and it must never sell an item twice or charge twice. That needs transactions across rows and constraints the database enforces. On scale, 5 million orders a month is about 2 per second on average, perhaps 50 at peak. One PostgreSQL primary with a couple of read replicas handles that with a great deal of room. So users, listings, orders and payments live there, normalized, as the system of record.
Then I look for data that does not fit.
- Search over listings needs full text, filters and ranking. That goes in a search index, fed from the database.
- Sessions and carts are read by one key at a high rate and are short-lived. Redis.
- Listing photos are large and immutable. Object storage, with a row in the database for each.
- Activity events such as views and clicks arrive at thousands per second, are append-only and are read by user and time. A log and a warehouse, or a wide-column store if the product reads them back.
Each extra store is another system to run and to keep consistent, so I add one only when a number or a query forces it.
What would change the core? If orders approached tens of thousands of writes per second, or had to be accepted in several regions, I would shard by buyer or move to a distributed SQL database. I would keep SQL and transactions. Moving orders to a key-value store would mean rebuilding constraints and joins by hand in application code.
Likely follow-ups
- Listings have very different attributes in each category. Does that push you to a document database?
- Not on its own. I would keep the common fields as columns and put the category-specific attributes in a JSONB column with an index, which PostgreSQL handles well. I would move listings to a document store only if their volume or query needs outgrew the relational database, and orders and payments would stay where they are.
- Traffic grows a hundredfold and orders reach 20,000 writes per second. What changes?
- One primary is no longer enough for the orders data. I would shard by a key that keeps an order's rows together, such as buyer id, or move to a distributed SQL database, so that I keep SQL and transactions inside a shard. Work that crosses shards, such as paying a seller, becomes an asynchronous step with a saga instead of one transaction.
- Why not use DynamoDB from day one, since it is managed and scales?
- It is a reasonable choice when the access patterns are known and stable, because every query has to be designed into the keys. A young product changes its queries often, and support tools and analysts need ad hoc access. I would be trading query flexibility and simple constraints for scale I do not need yet.
- How do you keep the search index in step with the database?
- The database is the source of truth. Changes flow to the index through change data capture or an outbox table, so the index lags by a second or so and can always be rebuilt. I would not write to both from the request handler, because a failure between the two writes leaves them different for good.
Common mistakes
- Naming a database before stating the queries, the invariants and the scale.
- Saying NoSQL scales without a number that shows one relational primary is not enough.
- Treating the choice as one decision for the whole system instead of one per dataset.
- Forgetting what is given up, such as joins, constraints, ad hoc queries and transactions across partitions.
- Adding four stores to a design and ignoring the cost of operating and synchronising them.
A strong answer includes
- Starts from access patterns, invariants and size, not from a product name
- Puts orders and payments in a relational database and says why (transactions and constraints)
- Gives a rough number showing the core data fits one primary with replicas
- Identifies data that suits another store (search, sessions, images, event streams) and names the store
- States what each extra store costs and how it stays consistent with the source of truth
- Says what would trigger a change, and prefers sharding or distributed SQL over abandoning transactions
Revise: SQL vs NoSQL