Network Hardening: Switch and Router Settings That Actually Matter for Business Networks

11 min read 2,136 words
Network Hardening: The Switch and Router Settings That Actually Matter for Business Networks featured image

When a MySQL database grows beyond what a single server can handle efficiently, sharding offers a way to distribute the load across multiple database instances. This approach to horizontal partitioning splits data by a key, such as a customer identifier or geographic region, so each shard manages only a portion of the total dataset.

Sharding is not a decision to make lightly. It introduces meaningful complexity into application architecture, backup procedures, and day-to-day operations. Understanding when sharding makes sense, how different sharding strategies work, and what the trade-offs involve helps you make a practical decision rather than reaching for a solution that may create more problems than it solves.

What horizontal partitioning means in practice

Horizontal partitioning divides a table into smaller segments, with each segment stored on a different database server. Unlike vertical partitioning, which splits columns into separate tables, horizontal partitioning keeps all the columns together but distributes the rows based on a shard key.

The shard key determines which shard stores a particular row. If you use customer ID as the shard key, rows with customer IDs 1 through 100,000 might live on one shard, while IDs 100,001 through 200,000 sit on another. The application code must determine the correct shard for each query before executing it.

This distribution allows each shard to operate with a smaller dataset, which can reduce query times and lower the storage demands on any single server. It also means you can scale horizontally by adding more shards as data grows.

Signs your database may be outgrowing a single server

Most MySQL deployments start on a single server and work well for a long time. Adding read replicas can reduce load for applications with more reads than writes. There comes a point, however, when a single server or replica setup is no longer sufficient.

Common indicators include database tables reaching tens of millions of rows or hundreds of gigabytes, where even indexed queries start to feel slow. Replication lag on read replicas can become noticeable when write volume is high. Storage costs on a single large server may exceed what makes financial sense for the application. In some cases, the server simply runs out of RAM for the working dataset, causing disk thrashing and degraded performance.

Before deciding on sharding, it is worth exhausting other options. Optimising slow queries, adding appropriate indexes, enabling query caching where suitable, and using read replicas for read-heavy workloads can extend the life of a single-server setup significantly. Vertical scaling, where you move to a server with more RAM, CPU, and faster storage, often costs less than managing multiple database servers.

Three common sharding approaches

Different sharding strategies suit different access patterns. Choosing the right one depends on how your application queries the data.

Key-based hashing

Key-based sharding applies a hash function to the shard key and uses the result to determine which shard stores the data. A typical implementation might take the customer ID, run it through a hash function, and then use modulo arithmetic against the number of shards to pick a destination.

This approach tends to distribute data fairly evenly across shards, which helps avoid hot spots where one shard accumulates far more data than others. The downside is that range queries become difficult. If you want to fetch all customers in a particular region or during a specific time window, the hash function provides no shortcut. The application may need to query every shard and combine the results.

Range-based sharding

Range-based sharding assigns data to shards based on value ranges of the shard key. For example, you might store customers from the United Kingdom on one shard, customers from Germany on another, and customers from France on a third.

This approach works naturally when your application frequently filters by the same column used as the shard key. Range queries become efficient because the application knows exactly which shard to check. The risk is uneven distribution. If most of your customers are in one region, that shard accumulates far more data than the others, creating exactly the bottleneck you were trying to avoid.

Directory-based sharding

Directory-based sharding maintains a lookup table that maps each shard key value to the correct shard. The application queries this directory first to find the right shard, then proceeds with the actual data query.

This approach offers flexibility. You can change which shard stores a particular key without affecting other keys. The trade-off is an additional dependency. The lookup table itself becomes a potential bottleneck and a single point of failure. If the directory service goes down, the application cannot route queries to the correct shards.

What changes in your application code

Sharding requires significant changes to application logic. Every database query must include logic to determine the correct shard before executing. Insert operations need to calculate the target shard based on the shard key. Update and delete operations must route to the correct shard for the affected rows.

Queries that span multiple shards need special handling. If a report requires data from every customer, the application must query each shard, retrieve the results, and combine them in code. This process is slower than a single query on a single server and may require pagination or streaming strategies to handle large result sets.

Transactions across shards are particularly challenging. MySQL supports transactions within a single server, but distributed transactions across multiple servers introduce complexity that most applications want to avoid. Some implementations handle this by ensuring that related data always lives on the same shard, which limits the need for cross-shard transactions.

Security and infrastructure considerations

Running multiple database servers means multiplied surface area for security and maintenance. Each server requires its own firewall rules, access controls, and regular updates. Network isolation between database servers and public-facing application servers becomes more important as the number of database endpoints grows.

Strong server hardening practices matter even more in a sharded environment. Limiting SSH access, using key-based authentication, and configuring firewall rules to permit only necessary traffic all contribute to a more secure setup. Resources on securing Ubuntu servers after installation and Ubuntu server hardening checklists for production use cover practical steps that apply whether you run one database server or ten.

Monitoring also becomes more complex. Each shard needs independent monitoring for disk usage, query performance, connection counts, and replication status where applicable. A centralised monitoring approach that aggregates metrics across all shards helps keep operational overhead manageable.

