Objectives

Upon completion of this lesson, you will be able to:

  • explain partitioning and sharding
  • contrast partitioning and sharding
  • know when to use each

Overview

Partitioning and sharding are two fragmentation techniques used in database design to improve performance, manageability, and scalability. While they may seem similar, they are different in terms of how they distribute data across multiple database instances or storage units. This lesson provides an explanation of each fragmentation approach and how they can help make large databases more efficient.

Partitioning

Partitioning is a technique used to divide a large table into smaller, more manageable pieces called partitions. Each partition is a subset of the data that shares a common attribute, such as a specific date range, location, or customer type. The partitions are typically stored together, and each partition can be accessed and maintained independently of the others. The goal of partitioning is to improve query performance by allowing the database to read or write data from a single, smaller partition instead of scanning the entire table.

There two common types of partitioning: horizontal and vertical. Of course, in practice, both techniques are often employed at the same time.

Horizontal partitioning (Row-Level): This involves dividing a table into multiple smaller tables, each containing a subset of rows based on a specific attribute or range. For example, a table with sales data could be partitioned by year or region.

Vertical partitioning (Column-Level): This involves dividing a table into smaller tables, each containing a subset of columns. For example, a table with user data could be partitioned so that one table contains user account information, and another table contains user preferences.

The main benefits of partitioning are improved performance, more efficient maintenance, and simpler data management. For example, queries that target only a subset of data can run faster because they operate on a smaller set of data. Administrative tasks like backups, index rebuilds, and data purging can be performed on individual partitions rather than the entire table. Lastly, partitions are easier to manage and archive.

Partitioning is often used in IoT devices and applications, such as when IoT applications generate large amounts of sensor data. For example, you can partition the data based on sensor type, location, or time range.

Sharding

Sharding is a technique used to distribute data across multiple database instances or nodes, typically to scale horizontally and improve performance. Unlike partitioning, where data is split within a single database, sharding spreads data across multiple databases, potentially reducing the load on any single server.

Each shard, or distributed partition, contains a unique subset of the data and operates independently of the others. Sharding helps distribute the load across multiple servers, making it possible to handle larger amounts of data and traffic without overloading a single server.

Sharding is also known as horizontal scaling, so, if a database table gets populated every day and is growing beyond a processable size and queries become slow and indexes are costly to maintain, then the table is split horizontally into multiple shards based on insertion time which prevents a single table from becoming too big.

In sharding, a key called the shard key is used to determine how the data is distributed among the shards. The shard key can be a specific column, such as a user ID, or a composite key made up of multiple columns. The choice of a shard key is crucial to ensure data distribution is uniform and minimizes the risk of hotspots, where a single shard is overwhelmed with traffic. This can help with scalability and support very large datasets and high transaction volumes by distributing the load across multiple servers. In addition, sharding reduces the load on individual servers and can lead to faster query response times, as each shard handles only a portion of the total data. Lastly, in the event of a server failure, only the shard on that server is affected, not the entire database, increasing fault tolerance.

Partitioning vs. Sharding

Partitioning and sharding as fragmentation approaches differ in several ways:

  1. Splitting: Partitioning is done within a single database or server, whereas sharding spreads data across multiple databases or servers.
  2. Scalability: Sharding is more scalable than partitioning as it distributed the data across database servers.
  3. Management Complexity: Sharding can be more complex to implement and manage, as it involves multiple database instances and potentially different physical locations. Partitioning is generally easier to manage data with, since all partitions are in one database instance.
  4. Availability: Sharding has higher availability than partitioning as failure of one database server does not take down the other servers.
  5. Use Case Specificity: Partitioning is often used to improve performance and manage large tables within a database, while sharding is used for very large-scale databases where partitioning alone is insufficient to handle the data volume and performance requirements.

Fragmentation

Both partitioning and sharding fragment the database by splitting a table into multiple smaller tables that are more easily managed and, in the case of sharding, distributed.

