System Design Guide

Database Connection Pooling: Managing Resources Efficiently

Connection pooling is a technique for managing database connections by maintaining a pool of reusable connections that can be shared across multiple requests. Rather than creating a new connection for each database operation and tearing it down afterward, applications reuse connections from the pool, dramatically improving performance and resource utilization.

The Cost of Database Connections

Establishing a database connection is expensive. It involves TCP handshake, authentication, session initialization, and allocating server resources. This overhead might take 50-100ms or more, particularly for SSL-encrypted connections or remote databases. For high-throughput applications handling thousands of requests per second, creating connections for each request is prohibitively expensive.

Database Indexing: Optimizing Query Performance

Database indexes are specialized data structures that dramatically improve query performance by allowing the database to locate data without scanning entire tables. Understanding how indexes work, when to use them, and their tradeoffs is fundamental to building performant database-backed applications.

How Indexes Work

Without an index, a database must perform a full table scan to find matching rows: examining every row in the table sequentially. For a table with millions of rows, this is prohibitively slow. An index provides a sorted structure that enables efficient lookups, similar to how a book’s index lets you find topics without reading every page.

Database Replication: High Availability and Read Scaling

Database replication involves maintaining copies of a database across multiple servers, with changes to one copy automatically propagated to others. This fundamental technique serves two primary purposes: providing high availability through redundancy and enabling read scaling by distributing read queries across multiple servers.

Replication Models

Primary-Replica (Master-Slave) Replication is the most common model. One primary database accepts all writes, which are then replicated to one or more replica databases. Replicas serve read queries, distributing read load across multiple servers. This model is straightforward to implement and reason about, with a clear authority for data changes.

Database Sharding: Partitioning Data for Scale

Database sharding is a horizontal partitioning technique that splits a large database into smaller, more manageable pieces called shards. Each shard is a separate database that contains a subset of the total data, allowing the system to scale beyond the limitations of a single database server. While powerful, sharding introduces significant complexity and should be considered carefully.

Why Shard?

As data volume grows, a single database eventually becomes a bottleneck. Storage capacity, I/O throughput, CPU, and memory all have physical limits. Even powerful servers have ceilings, and vertical scaling becomes prohibitively expensive at extreme scales. Companies like Facebook, Twitter, and Instagram serve billions of users and petabytes of data, making single-database architectures impossible.

SQL vs NoSQL: Choosing the Right Database

The choice between SQL and NoSQL databases is one of the most fundamental decisions in system design. Both paradigms have their strengths, and understanding when to use each is crucial for building efficient, scalable applications. This isn’t about which is better in absolute terms, but rather which is better for your specific use case.

SQL Databases: Structured and Reliable

SQL (Structured Query Language) databases, also known as relational databases, organize data into tables with predefined schemas. Each table has rows representing records and columns representing attributes. Relationships between tables are established through foreign keys, enabling complex queries across multiple tables using joins.