Backup and recovery complexity

Backing up a sharded database requires a strategy for each shard. You cannot run a single mysqldump command and capture everything. Each shard needs its own backup process, and the timing of those backups matters if you need consistent point-in-time recovery across all data.

Recovery procedures take longer when you need to restore multiple shards. If one shard fails, you restore that shard independently while the others continue serving traffic. The restore process for each shard includes replaying binary logs to bring data up to the desired point in time. Testing these restore procedures regularly is important because the complexity makes it easy to miss something until you actually need the backup.

When sharding is worth the complexity

Sharding makes sense when the alternatives have been exhausted and the operational complexity is justified by real performance or capacity requirements. Applications with very high write volumes that exceed what a single server can handle may benefit from distributing writes across shards. Datasets that genuinely require more storage than a single server can provide are another genuine use case.

Multi-tenant applications where each tenant's data can be isolated to a single shard may find sharding aligns well with their architecture. Analytics systems that need to process large volumes of time-series data might also benefit from range-based sharding on date columns.

When to look at simpler alternatives first

Many database performance problems do not require sharding. Query optimisation often reveals missing indexes, inefficient joins, or queries that fetch more data than needed. Read replicas handle read-heavy workloads effectively in most cases. Vertical scaling may cost less than managing a sharded infrastructure.

Caching frequently accessed data with Redis or Memcached reduces database load without any partitioning complexity. Vertical partitioning, where you move certain tables to separate servers without distributing individual tables, can reduce contention on a single server and is simpler to implement than full sharding.

If you have not already optimised your queries, added appropriate indexes, and tested whether read replicas or caching solve the problem, sharding is premature. The complexity it introduces should be reserved for situations where simpler solutions genuinely do not fit.

Steps to take before implementing sharding

If you decide sharding is necessary, a careful approach reduces risk during migration.

  1. Analyse your data access patterns: Review which queries run most frequently, which tables are largest, and which columns appear most often in WHERE clauses and JOIN conditions.
  2. Choose the shard key carefully: The shard key should align with the most common query patterns. Changing the shard key after data is distributed is expensive and disruptive.
  3. Test data distribution: Before going live, verify that data will distribute evenly across shards. Uneven distribution leads to hot spots that undermine the benefits of sharding.
  4. Plan the migration: Decide how to move existing data to sharded tables. A phased approach, where new data goes to shards while existing data migrates in batches, reduces risk compared to a big-bang migration.
  5. Test cross-shard queries: Verify that reports and analytics that span multiple shards perform acceptably in your staging environment.
  6. Document the routing logic: The code that determines which shard to use for each query becomes critical infrastructure. Clear documentation helps when you need to debug issues or onboard new team members.

Long-term operational realities

Once sharding is in place, certain operational tasks become ongoing responsibilities. Schema changes require careful planning because modifying a table means updating every shard. Adding a new index involves running ALTER TABLE on each shard, which can lock tables and consume significant resources.

Resharding, where you need to add more shards because data has grown beyond the initial distribution, is one of the most challenging operations in a sharded system. Moving data from existing shards to new shards while the application continues to serve traffic requires careful choreography. Some teams avoid this by over-sharding initially, building more shards than currently needed to leave room for growth.

Each shard operates as an independent MySQL instance, which means you manage multiple server configurations, multiple sets of logs, and multiple backup schedules. Automation helps. Tools that handle backup scheduling, monitoring aggregation, and deployment across shards reduce the manual overhead significantly.

Frequently Asked Questions

How does sharding differ from using MySQL read replicas?
Read replicas copy the entire database to additional servers and handle read traffic by directing queries to replicas. All data exists on every replica. Sharding splits the data itself, so each server holds only a portion of the total dataset. Read replicas work well when you need to scale read performance. Sharding becomes relevant when a single server can no longer handle the total dataset size or write volume.
Can I shard a database without changing application code?
Most sharding implementations require changes to application code because queries must route to the correct shard. Some database proxies and middleware products claim to handle shard routing automatically, but they work only for specific query patterns and may introduce performance overhead. Genuine sharding typically involves writing shard-aware application logic or using an ORM that supports sharding natively.
What happens if one shard fails?
A single shard failure affects only the portion of data stored on that shard. Other shards continue operating normally. The application must handle this gracefully, either by serving degraded responses for data on the failed shard or by failing requests that depend on it. Recovery involves restoring the failed shard from its most recent backup and replaying binary logs to bring it up to date.
Is sharding reversible?
Consolidating a sharded database back to a single server or fewer shards is technically possible but expensive and time-consuming. It requires reading data from all shards, re-partitioning it, and loading it into a consolidated database. Planning the shard key carefully and testing distribution before going live reduces the likelihood of needing to reshuffle data later.
Does sharding improve database security?
Sharding itself does not improve security. Distributing data across more servers can increase the attack surface if those servers are not properly secured. Security depends on proper configuration of each database server, network isolation, access controls, and regular updates. Treating each shard as an independent database server from a security perspective ensures consistent protection across the infrastructure.