Pub/Sub with Cloud Functions integration in GCP - Time & Space Complexity
When using Pub/Sub with Cloud Functions, it's important to understand how the number of messages affects processing time.
We want to know how the system scales as more messages arrive.
Analyze the time complexity of the following operation sequence.
// Create a Pub/Sub topic
const topic = pubsub.topic('my-topic');
// Deploy a Cloud Function triggered by the topic
exports.processMessage = (message, context) => {
const data = Buffer.from(message.data, 'base64').toString();
// Process the message data
console.log(`Received message: ${data}`);
};
This sequence sets up a Pub/Sub topic and a Cloud Function that runs each time a message is published to the topic.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Cloud Function invocation triggered by each Pub/Sub message.
- How many times: Once per message published to the topic.
Each new message causes one Cloud Function run, so the total work grows directly with the number of messages.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 function invocations |
| 100 | 100 function invocations |
| 1000 | 1000 function invocations |
Pattern observation: The number of function calls grows linearly as messages increase.
Time Complexity: O(n)
This means the total processing time grows in direct proportion to the number of messages.
[X] Wrong: "The Cloud Function runs only once no matter how many messages arrive."
[OK] Correct: Each message triggers a separate function run, so more messages mean more executions.
Understanding how event-driven systems scale helps you design efficient cloud solutions and answer real-world questions confidently.
"What if the Cloud Function batches multiple messages in one invocation? How would the time complexity change?"
Practice
What is the main role of Google Cloud Pub/Sub when integrated with Cloud Functions?
Solution
Step 1: Understand Pub/Sub's purpose
Pub/Sub is designed to send messages between parts of an app, acting as a messenger.Step 2: Connect Pub/Sub with Cloud Functions
Cloud Functions run code automatically when they receive messages from Pub/Sub.Final Answer:
To send messages that trigger Cloud Functions automatically -> Option AQuick Check:
Pub/Sub triggers Cloud Functions [OK]
- Thinking Pub/Sub stores files
- Confusing Pub/Sub with UI or authentication
- Assuming Cloud Functions manage user login
Which of the following is the correct way to specify a Pub/Sub trigger in a Cloud Function deployment command?
gcloud functions deploy myFunction --trigger-?
Solution
Step 1: Identify the trigger type for Pub/Sub
Pub/Sub triggers require specifying the topic name with --trigger-topic.Step 2: Match the correct flag
--trigger-topic is the correct flag; others relate to storage or HTTP triggers.Final Answer:
--trigger-topic my-topic -> Option CQuick Check:
Pub/Sub trigger uses --trigger-topic [OK]
- Using --trigger-bucket which is for Cloud Storage
- Using --trigger-http which is for HTTP triggers
- Confusing event triggers with topic triggers
Given this Cloud Function code triggered by Pub/Sub messages, what will be logged when a message with data "Hello" is published?
def hello_pubsub(event, context):
import base64
message = base64.b64decode(event['data']).decode('utf-8')
print(f"Received message: {message}")Solution
Step 1: Decode the Pub/Sub message data
The message data is base64 encoded; decoding it gives the original string "Hello".Step 2: Print the decoded message
The print statement outputs "Received message: Hello" exactly.Final Answer:
Received message: Hello -> Option BQuick Check:
Base64 decode then print = Received message: Hello [OK]
- Printing raw base64 bytes instead of decoded string
- Using event['data'] directly without decoding
- Assuming 'data' key is missing
What is the likely cause if a Cloud Function triggered by Pub/Sub never runs after publishing messages?
Solution
Step 1: Check the Cloud Function trigger configuration
If the function is not subscribed to the right topic, it won't receive messages.Step 2: Rule out other causes
Syntax errors cause deployment failure, not silent no-run; topic existence is needed but usually checked; memory size doesn't stop triggers.Final Answer:
The Cloud Function is not subscribed to the correct Pub/Sub topic -> Option AQuick Check:
Wrong topic subscription = no trigger [OK]
- Assuming syntax error causes silent no-run
- Ignoring topic existence or permissions
- Blaming memory size for trigger issues
You want to build a system where multiple Cloud Functions react independently to the same Pub/Sub messages. What is the best way to set this up?
Solution
Step 1: Understand Pub/Sub fan-out pattern
One topic can have multiple subscriptions; each subscription can trigger a different Cloud Function independently.Step 2: Compare options
Multiple topics add complexity; one function calling others is indirect; publishing multiple times is inefficient.Final Answer:
Create one Pub/Sub topic and multiple subscriptions, each triggering a different Cloud Function -> Option DQuick Check:
One topic, many subscriptions = multiple triggers [OK]
- Creating many topics unnecessarily
- Using one function to call others instead of triggers
- Publishing duplicate messages to multiple topics
Ready to prove it?
Pass the test and this pattern moves unknown
