Google Cloud Pub/Sub
Google Cloud Pub/Sub provides managed messaging for event sourcing on GCP with automatic scaling.
Why Pub/Sub
Section titled “Why Pub/Sub”| Strength | Benefit |
|---|---|
| Fully managed | No infrastructure to operate |
| Global | Multi-region by default |
| Automatic scaling | Handles traffic spikes |
| GCP integration | Dataflow, BigQuery, Cloud Functions |
| At-least-once | Guaranteed delivery |
Trade-offs
Section titled “Trade-offs”| Concern | Consideration |
|---|---|
| GCP lock-in | Not portable to other clouds |
| Cost | Pay per message/data |
| Ordering | Requires ordering keys |
Configuration
Section titled “Configuration”[bus]backend = "pubsub"
[bus.pubsub]project_id = "my-gcp-project"topic_prefix = "angzarr"Environment Variables
Section titled “Environment Variables”export PUBSUB_PROJECT_ID="my-gcp-project"export PUBSUB_TOPIC_PREFIX="angzarr"export BUS_BACKEND="pubsub"export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json"Topic/Subscription Layout
Section titled “Topic/Subscription Layout”flowchart LR
TP["Topic: angzarr-events-player"] --> PP[Subscription: player-projector]
TP --> OP1[Subscription: output-projector]
TH["Topic: angzarr-events-hand"] --> HS[Subscription: hand-saga]
TH --> OP2[Subscription: output-projector]
Message Ordering
Section titled “Message Ordering”Enable ordering with ordering keys:
// Publish with ordering key (aggregate root)publisher.publish_with_ordering_key( event_bytes, &format!("{}#{}", domain, root),).await?;Subscription Configuration
Section titled “Subscription Configuration”# Terraform/gcloud configurationsubscription: name: player-projector topic: angzarr-events-player ack_deadline_seconds: 60 message_retention_duration: 604800s # 7 days enable_message_ordering: true dead_letter_policy: dead_letter_topic: angzarr-dlq max_delivery_attempts: 5Dead Letter Handling
Section titled “Dead Letter Handling”Failed messages route to a dead letter topic:
flowchart LR
T["angzarr-dlq (topic)"] --> S["dlq-monitor (subscription)"]
Monitor via Cloud Console or subscribe programmatically.
# Create topicsgcloud pubsub topics create angzarr-events-playergcloud pubsub topics create angzarr-events-handgcloud pubsub topics create angzarr-dlq
# Create subscriptionsgcloud pubsub subscriptions create player-projector \ --topic=angzarr-events-player \ --enable-message-ordering \ --dead-letter-topic=angzarr-dlq \ --max-delivery-attempts=5Helm Deployment
Section titled “Helm Deployment”bus: backend: pubsub
pubsub: enabled: true projectId: my-gcp-project credentials: secretName: pubsub-credentials keyFile: service-account.jsonWhen to Use Pub/Sub
Section titled “When to Use Pub/Sub”- GCP native — Already on Google Cloud
- Serverless — Cloud Functions/Run integration
- Global reach — Multi-region by default
- Managed — No operational overhead