Databases (SQL vs NoSQL) in System Designing
medium · System Designing
When entering the data tier of system design, the choice between SQL (Relational) and NoSQL (Non-Relational) is a foundational architectural decision. This choice determines how your data is stored, how it scales, and how much consistency you can guarantee during concurrent operations. 1. SQL (Relational Database Management Systems - RDBMS) SQL databases store data in highly structured tables consisting of rigid rows and columns. They rely on predefined schemas and formal relationships between tables using foreign keys. Core Characteristics: Structured Schema: You must explicitly define your tables, columns, and data types (e.g., VARCHAR , INT ) before inserting a single row. Altering this structure later requires a migration. ACID Compliance: This is the ultimate guarantee for data integrity. SQL databases natively enforce four critical principles: Atomicity: All operations in a transaction succeed, or the entire transaction is rolled back (All-or-Nothing). Consistency: A transaction can only transition the database from one valid state to another, maintaining all schema constraints. Isolation: Concurrent transactions execute without interfering with one another. Durability: Once a transaction is committed, its changes are permanently written to non-volatile disk storage. Complex Joins: Highly optimized to stitch data together across multiple tables on the fly using standard SQL queries. Examples: * PostgreSQL: Highly extensible, robust open-source database with enterprise-level indexing capabilities. MySQL: Ubiquitous, highly reliable, and powers a massive portion of standard web infrastructure. 2. NoSQL (Non-Relational Databases) NoSQL databases discard the rigid tabular model in favor of flexible data structures optimized for specific access patterns and extreme scale. Core Characteristics: Flexible Schema: Data can be structured as key-value pairs, wide-column graphs, or self-contained documents (like JSON). Different records within the same collection can have completely different structures. Horizontal Scaling: Designed from day one to scale out across a cluster of multiple physical machines. Instead of using complex JOIN logic, NoSQL typically stores related data together in a single record ( denormalization ), making distributed reads incredibly fast. BASE over ACID: Instead of enforcing immediate, strict consistency, many NoSQL databases lean into the BASE model: Basically Available: The system guarantees availability. Soft State: The state of the data can change over time without explicit user interaction due to background updates. Eventual Consistency: Data will eventually become consistent across all nodes, but a read immediately following a write might return slightly stale data. Examples: MongoDB (Document Store): Stores data as BSON (Binary JSON). Ideal for content management, user profiles, or rapidly changing data shapes. Cassandra (Wide-Column Store): Engineered by Facebook to handle massive write volumes across multiple data centers with zero single points of failure. 3. Structural Comparison Feature SQL Databases NoSQL Databases Data Model Tabular (Rows/Columns) Document, Key-Value, Graph, Wide-Column Relationships Enforced via Foreign Keys / Joins Denormalized (Nested data or references) Scaling Primarily Vertical (Scale Up) Primarily Horizontal (Scale Out) Transactions Strict ACID Guarantees BASE (Eventual Consistency standard) 4. How Scaling Differs The structural difference between SQL and NoSQL completely changes how they handle massive traffic loads. SQL Scaling Challenge: Because tables are linked via relational constraints, running a single JOIN query across tables that live on two different physical servers is highly inefficient. Therefore, SQL databases are typically scaled vertically by purchasing larger hardware. NoSQL Scaling Advantage: Because documents are self-contained and denormalized, they can easily be divided and distributed across separate machines using a technique called Sharding (partitioning data based on a shard key, like user_id ). 5. How to Choose: Decision Framework Choose SQL if: Your data is highly structured and relationships are complex. You require absolute data integrity and strict transaction safety (e.g., Financial ledger systems , payment processing, inventory tracking). Your query patterns are unpredictable, requiring ad-hoc analysis and heavy usage of analytical joins. Choose NoSQL if: You are dealing with massive volumes of data (terabytes/petabytes) requiring seamless horizontal scaling . Your data is unstructured or semi-structured (e.g., IoT sensor telemetry, product catalogs with varying attributes, social media feeds). Your access patterns are clean and predictable (e.g., retrieving a user profile directly by its ID key).