Fragmentation of a relation \(R\) is a splitting of the relation into two or more fragments \(R_1, R_2, …, R_n\), such that the original relation can be reconstructed from its fragments. The fragmentation can be vertical (column-level) or horizontal (row-level). The diagram below illustrates the two fragmentation approaches.

Horizontal Fragmentation: Horizontal fragmentation involves breaking down a relation \(R\) by allocating each tuple of \(R\) to two or more fragments. In this type of fragmentation, each fragment comprises a subset of the tuples from the original relation. Because every tuple in relation \(R\) is part of at least one fragment, the original relation can be reconstructed.

Typically, a horizontal fragment is created based on n selections applied to relation \(R\). This means that the tuples belonging to a horizontal fragment are determined by a condition applied to one or more attributes of the relation. For example, a fragment \(R_i\) can be constructed using the following criteria:

\(R_i = \sigma_{condition_i}(R)\)

The original relation \(R\) can be reconstructed by creating a union from all n fragments, i.e.,

\(R=\bigcup\limits_{i=1}^{n} (R_i)\)

Vertical Fragmentation: Vertical fragmentation involves decomposing the schema of a relation \(R\) into n projections. A vertical fragment is defined as a projection on relation \(R\), represented as follows:

\(R_i = \pi_{R_i}(R)\)

The fragmentation must be structured in a way that allows the original relation to be reconstructed by performing a natural join on the fragments:

\(R = R_1 \bowtie R_2 \bowtie \ldots \bowtie R_n\)

To ensure the successful reconstruction of a relation from the fragments, one of the following options can be employed:

  1. Include key attributes of \(R\) in each fragment \(R_i\).
  2. Attach a unique tuple ID to each tuple in the original relation, which is then associated with each vertical fragment.

Hybrid Fragmentation: Hybrid (or mixed) fragmentation involves the application of both horizontal and vertical fragmentation to a single schema. This means that the fragments obtained from horizontally fragmenting a relation can be further divided vertically, or vice versa. This combination of horizontal and vertical fragmentation is referred to as hybrid or mixed fragmentation. The original relation is reconstructed through a combination of join and union operations on the fragment relations.

Sharding Approaches

Sharding is a method used to distribute data across multiple databases, and the approach can vary based on several factors such as application needs, data structure, system architecture, geographic considerations, and scalability objectives. Below are four principal types of sharding, although the same principles are applicable to partitioning, albeit that the splits all reside within the same database.

Range-Based Sharding

This type involves segmenting data into shards based on specific ranges, such as dates, numerical values, or alphanumeric sequences. It is ideal for data that naturally orders itself and for queries targeting specific intervals. For example, an e-commerce platform might implement range-based sharding to sort orders by date.

Advantages: - Optimizes range queries with orderly data distribution. - Simplifies data archiving and purging by removing whole shards. - Well-suited for time-series and historical data.

Challenges: - Potential for uneven shard sizes due to irregular data distribution. - Difficulties with skewed data distributions. - Limited flexibility for non-uniform data access patterns.

Hash-Based Sharding

In this approach, data is assigned to shards using a hash function that maps certain attributes to a shard identifier. This method is commonly used when data lacks natural order or requires uniform distribution, as seen in the Hazelcast Platform.

Advantages: - Ensures even data distribution, preventing load imbalances. - Effective where data order is irrelevant. - Scalable and straightforward to implement.

Challenges: - Complicates the retrieval of specific data ranges. - Shard rebalancing becomes complex as data grows. - Reshuffling data is necessary when adding or removing shards.

Directory-Based Sharding

Directory-based sharding is also known as metadata-based sharding, this method uses a separate service or a metadata repository to track the mapping of data to shards. Each data item is associated with metadata that indicates its shard location, allowing for flexible data distribution based on various criteria.

Advantages: - Highly adaptable to specific distribution needs. - Facilitates shard management and rebalancing. - Accommodates dynamic adjustments in data distribution rules.

Challenges: - Increased complexity due to the additional metadata service. - Performance may suffer from metadata lookups. - Metadata service can become a single point of failure.

Geo-Based Sharding

This strategy segments data by geographic location to bring data closer to users, minimizing latency. It is especially relevant for distributed systems and applications with a global user base, such as CDNs and worldwide services.

