Why messaging matters in GCP - Performance Analysis
When using messaging services in cloud systems, it's important to know how the time to send and receive messages changes as the number of messages grows.
We want to understand how the work involved grows when more messages are handled.
Analyze the time complexity of the following operation sequence.
// Create a Pub/Sub topic
const topic = pubsub.topic('my-topic');
// Publish multiple messages
for (let i = 0; i < messages.length; i++) {
await topic.publishMessage({ data: Buffer.from(messages[i]) });
}
// Pull messages from subscription
const subscription = topic.subscription('my-subscription');
const [receivedMessages] = await subscription.pull({ maxMessages: messages.length });
This sequence creates a topic, publishes multiple messages one by one, then pulls all messages from a subscription.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Publishing each message individually to the topic.
- How many times: Once per message, so as many times as there are messages.
- Secondary operation: Pulling messages from the subscription happens once but retrieves multiple messages.
As the number of messages increases, the number of publish calls grows directly with it.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 publish calls + 1 pull call |
| 100 | 100 publish calls + 1 pull call |
| 1000 | 1000 publish calls + 1 pull call |
Pattern observation: The number of publish operations grows linearly with the number of messages, while pulling happens once regardless of message count.
Time Complexity: O(n)
This means the time to send all messages grows directly in proportion to how many messages you send.
[X] Wrong: "Publishing many messages takes about the same time as publishing one message."
[OK] Correct: Each message requires a separate publish call, so more messages mean more calls and more time.
Understanding how messaging scales helps you design systems that handle growing workloads smoothly, a key skill in cloud architecture.
"What if we batch multiple messages into a single publish call? How would the time complexity change?"