user@elrise.io:~/projects/$ ← elrise.io
· [archived] tags: phpsymfonydoctrineshardingbundle

doctrine-shard-manager-bundle — Doctrine sharding for Symfony

An archived Symfony bundle for routing Doctrine ORM and DBAL operations across shards with hash, range, or composite strategies and a shard-aware execution context.

→ repository

Doctrine Shard Manager is a Symfony bundle for distributing Doctrine ORM and DBAL operations across multiple databases or schemas according to an application-defined sharding key.

Key features

  • Routes entities by keys such as userId or clientId.
  • Provides hash, range, and composite sharding strategies.
  • Resolves sharding metadata from configuration or the #[Sharding] attribute.
  • Switches Doctrine connections and entity managers through ShardContext.
  • Supports custom strategies through Symfony tagged iterators.
  • Integrates sharding into repositories without moving routing logic into business services.

Installation

composer require elrise/doctrine-shard-manager

Register the bundle when Symfony Flex is not available:

return [
    Elrise\Bundle\DoctrineShardManager\ElriseDoctrineShardBundle::class => ['all' => true],
];

Configuration

A sharded entity declares its strategy, key, and shard count in configuration or through an attribute:

use Elrise\Bundle\DoctrineShardManager\Attribute\Sharding;

#[Sharding(
    strategy: 'hash',
    key: 'userId',
    shardCount: 4
)]
class User
{
}

Connections and entity managers are registered per shard. Range strategies additionally receive an ordered list of key ranges and target shard identifiers.

Usage

ShardContext selects the target shard for a unit of work and exposes the matching entity manager:

$this->shardContext->withKey(
    $userId,
    function (EntityManagerInterface $entityManager, string $shardId) use ($userId): void {
        $user = $entityManager->getRepository(User::class)->find($userId);
    }
);

Project status

This portfolio entry is archived. The source remains available as a reference implementation of application-level sharding for Symfony and Doctrine.

Source

github.com/alexk136/doctrine-shard-manager-bundle