Multi-Tenancy & Sharding Documentation

January 8, 2026 · View on GitHub

This directory contains documentation for PropertyWebBuilder's multi-tenancy architecture and database sharding system.

Core Documentation

Architecture & Reference

Sharding

Shard Admin Implementation (NEW - 2026-01-08)

Shard Admin Implementation

Overview

The Shard Admin project adds a complete UI for managing database shards in the Tenant Admin interface.

Status: Planning Complete ✅
Implementation: Not Started
Estimated Effort: 2-3 weeks

What Will Be Built

Phase 1 (Week 1): Foundation

  • Shard dashboard showing all configured shards
  • Website shard assignment UI
  • Audit logging for all changes
  • Health monitoring basics

Phase 2 (Week 2): Advanced Features

  • Detailed health monitoring with PgHero
  • Bulk shard assignment
  • Statistics & capacity tracking
  • Turbo/Stimulus dynamic UI

Phase 3 (Week 3): Migration Tools

  • Background migration jobs
  • Progress tracking
  • Verification & rollback
  • Email notifications

Key Features

Read from Actual Codebase

  • Uses Pwb::ShardRegistry (not invented API)
  • Uses Pwb::TenantShardMigrator (actual class name)
  • Integrates with existing PgHero installation

Modern Rails 8

  • Turbo Streams for instant updates
  • Stimulus controllers for interactivity
  • Solid Queue for background jobs
  • Feature flags for gradual rollout

Production Ready

  • Comprehensive validation
  • Risk mitigation strategies
  • Audit trail for all operations
  • CLI tools (Rake tasks)
  • API endpoints for monitoring

Quick Reference

Access: /tenant_admin/shards (requires TENANT_ADMIN_EMAILS)

CLI Commands:

# Status check
bin/rails pwb:shards:status

# Assign website to shard
bin/rails pwb:shards:assign[42,shard_1]

# Health check
bin/rails pwb:shards:health

Feature Flags (Progressive Rollout):

SHARD_ADMIN_ENABLED=true          # Enable dashboard
SHARD_ASSIGNMENT_ENABLED=true     # Enable assignment
SHARD_MIGRATIONS_ENABLED=true     # Enable data migration

Getting Started

  1. Read the Plan:

    • Start with SHARD_ADMIN_IMPLEMENTATION_PLAN.md
    • Review SHARD_ADMIN_IMPLEMENTATION_UPDATES.md for codebase-specific details
  2. Understand Dependencies:

    • Existing: Pwb::ShardRegistry, Pwb::TenantShardMigrator, PgHero
    • New: Pwb::ShardService, Pwb::ShardHealthCheck, Pwb::ShardAuditLog
  3. Phase 1 Implementation:

    • Create database migration for pwb_shard_audit_logs
    • Create routes in tenant_admin namespace
    • Create ShardsController
    • Create views with Tailwind CSS
    • Add shard column to websites index

Risk Assessment

RiskMitigation
Unconfigured shard assignmentValidate with ShardRegistry.configured?
Data loss during migrationTransactions, verification, source preservation
Concurrent operationsDatabase locks, single-queue processing
Permission escalationStrict TENANT_ADMIN_EMAILS validation

Success Criteria

  • All tests passing (unit, controller, integration)
  • Can view all configured shards
  • Can assign websites to shards
  • Audit logs created for all changes
  • Health monitoring working
  • Documentation complete

Additional Resources

  • Database configuration: config/database.yml
  • Shard registry: app/lib/pwb/shard_registry.rb
  • Migrator service: app/services/pwb/tenant_shard_migrator.rb
  • Application record: app/models/pwb_tenant/application_record.rb

Questions?

See the implementation plan for detailed answers on:

  • Architecture decisions
  • Security model
  • Testing strategy
  • Deployment procedures
  • Troubleshooting guide