Skip to content
Mar 1

System Design Database Selection

MT
Mindli Team

AI-Generated Content

System Design Database Selection

Choosing the right database is one of the most critical decisions in system design, directly determining your application's performance, reliability, and future scalability. It's not about finding a single "best" database, but about matching the technology's strengths to your specific data patterns, access requirements, and business goals. This process requires a deep understanding of the fundamental trade-offs between consistency, availability, and data structure.

Understanding Core Data Models

The data model—how information is structured and stored—is the primary filter for database selection. Each model is optimized for different access patterns and relationships.

Relational databases (e.g., PostgreSQL, MySQL) organize data into tables of rows and columns with predefined schemas. They excel at handling complex queries involving joins across multiple tables and are the default choice for transactional systems where data integrity is paramount. Their strength lies in ACID compliance (Atomicity, Consistency, Isolation, Durability), a set of properties that guarantee database transactions are processed reliably.

Document stores (e.g., MongoDB, Couchbase) store data in flexible, semi-structured documents, typically in JSON or BSON format. This offers significant schema flexibility, allowing the structure of records to evolve over time without costly migrations. They are ideal for catalogues, user profiles, or content management systems where each entity is a self-contained document and relationships between documents are minimal.

Key-value stores (e.g., Redis, DynamoDB) are the simplest model, providing lightning-fast speed for operations that store and retrieve data using a unique key. They are essentially highly optimized hash tables and are perfect for caching session data, user preferences, or real-time leaderboards. Their performance comes at the cost of limited query capabilities; you can only fetch data by its primary key.

Graph databases (e.g., Neo4j, Amazon Neptune) are built to store and navigate relationships. They use nodes (entities), edges (relationships), and properties to model relationships explicitly. When your core queries involve traversing complex networks—such as social connections, recommendation engines, or fraud detection—graph databases can perform orders of magnitude faster than relational databases performing recursive joins.

The CAP Theorem and Its Trade-Offs

The CAP theorem is a fundamental principle that states a distributed database system can only simultaneously provide two out of three guarantees: Consistency, Availability, and Partition Tolerance. Since network partitions (P) are an unavoidable reality in distributed systems, you are often making a conscious choice between Consistency (C) and Availability (A).

  • Consistency (C): Every read receives the most recent write. Every user sees the same data at the same time, regardless of which node they query.
  • Availability (A): Every request receives a (non-error) response, even if some nodes are down or partitioned, though the data may not be the most recent.
  • Partition Tolerance (P): The system continues to operate despite arbitrary network partitions that prevent communication between nodes.

A CP database (like MongoDB in certain configurations, or HBase) chooses consistency over availability. If a network partition occurs, the system will return an error or time out to ensure data is not read inconsistently. An AP database (like Cassandra, DynamoDB) chooses availability over strict consistency. During a partition, it will remain operational and return the most recent available data, even if it is stale, accepting eventual consistency. Understanding this trade-off is essential for selecting a database that aligns with your application's tolerance for stale data versus its need for constant uptime.

Consistency Models and Query Patterns

Beyond the CAP theorem's binary choice, databases implement specific consistency models that define the guarantees around data recency. Strong consistency is the ACID model, where a read always sees the latest write. Eventual consistency guarantees that if no new updates are made, all reads will eventually return the same last updated value. This model enables higher availability and lower latency.

Your query patterns—how you read and write data—must dictate your choice. You should analyze:

  1. Read/Write Ratio: Is the workload read-heavy (e.g., a news site), write-heavy (e.g., IoT sensor logging), or balanced?
  2. Access Patterns: Do you query by a primary key, a range of keys, or by attributes (secondary indexes)? Do you need complex aggregations or joins?
  3. Latency Requirements: What are the p95 or p99 latency requirements for your core queries?

For example, a relational database with strong secondary indexing is excellent for complex, ad-hoc queries. A key-value store is unmatched for simple primary-key lookups with microsecond latency. A columnar store (like BigQuery) is optimized for scanning vast amounts of data for analytical queries. Your database should be shaped by your most frequent and performance-critical access paths.

Scaling Strategies: Vertical vs. Horizontal

Scalability needs dictate whether you prioritize vertical or horizontal scaling architectures. Vertical scaling (scaling up) involves adding more power (CPU, RAM) to a single server. Traditional relational databases often scale vertically. It's simpler but hits a physical and financial ceiling.

Horizontal scaling (scaling out) involves adding more servers to a pool. NoSQL databases like Cassandra and DynamoDB are designed from the ground up for horizontal scaling. They distribute data across a cluster using techniques like sharding (partitioning data based on a key). While this offers near-limitless scale, it introduces complexity in managing data distribution, consistency, and cross-shard queries. Your growth projections and data volume should guide whether you need a database built for horizontal scale from day one.

Common Pitfalls

  1. Defaulting to a Relational Database for Everything: This is the most common mistake. Using an RDBMS for a social graph or a high-volume session cache leads to poor performance, complex sharding, and unnecessary costs. Let the data model and access patterns drive the choice.
  2. Ignoring Operational Complexity: A cutting-edge database may fit your technical requirements but could have a steep learning curve, poor tooling, or scarce hiring prospects. Evaluate the ecosystem and total cost of ownership, not just the features.
  3. Over-Engineering for Scale: Designing for Facebook-scale when you are building an MVP is wasteful. Start with a simple, well-understood technology (often a relational database) that can solve your immediate problem. Plan for, but do not prematurely implement, complex scaling strategies. You can often decouple components and introduce specialized databases (like a Redis cache) later as scaling needs become concrete.
  4. Misunderstanding "Eventual Consistency": Assuming eventual consistency is "good enough" without analyzing the business impact. For a shopping cart, showing a temporarily out-of-stock item (stale read) might be acceptable; deducting the wrong amount from a bank account is not. You must define what "eventual" means for your use case and whether users will tolerate the potential anomalies.

Summary

  • Database selection is a strategic matching process, aligning technology strengths to your specific data model, consistency requirements, query patterns, and scalability needs.
  • Relational databases provide strong ACID compliance and are ideal for complex, transactional data with rigid relationships.
  • Document stores offer schema flexibility for evolving, self-contained entities, while key-value stores deliver extreme speed for simple key-based access.
  • Graph databases are purpose-built to efficiently model relationships and traverse networks.
  • The CAP theorem forces a fundamental trade-off in distributed systems, guiding you to choose between strong Consistency and high Availability when network partitions occur, a decision that must be made based on your application's core requirements.

Write better notes with AI

Mindli helps you capture, organize, and master any subject with AI-powered summaries and flashcards.