PHPackages                             tourze/symfony-snowflake-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. tourze/symfony-snowflake-bundle

ActiveSymfony-bundle[Database &amp; ORM](/categories/database)

tourze/symfony-snowflake-bundle
===============================

Symfony Bundle for Snowflake ID generation with Redis support

1.0.2(3mo ago)224.8k1MITPHPCI passing

Since Apr 20Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/tourze/symfony-snowflake-bundle)[ Packagist](https://packagist.org/packages/tourze/symfony-snowflake-bundle)[ RSS](/packages/tourze-symfony-snowflake-bundle/feed)WikiDiscussions master Synced today

READMEChangelog (7)Dependencies (21)Versions (8)Used By (1)

Symfony Snowflake Bundle
========================

[](#symfony-snowflake-bundle)

[English](README.md) | [中文](README.zh-CN.md)

[![Packagist](https://camo.githubusercontent.com/e8d9c78b71e399ab7f1497d3972c5393f2e433a6cfa4d8f06eb42d952eb39dce/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746f75727a652f73796d666f6e792d736e6f77666c616b652d62756e646c652e737667)](https://packagist.org/packages/tourze/symfony-snowflake-bundle)

[![PHP Version](https://camo.githubusercontent.com/12f8fab7c0bfb75ada5a780a6443e4270d99bbed9520c1ae20fe4bf686b35756/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f746f75727a652f73796d666f6e792d736e6f77666c616b652d62756e646c652e737667)](https://packagist.org/packages/tourze/symfony-snowflake-bundle)

[![Build Status](https://camo.githubusercontent.com/07c5a0015c097e0dfbb44d4220df0eed6a623d83eceb02ac299fe96b8e4e1a73/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f746f75727a652f7068702d6d6f6e6f7265706f2f63692e796d6c3f6272616e63683d6d6173746572)](https://github.com/tourze/php-monorepo/actions)

[![Coverage Status](https://camo.githubusercontent.com/9cb168340a6d5a1bdda8e16dafe8eed3d60f1441986fb63de17bff84ee6a18f0/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f746f75727a652f7068702d6d6f6e6f7265706f)](https://codecov.io/gh/tourze/php-monorepo)

[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](LICENSE)

Table of Contents
-----------------

[](#table-of-contents)

- [Introduction](#introduction)
- [Features](#features)
- [Installation](#installation)
    - [Requirements](#requirements)
    - [Install via Composer](#install-via-composer)
- [Quick Start](#quick-start)
    - [1. Register the Bundle (if not auto-discovered)](#1-register-the-bundle-if-not-auto-discovered)
    - [2. Generate a Snowflake ID](#2-generate-a-snowflake-id)
- [Usage](#usage)
    - [Basic Usage](#basic-usage)
    - [ID Format and Structure](#id-format-and-structure)
    - [WorkerId Generation](#workerid-generation)
    - [Redis-Based Sequence Resolver](#redis-based-sequence-resolver)
- [Configuration](#configuration)
- [Advanced Usage](#advanced-usage)
    - [Custom Sequence Resolver](#custom-sequence-resolver)
- [Best Practices](#best-practices)
- [Potential Pitfalls](#potential-pitfalls)
- [Contributing](#contributing)
- [License](#license)

Introduction
------------

[](#introduction)

A high-performance, distributed Snowflake ID generator bundle for Symfony applications. This bundle implements Twitter's Snowflake algorithm to generate unique, time-ordered, 64-bit IDs for distributed systems. It is designed for scenarios requiring globally unique IDs under high concurrency.

Features
--------

[](#features)

- Generates 64-bit unique IDs based on [godruoyi/php-snowflake](https://github.com/godruoyi/php-snowflake) library
- Built-in Redis sequence resolver to ensure uniqueness in high-concurrency environments
- Auto-generates WorkerId based on hostname for distributed scenario support
- Zero configuration required for basic usage
- Fully compatible with Symfony 7.3+
- Graceful fallback from Redis to RandomSequenceResolver when Redis is unavailable
- Autowiring support for easy integration with Symfony services
- Thread-safe ID generation
- Time-ordered IDs for efficient database indexing

Installation
------------

[](#installation)

### Requirements

[](#requirements)

- PHP &gt;= 8.1
- Symfony &gt;= 7.3
- Redis (recommended for distributed sequence safety)

### Install via Composer

[](#install-via-composer)

```
composer require tourze/symfony-snowflake-bundle
```

Quick Start
-----------

[](#quick-start)

### 1. Register the Bundle (if not auto-discovered)

[](#1-register-the-bundle-if-not-auto-discovered)

Add to `config/bundles.php`:

```
return [
    // ...
    Tourze\SnowflakeBundle\SnowflakeBundle::class => ['all' => true],
];
```

### 2. Generate a Snowflake ID

[](#2-generate-a-snowflake-id)

Inject the Snowflake service into your service or controller:

```
