Nativa PHP Core

Article

Outbox Pattern ... Domain Events bez porušenia ACID

Ilustračný obrázok článku: Architecture, php, architecture, database

Pošleš email. Potom uložíš objednávku do databázy.
Email sa pošle, ale databáza zlyhá.

Výsledok: email odoslaný, objednávka neexistuje.

To je problém, ktorý Outbox pattern rieši.

Problém: Side efekty bez ACID

// BEZ Outbox
$mailer->send($email);  //  Email odoslaný
$pdo->prepare('INSERT INTO orders ...')->execute([...]); //  Zlyhalo
// Výsledok: Email odoslaný, ale objednávka neexistuje

Prečo sa to stáva:

  • Email je vonkajší systém (SMTP server)
  • DB je vnútorný systém (SQLite)
  • Nemôžeš dať oba do jednej transakcie

Riešenie: Outbox

Outbox pattern hovorí: ulož event do outbox tabuľky, spracuj neskôr.

$uow = new UnitOfWork($pdo);
$uow->transaction(function (\PDO $pdo) use ($order, $email): void {
    // 1. Uložiť objednávku
    $pdo->prepare('INSERT INTO orders ...')->execute([...]);

    // 2. Uložiť event do outbox tabuľky
    $pdo->prepare('INSERT INTO outbox_events ...')->execute([
        'event_type' => 'OrderCreated',
        'payload' => json_encode(['order_id' => $orderId]),
    ]);
    // Oboje v jednej transakcii ... buď obe, alebo ani jedno
});

Neskôr (async): OutboxProcessor spracuje eventy:

  • Načíta pending events z outbox
  • Pošle email
  • Označí event ako processed

Implementácia v Nativa

1. OutboxMessage ... immutable entity

final readonly class OutboxMessage
{
    public function __construct(
        private string $id,
        private string $eventType,
        private array $payload,
        private string $status,
        private int $attempts,
        private int $maxAttempts,
        private string $createdAt,
        private ?string $processedAt = null,
        private ?string $errorMessage = null,
    ) {
    }

    public static function create(string $eventType, array $payload): self
    {
        return new self(
            id: bin2hex(random_bytes(16)),
            eventType: $eventType,
            payload: $payload,
            status: 'pending',
            attempts: 0,
            maxAttempts: 3,
            createdAt: date('Y-m-d H:i:s'),
        );
    }

    public function withProcessing(): self
    {
        return new self(
            ...array_merge((array) $this, [
                'status' => 'processing',
                'attempts' => $this->attempts + 1,
            ]),
        );
    }

    public function withProcessed(): self
    {
        return new self(
            ...array_merge((array) $this, [
                'status' => 'processed',
                'processedAt' => date('Y-m-d H:i:s'),
            ]),
        );
    }

    public function withFailed(string $error): self
    {
        return new self(
            ...array_merge((array) $this, [
                'status' => 'failed',
                'errorMessage' => $error,
            ]),
        );
    }
}

Kľúčové vlastnosti:

  • Immutable ... každá zmena vytvorí novú inštanciu
  • Status transitions ... pending → processing → processed/failed
  • Retry logic ... maxAttempts, withRetry()

2. OutboxRepository ... persistence

final readonly class SqliteOutboxRepository
{
    public function __construct(private \PDO $pdo) {}

    public function save(OutboxMessage $message): void
    {
        $stmt = $this->pdo->prepare('
            INSERT INTO outbox_messages (id, event_type, payload, status, attempts, max_attempts, created_at)
            VALUES (?, ?, ?, ?, ?, ?, ?)
        ');
        $stmt->execute([
            $message->id(),
            $message->eventType(),
            json_encode($message->payload()),
            $message->status(),
            $message->attempts(),
            $message->maxAttempts(),
            $message->createdAt(),
        ]);
    }

    public function findPending(int $limit = 100): array
    {
        $stmt = $this->pdo->prepare('
            SELECT * FROM outbox_messages
            WHERE status = \'pending\' AND attempts < max_attempts
            ORDER BY created_at ASC
            LIMIT ?
        ');
        $stmt->execute([$limit]);
        // ...
    }
}

3. OutboxProcessor ... background job

final class OutboxProcessor
{
    /** @var array<string, callable(OutboxMessage): void> */
    private array $handlers = [];

    public function registerHandler(string $eventType, callable $handler): void
    {
        $this->handlers[$eventType] = $handler;
    }

    public function process(int $batchSize = 100): int
    {
        $messages = $this->repository->findPending($batchSize);
        $processed = 0;

        foreach ($messages as $message) {
            try {
                $message = $message->withProcessing();
                $this->repository->save($message);

                $handler = $this->handlers[$message->eventType()] ?? null;
                if (null !== $handler) {
                    $handler($message);
                }

                $message = $message->withProcessed();
                $this->repository->save($message);
                $processed++;
            } catch (\Throwable $e) {
                $message = $message->withFailed($e->getMessage());
                $this->repository->save($message);
            }
        }

        return $processed;
    }
}

4. Integrácia s UnitOfWork

// V akcii ... uložiť event v jednej transakcii
$uow = new UnitOfWork($pdo);
$uow->transaction(function (\PDO $pdo) use ($order): void {
    // Business operácia
    $pdo->prepare('INSERT INTO orders ...')->execute([...]);

    // Domain event
    OutboxServices::repository()->save(OutboxMessage::create(
        eventType: 'OrderCreated',
        payload: ['order_id' => $order->id()],
    ));
});

// Neskôr (cron, queue worker) ... spracovať eventy
$processor = OutboxServices::processor();
$processor->registerHandler('OrderCreated', function (OutboxMessage $msg) {
    $payload = $msg->payload();
    // Poslať email
});
$processor->process(100);

Kedy použiť Outbox

ScénárPoužiť Outbox?Prečo
Poslať email po registráciiÁnoEmail = vonkajší systém
Notifikácia po objednávkeÁnoNotifikácia = vonkajší systém
Audit logNieAudit je vnútorný (môže bežať v transakcii)
Update používateľaNieLen DB operácia

Pravidlo: Ak side efekt zahŕňa vonkajší systém (email, API, webhook), použi Outbox.

Outbox vs Priame volanie

Priame volanieOutbox
KonzistenciaPorušuje ACIDACID konzistentné
RetryManuálnyAutomatický
VisibilityNeviem čo zlyhaloStatus v DB
ComplexityJednoduchéväčšia réžia
  • Problém: Side efekty bez ACID
  • Riešenie: Outbox = ulož event, spracuj neskôr
  • Integrácia: UnitOfWork + Outbox = transakčná konzistencia
  • Filozofia: Vonkajšie systémy cez outbox, nie na priamo

Nepotrebujeme message queue pre každý side efekt. Potrebujeme pattern, ktorý funguje s SQLite.


Zdroje

Comments (0)

No comments yet — be the first to react.

Reader questions (0)

No questions yet — be the first to ask.

Stuck on something in this article? Ask below. Your name is shown publicly; your email is never stored. A question appears here once a moderator approves it.