Messaging - Topics

1. System Context

Developers use Nitric to define required topics to enable event-driven communication within their application.

  • App code interacts with the Topic resource through defined topics and subscriptions.
  • Developers define topics their application requires and implement application logic to publish or subscribe to messages.

Operations use default or overridden IaC (e.g Terraform modules) to provision the necessary resources for their target cloud.

Example AWS Provider
  • AWS SNS (Simple Notification Service) serves as the messaging and event notification service.
  • AWS Lambda functions are subscribed to SNS topics to process incoming messages.
  • AWS IAM manages roles and policies for secure access to SNS topics and Lambda functions.
  • AWS Step Functions make it possible to delay or schedule notifications, batch jobs, or any process that requires a timed pause before continuing with a task.
Code
Terraform
Create SNS Topic
Configure Subscriptions
Publish Messages
Invoke Lambda
Manage Permissions
Delayed Messages
Developer
Operations
nitric up
AWS SNS Topic
AWS Lambda Functions
AWS IAM
AWS Step Functions
Example GCP Provider
  • Google Pub/Sub serves as the messaging and event notification service.
  • Google Cloud Run services are subscribed to Pub/Sub topics to process incoming messages.
  • Google IAM manages roles and policies for secure access to Pub/Sub topics and Cloud Run services.
  • Retry Policy and Push Configuration ensure reliable delivery and handling of messages to Cloud Run services.
Code
Terraform
Create Pub/Sub Topic
Configure Subscriptions
Publish Messages
Push to Services
Manage Permissions
Retry Delivery
Developer
Operations
nitric up
Google Pub/Sub Topic
Google Cloud Run Services
Google IAM
Retry Policy & Push Config

2. Sequence

Build Sequence

Below is a sequence diagram showing the sequence of events when a developer registers a Topic with Nitric and optionally registers Subscribers. This is the process that occurs when using the nitric up command.

App Worker(s)Nitric SDKNitric CLINitric Provider (plugin)IaC (e.g. Terraform)opt[Subscribers]opt[Notifications]opt[Delayed Messaging]Register QueueRegister Access RequirementsRegister QueueRegister Access RequirementsRegister Subscriber CallbackRegister SubscriberForward Nitric SpecProvision QueueProvision IAMProvision Event Rule(s)Provision IAMProvision Delayed MessagingApp Worker(s)Nitric SDKNitric CLINitric Provider (plugin)IaC (e.g. Terraform)

Runtime Sequence

Below is the runtime flow when performing operations on a Topic using Nitric, such as publishing or subscribing to messages. The example shows both publish and subscribe operations, which send or receive messages to/from the topic.

App CodeNitric SDKNitric Runtime (plugin)Message Broker (e.g. AWS SNS)alt[Publisher][Subscriber]Publish(message)Forward RequestConvert RequestSend Message to TopicMessageConvert MessageForward MessageSubscription CallbackApp CodeNitric SDKNitric Runtime (plugin)Message Broker (e.g. AWS SNS)

3. Component

Topic Module

  • Dynamically creates unique topics for messaging and event-driven communication, ensuring consistent naming and avoiding conflicts.
  • Configures metadata or tags for resource identification, governance, and lifecycle management.
  • Supports subscribing various endpoints (e.g., functions, services, queues) to the topic, enabling scalable publish-subscribe patterns.
  • Automates the assignment of permissions to ensure secure publishing and subscription handling, following the principle of least privilege.
  • Provides configurable retry policies and message delivery options to ensure reliable and fault-tolerant communication between services.
  • Abstracts the underlying cloud messaging infrastructure, offering a unified interface for event-driven architectures across different providers.
  • Optionally integrates with workflows or scheduling systems for advanced scenarios like delayed notifications or batched processing.

4. Code

Developers write application code that imports and declares the Topic resource from the SDK, and implements the application logic to publish and subscribe to topics.

SDK Reference by language -

Operations will use or extend the Nitric infrastructure modules, including both Terraform and Pulumi:

Last updated on Feb 15, 2025