Two Laravel devs that won't disappear on you. Finally! Hire Joel and Aaron from No Compromises.

Idempotency Laravel

Idempotency Laravel stats

Downloads
1.5K
Stars
83
Open Issues
0
Forks
1

View on GitHub →

Elegant and production-ready idempotency middleware for Laravel APIs.

Idempotency for Laravel

A production-ready Laravel middleware for implementing idempotency in your API requests. Safely retry API calls without worrying about duplicate processing.

What Is Idempotency?

Idempotency ensures that an API operation produces the same result regardless of how many times it is executed. This is critical for payment processing, order submissions, and other operations where duplicate execution could have unintended consequences.

Features

  • Robust Cache Mechanism: Reliably stores and serves cached responses
  • Lock-Based Concurrency Control: Prevents race conditions with distributed locks
  • Comprehensive Telemetry: Track and monitor idempotency operations
  • Alert System: Get notified about suspicious activity
  • Payload Validation: Detect when the same key is used with different payloads
  • Detailed Logging: Easily debug idempotency issues

Installation

You can install the package via composer:

composer require infinitypaul/idempotency-laravel

Configuration

php artisan vendor:publish --provider="Infinitypaul\Idempotency\IdempotencyServiceProvider"

This will create a config/idempotency.php file with the following options:

return [
// Enable or disable idempotency functionality
'enabled' => env('IDEMPOTENCY_ENABLED', true),
 
// HTTP methods that should be considered for idempotency
'methods' => ['POST', 'PUT', 'PATCH', 'DELETE'],
 
// How long to cache idempotent responses (in minutes)
'ttl' => env('IDEMPOTENCY_TTL', 1440), // 24 hours
 
// Validation settings
'validation' => [
// Pattern to validate idempotency keys (UUID format by default)
'pattern' => '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/',
 
// Maximum response size to cache (in bytes)
'max_length' => env('IDEMPOTENCY_MAX_LENGTH', 10485760), // 10MB
],
 
// Alert settings
'alert' => [
// Number of hits before sending an alert
'threshold' => env('IDEMPOTENCY_ALERT_THRESHOLD', 5),
],
 
// Telemetry settings
'telemetry' => [
// Default telemetry driver
'driver' => env('IDEMPOTENCY_TELEMETRY_DRIVER', 'inspector'),
 
// Custom driver class if using 'custom' driver
'custom_driver_class' => null,
],
];

Usage

Add the middleware to your routes or route groups in your routes/api.php file:

Route::middleware(['auth:api', \Infinitypaul\Idempotency\Middleware\EnsureIdempotency::class])
->group(function () {
Route::post('/payments', [PaymentController::class, 'store']);
// Other routes...
});

Using With Requests

To make an idempotent request, clients should include an Idempotency-Key header with a unique UUID:

POST /api/payments HTTP/1.1
Content-Type: application/json
Idempotency-Key: 123e4567-e89b-12d3-a456-426614174000
 
{
"amount": 1000,
"currency": "USD",
"description": "Order #1234"
}

If the same idempotency key is used again with the same payload, the original response will be returned without re-executing the operation.

Response Headers

The middleware adds these headers to responses:

  • Idempotency-Key: The key used for the request
  • Idempotency-Status: Either Original (first request) or Repeated (cached response)

Telemetry

The package provides built-in telemetry through various service. The telemetry records:

  • Request processing time
  • Cache hits and misses
  • Lock acquisition time
  • Response sizes
  • Error rates

Telemetry Drivers

I intend to add more drivers in my free time

Custom Driver

To use a custom telemetry driver, implement the TelemetryDriver interface:

<?php
 
namespace App\Telemetry;
 
use Infinitypaul\Idempotency\Telemetry\TelemetryDriver;
 
class CustomTelemetryDriver implements TelemetryDriver
{
// Implement the required methods...
}

Then update your configuration:

'telemetry' => [
'driver' => 'custom',
'custom_driver_class' => \App\Telemetry\CustomTelemetryDriver::class,
],

Advanced Usage

Custom Events

The package dispatches an events that you can listen for:

\Infinitypaul\Idempotency\Events\IdempotencyAlertFired::class
infinitypaul photo

Software Developer

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Infinitypaul Idempotency Laravel Related Articles

Safely Retry API calls in Laravel image

Safely Retry API calls in Laravel

Read article
DreamzTech logo

DreamzTech

Hire 6-10+ Yrs. experienced skilled Laravel Developers from DreamzTech. We ensure NDA protected, 100% quality delivery. Contact Us & Discuss Your Need.

DreamzTech
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit