1. Home
  2. Explore
  3. Computer Science
  4. Database Selection in System Design

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Database Selection in System Design

Choosing Databases in System Design

Database choice affects how well a system meets its non-functional requirements (NFRs), such as scalability, latency, availability, and reliability. Many different databases can support the same functional requirements, but they differ in how efficiently they handle particular data structures, query patterns, and scales.

A practical database choice depends mainly on:

  • Data structure: Is the data highly structured, flexible, or largely unstructured?

  • Query pattern: What kinds of reads, writes, searches, and updates will the system perform?

  • Scale: How much data and traffic must the system support, and how quickly are they growing?

No single database is best for every workload. Real systems often combine several storage technologies, each serving a particular purpose.


Caching

A cache stores frequently needed results so that they can be returned without repeatedly querying a slower or more distant system. Caching can reduce database load and improve response times.

Common reasons to use a cache include:

  • Avoiding repeated reads from a database.

  • Storing the result of a high-latency call to another service.

  • Reusing frequently requested data close to the application that needs it.

Key-value model

Many caches use a key-value structure:

  • Key: Identifies the request or data, such as a database lookup key or a set of API request parameters.

  • Value: Stores the result associated with that key, such as a database record or a service response.

A cache should be treated as a performance layer rather than automatically as the authoritative store. Cached data can become stale, expire, or be evicted, so the system needs a way to obtain the authoritative value when necessary.

Common caching systems

  • Redis: A widely used, general-purpose in-memory data store commonly used for caching.

  • Memcached: A simple distributed in-memory caching system.

  • etcd: A distributed key-value store often used for coordination and configuration, rather than as a general-purpose application cache.

  • Hazelcast: An in-memory computing and data-grid platform that can support caching and distributed data processing.

Redis is a common default in system-design discussions, but the best choice depends on the required features and operating environment.


File and Blob Storage

Images, videos, and other files are usually stored in blob storage (also called object storage), rather than in a conventional database. These files are generally retrieved as complete objects, not queried internally like rows and columns.

For example, an e-commerce system might store product images and videos, while a media-streaming system might store video files.

Object storage and content delivery networks

A common architecture uses:

  1. Object storage as the primary location for files. Amazon S3 is one example.

  2. A content delivery network (CDN) to cache and distribute copies of files across geographically separated locations.

A CDN can serve a file from a location nearer to the user, reducing latency and avoiding the need for every request to reach the object store’s origin location. The object store remains the durable origin; the CDN provides faster delivery of frequently accessed content.


Text Search

Applications that search text across titles, descriptions, names, genres, or other fields often use a dedicated search engine. Examples include product search, movie search, and location or business search.

Elasticsearch and Apache Solr are common search platforms built on Apache Lucene, a library that provides core text-indexing and search capabilities.

Search engines are useful for search-specific operations, including:

  • Searching and ranking text matches.

  • Finding documents based on indexed fields.

  • Supporting fuzzy matching, where a query can still find likely results despite a spelling error.

Fuzzy search and edit distance

A fuzzy search can return likely matches even when the query contains a typo. For instance, a user who types “AIRPROT” may intend “AIRPORT.”

One way to describe the difference between two words is edit distance: the minimum number of permitted character edits required to transform one string into another. Under the standard Levenshtein definition, the transposition in “AIRPROT” versus “AIRPORT” can be represented as two edits: changing one character and changing the other. Some algorithms, such as Damerau–Levenshtein distance, treat the adjacent transposition as a single operation. Search systems may use additional techniques beyond a simple edit-distance threshold.

Search index versus source of truth

A search index is generally a derived representation of data held elsewhere. The primary database or storage system should remain the authoritative source when the application needs durable, transactionally reliable records. Search indexes can be rebuilt or refreshed from that source if necessary.

Search platforms may offer persistence and redundancy features, but those features do not by themselves make a search index the right authoritative store for every application. They are optimized for retrieval and ranking, not necessarily for the same guarantees or workload as a primary transactional database.


Time-Series Data

A time-series database is designed for data recorded over time, such as application metrics, CPU utilization, request throughput, or latency.

