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

Earmark Laravel Package

poing/earmark

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Use Case Alignment: The package excels in scenarios requiring sequential, gap-free value generation (e.g., phone extensions, ticket numbers, inventory IDs) with recyclable/reusable values. Ideal for systems where:
    • Values must be globally unique (e.g., across users/sessions).
    • Temporary reservations are needed (e.g., checkout flows, call queues).
    • Gap elimination is critical (e.g., avoiding "hole" in sequences like 1, 2, 4).
  • Laravel Synergy: Leverages Laravel’s database transactions, queues, and service container, reducing boilerplate for:
    • Database-backed sequence management.
    • Locking mechanisms (via DB::transaction or lockForUpdate).
    • Configurable behavior (e.g., TTL for recycled values).

Integration Feasibility

  • Low Friction: Designed for Laravel’s ecosystem with:
    • Service Provider: Auto-registers Earmark facade/class.
    • Artisan Commands: Pre-packaged migrations/config publishing.
    • Eloquent Integration: Optional HasEarmarks trait for models.
  • Database Agnostic: Works with any PDO-supported DB (MySQL, PostgreSQL, SQLite), but locking behavior may vary by DB (e.g., SELECT ... FOR UPDATE compatibility).
  • Event-Driven Hooks: Supports EarmarkReserved/EarmarkUnset events for custom logic (e.g., logging, notifications).

Technical Risk

Risk Area Mitigation Strategy
Database Locking Test under high concurrency; adjust lock_timeout in config.
Sequence Gaps Validate unset() logic aligns with business rules (e.g., soft vs. hard deletes).
Migration Conflicts Check for existing earmark_series table; handle schema conflicts proactively.
Performance Monitor reserve()/unset() latency; consider caching for read-heavy use cases.
Recycling Logic Define TTL policies for recycled values (e.g., "reusable after 24h").

Key Questions

  1. Business Rules:
    • Are recycled values immediately reusable or subject to delays?
    • Should sequences be scoped per tenant/user (e.g., user_id prefix)?
  2. Scalability:
    • Will sequences need sharding (e.g., by region) at scale?
    • Is distributed locking (e.g., Redis) required for multi-server setups?
  3. Observability:
    • How will we track orphaned/reserved-but-unused values?
    • Are audit logs needed for compliance (e.g., who reserved/unset a value)?
  4. Fallbacks:
    • What happens if the database is down? (Queue retries? Manual override?)
  5. Testing:
    • How will we simulate race conditions in CI/CD?

Integration Approach

Stack Fit

  • Laravel Core: Native support for:
    • Service Container: Bind Earmark to interfaces for mocking in tests.
    • Events: Extend with custom listeners (e.g., trigger Slack alerts on high usage).
    • Queues: Offload unset() operations to background jobs.
  • Database:
    • MySQL/PostgreSQL: Preferred for FOR UPDATE locking.
    • SQLite: May require adjustments (e.g., BEGIN IMMEDIATE).
  • Testing:
    • Pest/Laravel: Use refreshDatabase() to test sequence gaps.
    • Mocking: Stub DB::lockForUpdate() for unit tests.

Migration Path

  1. Discovery Phase (1–2 days):
    • Audit existing sequence generation (e.g., custom queries, UUIDs).
    • Map use cases to Earmark features (e.g., "phone extensions" → reserve()).
  2. Pilot Implementation (3–5 days):
    • Install package + publish config.
    • Run migrations in a staging-like environment.
    • Test with a non-critical sequence (e.g., "internal ticket IDs").
  3. Incremental Rollout:
    • Phase 1: Replace simple AUTO_INCREMENT with Earmark for 1–2 sequences.
    • Phase 2: Add recycling logic (e.g., unset() after checkout completion).
    • Phase 3: Extend to multi-tenant or scoped sequences.
  4. Deprecation:
    • Phase out legacy sequence logic via feature flags.

Compatibility

  • Laravel Version: Tested on Laravel 8+; verify compatibility with your version (e.g., pestphp/pest for testing).
  • PHP Version: Requires PHP 8.0+ (check composer.json constraints).
  • Dependencies:
    • Doctrine DBAL: For database abstraction (no conflicts if already in use).
    • Laravel Events: Ensure events array is configured in config/app.php.

Sequencing

  1. Pre-requisites:
    • Database connection configured in .env.
    • Laravel’s queue worker running if using async unset().
  2. Order of Operations:
    sequenceDiagram
      participant App as Application
      participant Earmark as Earmark Package
      participant DB as Database
      App->>Earmark: reserve('phone_extensions', 1)
      Earmark->>DB: START TRANSACTION
      DB-->>Earmark: Lock row
      Earmark->>DB: SELECT next_value
      Earmark->>DB: UPDATE last_value + 1
      Earmark->>DB: INSERT reservation
      Earmark-->>App: 12345
      App->>Earmark: unset(12345)
      Earmark->>DB: DELETE reservation
      Earmark->>DB: COMMIT
    
  3. Critical Path:
    • Reserve: Must be atomic (no duplicates).
    • Unset: Should not create gaps (validate with SELECT MAX(value)).

Operational Impact

Maintenance

  • Configuration:
    • Centralized in config/earmark.php (e.g., default_ttl, lock_timeout).
    • Monitor: Watch for earmark_series table bloat (e.g., stale reservations).
  • Updates:
    • Minor updates: composer update poing/earmark.
    • Major updates: Test migrations/config changes in staging.
  • Backups:
    • earmark_series table is small; include in DB backups.

Support

  • Common Issues:
    • Deadlocks: Increase lock_timeout or optimize transaction scope.
    • Sequence Exhaustion: Monitor last_value and set alerts.
    • Recycling Delays: Adjust TTL or add manual override UI.
  • Debugging:
    • Enable Laravel’s query logging to inspect locks.
    • Use Earmark::debug() (if available) to trace reservations.

Scaling

  • Horizontal Scaling:
    • Stateless: Package itself is stateless; scale app servers.
    • Database: Ensure DB can handle high concurrency on earmark_series.
  • Performance Tuning:
    • Indexing: Add indexes to series_name and reserved_at columns.
    • Batch Reserving: Use reserveMany() for bulk operations (e.g., pre-generating IDs).
    • Caching: Cache next_value for read-heavy, low-write scenarios (invalidate on reserve()).
  • Sharding:
    • Partition earmark_series by series_name or use database views.

Failure Modes

Scenario Impact Mitigation
DB Lock Timeout reserve() fails silently. Retry with exponential backoff.
Stale Reservations Values stuck "reserved". Add earmark:prune command.
Migration Failure Schema conflicts. Test migrations in CI.
High Concurrency Throttling/deadlocks. Adjust lock_timeout; use queues.
Package Bug Undocumented gaps. Contribute fixes; fork if needed.

Ramp-Up

  • Onboarding:
    • Documentation: Create internal runbook for:
      • reserve()/unset() workflows.
      • Configuring new series (e.g., php artisan earmark:series phone_extensions).
    • Examples: Share snippets for:
      • Scoped sequences (e.g., user_{id}_extension).
      • Event listeners (e.g., log reservations
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.
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
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
testo/bridge-symfony
spatie/flare-daemon-runtime