Understanding Horizontal Scalability

Definition and Importance of Horizontal Scalability

Horizontal scalability, or scale-out, refers to the ability of a database system to handle increased loads by adding new nodes to the system. Unlike vertical scalability, which involves augmenting the capacity of a single machine (e.g., adding more CPU or memory), horizontal scalability distributes the load across multiple machines, each handling a portion of the workload.

In today’s data-driven world, the importance of horizontal scalability cannot be overstated. Organizations are dealing with ever-growing datasets and user bases, which demand databases that can efficiently handle large volumes of transactions and queries without compromising on performance. Horizontal scalability ensures that systems can grow seamlessly as the demands increase, maintaining performance consistency and supporting business growth.

An illustration comparing vertical scalability (single machine upgrade) vs. horizontal scalability (adding multiple nodes).

Comparison: Horizontal vs. Vertical Scalability

Vertical scalability might be simpler to implement since it involves upgrading the existing hardware. However, it comes with inherent limitations. The maximum performance boost achievable is constrained by the physical limits and technological advancements of a single machine. Moreover, increasing the capabilities of a single node can be costly, as high-end hardware components and systems are expensive.

On the other hand, horizontal scalability offers flexibility and cost efficiency as it allows organizations to scale their systems incrementally. By adding commodity hardware, businesses can achieve a distributed system that not only handles the increased load but also offers redundancy and fault tolerance. This distributed nature also means that failures in individual nodes do not necessarily lead to system-wide failures, enhancing the overall availability and reliability of the database system.

Key Benefits of Achieving Horizontal Scalability in Databases

  • Cost Efficiency: Unlike vertical scaling that requires expensive hardware upgrades, horizontal scaling leverages less costly commodity hardware, providing a more economical path to scalability.
  • Enhanced Performance: With the ability to distribute workloads across multiple nodes, horizontally scalable databases can handle higher volumes of transactions and queries without performance degradation.
  • Fault Tolerance and Availability: Horizontal scalability inherently supports redundancy. Data replication across nodes ensures that the system remains available even if some nodes fail.
  • Elasticity: Horizontal scaling provides the ability to dynamically adjust the number of nodes in the system based on current demands, ensuring resources are optimized and costs are minimized.

Horizontal scalability is pivotal for databases that serve large-scale applications with high throughput and low latency requirements. TiDB, with its innovative architecture, exemplifies a system designed to leverage horizontal scalability effectively.

For a deeper dive into horizontal scalability, refer to the TiDB Best Practices guide.

Techniques for Achieving Horizontal Scalability with TiDB

Distributed SQL Execution

TiDB employs a distributed SQL execution engine, which allows SQL queries to be processed across multiple nodes efficiently. The key concept here is the separation of compute and storage layers, enabling independent scaling of both components.

TiDB uses a layered architecture where SQL queries are first processed by TiDB servers (compute nodes). These nodes parse, plan, and distribute the execution of SQL queries to TiKV (storage nodes), which store the actual data. This separation provides the flexibility to scale out compute and storage resources independently.

Example

Here’s a sample SQL execution flow in a distributed TiDB cluster:

  1. Client Submission: A client submits a SQL query to a TiDB server.
  2. Parsing and Planning: The TiDB server parses and plans the query, breaking it down into smaller tasks.
  3. Task Distribution: The server distributes these tasks to multiple TiKV nodes for execution.
  4. Result Aggregation: TiKV nodes execute the tasks and return results to the TiDB server, which then aggregates and sends the final result back to the client.
-- Sample Query
SELECT user_name, email FROM users WHERE age > 25;

In this case, the TiDB server would parse the query and determine the relevant partitions in the TiKV storage nodes that contain the users table data. It then distributes the task to retrieve data across multiple nodes, resulting in efficient query execution.

A diagram illustrating the distributed SQL execution flow in a TiDB cluster, from client submission to result aggregation.

Sharding and Data Partitioning

TiDB leverages automatic sharding and partitioning to distribute data across multiple nodes. Each table is broken down into smaller chunks called regions, each of which can be managed independently on different nodes. This strategy ensures that data access patterns do not lead to bottlenecks.

Regions are dynamically split and merged based on data size and access patterns, maintaining optimal performance.

