Database Selection in System Design


Database Selection in System Design

Corey
Let’s talk database choices in system design. The database you choose can strongly affect how well a design meets its non-functional requirements, especially its ability to scale. The functional requirements may often be achievable with different database types, but workload details—like data shape, query patterns, and scale—make some options a better fit than others.

Maria
So we shouldn’t begin with “Which database is best?” We should ask what kind of data we have, how the system will read and write it, and how much of it we need to handle.

Corey
Exactly. Those three factors—data structure, query pattern, and scale—are a useful starting point. Let’s go through several common use cases, then compare relational and non-relational databases, and finish with examples where combining databases makes sense.

Maria
Let’s start with caching, since it appears in many system designs. A cache can keep us from repeatedly querying a database, or it can hold a response from a remote service when that call has high latency.

Corey
The basic model is a key and a value. The key might correspond to a database query’s conditions or to the parameters of an API request, and the value is the response we want to reuse. Common choices include Redis, Memcached, etcd, and Hazelcast. Redis is a familiar, battle-tested interview choice, though it’s not the only valid one.

Maria
Next, what if the data is a product image or a video? Those files are usually served as files rather than queried like ordinary records, so a traditional database isn’t necessarily the right storage tool.

Corey
That’s where blob storage fits. Amazon S3 is a common, cost-effective option for storing images and videos. For a globally accessed image, you can pair S3 with a content delivery network, or CDN, which distributes copies across locations so people can retrieve the content from a nearby server.

Maria
So the simple interview pattern for blob content is S3 for primary storage, plus a CDN when geographic distribution and faster access matter.

Corey
Right. Now consider searching text: product titles and descriptions, movie names and genres, or place names in a map-like service. A text search engine is designed for those kinds of searches, including fuzzy search where a user makes a typo.

Maria
The common examples here are Elasticsearch and Solr, both built on Apache Lucene. Fuzzy search can tolerate some difference between the typed word and the intended word. For example, someone might type “AIRPROT” while intending “AIRPORT,” and the search engine can look for a close match.

Corey
The source describes that example in terms of changing two characters, an edit-distance idea, and says you can configure a fuzziness level. The important point is that fuzzy search helps avoid returning no results just because a query contains a spelling error.

Maria
There’s an important caveat: Elasticsearch and Solr are search engines, not the primary source of truth for durable application data. The content says they offer availability and redundancy, but data could potentially be lost. Keep the authoritative data elsewhere, then load it into a search engine to support efficient searching.

Corey
Another specialized workload is application metrics—things like throughput, CPU utilization, and latency. For that, consider a time-series database, such as InfluxDB or OpenTSDB.

Maria
Why not use an ordinary relational database for those metrics? The access pattern is different. Writes are generally sequential and append-only: a record at time T1 is followed by later records at T2 and T3, rather than lots of random updates.

Corey
And reads are often bulk queries over a time range: show the last few minutes, hours, or days. Time-series databases are optimized around that combination of time-ordered writes and range-based reads.

Maria
What about company-wide reporting? If we want questions like how many orders we have, which geographies generate revenue, or which products are popular, that sounds like a data warehouse use case.

Corey
Yes. A data warehouse holds large amounts of data from transactional systems and supports reporting and analytics. It’s generally for offline analysis rather than serving the transactional workload itself. Hadoop is one option mentioned for collecting data and building reporting systems on top of it.

Maria
Now let’s build a decision path for relational versus non-relational databases. First ask whether the data is structured—something that can naturally be represented as tables, rows, and columns.

Corey
A user profile is a straightforward example: name, email address, city, and phone number are standard fields shared by each user. If the data is structured, the next question is whether the application needs transactional guarantees such as atomicity and consistency.

Maria
A payment transfer makes the need clear. Suppose one operation subtracts money from account A and another adds it to account B. They must succeed together or fail together; we must not debit one account without crediting the other.

Corey
The database should also provide consistency for the reads that follow. After a transaction, a request to fetch the balances should reflect the transaction reliably, rather than sometimes showing the right amount and sometimes not.

