Skip to main content

Topics and subscriptions

This guide details the Pub/Sub resource model: topic configurations, storage quota mechanics, retention rules, subscription types, and resource modification options.

Environment setup

Set the required environment variables for API requests:

export CAI_PUBSUB_API="https://api.codyhill.dev"
export CAI_PROJECT="<your-project-id>"
export CAI_TOKEN="<your-api-key-or-session-token>"

Topics: Named messaging channels

A topic is a named messaging channel where producers publish messages.

Topic settings

PropertyRequiredDefaultDescription
nameYesImmutable. Lowercase letters, digits, and hyphens (1–63 characters). Must start with a letter.
display_nameNoEmptyEditable human-readable label.
max_bytesNo16MiReclaimed storage budget reserved for this topic's unacknowledged backlog.
max_ageNoNoneMaximum retention period for messages (e.g. 24h, 30m).
discardNooldCapacity overflow behavior: old drops the oldest unacknowledged messages; new rejects new publishes with HTTP 409.

Creating a topic

platformctl pubsub topics create orders --display-name "Order Events" \
--max-bytes 64Mi --max-age 24h --discard new

Storage budget allocation

Each project has a Pub/Sub message storage budget (default 1 GiB).

  • Claimed Budget: A topic claims its max_bytes allocation immediately upon creation, even when empty.
  • Capacity Management: Deleting unused topics or lowering max_bytes frees storage budget for other topics.
  • Monitoring Capacity: Check quota usage anytime via CLI, API, or Console.
platformctl pubsub quota

Subscriptions: Durable readers

Subscriptions track reader progress and retain unacknowledged messages for processing.

Subscription configuration

PropertyRequiredDefaultDescription
nameYesUnique identifier within the topic.
typeNosharedOrdering and consumer model: shared, key-shared, exclusive, failover. Immutable after creation.
ack_deadline_secondsNo30Time window (1–600 seconds) for consumers to acknowledge pulled messages before redelivery.
max_deliverNo5Maximum delivery attempts before forwarding to a dead-letter topic (push subscriptions).
start_fromNoallInitial read position: all (earliest available) or new (from subscription creation onwards). Immutable.
dead_letterNoNoneTarget topic configuration for unprocessable messages: {"topic": "dead-letter-topic", "after_attempts": 5}.

Subscription ordering models

  • shared: Competing consumer model. Messages are distributed across active workers for parallel processing.
  • key-shared: Partitioned queue. Messages sharing the same key attribute are delivered in strict order to the same consumer instance.
  • exclusive: Single active consumer stream with strict total ordering.
  • failover: Primary single consumer stream with automatic failover to standby consumers.

Creating a subscription

platformctl pubsub subscriptions create workers --topic orders \
--type key-shared --ack-deadline-seconds 60 --max-ack-pending 500 --start-from new

Updating resource settings

Dynamic topic and subscription settings can be modified in place. Immutable properties (name, type, start_from) require deleting and recreating the resource.

Updating a topic limit

platformctl pubsub topics update orders --max-bytes 128Mi

Summary best practices

  1. Pre-create Subscriptions: Ensure subscriptions exist prior to publishing messages so no events are missed.
  2. Configure Dead-Letter Topics: Set up dead-letter targets on push subscriptions to capture and isolate unprocessable messages.
  3. Select Appropriate Subscription Types: Use shared for parallel task queues and key-shared for entity-ordered stream processing.