Backend7 min read

Event-Driven Architect

Designs event-driven systems - Kafka topics, consumer groups, event sourcing, CQRS, the outbox pattern, and idempotent event processing.

event-drivenKafkaevent sourcingCQRSpub/submessage queue

The Event-Driven Architect designs systems where services communicate by publishing and consuming events rather than direct calls. This decouples services, enables temporal decoupling, and creates a natural audit log. The hard problems are event schema evolution, idempotency, and handling out-of-order delivery - this agent addresses all three.

Event Design Principles

// Events are facts - past tense, immutable
// ✅ Good event names:
// order.created, payment.succeeded, user.email.verified

// Event schema - include everything downstream consumers need
{
  "eventId": "evt_01hzx...",           // Unique - enables idempotency
  "eventType": "order.created",
  "eventVersion": "1.0",              // Schema version - enables evolution
  "timestamp": "2026-08-19T10:00:00Z",
  "aggregateId": "order_123",
  "aggregateType": "Order",
  "payload": {
    "userId": "user_456",
    "items": [{ "productId": "prod_789", "quantity": 2, "price": 2999 }],
    "totalCents": 5998
  }
}

Kafka Setup (Node.js)

import { Kafka } from 'kafkajs';

const kafka = new Kafka({
  clientId: 'order-service',
  brokers: (process.env.KAFKA_BROKERS ?? '').split(','),
});

// Producer - transactional to ensure at-least-once delivery
const producer = kafka.producer({ transactionTimeout: 30_000 });
await producer.connect();

await producer.send({
  topic: 'order.events',
  messages: [{
    key: order.id,              // Partition key - same order always goes to same partition
    value: JSON.stringify(event),
    headers: { 'schema-version': '1.0' },
  }],
});

// Consumer - consumer group for load balancing
const consumer = kafka.consumer({ groupId: 'notification-service' });
await consumer.connect();
await consumer.subscribe({ topic: 'order.events', fromBeginning: false });

await consumer.run({
  eachMessage: async ({ message }) => {
    const event = JSON.parse(message.value!.toString());
    // Idempotency: check if we've already processed this eventId
    if (await isAlreadyProcessed(event.eventId)) return;
    await processEvent(event);
    await markAsProcessed(event.eventId);
  },
});

Outbox Pattern

// Guarantees event publication is atomic with the DB transaction
// Without outbox: save to DB succeeds but event publish fails → inconsistency

async function createOrderWithOutbox(orderData) {
  await db.transaction(async (tx) => {
    // Save the business record
    const order = await tx.orders.create({ data: orderData });

    // Save the event to outbox table - same transaction
    await tx.outboxEvents.create({
      data: {
        eventType: 'order.created',
        aggregateId: order.id,
        payload: JSON.stringify(orderEventPayload(order)),
        processedAt: null,
      },
    });
    // If this transaction commits, the event record is guaranteed to exist
  });
}

// Separate process polls outbox and publishes to Kafka, then marks as processed

Explore all agents

Browse the full library of specialized AI agents.