|
| 1 | +# Sharding |
| 2 | + |
| 3 | +Sharding is a database scaling technique that horizontally partitions data across multiple database instances (shards). This allows you to distribute large datasets across multiple databases, improving performance and scalability. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +PdoDb's sharding feature automatically routes queries to the appropriate shard based on the shard key value in WHERE conditions. This is transparent to your application code - you write queries normally, and PdoDb handles the routing automatically. |
| 8 | + |
| 9 | +## Configuration |
| 10 | + |
| 11 | +Sharding is configured using a fluent API. First, add your shard connections to the connection pool, then configure sharding to use them: |
| 12 | + |
| 13 | +```php |
| 14 | +// Add connections to the pool |
| 15 | +$db->addConnection('shard1', [ |
| 16 | + 'driver' => 'mysql', |
| 17 | + 'host' => 'shard1.example.com', |
| 18 | + 'dbname' => 'users_db', |
| 19 | + 'user' => 'user', |
| 20 | + 'password' => 'password', |
| 21 | +]); |
| 22 | + |
| 23 | +$db->addConnection('shard2', [ |
| 24 | + 'driver' => 'mysql', |
| 25 | + 'host' => 'shard2.example.com', |
| 26 | + 'dbname' => 'users_db', |
| 27 | + 'user' => 'user', |
| 28 | + 'password' => 'password', |
| 29 | +]); |
| 30 | + |
| 31 | +$db->addConnection('shard3', [ |
| 32 | + 'driver' => 'mysql', |
| 33 | + 'host' => 'shard3.example.com', |
| 34 | + 'dbname' => 'users_db', |
| 35 | + 'user' => 'user', |
| 36 | + 'password' => 'password', |
| 37 | +]); |
| 38 | + |
| 39 | +// Configure sharding to use existing connections |
| 40 | +$db->shard('users') |
| 41 | + ->shardKey('user_id') |
| 42 | + ->strategy('range') // or 'hash', 'modulo' |
| 43 | + ->ranges([ |
| 44 | + 'shard1' => [0, 1000], |
| 45 | + 'shard2' => [1001, 2000], |
| 46 | + 'shard3' => [2001, 3000], |
| 47 | + ]) |
| 48 | + ->useConnections(['shard1', 'shard2', 'shard3']) |
| 49 | + ->register(); |
| 50 | +``` |
| 51 | + |
| 52 | +## Sharding Strategies |
| 53 | + |
| 54 | +### Range Strategy |
| 55 | + |
| 56 | +Distributes data based on numeric ranges. Each shard handles a specific range of shard key values. |
| 57 | + |
| 58 | +```php |
| 59 | +$db->shard('users') |
| 60 | + ->shardKey('user_id') |
| 61 | + ->strategy('range') |
| 62 | + ->ranges([ |
| 63 | + 'shard1' => [0, 1000], |
| 64 | + 'shard2' => [1001, 2000], |
| 65 | + 'shard3' => [2001, 3000], |
| 66 | + ]) |
| 67 | + ->useConnections(['shard1', 'shard2', 'shard3']) |
| 68 | + ->register(); |
| 69 | +``` |
| 70 | + |
| 71 | +**Use when:** |
| 72 | +- Shard key values are numeric and sequential |
| 73 | +- You want predictable distribution |
| 74 | +- You need to query ranges of values |
| 75 | + |
| 76 | +### Hash Strategy |
| 77 | + |
| 78 | +Distributes data based on hash of the shard key value. Uses CRC32 for consistent hashing. |
| 79 | + |
| 80 | +```php |
| 81 | +$db->shard('users') |
| 82 | + ->shardKey('user_id') |
| 83 | + ->strategy('hash') |
| 84 | + ->useConnections(['shard1', 'shard2', 'shard3']) |
| 85 | + ->register(); |
| 86 | +``` |
| 87 | + |
| 88 | +**Use when:** |
| 89 | +- You want even distribution across shards |
| 90 | +- Shard key values are not sequential |
| 91 | +- You want consistent routing (same value always goes to same shard) |
| 92 | + |
| 93 | +### Modulo Strategy |
| 94 | + |
| 95 | +Distributes data based on modulo operation: `value % shard_count`. |
| 96 | + |
| 97 | +```php |
| 98 | +$db->shard('users') |
| 99 | + ->shardKey('user_id') |
| 100 | + ->strategy('modulo') |
| 101 | + ->useConnections(['shard1', 'shard2', 'shard3']) |
| 102 | + ->register(); |
| 103 | +``` |
| 104 | + |
| 105 | +**Use when:** |
| 106 | +- Shard key values are numeric |
| 107 | +- You want simple, predictable distribution |
| 108 | +- You need to easily add or remove shards |
| 109 | + |
| 110 | +## Usage |
| 111 | + |
| 112 | +Once configured, sharding works transparently: |
| 113 | + |
| 114 | +```php |
| 115 | +// Insert - automatically routed to appropriate shard |
| 116 | +$db->find()->table('users')->insert([ |
| 117 | + 'user_id' => 500, |
| 118 | + 'name' => 'Alice', |
| 119 | + |
| 120 | +]); |
| 121 | + |
| 122 | +// Query - automatically routed to appropriate shard |
| 123 | +$user = $db->find() |
| 124 | + ->from('users') |
| 125 | + ->where('user_id', 500) |
| 126 | + ->getOne(); |
| 127 | + |
| 128 | +// Update - automatically routed to appropriate shard |
| 129 | +$db->find() |
| 130 | + ->table('users') |
| 131 | + ->where('user_id', 500) |
| 132 | + ->update(['name' => 'Alice Updated']); |
| 133 | + |
| 134 | +// Delete - automatically routed to appropriate shard |
| 135 | +$db->find() |
| 136 | + ->table('users') |
| 137 | + ->where('user_id', 500) |
| 138 | + ->delete(); |
| 139 | +``` |
| 140 | + |
| 141 | +## Shard Key Requirements |
| 142 | + |
| 143 | +For sharding to work, the shard key must be present in WHERE conditions: |
| 144 | + |
| 145 | +```php |
| 146 | +// ✅ Works - shard key in WHERE |
| 147 | +$user = $db->find() |
| 148 | + ->from('users') |
| 149 | + ->where('user_id', 500) |
| 150 | + ->getOne(); |
| 151 | + |
| 152 | +// ❌ Falls back to normal routing - shard key not in WHERE |
| 153 | +$users = $db->find() |
| 154 | + ->from('users') |
| 155 | + ->where('name', 'Alice') |
| 156 | + ->get(); |
| 157 | +``` |
| 158 | + |
| 159 | +If the shard key is not found in WHERE conditions, the query will fall back to normal routing (using the default connection or read/write splitting if enabled). |
| 160 | + |
| 161 | +## Advanced Usage |
| 162 | + |
| 163 | +### Using Existing Connections |
| 164 | + |
| 165 | +You can use existing connections from the connection pool: |
| 166 | + |
| 167 | +```php |
| 168 | +// Add connections to pool |
| 169 | +$db->addConnection('shard1', [...]); |
| 170 | +$db->addConnection('shard2', [...]); |
| 171 | + |
| 172 | +// Get connections and register them with shard router |
| 173 | +$shardRouter = $db->getShardRouter(); |
| 174 | +$connections = // get connections from pool |
| 175 | + |
| 176 | +foreach (['shard1', 'shard2'] as $shard) { |
| 177 | + $shardRouter->addShardConnection('users', $shard, $connections[$shard]); |
| 178 | +} |
| 179 | +``` |
| 180 | + |
| 181 | +### Manual Shard Resolution |
| 182 | + |
| 183 | +You can manually resolve which shard a value belongs to: |
| 184 | + |
| 185 | +```php |
| 186 | +$shardRouter = $db->getShardRouter(); |
| 187 | +$config = $shardRouter->getShardConfig('users'); |
| 188 | + |
| 189 | +$strategy = // create strategy from config |
| 190 | +$shardName = $strategy->resolveShard(500); |
| 191 | +``` |
| 192 | + |
| 193 | +## Best Practices |
| 194 | + |
| 195 | +1. **Choose appropriate shard key**: The shard key should be: |
| 196 | + - Present in most queries |
| 197 | + - Evenly distributed (for hash/modulo) or sequential (for range) |
| 198 | + - Never changed after creation |
| 199 | + |
| 200 | +2. **Keep table structure consistent**: All shards must have the same table structure. |
| 201 | + |
| 202 | +3. **Handle shard key absence**: If a query doesn't include the shard key, it will fall back to normal routing. Consider: |
| 203 | + - Adding the shard key to WHERE conditions when possible |
| 204 | + - Using a different query strategy for queries without shard key |
| 205 | + - Implementing cross-shard queries if needed |
| 206 | + |
| 207 | +4. **Monitor shard distribution**: Regularly check that data is evenly distributed across shards. |
| 208 | + |
| 209 | +5. **Plan for shard migration**: When adding or removing shards, you may need to migrate data. |
| 210 | + |
| 211 | +## Limitations |
| 212 | + |
| 213 | +- Sharding only works when the shard key is present in WHERE conditions |
| 214 | +- Cross-shard queries are not supported (queries that need data from multiple shards) |
| 215 | +- Transactions spanning multiple shards are not supported |
| 216 | +- JOINs between sharded tables are not supported |
| 217 | + |
| 218 | +## Testing |
| 219 | + |
| 220 | +For testing, you can use multiple SQLite in-memory databases to simulate shards: |
| 221 | + |
| 222 | +```php |
| 223 | +$db->addConnection('shard1', ['driver' => 'sqlite', 'path' => ':memory:']); |
| 224 | +$db->addConnection('shard2', ['driver' => 'sqlite', 'path' => ':memory:']); |
| 225 | + |
| 226 | +// Configure sharding with in-memory databases |
| 227 | +$db->shard('users') |
| 228 | + ->shardKey('user_id') |
| 229 | + ->strategy('range') |
| 230 | + ->ranges([ |
| 231 | + 'shard1' => [0, 1000], |
| 232 | + 'shard2' => [1001, 2000], |
| 233 | + ]) |
| 234 | + ->nodes([ |
| 235 | + 'shard1' => ['driver' => 'sqlite', 'path' => ':memory:'], |
| 236 | + 'shard2' => ['driver' => 'sqlite', 'path' => ':memory:'], |
| 237 | + ]) |
| 238 | + ->register(); |
| 239 | +``` |
| 240 | + |
| 241 | +This allows you to test sharding functionality without setting up multiple database instances. |
0 commit comments