[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M6QSSZ1QB138RAEW0PY2HMRJ":3,"skill-same:01M6QSSZ1QB138RAEW0PY2HMRJ":113},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":18,"publishedAt":39,"linkOnly":40,"markdownContent":41,"markdownBody":42,"frontMatter":43,"refName":45,"commitSha":46,"skillDirectory":47,"forksCount":48,"repositoryHtmlUrl":49,"repositoryHomepageUrl":50,"ownerHtmlUrl":51,"ownerBlogUrl":52,"ownerDescription":53,"updatedAt":54,"install":55,"resources":68},"01M6QSSZ1QB138RAEW0PY2HMRJ","database-sharding",null,"Database sharding for PostgreSQL\u002FMySQL with hash\u002Frange\u002Fdirectory strategies. Use for horizontal scaling, multi-tenant isolation, billions of records, or encountering wrong shard keys, hotspots, cross-shard transactions, rebalancing issues.","VRIL-LABS\u002Fskill-jam","VRIL-LABS","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F272046472?v=4","01M6QR9PNWM28WZYJJASER1RG2","𝗩𝗥𝗜𝗟 𝗟𝗔𝗕𝗦","01M6QR9PT6Q6JDCQ97EQBDX6E4",2,"MIT","https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fai-ml\u002Fclaude-skills-main\u002Fclaude-skills-main\u002Fplugins\u002Fdatabase-sharding\u002Fskills\u002Fdatabase-sharding\u002FSKILL.md",[],[19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38],"database","database sharding","Sharding","selection","phase","tenant","Saga Pattern","scaling","strategies","pattern","scalability","directory","router","transactions","shard","postgresql","mysql","distributed database","issues","commit","2026-11-10 10:12:30",false,"---\nname: database-sharding\ndescription: Database sharding for PostgreSQL\u002FMySQL with hash\u002Frange\u002Fdirectory strategies. Use for horizontal scaling, multi-tenant isolation, billions of records, or encountering wrong shard keys, hotspots, cross-shard transactions, rebalancing issues.\nkeywords: database sharding, horizontal partitioning, shard key, consistent hashing,\n  hash sharding, range sharding, directory sharding, cross-shard queries, shard rebalancing,\n  scatter-gather pattern, multi-tenant sharding, shard router, distributed database,\n  database scalability, shard migration, hotspot shards, shard key selection,\n  cross-shard aggregation, two-phase commit, saga pattern, virtual shards\nlicense: MIT\n---\n\n# database-sharding\n\nComprehensive database sharding patterns for horizontal scaling with hash, range, and directory-based strategies.\n\n---\n\n## Quick Start (10 Minutes)\n\n**Step 1**: Choose sharding strategy from templates:\n```bash\n# Hash-based (even distribution)\ncat templates\u002Fhash-router.ts\n\n# Range-based (time-series data)\ncat templates\u002Frange-router.ts\n\n# Directory-based (multi-tenancy)\ncat templates\u002Fdirectory-router.ts\n```\n\n**Step 2**: Select shard key criteria:\n- ✅ **High cardinality** (millions of unique values)\n- ✅ **Even distribution** (no single value > 5%)\n- ✅ **Immutable** (never changes)\n- ✅ **Query alignment** (in 80%+ of WHERE clauses)\n\n**Step 3**: Implement router:\n```typescript\nimport { HashRouter } from '.\u002Fhash-router';\n\nconst router = new HashRouter([\n  { id: 'shard_0', connection: { host: 'db0.example.com' } },\n  { id: 'shard_1', connection: { host: 'db1.example.com' } },\n  { id: 'shard_2', connection: { host: 'db2.example.com' } },\n  { id: 'shard_3', connection: { host: 'db3.example.com' } },\n]);\n\n\u002F\u002F Query single shard\nconst user = await router.query('user_123', 'SELECT * FROM users WHERE id = $1', ['user_123']);\n```\n\n---\n\n## Critical Rules\n\n### ✓ Always Do\n\n| Rule | Reason |\n|------|--------|\n| **Include shard key in queries** | Avoid scanning all shards (100x slower) |\n| **Monitor shard distribution** | Detect hotspots before they cause outages |\n| **Plan for rebalancing upfront** | Cannot easily add shards later |\n| **Choose immutable shard key** | Changing key = data migration nightmare |\n| **Test distribution with production data** | Synthetic data hides real hotspots |\n| **Denormalize for data locality** | Keep related data on same shard |\n\n### ✗ Never Do\n\n| Anti-Pattern | Why It's Bad |\n|--------------|--------------|\n| **Sequential ID with range sharding** | Latest shard gets all writes (hotspot) |\n| **Timestamp as shard key** | Recent shard overwhelmed |\n| **Cross-shard transactions without 2PC** | Data corruption, inconsistency |\n| **Simple modulo without consistent hashing** | Cannot add shards without full re-shard |\n| **Nullable shard key** | Special NULL handling creates hotspots |\n| **No shard routing layer** | Hardcoded shards = cannot rebalance |\n\n---\n\n## Top 7 Critical Errors\n\n### Error 1: Wrong Shard Key Choice (Hotspots)\n**Symptom**: One shard receives 80%+ of traffic\n**Fix**:\n```typescript\n\u002F\u002F ❌ Bad: Low cardinality (status field)\nshard_key = order.status; \u002F\u002F 90% are 'pending' → shard_0 overloaded\n\n\u002F\u002F ✅ Good: High cardinality (user_id)\nshard_key = order.user_id; \u002F\u002F Millions of users, even distribution\n```\n\n### Error 2: Missing Shard Key in Queries\n**Symptom**: Queries scan ALL shards (extremely slow)\n**Fix**:\n```typescript\n\u002F\u002F ❌ Bad: No shard key\nSELECT * FROM orders WHERE status = 'shipped'; \u002F\u002F Scans all 100 shards!\n\n\u002F\u002F ✅ Good: Include shard key\nSELECT * FROM orders WHERE user_id = ? AND status = 'shipped'; \u002F\u002F Targets 1 shard\n```\n\n### Error 3: Sequential IDs with Range Sharding\n**Symptom**: Latest shard gets all writes\n**Fix**:\n```typescript\n\u002F\u002F ❌ Bad: Range sharding with auto-increment\n\u002F\u002F Shard 0: 1-1M, Shard 1: 1M-2M, Shard 2: 2M+ → All new writes to Shard 2!\n\n\u002F\u002F ✅ Good: Hash-based sharding\nconst shardId = hash(id) % shardCount; \u002F\u002F Even distribution\n```\n\n### Error 4: No Rebalancing Strategy\n**Symptom**: Stuck with initial shard count, cannot scale\n**Fix**:\n```typescript\n\u002F\u002F ❌ Bad: Simple modulo\nconst shardId = hash(key) % shardCount; \u002F\u002F Adding 5th shard breaks ALL keys\n\n\u002F\u002F ✅ Good: Consistent hashing\nconst ring = new ConsistentHashRing(shards);\nconst shardId = ring.getNode(key); \u002F\u002F Only ~25% of keys move when adding shard\n```\n\n### Error 5: Cross-Shard Transactions\n**Symptom**: Data inconsistency, partial writes\n**Fix**:\n```typescript\n\u002F\u002F ❌ Bad: Cross-shard transaction (will corrupt)\nBEGIN;\nUPDATE shard_1.accounts SET balance = balance - 100 WHERE id = 'A';\nUPDATE shard_2.accounts SET balance = balance + 100 WHERE id = 'B';\nCOMMIT; \u002F\u002F If shard_2 fails, shard_1 already committed!\n\n\u002F\u002F ✅ Good: Two-Phase Commit or Saga pattern\nconst txn = new TwoPhaseCommitTransaction();\ntxn.addOperation(shard_1, 'UPDATE accounts SET balance = balance - 100 WHERE id = ?', ['A']);\ntxn.addOperation(shard_2, 'UPDATE accounts SET balance = balance + 100 WHERE id = ?', ['B']);\nawait txn.execute(); \u002F\u002F Atomic across shards\n```\n\n### Error 6: Mutable Shard Key\n**Symptom**: Records move shards, causing duplicates\n**Fix**:\n```typescript\n\u002F\u002F ❌ Bad: Shard by country (user relocates)\nshard_key = user.country; \u002F\u002F User moves US → CA, now in different shard!\n\n\u002F\u002F ✅ Good: Shard by immutable user_id\nshard_key = user.id; \u002F\u002F Never changes\n```\n\n### Error 7: No Monitoring\n**Symptom**: Silent hotspots, sudden performance degradation\n**Fix**:\n```typescript\n\u002F\u002F ✅ Required metrics\n- Per-shard record counts (should be within 20%)\n- Query distribution (no shard > 40% of queries)\n- Storage per shard (alert at 80%)\n- Latency p99 per shard\n```\n\n**Load** `references\u002Ferror-catalog.md` for all 10 errors with detailed fixes.\n\n---\n\n## Sharding Strategies\n\n| Strategy | Best For | Pros | Cons |\n|----------|----------|------|------|\n| **Hash** | User data, even load critical | No hotspots, predictable | Range queries scatter |\n| **Range** | Time-series, logs, append-only | Range queries efficient, archival | Recent shard hotspot |\n| **Directory** | Multi-tenancy, complex routing | Flexible, easy rebalancing | Lookup overhead, SPOF |\n\n**Load** `references\u002Fsharding-strategies.md` for detailed comparisons with production examples (Instagram, Discord, Salesforce).\n\n---\n\n## Shard Key Selection Criteria\n\n| Criterion | Importance | Check Method |\n|-----------|------------|--------------|\n| **High cardinality** | Critical | `COUNT(DISTINCT shard_key)` > shard_count × 100 |\n| **Even distribution** | Critical | No value > 5% of total |\n| **Immutable** | Critical | Value never changes |\n| **Query alignment** | High | 80%+ queries include it |\n| **Data locality** | Medium | Related records together |\n\n**Decision Tree**:\n- User-focused app → `user_id`\n- Multi-tenant SaaS → `tenant_id`\n- Time-series\u002Flogs → `timestamp` (range sharding)\n- Product catalog → `product_id`\n\n**Load** `references\u002Fshard-key-selection.md` for comprehensive decision trees and testing strategies.\n\n---\n\n## Configuration Summary\n\n### Hash-Based Router\n\n```typescript\nimport { HashRouter } from '.\u002Ftemplates\u002Fhash-router';\n\nconst router = new HashRouter([\n  { id: 'shard_0', connection: { \u002F* PostgreSQL config *\u002F } },\n  { id: 'shard_1', connection: { \u002F* PostgreSQL config *\u002F } },\n]);\n\n\u002F\u002F Automatically routes to correct shard\nconst user = await router.query('user_123', 'SELECT * FROM users WHERE id = $1', ['user_123']);\n```\n\n### Range-Based Router\n\n```typescript\nimport { RangeRouter } from '.\u002Ftemplates\u002Frange-router';\n\nconst router = new RangeRouter(shardConfigs, [\n  { start: Date.parse('2024-01-01'), end: Date.parse('2024-04-01'), shardId: 'shard_q1' },\n  { start: Date.parse('2024-04-01'), end: Date.parse('2024-07-01'), shardId: 'shard_q2' },\n  { start: Date.parse('2024-07-01'), end: Infinity, shardId: 'shard_q3' },\n]);\n\n\u002F\u002F Range queries target specific shards\nconst janEvents = await router.queryRange(\n  Date.parse('2024-01-01'),\n  Date.parse('2024-02-01'),\n  'SELECT * FROM events WHERE created_at BETWEEN $1 AND $2'\n);\n```\n\n### Directory-Based Router\n\n```typescript\nimport { DirectoryRouter } from '.\u002Ftemplates\u002Fdirectory-router';\n\nconst router = new DirectoryRouter(directoryDBConfig, shardConfigs);\n\n\u002F\u002F Assign tenant to specific shard\nawait router.assignShard('tenant_acme', 'shard_enterprise');\n\n\u002F\u002F Route automatically\nconst users = await router.query('tenant_acme', 'SELECT * FROM users');\n```\n\n---\n\n## When to Load References\n\n### Choosing Strategy\n**Load** `references\u002Fsharding-strategies.md` when:\n- Deciding between hash, range, directory\n- Need production examples (Instagram, Discord)\n- Planning hybrid approaches\n\n### Selecting Shard Key\n**Load** `references\u002Fshard-key-selection.md` when:\n- Choosing shard key for new project\n- Evaluating existing shard key\n- Testing distribution with production data\n\n### Implementation\n**Load** `references\u002Fimplementation-patterns.md` when:\n- Building shard router from scratch\n- Implementing consistent hashing\n- Need transaction handling (2PC, Saga)\n- Setting up monitoring\u002Fmetrics\n\n### Cross-Shard Operations\n**Load** `references\u002Fcross-shard-queries.md` when:\n- Need to aggregate across shards (COUNT, SUM, AVG)\n- Implementing cross-shard joins\n- Building pagination across shards\n- Optimizing scatter-gather patterns\n\n### Rebalancing\n**Load** `references\u002Frebalancing-guide.md` when:\n- Adding new shards\n- Migrating data between shards\n- Planning zero-downtime migrations\n- Balancing uneven load\n\n### Error Prevention\n**Load** `references\u002Ferror-catalog.md` when:\n- Troubleshooting performance issues\n- Reviewing shard architecture\n- All 10 documented errors with fixes\n\n---\n\n## Complete Setup Checklist\n\n**Before Sharding**:\n- [ ] Tested shard key distribution with production data\n- [ ] Shard key in 80%+ of queries\n- [ ] Monitoring infrastructure ready\n- [ ] Rebalancing strategy planned\n\n**Router Implementation**:\n- [ ] Shard routing layer (not hardcoded shards)\n- [ ] Connection pooling per shard\n- [ ] Error handling and retries\n- [ ] Metrics collection (queries\u002Fshard, latency)\n\n**Shard Configuration**:\n- [ ] 4-8 shards initially (room to grow)\n- [ ] Consistent hashing or virtual shards\n- [ ] Replicas per shard (HA)\n- [ ] Backup strategy per shard\n\n**Application Changes**:\n- [ ] All queries include shard key\n- [ ] Cross-shard joins eliminated (denormalized)\n- [ ] Transaction boundaries respected\n- [ ] Connection pooling configured\n\n---\n\n## Production Example\n\n**Before** (Single database overwhelmed):\n```typescript\n\u002F\u002F Single PostgreSQL instance\nconst db = new Pool({ host: 'db.example.com' });\n\n\u002F\u002F All 10M users on one server\nconst users = await db.query('SELECT * FROM users WHERE status = $1', ['active']);\n\u002F\u002F Query time: 5000ms (slow!)\n\u002F\u002F DB CPU: 95%\n\u002F\u002F Disk: 500GB, growing\n```\n\n**After** (Sharded across 8 servers):\n```typescript\n\u002F\u002F Hash-based sharding with 8 shards\nconst router = new HashRouter([\n  { id: 'shard_0', connection: { host: 'db0.example.com' } },\n  { id: 'shard_1', connection: { host: 'db1.example.com' } },\n  \u002F\u002F ... 6 more shards\n]);\n\n\u002F\u002F Query single user (targets 1 shard)\nconst user = await router.query('user_123', 'SELECT * FROM users WHERE id = $1', ['user_123']);\n\u002F\u002F Query time: 10ms (500x faster!)\n\n\u002F\u002F Query all shards (scatter-gather)\nconst allActive = await router.queryAll('SELECT * FROM users WHERE status = $1', ['active']);\n\u002F\u002F Query time: 800ms (parallelized across 8 shards, 6x faster than single)\n\n\u002F\u002F Result: Each shard handles ~1.25M users\n\u002F\u002F DB CPU per shard: 20%\n\u002F\u002F Disk per shard: 65GB\n\u002F\u002F Can scale to 16 shards easily (consistent hashing)\n```\n\n---\n\n## Known Issues Prevention\n\nAll 10 documented errors prevented:\n1. ✅ Wrong shard key (hotspots) → Test distribution first\n2. ✅ Missing shard key in queries → Code review, linting\n3. ✅ Cross-shard transactions → Use 2PC or Saga pattern\n4. ✅ Sequential ID hotspots → Use hash-based sharding\n5. ✅ No rebalancing strategy → Consistent hashing from day 1\n6. ✅ Timestamp sharding hotspots → Hybrid hash+range approach\n7. ✅ Mutable shard key → Choose immutable keys (user_id)\n8. ✅ No routing layer → Abstract with router from start\n9. ✅ No monitoring → Track per-shard metrics\n10. ✅ Weak hash function → Use MD5, MurmurHash3, xxHash\n\n**See**: `references\u002Ferror-catalog.md` for detailed fixes\n\n---\n\n## Resources\n\n**Templates**:\n- `templates\u002Fhash-router.ts` - Hash-based sharding\n- `templates\u002Frange-router.ts` - Range-based sharding\n- `templates\u002Fdirectory-router.ts` - Directory-based sharding\n- `templates\u002Fcross-shard-aggregation.ts` - Aggregation patterns\n\n**References**:\n- `references\u002Fsharding-strategies.md` - Strategy comparison\n- `references\u002Fshard-key-selection.md` - Key selection guide\n- `references\u002Fimplementation-patterns.md` - Router implementations\n- `references\u002Fcross-shard-queries.md` - Query patterns\n- `references\u002Frebalancing-guide.md` - Migration strategies\n- `references\u002Ferror-catalog.md` - All 10 errors documented\n\n**Production Examples**:\n- Instagram: Range sharding for media\n- Discord: Hash sharding for messages\n- Salesforce: Directory sharding for orgs\n\n---\n\n**Production-tested** | **10 errors prevented** | **MIT License**\n","# database-sharding\n\nComprehensive database sharding patterns for horizontal scaling with hash, range, and directory-based strategies.\n\n---\n\n## Quick Start (10 Minutes)\n\n**Step 1**: Choose sharding strategy from templates:\n```bash\n# Hash-based (even distribution)\ncat templates\u002Fhash-router.ts\n\n# Range-based (time-series data)\ncat templates\u002Frange-router.ts\n\n# Directory-based (multi-tenancy)\ncat templates\u002Fdirectory-router.ts\n```\n\n**Step 2**: Select shard key criteria:\n- ✅ **High cardinality** (millions of unique values)\n- ✅ **Even distribution** (no single value > 5%)\n- ✅ **Immutable** (never changes)\n- ✅ **Query alignment** (in 80%+ of WHERE clauses)\n\n**Step 3**: Implement router:\n```typescript\nimport { HashRouter } from '.\u002Fhash-router';\n\nconst router = new HashRouter([\n  { id: 'shard_0', connection: { host: 'db0.example.com' } },\n  { id: 'shard_1', connection: { host: 'db1.example.com' } },\n  { id: 'shard_2', connection: { host: 'db2.example.com' } },\n  { id: 'shard_3', connection: { host: 'db3.example.com' } },\n]);\n\n\u002F\u002F Query single shard\nconst user = await router.query('user_123', 'SELECT * FROM users WHERE id = $1', ['user_123']);\n```\n\n---\n\n## Critical Rules\n\n### ✓ Always Do\n\n| Rule | Reason |\n|------|--------|\n| **Include shard key in queries** | Avoid scanning all shards (100x slower) |\n| **Monitor shard distribution** | Detect hotspots before they cause outages |\n| **Plan for rebalancing upfront** | Cannot easily add shards later |\n| **Choose immutable shard key** | Changing key = data migration nightmare |\n| **Test distribution with production data** | Synthetic data hides real hotspots |\n| **Denormalize for data locality** | Keep related data on same shard |\n\n### ✗ Never Do\n\n| Anti-Pattern | Why It's Bad |\n|--------------|--------------|\n| **Sequential ID with range sharding** | Latest shard gets all writes (hotspot) |\n| **Timestamp as shard key** | Recent shard overwhelmed |\n| **Cross-shard transactions without 2PC** | Data corruption, inconsistency |\n| **Simple modulo without consistent hashing** | Cannot add shards without full re-shard |\n| **Nullable shard key** | Special NULL handling creates hotspots |\n| **No shard routing layer** | Hardcoded shards = cannot rebalance |\n\n---\n\n## Top 7 Critical Errors\n\n### Error 1: Wrong Shard Key Choice (Hotspots)\n**Symptom**: One shard receives 80%+ of traffic\n**Fix**:\n```typescript\n\u002F\u002F ❌ Bad: Low cardinality (status field)\nshard_key = order.status; \u002F\u002F 90% are 'pending' → shard_0 overloaded\n\n\u002F\u002F ✅ Good: High cardinality (user_id)\nshard_key = order.user_id; \u002F\u002F Millions of users, even distribution\n```\n\n### Error 2: Missing Shard Key in Queries\n**Symptom**: Queries scan ALL shards (extremely slow)\n**Fix**:\n```typescript\n\u002F\u002F ❌ Bad: No shard key\nSELECT * FROM orders WHERE status = 'shipped'; \u002F\u002F Scans all 100 shards!\n\n\u002F\u002F ✅ Good: Include shard key\nSELECT * FROM orders WHERE user_id = ? AND status = 'shipped'; \u002F\u002F Targets 1 shard\n```\n\n### Error 3: Sequential IDs with Range Sharding\n**Symptom**: Latest shard gets all writes\n**Fix**:\n```typescript\n\u002F\u002F ❌ Bad: Range sharding with auto-increment\n\u002F\u002F Shard 0: 1-1M, Shard 1: 1M-2M, Shard 2: 2M+ → All new writes to Shard 2!\n\n\u002F\u002F ✅ Good: Hash-based sharding\nconst shardId = hash(id) % shardCount; \u002F\u002F Even distribution\n```\n\n### Error 4: No Rebalancing Strategy\n**Symptom**: Stuck with initial shard count, cannot scale\n**Fix**:\n```typescript\n\u002F\u002F ❌ Bad: Simple modulo\nconst shardId = hash(key) % shardCount; \u002F\u002F Adding 5th shard breaks ALL keys\n\n\u002F\u002F ✅ Good: Consistent hashing\nconst ring = new ConsistentHashRing(shards);\nconst shardId = ring.getNode(key); \u002F\u002F Only ~25% of keys move when adding shard\n```\n\n### Error 5: Cross-Shard Transactions\n**Symptom**: Data inconsistency, partial writes\n**Fix**:\n```typescript\n\u002F\u002F ❌ Bad: Cross-shard transaction (will corrupt)\nBEGIN;\nUPDATE shard_1.accounts SET balance = balance - 100 WHERE id = 'A';\nUPDATE shard_2.accounts SET balance = balance + 100 WHERE id = 'B';\nCOMMIT; \u002F\u002F If shard_2 fails, shard_1 already committed!\n\n\u002F\u002F ✅ Good: Two-Phase Commit or Saga pattern\nconst txn = new TwoPhaseCommitTransaction();\ntxn.addOperation(shard_1, 'UPDATE accounts SET balance = balance - 100 WHERE id = ?', ['A']);\ntxn.addOperation(shard_2, 'UPDATE accounts SET balance = balance + 100 WHERE id = ?', ['B']);\nawait txn.execute(); \u002F\u002F Atomic across shards\n```\n\n### Error 6: Mutable Shard Key\n**Symptom**: Records move shards, causing duplicates\n**Fix**:\n```typescript\n\u002F\u002F ❌ Bad: Shard by country (user relocates)\nshard_key = user.country; \u002F\u002F User moves US → CA, now in different shard!\n\n\u002F\u002F ✅ Good: Shard by immutable user_id\nshard_key = user.id; \u002F\u002F Never changes\n```\n\n### Error 7: No Monitoring\n**Symptom**: Silent hotspots, sudden performance degradation\n**Fix**:\n```typescript\n\u002F\u002F ✅ Required metrics\n- Per-shard record counts (should be within 20%)\n- Query distribution (no shard > 40% of queries)\n- Storage per shard (alert at 80%)\n- Latency p99 per shard\n```\n\n**Load** `references\u002Ferror-catalog.md` for all 10 errors with detailed fixes.\n\n---\n\n## Sharding Strategies\n\n| Strategy | Best For | Pros | Cons |\n|----------|----------|------|------|\n| **Hash** | User data, even load critical | No hotspots, predictable | Range queries scatter |\n| **Range** | Time-series, logs, append-only | Range queries efficient, archival | Recent shard hotspot |\n| **Directory** | Multi-tenancy, complex routing | Flexible, easy rebalancing | Lookup overhead, SPOF |\n\n**Load** `references\u002Fsharding-strategies.md` for detailed comparisons with production examples (Instagram, Discord, Salesforce).\n\n---\n\n## Shard Key Selection Criteria\n\n| Criterion | Importance | Check Method |\n|-----------|------------|--------------|\n| **High cardinality** | Critical | `COUNT(DISTINCT shard_key)` > shard_count × 100 |\n| **Even distribution** | Critical | No value > 5% of total |\n| **Immutable** | Critical | Value never changes |\n| **Query alignment** | High | 80%+ queries include it |\n| **Data locality** | Medium | Related records together |\n\n**Decision Tree**:\n- User-focused app → `user_id`\n- Multi-tenant SaaS → `tenant_id`\n- Time-series\u002Flogs → `timestamp` (range sharding)\n- Product catalog → `product_id`\n\n**Load** `references\u002Fshard-key-selection.md` for comprehensive decision trees and testing strategies.\n\n---\n\n## Configuration Summary\n\n### Hash-Based Router\n\n```typescript\nimport { HashRouter } from '.\u002Ftemplates\u002Fhash-router';\n\nconst router = new HashRouter([\n  { id: 'shard_0', connection: { \u002F* PostgreSQL config *\u002F } },\n  { id: 'shard_1', connection: { \u002F* PostgreSQL config *\u002F } },\n]);\n\n\u002F\u002F Automatically routes to correct shard\nconst user = await router.query('user_123', 'SELECT * FROM users WHERE id = $1', ['user_123']);\n```\n\n### Range-Based Router\n\n```typescript\nimport { RangeRouter } from '.\u002Ftemplates\u002Frange-router';\n\nconst router = new RangeRouter(shardConfigs, [\n  { start: Date.parse('2024-01-01'), end: Date.parse('2024-04-01'), shardId: 'shard_q1' },\n  { start: Date.parse('2024-04-01'), end: Date.parse('2024-07-01'), shardId: 'shard_q2' },\n  { start: Date.parse('2024-07-01'), end: Infinity, shardId: 'shard_q3' },\n]);\n\n\u002F\u002F Range queries target specific shards\nconst janEvents = await router.queryRange(\n  Date.parse('2024-01-01'),\n  Date.parse('2024-02-01'),\n  'SELECT * FROM events WHERE created_at BETWEEN $1 AND $2'\n);\n```\n\n### Directory-Based Router\n\n```typescript\nimport { DirectoryRouter } from '.\u002Ftemplates\u002Fdirectory-router';\n\nconst router = new DirectoryRouter(directoryDBConfig, shardConfigs);\n\n\u002F\u002F Assign tenant to specific shard\nawait router.assignShard('tenant_acme', 'shard_enterprise');\n\n\u002F\u002F Route automatically\nconst users = await router.query('tenant_acme', 'SELECT * FROM users');\n```\n\n---\n\n## When to Load References\n\n### Choosing Strategy\n**Load** `references\u002Fsharding-strategies.md` when:\n- Deciding between hash, range, directory\n- Need production examples (Instagram, Discord)\n- Planning hybrid approaches\n\n### Selecting Shard Key\n**Load** `references\u002Fshard-key-selection.md` when:\n- Choosing shard key for new project\n- Evaluating existing shard key\n- Testing distribution with production data\n\n### Implementation\n**Load** `references\u002Fimplementation-patterns.md` when:\n- Building shard router from scratch\n- Implementing consistent hashing\n- Need transaction handling (2PC, Saga)\n- Setting up monitoring\u002Fmetrics\n\n### Cross-Shard Operations\n**Load** `references\u002Fcross-shard-queries.md` when:\n- Need to aggregate across shards (COUNT, SUM, AVG)\n- Implementing cross-shard joins\n- Building pagination across shards\n- Optimizing scatter-gather patterns\n\n### Rebalancing\n**Load** `references\u002Frebalancing-guide.md` when:\n- Adding new shards\n- Migrating data between shards\n- Planning zero-downtime migrations\n- Balancing uneven load\n\n### Error Prevention\n**Load** `references\u002Ferror-catalog.md` when:\n- Troubleshooting performance issues\n- Reviewing shard architecture\n- All 10 documented errors with fixes\n\n---\n\n## Complete Setup Checklist\n\n**Before Sharding**:\n- [ ] Tested shard key distribution with production data\n- [ ] Shard key in 80%+ of queries\n- [ ] Monitoring infrastructure ready\n- [ ] Rebalancing strategy planned\n\n**Router Implementation**:\n- [ ] Shard routing layer (not hardcoded shards)\n- [ ] Connection pooling per shard\n- [ ] Error handling and retries\n- [ ] Metrics collection (queries\u002Fshard, latency)\n\n**Shard Configuration**:\n- [ ] 4-8 shards initially (room to grow)\n- [ ] Consistent hashing or virtual shards\n- [ ] Replicas per shard (HA)\n- [ ] Backup strategy per shard\n\n**Application Changes**:\n- [ ] All queries include shard key\n- [ ] Cross-shard joins eliminated (denormalized)\n- [ ] Transaction boundaries respected\n- [ ] Connection pooling configured\n\n---\n\n## Production Example\n\n**Before** (Single database overwhelmed):\n```typescript\n\u002F\u002F Single PostgreSQL instance\nconst db = new Pool({ host: 'db.example.com' });\n\n\u002F\u002F All 10M users on one server\nconst users = await db.query('SELECT * FROM users WHERE status = $1', ['active']);\n\u002F\u002F Query time: 5000ms (slow!)\n\u002F\u002F DB CPU: 95%\n\u002F\u002F Disk: 500GB, growing\n```\n\n**After** (Sharded across 8 servers):\n```typescript\n\u002F\u002F Hash-based sharding with 8 shards\nconst router = new HashRouter([\n  { id: 'shard_0', connection: { host: 'db0.example.com' } },\n  { id: 'shard_1', connection: { host: 'db1.example.com' } },\n  \u002F\u002F ... 6 more shards\n]);\n\n\u002F\u002F Query single user (targets 1 shard)\nconst user = await router.query('user_123', 'SELECT * FROM users WHERE id = $1', ['user_123']);\n\u002F\u002F Query time: 10ms (500x faster!)\n\n\u002F\u002F Query all shards (scatter-gather)\nconst allActive = await router.queryAll('SELECT * FROM users WHERE status = $1', ['active']);\n\u002F\u002F Query time: 800ms (parallelized across 8 shards, 6x faster than single)\n\n\u002F\u002F Result: Each shard handles ~1.25M users\n\u002F\u002F DB CPU per shard: 20%\n\u002F\u002F Disk per shard: 65GB\n\u002F\u002F Can scale to 16 shards easily (consistent hashing)\n```\n\n---\n\n## Known Issues Prevention\n\nAll 10 documented errors prevented:\n1. ✅ Wrong shard key (hotspots) → Test distribution first\n2. ✅ Missing shard key in queries → Code review, linting\n3. ✅ Cross-shard transactions → Use 2PC or Saga pattern\n4. ✅ Sequential ID hotspots → Use hash-based sharding\n5. ✅ No rebalancing strategy → Consistent hashing from day 1\n6. ✅ Timestamp sharding hotspots → Hybrid hash+range approach\n7. ✅ Mutable shard key → Choose immutable keys (user_id)\n8. ✅ No routing layer → Abstract with router from start\n9. ✅ No monitoring → Track per-shard metrics\n10. ✅ Weak hash function → Use MD5, MurmurHash3, xxHash\n\n**See**: `references\u002Ferror-catalog.md` for detailed fixes\n\n---\n\n## Resources\n\n**Templates**:\n- `templates\u002Fhash-router.ts` - Hash-based sharding\n- `templates\u002Frange-router.ts` - Range-based sharding\n- `templates\u002Fdirectory-router.ts` - Directory-based sharding\n- `templates\u002Fcross-shard-aggregation.ts` - Aggregation patterns\n\n**References**:\n- `references\u002Fsharding-strategies.md` - Strategy comparison\n- `references\u002Fshard-key-selection.md` - Key selection guide\n- `references\u002Fimplementation-patterns.md` - Router implementations\n- `references\u002Fcross-shard-queries.md` - Query patterns\n- `references\u002Frebalancing-guide.md` - Migration strategies\n- `references\u002Ferror-catalog.md` - All 10 errors documented\n\n**Production Examples**:\n- Instagram: Range sharding for media\n- Discord: Hash sharding for messages\n- Salesforce: Directory sharding for orgs\n\n---\n\n**Production-tested** | **10 errors prevented** | **MIT License**\n",{"name":5,"license":15,"keywords":44,"description":7},"database sharding, horizontal partitioning, shard key, consistent hashing, hash sharding, range sharding, directory sharding, cross-shard queries, shard rebalancing, scatter-gather pattern, multi-tenant sharding, shard router, distributed database, database scalability, shard migration, hotspot shards, shard key selection, cross-shard aggregation, two-phase commit, saga pattern, virtual shards","main","5b00ad8b9f0787969474ff5e9ec417bb55f23827","skills\u002Fai-ml\u002Fclaude-skills-main\u002Fclaude-skills-main\u002Fplugins\u002Fdatabase-sharding\u002Fskills\u002Fdatabase-sharding",0,"https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam","https:\u002F\u002Fvril.li","https:\u002F\u002Fgithub.com\u002FVRIL-LABS","vril.li","𝗥𝗲𝘀𝗲𝗮𝗿𝗰𝗵 𝗮𝗻𝗱 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗺𝗲𝗻𝘁 𝗖𝗼𝗻𝘀𝗼𝗿𝘁𝗶𝘂𝗺","2026-08-24 09:21:38",{"source":8,"skillName":5,"commands":56,"prompt":66,"zipAvailable":67},[57,60,63],{"tool":58,"command":59},"npx","npx skills add VRIL-LABS\u002Fskill-jam --skill database-sharding",{"tool":61,"command":62},"bunx","bunx skills add VRIL-LABS\u002Fskill-jam --skill database-sharding",{"tool":64,"command":65},"pnpm","pnpm dlx skills add VRIL-LABS\u002Fskill-jam --skill database-sharding","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fai-ml\u002Fclaude-skills-main\u002Fclaude-skills-main\u002Fplugins\u002Fdatabase-sharding\u002Fskills\u002Fdatabase-sharding\u002FSKILL.md",true,[69,75,79,83,87,91,95,101,105,109],{"path":70,"resourceType":71,"resourceTypeDesc":72,"sizeBytes":73,"sourceHtmlUrl":74},"references\u002Fcross-shard-queries.md","REFERENCE","参考文档",8451,"https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fai-ml\u002Fclaude-skills-main\u002Fclaude-skills-main\u002Fplugins\u002Fdatabase-sharding\u002Fskills\u002Fdatabase-sharding\u002Freferences\u002Fcross-shard-queries.md",{"path":76,"resourceType":71,"resourceTypeDesc":72,"sizeBytes":77,"sourceHtmlUrl":78},"references\u002Ferror-catalog.md",16134,"https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fai-ml\u002Fclaude-skills-main\u002Fclaude-skills-main\u002Fplugins\u002Fdatabase-sharding\u002Fskills\u002Fdatabase-sharding\u002Freferences\u002Ferror-catalog.md",{"path":80,"resourceType":71,"resourceTypeDesc":72,"sizeBytes":81,"sourceHtmlUrl":82},"references\u002Fimplementation-patterns.md",13100,"https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fai-ml\u002Fclaude-skills-main\u002Fclaude-skills-main\u002Fplugins\u002Fdatabase-sharding\u002Fskills\u002Fdatabase-sharding\u002Freferences\u002Fimplementation-patterns.md",{"path":84,"resourceType":71,"resourceTypeDesc":72,"sizeBytes":85,"sourceHtmlUrl":86},"references\u002Frebalancing-guide.md",8279,"https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fai-ml\u002Fclaude-skills-main\u002Fclaude-skills-main\u002Fplugins\u002Fdatabase-sharding\u002Fskills\u002Fdatabase-sharding\u002Freferences\u002Frebalancing-guide.md",{"path":88,"resourceType":71,"resourceTypeDesc":72,"sizeBytes":89,"sourceHtmlUrl":90},"references\u002Fshard-key-selection.md",14312,"https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fai-ml\u002Fclaude-skills-main\u002Fclaude-skills-main\u002Fplugins\u002Fdatabase-sharding\u002Fskills\u002Fdatabase-sharding\u002Freferences\u002Fshard-key-selection.md",{"path":92,"resourceType":71,"resourceTypeDesc":72,"sizeBytes":93,"sourceHtmlUrl":94},"references\u002Fsharding-strategies.md",13584,"https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fai-ml\u002Fclaude-skills-main\u002Fclaude-skills-main\u002Fplugins\u002Fdatabase-sharding\u002Fskills\u002Fdatabase-sharding\u002Freferences\u002Fsharding-strategies.md",{"path":96,"resourceType":97,"resourceTypeDesc":98,"sizeBytes":99,"sourceHtmlUrl":100},"templates\u002Fcross-shard-aggregation.ts","SCRIPT","脚本",6265,"https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fai-ml\u002Fclaude-skills-main\u002Fclaude-skills-main\u002Fplugins\u002Fdatabase-sharding\u002Fskills\u002Fdatabase-sharding\u002Ftemplates\u002Fcross-shard-aggregation.ts",{"path":102,"resourceType":97,"resourceTypeDesc":98,"sizeBytes":103,"sourceHtmlUrl":104},"templates\u002Fdirectory-router.ts",5015,"https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fai-ml\u002Fclaude-skills-main\u002Fclaude-skills-main\u002Fplugins\u002Fdatabase-sharding\u002Fskills\u002Fdatabase-sharding\u002Ftemplates\u002Fdirectory-router.ts",{"path":106,"resourceType":97,"resourceTypeDesc":98,"sizeBytes":107,"sourceHtmlUrl":108},"templates\u002Fhash-router.ts",2537,"https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fai-ml\u002Fclaude-skills-main\u002Fclaude-skills-main\u002Fplugins\u002Fdatabase-sharding\u002Fskills\u002Fdatabase-sharding\u002Ftemplates\u002Fhash-router.ts",{"path":110,"resourceType":97,"resourceTypeDesc":98,"sizeBytes":111,"sourceHtmlUrl":112},"templates\u002Frange-router.ts",3066,"https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fai-ml\u002Fclaude-skills-main\u002Fclaude-skills-main\u002Fplugins\u002Fdatabase-sharding\u002Fskills\u002Fdatabase-sharding\u002Ftemplates\u002Frange-router.ts",[114,141,155,168,193,216],{"skillNo":115,"name":116,"nameZh":6,"description":117,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":118,"categories":119,"keywords":120,"publishedAt":39},"01M6R2CQRG190P09DAHMF8M5QP","three-best-practices","Three.js performance optimization and best practices guidelines. Use when writing, reviewing, or optimizing Three.js code. Triggers on tasks involving 3D scenes, WebGL\u002FWebGPU rendering, geometries, materials, textures, lighting, shaders, or TSL.","https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fweb-frontend\u002Fneversight-skills_feed-three-best-practices-1.0.2\u002FSKILL.md",[],[121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140],"best practices","three","three best practices","writing","performance","webgpu","guidelines","tasks","code","shaders","Optimization","3D","use","agent-skills","scenes","lighting","triggers","rendering","when","best practices guidelines",{"skillNo":142,"name":143,"nameZh":6,"description":144,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":145,"categories":146,"keywords":147,"publishedAt":39},"01M6R2BPMDYK9XKWPARY0S5MS3","javascript-best-practices","Modern JavaScript best practices and performance optimization guidelines. Use when writing, reviewing, or refactoring JavaScript code to ensure optimal performance, maintainability, and modern patterns.","https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fweb-frontend\u002Fneversight-skills_feed-javascript-best-practices-1.0.2\u002FSKILL.md",[],[148,121,149,124,125,127,129,131,150,151,133,152,153,154,139],"javascript","JavaScript Best Practices","refactoring","performance optimization","patterns","maintainability","name",{"skillNo":156,"name":157,"nameZh":6,"description":158,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":159,"categories":160,"keywords":161,"publishedAt":39},"01M6R2B41BN09FYH76BAC5FT7X","css-best-practices","Guidelines for building modern, performant, and accessible CSS using MDN best practices. Use this when styling components, fixing layout shifts, or optimizing CSS performance.","https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fweb-frontend\u002Fneversight-skills_feed-css-best-practices-1.0.3\u002FSKILL.md",[],[162,121,163,125,164,127,133,165,166,167,139],"css","css best practices","components","styling","layout","for",{"skillNo":169,"name":170,"nameZh":6,"description":171,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":172,"categories":173,"keywords":174,"publishedAt":39},"01M6R2A1CEF123TZCNM1XJ2XQ4","frontend-dev","Full-stack frontend development combining premium UI design, cinematic animations,\nAI-generated media assets, persuasive copywriting, and visual art. Builds complete,\nvisually striking web pages with real media, advanced motion, and compelling copy.\nUse when: building landing pages, marketing sites, product pages, dashboards,\ngenerating media assets (image\u002Fvideo\u002Faudio\u002Fmusic), writing conversion copy,\ncreating generative art, or implementing cinematic scroll animations.","https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fweb-frontend\u002Fminimax-ai-skills-frontend-dev-1.0.1\u002FSKILL.md",[],[175,176,177,178,124,179,162,180,181,182,183,184,185,186,187,188,189,190,191,192],"frontend","dev","frontend dev","design","image","react","ui","Video","Media","Audio","Tailwind","stack","music","aida","framer motion","implementing","framer","generative",{"skillNo":194,"name":195,"nameZh":6,"description":196,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":197,"categories":198,"keywords":199,"publishedAt":39},"01M6R20VQ98KDX4J37TGE0FNHW","exa-rate-limits","Implement Exa rate limiting, backoff, and idempotency patterns.\nUse when handling rate limit errors, implementing retry logic,\nor optimizing API request throughput for Exa.\nTrigger with phrases like \"exa rate limit\", \"exa throttling\",\n\"exa 429\", \"exa retry\", \"exa backoff\".","https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fsearch-research\u002Fjeremylongshore-claude-code-plugins-plus-skills-exa-rate-limits-1.0.0\u002FSKILL.md",[],[200,201,202,203,204,205,206,207,208,152,209,210,211,212,213,190,167,214,215,139],"exa","exa rate limits","rate","limits","throttling","api","read","write","edit","io","throughput","retry","errors","limit","rate limit","request",{"skillNo":217,"name":218,"nameZh":6,"description":219,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":220,"categories":221,"keywords":222,"publishedAt":39},"01M6R20FV0TM7ZZTZNG7JQ1A82","exa-deploy-integration","Deploy Exa integrations to Vercel, Fly.io, and Cloud Run platforms.\nUse when deploying Exa-powered applications to production,\nconfiguring platform-specific secrets, or setting up deployment pipelines.\nTrigger with phrases like \"deploy exa\", \"exa Vercel\",\n\"exa production deploy\", \"exa Cloud Run\", \"exa Fly.io\".","https:\u002F\u002Fgithub.com\u002FVRIL-LABS\u002Fskill-jam\u002Fblob\u002Fmain\u002Fskills\u002Fsearch-research\u002Fjeremylongshore-claude-code-plugins-plus-skills-exa-deploy-integration-1.0.0\u002FSKILL.md",[],[223,224,200,225,226,227,228,229,230,231,206,207,208,232,233,234,209,235,236,139],"deploy","integration","exa deploy integration","deployment","run","secrets","fly","Bash","integrations","vercel","up","platform","trigger","cloud run"]