Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Doctrine Sharding Laravel Package

cyber/doctrine-sharding

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Use Case Alignment: The package fills a critical gap for Laravel applications requiring horizontal database scaling via sharding, particularly for high-traffic or partitioned data workloads. It aligns with architectures where:
    • Data is logically partitioned (e.g., by tenant, region, or shard key).
    • Read/write scalability is needed beyond vertical scaling.
    • Legacy systems or monolithic apps must adopt sharding without full rewrite.
  • Doctrine ORM Integration: Leverages Doctrine’s existing query builder, entity manager, and connection abstractions, reducing friction for teams already using Doctrine (common in Laravel via doctrine/dbal or laravel-doctrine).
  • Laravel Ecosystem Fit: While Laravel’s default Eloquent does not natively support sharding, this package could bridge the gap for:
    • Hybrid stacks (Eloquent + Doctrine for complex queries).
    • Microservices where Doctrine is the ORM of choice.
    • Legacy Laravel apps migrating to sharded databases.

Integration Feasibility

  • Core Dependencies:
    • Requires Doctrine DBAL (already a dependency for Laravel if using doctrine/dbal or laravel-doctrine/orm).
    • PHP 8.0+ (Laravel 9+ compatible).
    • Database Compatibility: Supports PostgreSQL, MySQL, and SQLite (via DBAL). Key caveat: Sharding logic is database-agnostic, but cross-shard transactions or distributed joins may require manual handling.
  • Laravel-Specific Challenges:
    • Eloquent vs. Doctrine: If the app uses Eloquent exclusively, integration requires:
      • Wrapping Eloquent queries in Doctrine’s QueryBuilder (manual effort).
      • Using a hybrid approach (e.g., Doctrine for sharded tables, Eloquent for non-sharded).
    • Migrations: Schema changes (e.g., adding shard keys) must be manually synchronized across shards.
    • Caching: Doctrine’s second-level cache may need configuration to avoid cross-shard inconsistencies.

Technical Risk

Risk Area Severity Mitigation Strategy
Query Complexity High Cross-shard queries (e.g., JOIN across shards) may fail or require application logic.
Transaction Isolation High Distributed transactions are not supported; use saga pattern or compensating transactions.
Performance Overhead Medium Shard routing adds latency; benchmark with production-like data volumes.
Schema Drift Medium Manual sync of schema changes across shards; consider database migration tools (e.g., Flyway).
Monitoring/Governance Medium Lack of built-in shard-aware metrics; integrate with Prometheus/Grafana for shard health.
Vendor Lock-in Low MIT license reduces risk; but sharding logic is tightly coupled to Doctrine.

Key Questions

  1. Sharding Strategy:
    • Is sharding needed for read scaling, write scaling, or data partitioning? This dictates routing logic (e.g., hash-based vs. range-based).
    • Are there hot shards (uneven data distribution)? If so, dynamic sharding or rebalancing may be needed.
  2. Query Patterns:
    • What percentage of queries are shard-local vs. cross-shard? Cross-shard queries may require application-level handling.
    • Are there complex joins or subqueries that span shards? These may not work out-of-the-box.
  3. Failure Modes:
    • How will the app handle shard failures (e.g., retry logic, fallback shards)?
    • Is multi-region sharding required? The package does not natively support geo-partitioning.
  4. Tooling & Observability:
    • Are there existing tools for shard monitoring (e.g., query latency per shard)?
    • How will schema migrations be synchronized across shards?
  5. Team Expertise:
    • Does the team have experience with Doctrine and sharding? Steep learning curve for cross-shard query optimization.
    • Is there a dedicated DBA team to manage shard-level optimizations (e.g., indexing, partitioning)?

Integration Approach

Stack Fit

  • Primary Fit:
    • Laravel apps using Doctrine ORM (laravel-doctrine/orm) or Doctrine DBAL (doctrine/dbal).
    • Monolithic apps with partitioned data (e.g., SaaS multi-tenancy, large-scale e-commerce).
  • Secondary Fit:
    • Apps using Eloquent can integrate via a hybrid layer (e.g., custom repository pattern to route queries through Doctrine).
    • Microservices where Doctrine is the ORM of choice for sharded services.
  • Non-Fit:
    • Apps relying exclusively on Eloquent without Doctrine integration (high refactoring cost).
    • Serverless or event-driven architectures where connection management is ephemeral.

Migration Path

  1. Assessment Phase:
    • Audit existing queries to identify shard-local vs. cross-shard patterns.
    • Define shard key strategy (e.g., user_id % 4 for 4 shards).
    • Evaluate database compatibility (e.g., PostgreSQL’s native partitioning may reduce need for sharding).
  2. Proof of Concept (PoC):
    • Implement a single sharded table (e.g., users) with manual routing.
    • Test with read-heavy and write-heavy workloads.
    • Validate query performance and failure handling.
  3. Incremental Rollout:
    • Phase 1: Migrate write-heavy tables to sharding (e.g., orders, logs).
    • Phase 2: Add read replicas per shard for scalability.
    • Phase 3: Optimize cross-shard queries (e.g., denormalization, materialized views).
  4. Tooling Integration:
    • Configure Doctrine caching to avoid cross-shard cache stampedes.
    • Set up shard-aware monitoring (e.g., track query latency per shard).

Compatibility

Component Compatibility Workarounds
Doctrine ORM High (direct integration) None
Doctrine DBAL High (core dependency) None
Eloquent Low (requires wrapper layer) Create custom repositories to route Eloquent queries via Doctrine.
Laravel Migrations Medium (manual sync needed) Use post-migration hooks to apply changes to all shards.
Laravel Queues Medium (connection management) Ensure queue workers use shard-aware connection pooling.
Caching (Redis) Medium (cache invalidation risk) Use shard-specific cache prefixes or disable caching for cross-shard queries.
Replication High (supports read replicas per shard) Configure Doctrine connection per shard.

Sequencing

  1. Pre-requisites:
    • Upgrade to Laravel 9+ (PHP 8.0+) and Doctrine DBAL 3.x.
    • Set up database shards (e.g., 4 MySQL instances for shard_1 to shard_4).
  2. Core Integration:
    • Install package: composer require cyber/doctrine-sharding.
    • Configure doctrine-sharding.yaml with shard mappings:
      shards:
        shard_1:
          url: mysql://user:pass@shard1-host/db
          tables: [users, orders]
        shard_2:
          url: mysql://user:pass@shard2-host/db
          tables: [users, orders]
      
    • Update EntityManager to use the sharding extension:
      $config = Setup::createAnnotationMetadataConfiguration([__DIR__.'/../src/Entity']);
      $config->addShardingExtension(new ShardingExtension());
      $em = EntityManager::create($connection, $config);
      
  3. Query Routing:
    • Annotate entities with @Shard or use runtime routing:
      $queryBuilder = $em->getConnection()->createQueryBuilder();
      $queryBuilder->from('users', 'u')
                   ->where('u.shard_key = :shard_key')
                   ->setParameter('shard_key', $user->getShardKey());
      
  4. Testing:
    • Write
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
babenkoivan/elastic-client
innmind/static-analysis
innmind/coding-standard
datacore/hub-sdk
alengo/sulu-http-cache-bundle
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity