PrepZone Logo
PrepZone

Event-Driven Architecture

Publish events, react asynchronously and build systems that scale by adding consumers.

When a StreamHub creator goes live, the stream service publishes stream.started. Notification, analytics, search, and moderation services each react independently — no central orchestrator, no synchronous call chain that breaks when one service is slow.

Events vs commands

AspectEvent (past tense)Command (imperative)
Namingstream.started, payment.completedSendNotification, ProcessPayment
CouplingPublisher doesn't know subscribersSender knows receiver
RoutingTopic/bus; many consumersPoint-to-point queue
FailureOne slow consumer doesn't block othersBlocked queue stalls sender
StreamHubKafka topic `stream.lifecycle`RabbitMQ transcode job queue
  • Naming

    Event (past tense)stream.started, payment.completed
    Command (imperative)SendNotification, ProcessPayment
  • Coupling

    Event (past tense)Publisher doesn't know subscribers
    Command (imperative)Sender knows receiver
  • Routing

    Event (past tense)Topic/bus; many consumers
    Command (imperative)Point-to-point queue
  • Failure

    Event (past tense)One slow consumer doesn't block others
    Command (imperative)Blocked queue stalls sender
  • StreamHub

    Event (past tense)Kafka topic `stream.lifecycle`
    Command (imperative)RabbitMQ transcode job queue

Event-driven flow

Amazon MSK event pipeline

produceCOMPUTE
EKS APIorder placed
INTEGRATION
Amazon MSKorders.placed.v1
COMPUTE
InventoryEKS worker
COMPUTE
Email svcEKS worker
ANALYTICS
AnalyticsFlink / EMR
API publishes events; worker fleets scale independently on consumer lag.

StreamHub production architecture (AWS)

HTTPSstaticmissAPICLIENT
Mobile / WebStreamHub cli…
NETWORK
Route 53GeoDNS routing
NETWORK
CloudFrontCDN + WAF edge
NETWORK
AWS ALBTLS terminati…
NETWORK
API GatewayJWT · rate li…
STORAGE
Amazon S3media origin
COMPUTE
Amazon EKSAPI · auth · …
DATABASE
ElastiCachesessions · ho…
DATABASE
RDS Postgresprimary + rep…
INTEGRATION
Amazon MSKdomain events
ANALYTICS
OpenSearchstream discov…
OPS
CloudWatchmetrics · X-R…
End-to-end path from user to data — reference this when placing any new service.

EDA building blocks

  • Event: Immutable record of something that happened, with schema version.
  • Event bus / broker: Kafka topic or cloud event bridge routing events to subscribers.
  • Event handler: Stateless consumer that reacts to one event type.
  • Event store (optional): Append-only log as source of truth (event sourcing).
  • Schema registry: Enforces Avro/Protobuf/JSON Schema compatibility across producers.

Choreography vs orchestration

StyleControl flowTrade-off
ChoreographyEach service reacts to events; no central coordinatorSimple, decoupled; hard to trace full flow
OrchestrationCentral saga/workflow engine coordinates stepsVisible flow; orchestrator is a bottleneck/SPOF
StreamHub checkoutChoreography for stream lifecycle eventsOrchestration for multi-step subscription saga
  • Choreography

    Control flowEach service reacts to events; no central coordinator
    Trade-offSimple, decoupled; hard to trace full flow
  • Orchestration

    Control flowCentral saga/workflow engine coordinates steps
    Trade-offVisible flow; orchestrator is a bottleneck/SPOF
  • StreamHub checkout

    Control flowChoreography for stream lifecycle events
    Trade-offOrchestration for multi-step subscription saga

StreamHub's go-live flow is choreographed: stream service publishes, others react. Subscription upgrade uses orchestration via a saga coordinator because payment + entitlement + email must succeed or roll back together.

Event schema example

Java
{
  "specversion": "1.0",
  "type": "com.streamhub.stream.started",
  "source": "/streams/sh_4420",
  "id": "evt_a1b2c3d4",
  "time": "2026-04-01T20:00:00Z",
  "data": {
    "stream_id": "live_9912",
    "streamer_id": "sh_4420",
    "title": "Friday Night Ranked",
    "category": "gaming"
  }
}

Handling failures in EDA

Resilience patterns

  • Idempotent handlers: Same event processed twice produces the same outcome.
  • Dead letter topic: Failed events after N retries go to DLQ for investigation.
  • Retry with backoff: Exponential delay prevents thundering herd on downstream recovery.
  • Outbox pattern: Write event to local DB outbox in same transaction as business data; separate publisher reads outbox — guarantees no lost events.
  • Saga compensations: On failure, publish compensating events (refund, revoke entitlement).
Java
-- Transactional outbox table
CREATE TABLE outbox (
    id         BIGSERIAL PRIMARY KEY,
    event_type VARCHAR(128) NOT NULL,
    payload    JSONB NOT NULL,
    created_at TIMESTAMPTZ DEFAULT now(),
    published  BOOLEAN DEFAULT false
);

Quick recall

Everything you need if you only revisit this box.

  • EDA publishes facts; consumers react independently without the producer knowing them.
  • Choreography is decoupled; orchestration coordinates multi-step flows with rollback.
  • Use Kafka for event streams; pair with schema registry for compatibility.
  • Transactional outbox ensures events are never lost relative to DB writes.
  • Idempotent handlers and DLQ are mandatory for at-least-once brokers.
  • Include trace_id in every event for end-to-end observability.

Test yourself

Answer these before moving on — recall is what makes it stick.