Skip to content

Shomrat-Gil/UlidCollisionPrevention

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

EnsureUniqueModelUlidServiceProvider

Introduction

The EnsureUniqueModelUlidServiceProvider for Laravel is designed to enhance the robustness and reliability of applications requiring globally unique identifiers. ULIDs (Universally Unique Lexicographically Sortable Identifiers) combine the benefits of UUIDs with the ability to sort them chronologically. They are ideal for use in distributed systems where both uniqueness and sortability are required. However, ensuring the uniqueness of ULIDs in high-throughput environments can be challenging. This service provider addresses that challenge by implementing a caching mechanism to prevent the reuse of ULIDs, thereby preventing collisions.

Why It Is Needed

ULIDs are unique based on their timestamp and random components. The timestamp uses a millisecond time component that provides a high degree of temporal granularity. This precise timing, while significantly reducing the likelihood of duplicate identifiers, still poses a minimal risk in high-throughput or distributed environments where ULIDs might be generated in close succession. The EnsureUniqueModelUlidServiceProvider mitigates this risk by caching each new ULID for 1 second. This short-term caching prevents the immediate reuse of the same ULID, ensuring that each generated ULID remains unique across the system.

Benefits

  • Uniqueness Assurance: Guarantees that every ULID generated is unique at the time of creation, preventing primary key conflicts in the database.
  • Performance Optimization: Minimizes database index fragmentation due to the chronological order of ULIDs, enhancing performance for insertions and queries.
  • Scalability: Facilitates scaling in distributed systems where multiple instances might generate IDs simultaneously.
  • Simplicity: Integrates seamlessly with Laravel's Eloquent models, requiring minimal configuration and setup by developers.
  • Precision Caching: Utilizes a 1-second cache to effectively prevent the duplication of ULIDs, capitalizing on the millisecond precision to maintain high granularity and reliability in ID generation.

Usage

Simply include this service provider in your application, and it will automatically apply to any model that uses a ULID as its primary key. No further action is required on the part of the developer, making it an easy and effective solution for managing ULIDs in Laravel applications.

Installation

To use the EnsureUniqueModelUlidServiceProvider in your Laravel project, follow these steps:

Register the EnsureUniqueModelUlidServiceProvider in your config/app.php under the providers array:

'providers' => [
    // Other Service Providers
    App\Providers\EnsureUniqueModelUlidServiceProvider::class,
],

License

This project is open-sourced software licensed under the MIT license.

About

Enhance the robustness and reliability of PK ULID.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages