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?"
Practice
Solution
Step 1: Understand messaging role in cloud systems
Messaging enables components to send and receive information without direct connection, ensuring reliable communication.Step 2: Identify the correct purpose of messaging
Messaging is not about speeding internet, storing files, or replacing databases but about reliable communication.Final Answer:
It allows different parts of a system to communicate reliably. -> Option DQuick Check:
Messaging = Reliable communication [OK]
- Confusing messaging with file storage
- Thinking messaging speeds up internet
- Assuming messaging replaces databases
Solution
Step 1: Recall gcloud Pub/Sub topic creation syntax
The correct command is 'gcloud pubsub topics create [TOPIC_NAME]'.Step 2: Match the command to options
Only gcloud pubsub topics create my-topic matches the correct syntax exactly.Final Answer:
gcloud pubsub topics create my-topic -> Option AQuick Check:
Correct gcloud topic creation command = gcloud pubsub topics create my-topic [OK]
- Mixing order of words in command
- Using 'create pubsub topic' instead of 'pubsub topics create'
- Omitting 'topics' keyword
from google.cloud import pubsub_v1
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path('my-project', 'my-topic')
future = publisher.publish(topic_path, b'Hello World')
print(future.result())Solution
Step 1: Understand publish method behavior
The publish method returns a future that resolves to a message ID string when successful.Step 2: Analyze the print statement
Calling future.result() waits for completion and returns the message ID, not the message content.Final Answer:
The message ID string confirming successful publish -> Option AQuick Check:
future.result() returns message ID [OK]
- Thinking publish returns message content
- Assuming publish is fully synchronous without future
- Confusing error output with success
from google.cloud import pubsub_v1
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path('my-project', 'my-subscription')
def callback(message):
print(f'Received: {message.data}')
message.ack()
subscriber.subscribe(subscription_path, callback=callback)What is the likely problem?
Solution
Step 1: Check subscription existence and linkage
If the subscription does not exist or is not linked to the topic, no messages will be received.Step 2: Verify other code parts
The callback acknowledges messages correctly, subscriber client is initialized properly, and subscription path uses correct subscription name.Final Answer:
The subscription does not exist or is not attached to the topic. -> Option CQuick Check:
Missing or unlinked subscription = no messages [OK]
- Forgetting to create subscription before subscribing
- Assuming callback runs without subscription
- Mixing topic and subscription names
Solution
Step 1: Understand message delivery to multiple consumers
Pub/Sub allows multiple subscriptions on one topic, so each service can get all messages independently.Step 2: Evaluate options for independent processing
Sharing one subscription means messages are load balanced, not duplicated; publishing to different topics duplicates effort; Cloud Storage is not for messaging.Final Answer:
Create multiple subscriptions on the same topic for each service. -> Option BQuick Check:
Multiple subscriptions = independent reliable processing [OK]
- Using one subscription shared by all services
- Publishing to separate topics unnecessarily
- Confusing storage with messaging service
Ready to prove it?
Pass the test and this pattern moves unknown
