Create idempotent consumer

From dykyi-roman/awesome-claude-code · View interactive version

Create idempotent consumer is an AI agent skill from the GitHub repository dykyi-roman/awesome-claude-code. Generates Idempotent Consumer components for PHP 8.4. Creates message deduplication infrastructure with idempotency key management, storage backends, middleware, and unit tests.

How to use this skill

Skill file

Idempotent Consumer Generator

Creates Idempotent Consumer pattern infrastructure for exactly-once message processing guarantees.

When to Use

| Scenario | Example |

|----------|---------|

| Payment processing | Prevent double charges on message replay |

| Inventory updates | Avoid duplicate stock deductions |

| Event handlers | Process domain events exactly once |

| Webhook processing | Handle duplicate webhook deliveries |

| Message queue consumers | Deduplicate redelivered messages |

Component Characteristics

IdempotencyKey

IdempotencyStoreInterface

DatabaseIdempotencyStore

RedisIdempotencyStore

IdempotentConsumerMiddleware

ProcessingResult


Generation Process

Step 1: Analyze Request

Determine:

Step 2: Generate Core Components

  1. Domain Layer (src/Domain/Shared/Idempotency/)
  1. Application Layer (src/Application/Shared/Idempotency/)
  1. Infrastructure Layer (src/Infrastructure/Idempotency/)
  1. Tests

File Placement

| Layer | Path |

|-------|------|

| Domain Types | src/Domain/Shared/Idempotency/ |

| Application Port | src/Application/Shared/Idempotency/ |

| Infrastructure | src/Infrastructure/Idempotency/ |

| Unit Tests | tests/Unit/{Layer}/{Path}/ |


Key Principles

Idempotency Strategy

  1. Generate deterministic key from message ID + handler name
  2. Check store BEFORE processing
  3. Mark as processed AFTER successful handling
  4. Use database unique constraint as safety net
  5. TTL cleanup prevents unbounded growth

Race Condition Handling

  1. Use INSERT ... ON CONFLICT (database)
  2. Use SETNX (Redis)
  3. If duplicate detected mid-processing, return Duplicate result

TTL Management

  1. Default TTL: 7 days (configurable)
  2. Cleanup via cron/scheduled command
  3. Redis handles TTL automatically via EXPIRE

Naming Conventions

| Component | Pattern | Example |

|-----------|---------|---------|

| Key VO | IdempotencyKey | IdempotencyKey |

| Result VO | ProcessingResult | ProcessingResult |

| Status Enum | ProcessingStatus | ProcessingStatus |

| Store Interface | IdempotencyStoreInterface | IdempotencyStoreInterface |

| DB Store | DatabaseIdempotencyStore | DatabaseIdempotencyStore |

| Redis Store | RedisIdempotencyStore | RedisIdempotencyStore |

| Middleware | IdempotentConsumerMiddleware | IdempotentConsumerMiddleware |

| Test | {ClassName}Test | IdempotencyKeyTest |


Quick Template Reference

IdempotencyKey

final readonly class IdempotencyKey
{
    public function __construct(
        public string $messageId,
        public string $handlerName,
    ) {}

    public static function fromMessage(string $messageId, string $handlerName): self;
    public function toString(): string;
    public function equals(self $other): bool;
}

IdempotencyStoreInterface

interface IdempotencyStoreInterface
{
    public function has(IdempotencyKey $key): bool;
    public function mark(IdempotencyKey $key, \DateTimeImmutable $expiresAt): void;
    public function remove(IdempotencyKey $key): void;
}

Usage

$middleware = new IdempotentConsumerMiddleware($store);

$result = $middleware->process(
    key: IdempotencyKey::fromMessage($message->id, 'handle_payment'),
    handler: fn() => $paymentHandler->handle($message),
    ttl: new \DateTimeImmutable('+7 days')
);

match ($result->status) {
    ProcessingStatus::Processed => $logger->info('Processed'),
    ProcessingStatus::Duplicate => $logger->info('Skipped duplicate'),
    ProcessingStatus::Failed => $logger->error('Failed', ['error' => $result->error]),
};

Database Schema

CREATE TABLE idempotency_keys (
    key VARCHAR(255) PRIMARY KEY,
    handler_name VARCHAR(255) NOT NULL,
    processed_at TIMESTAMP(6) NOT NULL,
    expires_at TIMESTAMP(6) NOT NULL
);

CREATE INDEX idx_idempotency_expires ON idempotency_keys (expires_at);

References

For complete PHP templates and test examples, see:

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.