Maria
Inventory is another example. If customers are buying the last item, we need to avoid selling more units than we have. When those kinds of atomicity, consistency, and transaction requirements matter, a relational database is a strong fit.

Corey
Common relational choices include MySQL, Oracle, SQL Server, and PostgreSQL. The specific name matters less than choosing a database that provides the ACID guarantees the use case needs.

Maria
If the data is structured but those transactional guarantees aren’t needed, either a relational or a non-relational database may work. User information, for example, can be represented in a NoSQL model too; the decision depends on the rest of the workload.

Corey
Now suppose the data isn’t naturally uniform. An e-commerce catalog illustrates this: a shirt might have size and color, a refrigerator might have capacity and an energy-rating attribute, and milk might have quantity and an expiry date.

Maria
You could store those varying attributes as JSON, but querying across arbitrary attributes can be tricky in a relational database. If you have many possible attributes and a wide variety of queries against them, a document database can be a better fit.

Corey
MongoDB and Couchbase are examples of document databases. Elasticsearch and Solr are also described as special cases of document databases, although their key role in our earlier example was text search.

Maria
There’s another kind of non-relational workload: data that grows continuously, with a relatively small number of query types. Think of drivers sending location pings. As the number of drivers grows, the amount of incoming location data grows too.

Corey
If the key query is something like “find all locations for this driver ID,” and the system has to handle a large and growing volume of records, a column-oriented database may fit well. Cassandra and HBase are common, established examples for this pattern.

Maria
The speaker’s interview preference is Cassandra because it’s less heavy to deploy than HBase, which tends to come with more components. The two are described as roughly similar in performance, with their own pros and cons, so either can be defensible in an interview.

Corey
And if none of these special patterns apply—no need for ACID transactions, no wide range of attributes and queries, and no huge ever-growing dataset with a small fixed query set—the system may be simple enough that many database choices would work.

Maria
Real systems often don’t fit into just one box, though. Let’s return to e-commerce inventory and orders. Inventory needs careful transactional handling so that two customers can’t both commit a purchase of the single remaining item.

Corey
That points toward a relational database for inventory or for orders that are still active and undelivered. But the total order history keeps growing, and records may need to be retained for legal reasons. That long-term, high-volume data may fit a column-oriented database such as Cassandra.

Maria
One proposed approach is to keep an order in the relational database while it’s active, then, after delivery, move it into Cassandra as a permanent store. That combines transactional handling for the current workflow with a store suited to accumulating order history.

Corey
A different challenge is flexible reporting. Suppose we want all users who bought sugar in the last five days. “Sugar” might correspond to many item IDs from different sellers, brands, or product qualities, so this isn’t a simple lookup on one fixed field.

Maria
The suggested design stores a subset of order information in a document database: user ID, order ID, date, and the item IDs and quantities. We can query that model to find matching users and orders, then use those order IDs to retrieve the fuller order records from the relational database or Cassandra.

Corey
So the overall system can use three databases for different jobs: a relational database for transactional data, Cassandra for a growing long-term order store, and a document database for flexible query patterns. The goal isn’t to use multiple databases for its own sake; it’s to match each workload to an appropriate tool.

Maria
Let’s recap the practical cheat sheet. Use a cache such as Redis for reusable responses; blob storage such as S3, often paired with a CDN, for images and videos; Elasticsearch or Solr for text search; a time-series database for timestamped metrics; and a warehouse-style system for offline analytics.

Corey
For the broader database choice, look at the structure of the data, the queries, and the scale. Structured data with important atomic transactions points toward an ACID relational database. Flexible attributes and varied queries can point toward a document database, while large, continuously growing data with a limited query pattern can point toward Cassandra or HBase.

Maria
And treat these recommendations as a starting point, not a claim that only the named products can work. There are other databases in each category, and an interview answer should explain how the choice meets the stated requirements. For a real-world system, investigate the options in more depth before choosing.

Ask anything
We've read this whole study set, so questions about it get the best answers.