Practical Example

  1. Create Table:

    CREATE TABLE IF NOT EXISTS orders (
    order_id BIGINT AUTO_INCREMENT PRIMARY KEY,
    customer_id BIGINT,
    order_total DECIMAL(10,2),
    order_date DATETIME
    ) SHARD_ROW_ID_BITS = 4 PRE_SPLIT_REGIONS = 3;

In this example, SHARD_ROW_ID_BITS = 4 means the data will be split into 16 shards. PRE_SPLIT_REGIONS = 3 ensures that the table is pre-split into 8 regions at creation.

  1. Splitting Regions:

    SPLIT TABLE orders BETWEEN (0) AND (9223372036854775807) REGIONS 128;

This splits the orders table into 128 regions, spreading the data storage across multiple TiKV nodes for balanced load distribution and improved query performance.

Load Balancing and Traffic Management

Load balancing is crucial in a distributed system to ensure that no single node becomes a performance bottleneck. TiDB’s Placement Driver (PD) component is responsible for load balancing by dynamically adjusting the distribution of regions among TiKV nodes based on their current workloads.

Key Concepts

  • Region Leaders: PD monitors region leaders and moves them across nodes to balance the load.
  • Resource Metrics: PD uses metrics like CPU usage, disk I/O, and network throughput to make informed decisions about where to place regions.

For more insights, explore how TiDB does Load Balancing.

Replica Management and Consistency

TiDB ensures strong consistency through Raft-based replication. Each region has multiple replicas, distributed across different nodes. The Raft consensus algorithm ensures that a majority of replicas must agree on changes before they are committed.

Benefits

  • Fault Tolerance: Data remains available even if some replicas fail.
  • Data Durability: With multiple replicas, the risk of data loss is minimized.

Example of configuring replication in TiDB:

[raftstore]
max-leader-lease = "9s"
log-wl-batch-interval = "100ms"

Elastic Scaling: Adding and Removing Nodes Dynamically

TiDB’s architecture supports elastic scaling, allowing nodes to be added or removed without significant downtime. This provides agility in resource management, ensuring that the system can adjust to varying loads seamlessly.

Example of Adding a Node

Using TiUP, operators can add a new TiKV node with the following command:

tiup cluster scale-out <cluster-name> --topology=scale-out.yaml

Example YAML File

tidb_servers:
  - host: 10.0.1.1
tikv_servers:
  - host: 10.0.1.2
pd_servers:
  - host: 10.0.1.3
monitoring_servers:
  - host: 10.0.1.4
grafana_servers:
  - host: 10.0.1.5

Challenges in Implementing Horizontal Scalability

Data Consistency and Partitioning Issues

One of the significant challenges in horizontally scalable systems is ensuring data consistency across distributed partitions. TiDB addresses this with the Raft protocol, which facilitates consensus among replicas. However, partitioning strategies need careful design to avoid hotspots and ensure even data distribution.

Network Latency and Throughput

Distributing data across multiple nodes inevitably introduces network latency. Optimizing network throughput and minimizing latency is crucial for maintaining performance. TiDB achieves this through efficient data distribution and replication strategies, but designers must be aware of potential network bottlenecks.

Maintaining High Availability

High availability is a core requirement for distributed databases. TiDB’s multi-raft replication ensures that data remains accessible even if some nodes fail. However, the complexity of the system increases with scale, necessitating robust monitoring and failover mechanisms.

Balancing Load Effectively

Effective load balancing requires dynamic adjustments based on real-time metrics. TiDB’s PD component plays a vital role, but administrators need to continually monitor and optimize configurations to address changing workloads and prevent bottlenecks.

Monitoring and Troubleshooting at Scale

Scaling out a database introduces challenges in monitoring and troubleshooting. TiDB’s integration with Grafana and Prometheus offers comprehensive insights, but managing and interpreting large volumes of monitoring data requires expertise and tools designed for big data environments.

Conclusion

Achieving horizontal scalability with TiDB involves leveraging its distributed SQL execution, sharding mechanisms, dynamic load balancing, robust replication, and elastic scaling capabilities. While challenges such as data consistency, network latency, and high availability must be addressed, TiDB provides a robust framework for building highly scalable and resilient database systems.

For more detailed insights and practices, explore the Highly Concurrent Write Best Practices guide.


Last updated September 14, 2024

Experience modern data infrastructure firsthand.

Try TiDB Serverless