Google Professional Cloud Developer Question 444
Single answerGoogle Cloud PlatformYou are developing a serverless application using Google Cloud Functions to process incoming HTTP requests. Due to high traffic, you want to optimize your application by batching multiple requests together before processing them to reduce API calls to an external service. Which approach should you implement to achieve this while adhering to best practices?
- A
Use a Pub/Sub topic to collect incoming requests and a subscriber to batch and process them at regular intervals.
- B
Directly batch requests in memory within the Cloud Function by collecting them until a threshold is met.
- C
Use Firestore to store incoming requests, and periodically query and batch them for processing.
- D
Implement batching logic directly in the client application to bundle requests before sending them to Cloud Functions.
Show answer and explanation
Correct answer: A
Explanation
The best solution for batching requests in a serverless application is to use a Pub/Sub system. Pub/Sub is designed to handle high-throughput data collection and allows for decoupling of message producers and consumers. By using a subscriber, you can implement efficient batching based on time or message volume, ensuring scalability and reliability in your serverless architecture.
- A. Correct.
This is the correct approach. Using Pub/Sub allows you to reliably collect and queue incoming requests, and a subscriber can handle batching and processing based on time intervals or message volume. This is scalable and adheres to serverless best practices.
- B. Incorrect.
This is incorrect because Cloud Functions are stateless, and storing requests in memory violates best practices. Additionally, this approach may lead to data loss if the function instance terminates unexpectedly.
- C. Incorrect.
While Firestore can be used for storing data, using it for batching in this scenario introduces unnecessary complexity and latency. It is not the most efficient or scalable solution for this use case.
- D. Incorrect.
Batching requests on the client side is not ideal because it adds complexity to the client application and may not be feasible if the clients are distributed or have varying traffic patterns.