These workloads commonly have two characteristics:

  • Append-oriented writes: New measurements are added with timestamps, usually in chronological order, rather than repeatedly updating old measurements.

  • Time-range reads: Queries commonly retrieve measurements from a recent or historical interval, such as the last few minutes, hours, or days.

For example, a sequence of measurements might be recorded at times T1T_1T1​, T2T_2T2​, and T3T_3T3​, with T1<T2<T3T_1<T_2<T_3T1​<T2​<T3​. A time-series database can optimize storage and queries around this pattern.

Examples include InfluxDB and OpenTSDB. A conventional relational database can also store time-stamped records, but a time-series database may offer more suitable features and performance for large, time-oriented workloads.


Data Warehouses and Analytics

A data warehouse stores and organizes data for analytical queries and reporting, rather than for the routine, transaction-by-transaction operation of an application.

A company may use analytical systems to answer questions such as:

  • How many orders were placed?

  • Which regions produced the most revenue?

  • Which products were most frequently purchased?

Data for analysis may be collected from multiple transactional systems and transformed or organized for reporting. Analytical queries often examine large amounts of historical data and may differ substantially from the short, frequent reads and writes handled by an application’s transactional database.

Hadoop is an example of a distributed data-processing ecosystem that has been used for large-scale storage and analytics. It is more accurate to view Hadoop as an ecosystem for distributed data storage and processing than as a single data warehouse product. The appropriate analytics platform depends on the workload and system architecture.


Relational and Non-Relational Databases

Structured data

Structured data can be modeled naturally as tables with rows and columns. A user profile, for example, might contain fields such as name, email address, city, and phone number.

Relational databases are often a natural fit for well-structured data, particularly when relationships, constraints, and transactions matter. Structured data can also be represented in non-relational databases, so structure alone does not determine the choice.

Transactions and ACID guarantees

A key question is whether the application needs atomicity and transactional guarantees.

Consider transferring money from account A to account B. The operation involves both debiting one account and crediting the other. The system must not leave the transfer half-completed. The two changes should behave as a single transaction: either both take effect or neither does.

This is an example of atomicity, the “all or nothing” property of a transaction. The application may also require consistent reads so that, after a successful transaction, a subsequent balance lookup reflects the completed change.

The ACID properties describe common transactional guarantees:

  • Atomicity: A transaction’s operations succeed or fail as a unit.

  • Consistency: A transaction preserves the database’s defined rules and constraints.

  • Isolation: Concurrent transactions are controlled so that their interactions do not produce invalid outcomes.

  • Durability: Committed changes persist despite failures, subject to the database’s guarantees.

Relational databases such as MySQL, PostgreSQL, Oracle Database, and Microsoft SQL Server are common choices for workloads requiring strong transactional behavior. Other databases can also provide transactional guarantees; the important consideration is whether the selected system supports the guarantees the application needs.

Transactional consistency is particularly important in cases such as:

  • Payments: Money must not be debited without being credited to the recipient.

  • Inventory: Concurrent purchases must not reduce stock below the available quantity.

When transactions are not central

If the data is structured but does not require multi-record transactions or strict transactional guarantees, a relational database may still be appropriate. A non-relational database may also work, depending on the access patterns, scale, and data model.

The choice should be driven by requirements rather than by the label “SQL” or “NoSQL” alone.


Flexible Data and Document Databases

Some data has a wide variety of attributes, and different records may have different fields. An e-commerce catalog illustrates this:

  • A shirt may have a size and color.

  • A refrigerator may have a volume and an energy rating.

  • Milk may have a quantity and an expiry date.

Representing flexible attributes as JSON is possible, including in some relational databases. However, applications may need to query and filter on many changing attributes. A document database is designed to store flexible, document-shaped records and can be a good fit when the data model and query requirements support it.

Examples include MongoDB and Couchbase. Search engines such as Elasticsearch and Solr also work with document-like records, but they are specialized for search rather than being interchangeable with every document database.

A document database is often considered when:

  • Records have many attributes or attributes vary by record.

  • The schema changes frequently.

  • Queries need to filter or retrieve records using those flexible attributes.

If the application has only a few straightforward queries, a document database may still be usable, but its flexible model is not automatically necessary. Relational and document databases can both represent many common application data models.


High-Volume, Growing Data and Wide-Column Databases

