PHPackages                             rasuvaeff/yii3-idempotency - 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. [Caching](/categories/caching)
4. /
5. rasuvaeff/yii3-idempotency

ActiveLibrary[Caching](/categories/caching)

rasuvaeff/yii3-idempotency
==========================

Idempotency key middleware for Yii3 APIs

v1.0.1(1mo ago)0111—0%1BSD-3-ClausePHPPHP 8.3 - 8.5CI passing

Since Jun 12Pushed 1w agoCompare

[ Source](https://github.com/rasuvaeff/yii3-idempotency)[ Packagist](https://packagist.org/packages/rasuvaeff/yii3-idempotency)[ Docs](https://github.com/rasuvaeff/yii3-idempotency)[ RSS](/packages/rasuvaeff-yii3-idempotency/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (27)Versions (4)Used By (1)

rasuvaeff/yii3-idempotency
==========================

[](#rasuvaeffyii3-idempotency)

[![Stable Version](https://camo.githubusercontent.com/dbe597ba388ed446d66317f0ba9b0c080ecd5743a70c80acf2017d5d320ad005/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7261737576616566662f796969332d6964656d706f74656e63792e7376673f6c6162656c3d737461626c65)](https://packagist.org/packages/rasuvaeff/yii3-idempotency)[![Total Downloads](https://camo.githubusercontent.com/13f08ae9f6b1fa75637776835a8e6855a67a1bb60cdb08b2e95f5d05283a8eb5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7261737576616566662f796969332d6964656d706f74656e63792e737667)](https://packagist.org/packages/rasuvaeff/yii3-idempotency)[![Build](https://camo.githubusercontent.com/6de2425b8b0c5aa8d0d1c21fc54e835a2ab616a9ad48c38c69381e9509c48fce/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7261737576616566662f796969332d6964656d706f74656e63792f6275696c642e796d6c3f6272616e63683d6d6173746572)](https://github.com/rasuvaeff/yii3-idempotency/actions)[![Static analysis](https://camo.githubusercontent.com/db0efc06cd514eafce15e01dea12234bb4ced3a0a3602e215169ee6827d8ad00/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7073616c6d2d6c6576656c2d312d626c7565)](https://psalm.dev)[![Coverage](https://camo.githubusercontent.com/32855e94577df9d0a30995653b17d33a5fbfdf644518f96ea0374313397d19b7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f7665726167652d3130302532352d627269676874677265656e)](https://github.com/rasuvaeff/yii3-idempotency)[![PHP](https://camo.githubusercontent.com/161b793f0f145d8338bbaf4e2fa52b36e4909b1fa9ba502a23c0befb58682d0b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f7261737576616566662f796969332d6964656d706f74656e63792f706870)](https://packagist.org/packages/rasuvaeff/yii3-idempotency)[![License](https://camo.githubusercontent.com/c7cf1ddddef0b95f95378d62c474dee80304cb79a2be14f21edb215ab8617564/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7261737576616566662f796969332d6964656d706f74656e63792e737667)](https://github.com/rasuvaeff/yii3-idempotency/blob/master/LICENSE.md)[Русская версия](README.ru.md)

Idempotency key middleware for Yii3 APIs. Prevents duplicate processing of POST/PUT/PATCH requests.

> Using an AI coding assistant? [llms.txt](llms.txt) contains a compact API reference you can feed to the LLM.

Requirements
------------

[](#requirements)

- PHP 8.3+
- `psr/clock` ^1.0
- `psr/http-message` ^2.0
- `psr/http-server-middleware` ^1.0

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

[](#installation)

```
composer require rasuvaeff/yii3-idempotency
```

Usage
-----

[](#usage)

### Basic setup

[](#basic-setup)

```
use Rasuvaeff\Yii3Idempotency\HeaderIdempotencyKeyExtractor;
use Rasuvaeff\Yii3Idempotency\IdempotencyMiddleware;
use Rasuvaeff\Yii3Idempotency\InMemoryIdempotencyStorage;

$middleware = new IdempotencyMiddleware(
    keyExtractor: new HeaderIdempotencyKeyExtractor(),
    storage: new InMemoryIdempotencyStorage($clock),
    responseFactory: $responseFactory,
    clock: $clock,
    ttlSeconds: 3600,
);
```

### How it works

[](#how-it-works)

ScenarioResultNo idempotency key, `PassThrough` policyRequest passes throughNo idempotency key, `Reject` policy400 Bad RequestFirst request with keyHandler processes, response storedSame key + same payloadStored response replayed (handler not called)Same key + different payload422 Unprocessable ContentSame key while first request is still processing409 ConflictNon-2xx handler response (3xx/4xx/5xx)Response NOT stored — claim released, client may retry with the same keyNon-configured method (e.g. `GET`, `DELETE`)Passes through untouched — idempotency applies only to `methods` (default POST/PUT/PATCH)Expired recordRequest processed as new### Configuration

[](#configuration)

```
// config/params.php
return [
    'rasuvaeff/yii3-idempotency' => [
        'headerName' => 'Idempotency-Key',
        'policy' => 'pass_through', // or 'reject'
        'ttlSeconds' => 3600,
        'methods' => ['POST', 'PUT', 'PATCH'], // methods idempotency applies to
    ],
];
```

Public API
----------

[](#public-api)

ClassDescription`IdempotencyMiddleware`PSR-15 middleware`IdempotencyKey`Validated key value object (1-255 chars, `[A-Za-z0-9._-]+`)`IdempotencyFingerprint`Request fingerprint (method + path + query + body hash)`IdempotencyRecord`Stored record with TTL`IdempotencyResponse`Captured response (status, headers, body)`IdempotencyStorage`Interface: load, claim, store, release`IdempotencyKeyExtractor`Interface for key extraction strategies`InMemoryIdempotencyStorage`In-memory implementation (for testing)`HeaderIdempotencyKeyExtractor`Extracts key from request header`IdempotencyPolicy`Enum: `PassThrough`, `Reject`Security
--------

[](#security)

- Fingerprint includes method, path, query string, and body — prevents payload substitution
- Request body stream is rewound after fingerprinting — handlers can re-read it
- Only 2xx responses are cached; non-2xx (incl. retryable 409/423/429 and any 5xx) release the claim, so a transient failure cannot be replayed for the whole TTL
- Idempotency applies only to the configured `methods` (default POST/PUT/PATCH) — safe methods pass through
- Atomic claim prevents race conditions (in persistent storage adapters)
- TTL prevents indefinite storage

Examples
--------

[](#examples)

See [`examples/`](examples/) for runnable scripts.

Development
-----------

[](#development)

```
make install
make build
make cs-fix
make test
make test-coverage
make mutation
make release-check
```

`make test-coverage` and `make mutation` bootstrap `pcov` inside the `composer:2` container because the base image has no coverage driver.

License
-------

[](#license)

BSD-3-Clause. See [LICENSE.md](LICENSE.md).

###  Health Score

46

—

FairBetter than 92% of packages

Maintenance96

Actively maintained with recent releases

Popularity14

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~15 days

Total

2

Last Release

31d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b0812d5572a7041dfe36e222d295b2e6dc55833a605350fcde58a51a5965ed30?d=identicon)[rasuvaeff](/maintainers/rasuvaeff)

---

Top Contributors

[![rasuvaeff](https://avatars.githubusercontent.com/u/1352718?v=4)](https://github.com/rasuvaeff "rasuvaeff (30 commits)")

---

Tags

apiduplicate-preventionidempotencymiddlewarephppsr-15redisyii3middlewareapiyii3idempotencydeduplication

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/rasuvaeff-yii3-idempotency/health.svg)

```
[![Health](https://phpackages.com/badges/rasuvaeff-yii3-idempotency/health.svg)](https://phpackages.com/packages/rasuvaeff-yii3-idempotency)
```

###  Alternatives

[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k16](/packages/tempest-framework)[cakephp/cakephp

The CakePHP framework

8.8k19.5M1.8k](/packages/cakephp-cakephp)[typo3/cms

TYPO3 CMS is a free open source Content Management Framework initially created by Kasper Skaarhoj and licensed under GNU/GPL.

1.2k1.9M122](/packages/typo3-cms)[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

85036.3k](/packages/flow-php-flow)[mcp/sdk

Model Context Protocol SDK for Client and Server applications in PHP

1.5k1.5M108](/packages/mcp-sdk)[typo3/cms-core

TYPO3 CMS Core

3713.2M5.2k](/packages/typo3-cms-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
