This repository contains a comprehensive multi-tenant database sharding solution with Spring Boot auto-configuration.
java-backend-dev/
โโโ sharding-springboot-starter/ # ๐ NEW: Advanced sharding library (Java + Lombok)
โโโ sample-sharded-app/ # ๐ NEW: Demo application
โโโ PROJECT_OVERVIEW.md # This file
Location: sharding-springboot-starter/
Language: Java with Lombok
Purpose: Production-ready multi-tenant database sharding with comprehensive features
โ
Directory-Based Sharding - Tenant-to-shard mapping via tenant_shard_mapping table
โ
Multi-Replica Support - Master-replica configuration with automatic read/write splitting
โ
Auto-Configuration - Zero-config Spring Boot integration
โ
Entity Validation - @ShardedEntity annotation validation during startup
โ
Query Validation - SQL-level tenant filtering with configurable strictness
โ
Connection Routing - Intelligent routing based on tenant context
โ
HikariCP Optimization - Database-specific tuning with optimal defaults
โ
Batch Processing - Tenant iterator for background jobs
โ
Monitoring - JMX metrics and routing statistics
โ
Multi-Database Support - Native MySQL and PostgreSQL support with database-specific optimizations
โ
Dual DataSource Configuration - Automatic separation of global and sharded entities with package-based routing
ShardingConfigProperties- Main configuration with flat structureHikariConfigProperties- Comprehensive HikariCP settingsShardingAutoConfiguration- Spring Boot auto-configurationShardingDataSourceAutoConfiguration- Dual DataSource auto-configuration with Lombok-based propertiesDualDataSourceProperties- Package-based routing configuration (Lombok)HikariConfigUtil- Optimal defaults and database-specific optimizations
TenantContext- Thread-local tenant informationTenantInfo- Immutable tenant data holder
TenantShardMappingRepository- Directory-based tenant-shard mappingTenantShardMapping- Data model for lookup tableShardUtils- Comprehensive shard management utilitiesDatabaseSqlProvider- Interface for database-specific SQL operationsMySQLSqlProvider- MySQL-specific SQL implementationPostgreSQLSqlProvider- PostgreSQL-specific SQL implementationDatabaseSqlProviderFactory- Automatic database detection and provider selection
ShardAwareDataSourceDelegate- Routes connections based on tenant contextRoutingDataSource- Spring DataSource integrationRoutingDataSource- Enhanced routing DataSource with dual configuration supportShardDataSources- Master-replica container with load balancing
QueryValidator- SQL query validation for tenant filteringEntityValidator-@ShardedEntityannotation validationValidatingDataSource- DataSource proxy for query interception
TenantIterator- Batch processing with parallel support- Background job utilities for cross-tenant operations
@ShardedEntity- Marker for sharded entities
PostgreSQL Configuration (Default):
# Global Database
app.sharding.global-db.url=jdbc:postgresql://localhost:5432/global_db
app.sharding.global-db.username=global_user
app.sharding.global-db.password=global_password
# Shard Configuration
app.sharding.shards.shard1.master.url=jdbc:postgresql://localhost:5432/shard1_db
app.sharding.shards.shard1.replicas.replica1.url=jdbc:postgresql://localhost:5433/shard1_db
app.sharding.shards.shard1.hikari.maximum-pool-size=20
app.sharding.shards.shard1.latest=true
# Validation
app.sharding.validation.strictness=STRICT
app.sharding.tenant-column-names=tenant_id,company_idAlternative Database Support:
- Full MySQL 5.7+ support with optimizations
- Full PostgreSQL 11+ support with optimizations
- Automatic database type detection from JDBC URLs
Add the sharding starter to your project's pom.xml:
<dependency>
<groupId>com.valarpirai</groupId>
<artifactId>sharding-springboot-starter</artifactId>
<version>1.0.0</version>
</dependency>@Entity
@ShardedEntity // Routes to tenant-specific shard
public class Customer {
@Column(name = "tenant_id", nullable = false)
private Long tenantId;
// ... other fields
}
@Service
public class CustomerService {
public Customer save(Customer customer) {
return TenantContext.executeInTenantContext(customer.getTenantId(), () -> {
return customerRepository.save(customer);
});
}
}Location: sample-sharded-app/
Purpose: Complete demonstration of the sharding library
- Sharded entities (
Customer) with tenant isolation - Non-sharded entities (
GlobalConfig) in global database - REST API with tenant context management
- Database setup scripts with sample data
- Configuration examples
POST /api/customers/set-tenant/{tenantId} # Set tenant context
POST /api/customers # Create customer
GET /api/customers # Get customers for tenant
GET /api/customers/{id} # Get specific customer
PUT /api/customers/{id} # Update customer
DELETE /api/customers/{id} # Delete customer
- STRICT: Throw exception if tenant_id missing (Production)
- WARN: Log warning but proceed (Development)
- LOG: Info logging only (Testing)
- DISABLED: No validation (Not recommended)
- โ MySQL 5.7+ - Full support with MySQL-specific optimizations
- โ PostgreSQL 11+ - Full support with PostgreSQL-specific optimizations
- ๐ SQL Server - Planned for future release
- ๐ Oracle - Planned for future release
- MySQL: Prepared statement caching, server-side preparation, UTF8MB4 charset support
- PostgreSQL: Statement cache optimization, prepared statement thresholds, cache size tuning
- SQL Server: Statement pooling configuration (planned)
- Oracle: Implicit statement caching (planned)
The library automatically detects the database type from JDBC URLs and applies appropriate SQL syntax and optimizations:
- MySQL: Uses
DATABASE()function and MySQL-specific table creation syntax - PostgreSQL: Uses
current_schema()function and PostgreSQL-specific syntax - Fallback: Defaults to MySQL provider if detection fails
maximum-pool-size: 20 (balanced for production)minimum-idle: 5 (maintain ready connections)connection-timeout: 30s (standard timeout)idle-timeout: 10m (cleanup idle connections)max-lifetime: 30m (connection refresh)
- ROUND_ROBIN: Distribute load across replicas
- RANDOM: Random replica selection
- FIRST_AVAILABLE: Always use first replica
# 1. Build the sharding library
mvn clean install -Dmaven.test.skip=true
# 2. Set up databases
cd sample-sharded-app
psql -U postgres -f database-setup.sql
# 3. Run the sample application
mvn spring-boot:run
# 4. Access API documentation
open http://localhost:8080/swagger-ui/- Migration Guide - Complete Liquibase migration guide with strategies
- Idempotency Guide - Understanding migration idempotency
- Zero-Downtime Best Practices - PayPal-inspired zero-downtime strategies
- Transaction Guide - Transaction patterns for sharded databases
- Account Signup Flow - Detailed signup implementation
- Custom Shard Lookup - Custom lookup implementations
- Library Specification - Technical specifications
- Sample App Guide - Complete demo application
- Database Setup - Database configuration
- Global Database: Contains
tenant_shard_mappingand global entities (PostgreSQL by default) - Shard Databases: Contains tenant-specific data with replicas (PostgreSQL by default)
- Connection Pools: Optimized per database type and load with automatic database detection
- Schema Management: Automatic table creation with database-specific syntax and optimizations
Quick PostgreSQL Setup:
cd sample-sharded-app
psql -U postgres -f database-setup-postgresql.sqlSee sample-sharded-app/DATABASE_SETUP.md for detailed setup instructions.
- Externalize database credentials
- Configure appropriate pool sizes per environment
- Set validation strictness to STRICT in production
- Enable JMX monitoring for observability
- Horizontal: Add new shards for capacity
- Vertical: Scale individual shard resources
- Replica Scaling: Add read replicas for read-heavy workloads
- Tenant Balancing: Use latest shard feature for growth
โ Zero Configuration - Auto-configuration with sensible defaults โ Type Safety - Compile-time validation of entity annotations โ Clear Errors - Helpful error messages and validation โ Flexible Validation - Configurable strictness levels โ Rich APIs - Comprehensive utilities for shard management โ Database Agnostic - Seamless MySQL and PostgreSQL support with automatic detection
โ Performance - Database-specific optimizations for MySQL and PostgreSQL โ Monitoring - JMX metrics and health endpoints โ Scalability - Easy shard addition and tenant balancing โ Reliability - Connection pooling and replica support โ Security - Query validation prevents data leakage โ Database Flexibility - Switch between MySQL and PostgreSQL without code changes
โ Multi-Tenancy - Complete tenant isolation โ Scalability - Handle millions of tenants across shards โ Performance - Optimized database operations โ Compliance - Data isolation and tenant security โ Cost Efficiency - Resource optimization per shard
Potential areas for extension:
- Cross-shard joins and distributed queries
- Automated shard rebalancing
- Read replica auto-failover
- GraphQL integration
- Distributed transaction support
- Tenant migration utilities
- Performance analytics dashboard
This project provides a production-ready foundation for multi-tenant applications requiring sophisticated database sharding with comprehensive Spring Boot integration.