Advantages: - Reduces latency and enhances performance for global applications. - Efficiently handles geospatial queries and location-based services. - Provides geographic redundancy for better disaster recovery and fault tolerance.

Challenges: - Implementation complexity due to geographical data considerations. - Maintaining consistent data across regions can be difficult. - Vulnerable to shifts in user distribution and access patterns.

Each sharding type has unique benefits and challenges, making the choice of sharding strategy critical based on the specific requirements of the application and data environment.

Implemention of Sharding

Implementing sharding in a data management system requires careful planning and execution across several phases:

  1. Data Modeling
    • Objective: Identify the sharding key, which is the attribute or combination of attributes that determines how data is distributed across shards. The selection of an effective sharding key is crucial as it significantly impacts the system’s performance and the evenness of data distribution.
  2. Shard Creation
    • Process: Establish and set up the shards where the data will be allocated. Shards can be configured on various platforms, such as physical servers, virtual machines, or containers, depending on the underlying system architecture.
  3. Data Migration
    • Implementation: Transition the existing data to the shards according to the designated sharding key. Utilizing data migration tools and scripts can facilitate and streamline this step.
  4. Query Routing
    • Mechanism: Develop a system for directing queries and transactions to the correct shard based on the sharding key. This typically requires a middleware layer that manages the routing of queries to ensure they reach the appropriate shard.
  5. Shard Management
    • Strategies: Set up procedures and tools for ongoing shard management. This includes tasks such as adding new shards, removing existing ones, rebalancing data across shards, and managing failures within individual shards.
  6. Monitoring and Maintenance
    • Routine: Establish continuous monitoring and maintenance protocols to ensure the sharded database operates smoothly and efficiently. Focus on monitoring indicators like uneven shard sizes, high query latencies, and potential hardware failures to preemptively address issues.

The above strategies help in the implementation of sharding to improve the scalability, performance, and manageability of very large databases.

Sharding Use Cases

Sharding is a commonly deployed strategy employed various industries and applications to enhance scalability and improve performance. Below are some use cases where sharding plays a pivotal role:

  1. Social Media Platforms
    • Use Case: Social media networks utilize sharding to efficiently handle vast quantities of user-generated content, such as posts, images, and videos. This approach ensures rapid data access and maintains high system availability, crucial for user satisfaction and engagement.
  2. E-commerce
    • Use Case: E-commerce sites deploy sharding to manage extensive product catalogs and support high traffic volumes, particularly during peak shopping periods. Sharding is vital for efficiently processing orders and managing inventory across different regions.
  3. Online Gaming
    • Use Case: Online gaming platforms implement sharding to distribute game state information and player data. This method is essential for providing low-latency experiences in multiplayer games that have a global player base.
  4. Financial Services
    • Use Case: Financial institutions use sharding to handle enormous volumes of transactions, customer information, and financial records. By sharding their databases, these institutions can boost performance and enhance data security, critical for regulatory compliance and customer trust.
  5. Content Delivery Networks (CDNs)
    • Use Case: CDNs apply geo-based sharding to optimize the storage and delivery of web content. By distributing data across edge servers located near users, CDNs can significantly reduce latency and speed up content delivery, improving the overall user experience.
  6. IoT and Telemetry
    • Use Case: Platforms catering to the Internet of Things (IoT) utilize sharding to manage the continuous stream of data from a myriad of sensors and devices. This setup facilitates the real-time processing and analysis of telemetry data, enabling more responsive and intelligent IoT applications.

Sharding is essential in scenarios requiring high availability, low latency, and a high-degree of scalability. It is a key design pattern for modern digital data infrastructures.

Summary

In summary, partitioning is primarily focused on breaking up large tables into smaller, more manageable subsets of data to improve query performance, while sharding is focused on distributing data across multiple database instances or nodes to improve overall performance and scalability. Both techniques can be used together in some cases to achieve optimal performance and manageability.


Files & Resources

All Files for Lesson 60.732

References

None.

Errata

Let us know.