Some systems produce a very large and continually growing volume of records. A ride-sharing service, for example, may receive location pings from many drivers. The number of records can grow as the number of drivers and the amount of activity increase.

When a workload has a high write volume, large data growth, and a limited set of predictable queries, a wide-column database may be appropriate. Wide-column databases organize data around partitioning and access patterns, and are designed to distribute large workloads across machines.

Examples include Apache Cassandra and Apache HBase. Cassandra is often selected for its distributed architecture and operational characteristics; HBase is commonly associated with the Hadoop ecosystem. Their suitability depends on details such as data modeling, consistency needs, deployment, and operational constraints.

A key design principle for these systems is to model data around the queries the application must support. For example, if the primary query is “retrieve the location records for this driver,” the data model should make that lookup efficient.

The term wide-column database is more precise here than “columnar database.” Columnar analytical databases, which store data by columns for analytical scans, are a distinct category.


Choosing a Database by Workload

A useful decision process is to start with requirements rather than with a product name:

  1. Identify the data shape. Determine whether records are fixed and structured, flexible and document-like, or primarily time-stamped events.

  2. List the important queries. Identify the fields used for lookup, filtering, search, reporting, and range scans.

  3. Determine the write and read patterns. Establish whether writes are transactional, append-oriented, highly concurrent, or distributed.

  4. Specify the guarantees. Decide whether the application needs ACID transactions, strong consistency, durable storage, or other guarantees.

  5. Estimate scale and growth. Consider data volume, request rate, and how quickly each is expected to increase.

  6. Select a store—or stores—that fit the workload. Different parts of a system may require different technologies.

If the system has modest scale, a small data set, and simple access patterns, several database types may be adequate. If it has specialized requirements—such as text search, geographic distribution of files, time-range analytics, or strict inventory transactions—a system designed for that workload is more likely to meet its performance and reliability goals.


Combining Databases in One System

Real-world applications often use multiple databases because different data and queries need different guarantees.

E-commerce inventory and order history

An e-commerce system may need to prevent overselling. If only one item remains and several customers try to buy it at the same time, inventory updates must be coordinated so that only a valid purchase can succeed. A transactional relational database can be a suitable choice for this inventory operation.

The system may also need to retain a large and continually growing order history. That history can have different scale and access requirements from active inventory. A distributed store such as Cassandra may be considered for suitable high-volume workloads, provided the data model and consistency requirements fit.

The division of data should be based on access needs and guarantees. Moving delivered orders from one system to another can be part of an architecture, but it requires a reliable transfer process, a clear source of truth, and a plan for failures; it is not automatically required for every design.

Orders and flexible reporting queries

Reporting questions can require access patterns that differ from those used to create and retrieve orders. For example, a business may want to find users who bought a particular kind of product during a particular period. Product categories and attributes may vary, and the query may span many orders.

One approach is to maintain a query-oriented representation in a document database or another suitable index. That representation might include user IDs, order IDs, dates, item IDs, and quantities. The reporting system can use it to identify matching users or orders, then retrieve authoritative order details from the system of record when needed.

This illustrates an important design pattern:

  • Keep authoritative transactional data in a store suited to transactions.

  • Maintain additional indexes or derived representations for specialized queries.

  • Ensure those representations can be updated, reconciled, or rebuilt when needed.

Using multiple systems can meet different requirements, but it also adds operational complexity. Data synchronization, consistency between stores, failure handling, and ownership of the authoritative record must all be designed explicitly.


Key Principles

  • Database choice primarily affects non-functional characteristics such as scale, latency, and reliability; it is not determined by functional requirements alone.

  • The most useful starting points are data structure, query pattern, scale, and required guarantees.

  • Use caches to reduce repeated work, object storage for files, CDNs for geographically efficient delivery, search engines for text retrieval, and time-series systems for time-oriented metrics when those workloads call for them.

  • Use transactional storage when operations must succeed or fail together, as in payments or tightly controlled inventory.

  • Use flexible or distributed data stores when their data model and access patterns better match the workload.

  • A search index, cache, or analytics representation is often a derived view rather than the authoritative source of application data.

  • A database combination is common in production systems, but each additional store introduces synchronization and operational responsibilities.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent