PHPackages                             rasuvaeff/yii3-idempotency-db - 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. rasuvaeff/yii3-idempotency-db

ActiveLibrary[Database &amp; ORM](/categories/database)

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

Database-backed idempotency storage for Yii3 APIs

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

Since Jun 12Pushed 1w agoCompare

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

READMEChangelogDependencies (31)Versions (4)Used By (0)

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

[](#rasuvaeffyii3-idempotency-db)

[![Stable Version](https://camo.githubusercontent.com/bf61c18a09f43587289297818481b6a156e15140a8a5b4acf4d98e1ae327976b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7261737576616566662f796969332d6964656d706f74656e63792d64622e7376673f6c6162656c3d737461626c65)](https://packagist.org/packages/rasuvaeff/yii3-idempotency-db)[![Total Downloads](https://camo.githubusercontent.com/15d2e3a84716ef46318f0ffa641053a70810876eda8e5ad90e986f177a2f1d56/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7261737576616566662f796969332d6964656d706f74656e63792d64622e737667)](https://packagist.org/packages/rasuvaeff/yii3-idempotency-db)[![Build](https://camo.githubusercontent.com/2e72dab6cd239df14a6252246bccba7bbf343fbb14b5006c7d6f2b6c62ff0512/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7261737576616566662f796969332d6964656d706f74656e63792d64622f6275696c642e796d6c3f6272616e63683d6d6173746572)](https://github.com/rasuvaeff/yii3-idempotency-db/actions)[![Static analysis](https://camo.githubusercontent.com/ab9795bdb4671297a7a8ecd05eee5bd3077d7d6f38efc5090af26b9f92999a1f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7261737576616566662f796969332d6964656d706f74656e63792d64622f7374617469632d616e616c797369732e796d6c3f6272616e63683d6d6173746572266c6162656c3d7073616c6d)](https://github.com/rasuvaeff/yii3-idempotency-db/actions)[![License](https://camo.githubusercontent.com/e23cc229c717a4dc2fe1a75e62dcbbd83e79fed3f4133966df56a3b2006967dc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7261737576616566662f796969332d6964656d706f74656e63792d64622e737667)](https://github.com/rasuvaeff/yii3-idempotency-db/blob/master/LICENSE.md)[Русская версия](README.ru.md)

Database-backed idempotency storage for Yii3 APIs. Implements `IdempotencyStorage` from `rasuvaeff/yii3-idempotency` with atomic claim via `INSERT` (unique PK), response replay, and TTL-based expiration.

> Using an AI coding assistant? [llms.txt](llms.txt) contains a compact API reference you can paste into your prompt.

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

[](#requirements)

- PHP 8.3+
- `rasuvaeff/yii3-idempotency` ^1.0
- `yiisoft/db` ^2.0
- `yiisoft/db-migration` ^2.0
- `psr/clock` ^1.0

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

[](#installation)

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

Usage
-----

[](#usage)

### Basic setup

[](#basic-setup)

```
use Rasuvaeff\Yii3IdempotencyDb\DbIdempotencyStorage;
use Rasuvaeff\Yii3Idempotency\HeaderIdempotencyKeyExtractor;
use Rasuvaeff\Yii3Idempotency\IdempotencyMiddleware;

$storage = new DbIdempotencyStorage(
    db: $connection,           // yiisoft/db ConnectionInterface
    clock: $clock,             // PSR-20 ClockInterface
    table: 'idempotency_keys',
    claimTtlSeconds: 3600,     // deadline for in-flight claims (stale-claim recovery)
);

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

### Run migration

[](#run-migration)

```
yii migrate/up
```

Or use the migration class directly:

```
use M260611000000CreateIdempotencyKeysTable;

$migration = new M260611000000CreateIdempotencyKeysTable(table: 'idempotency_keys');
$migration->up($builder);
```

### Table schema

[](#table-schema)

ColumnTypeDescription`key``VARCHAR(255)` PKIdempotency key value`fingerprint``VARCHAR(64)`SHA-256 hash of method + path + query + body`status_code``SMALLINT`HTTP response status code`headers``TEXT`JSON-encoded response headers (`array`)`body``TEXT`Response body`expires_at``VARCHAR(30)`Expiration timestamp (UTC, `Y-m-d H:i:s`)`claimed``BOOLEAN`Whether the key is claimed (in-progress)### Yii3 integration

[](#yii3-integration)

The package provides `config/di.php` and `config/params.php` for `yiisoft/config`.

Default params:

```
// config/params.php
return [
    'rasuvaeff/yii3-idempotency-db' => [
        'table' => 'idempotency_keys',
        'claimTtlSeconds' => 3600,
    ],
];
```

DI wiring binds `IdempotencyStorage::class` to `DbIdempotencyStorage`.

How it works
------------

[](#how-it-works)

1. **Claim**: `INSERT` with unique PK on `key` and `expires_at = now + claimTtlSeconds`. If the insert succeeds, the key is claimed atomically. A duplicate key raises a DB integrity error, which `claim()` converts to `false`; any other DB error propagates.
2. **Store**: After the handler completes, the response is upserted into the row and `claimed` is set to `0`; `expires_at` becomes the record TTL deadline.
3. **Load**: On a subsequent request with the same key, `load()` reads the row. An active claim (`claimed = 1`, deadline not reached) returns `null` without deleting the row — the middleware then fails its own `claim()` and responds 409. A stale claim (deadline passed — crashed process) is deleted and may be re-claimed. A completed record is rehydrated via `IdempotencyRecord::restore()` and checked against its TTL; expired records are deleted.
4. **Release**: If the handler throws (or returns 5xx), `release()` deletes the claim row.
5. **Cleanup**: `deleteExpired()` removes all rows past `expires_at` (uses the `idx_idempotency_expires_at` index) — call it from a cron task.

Security
--------

[](#security)

- Idempotency keys are validated by core (`IdempotencyKey`).
- Fingerprints are SHA-256 hashes — no raw user input stored beyond the key.
- Response bodies are stored as-is; avoid storing sensitive data without encryption at the application layer.
- All timestamps are stored in UTC — storage behavior does not depend on the PHP default timezone.

Examples
--------

[](#examples)

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

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

[](#development)

```
make install        # composer install
make build          # full gate (validate + normalize + cs + psalm + test)
make cs-fix         # fix code style
make psalm          # static analysis
make test           # run testo
make test-coverage  # testo with coverage
make mutation       # mutation testing
make release-check  # build + rector + bc-check + mutation
```

License
-------

[](#license)

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

###  Health Score

42

—

FairBetter than 88% of packages

Maintenance96

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community6

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 (12 commits)")

---

Tags

databasedbidempotencyphpstorageyii3databasestoragedbyii3idempotency

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[robmorgan/phinx

Phinx makes it ridiculously easy to manage the database migrations for your PHP app.

4.5k48.7M472](/packages/robmorgan-phinx)[kreait/firebase-php

Firebase Admin SDK

2.4k44.4M87](/packages/kreait-firebase-php)[spatie/laravel-translation-loader

Store your language lines in the database, yaml or other sources

8453.2M74](/packages/spatie-laravel-translation-loader)[aura/sqlquery

Object-oriented query builders for MySQL, Postgres, SQLite, and SQLServer; can be used with any database connection library.

4883.1M40](/packages/aura-sqlquery)[sonata-project/entity-audit-bundle

Audit for Doctrine Entities

6421.1M1](/packages/sonata-project-entity-audit-bundle)[danielme85/laravel-log-to-db

Custom Laravel Log channel handler that can store log events to SQL or MongoDB databases. Uses Laravel native logging functionality.

1341.0M1](/packages/danielme85-laravel-log-to-db)

PHPackages © 2026

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