PROFESSIONAL-CLOUD-DEVELOPER · Question #255
You are a lead developer working on a new retail system that runs on Cloud Run and Firestore in Datastore mode. A web UI requirement is for the system to display a list of available products when…
The correct answer is D. Modify the query that returns the product list using cursors. The root problem is fetching the entire product catalog into memory on every request. The solution is server-side pagination. Firestore (Datastore mode) cursors allow you to retrieve a fixed-size page of results and receive an opaque cursor pointing to the next position in the…
Question
You are a lead developer working on a new retail system that runs on Cloud Run and Firestore in Datastore mode. A web UI requirement is for the system to display a list of available products when users access the system and for the user to be able to browse through all products. You have implemented this requirement in the minimum viable product (MVP) phase by returning a list of all available products stored in Firestore. A few months after go-live, you notice that Cloud Run instances are terminated with HTTP 500:
Container instances are exceeding memory limits errors during busy times. This error coincides with spikes in the number of Datastore entity reads. You need to prevent Cloud Run from crashing and decrease the number of Datastore entity reads. You want to use a solution that optimizes system performance. What should you do?
Options
- AModify the query that returns the product list using integer offsets.
- BModify the query that returns the product list using limits.
- CModify the Cloud Run configuration to increase the memory limits.
- DModify the query that returns the product list using cursors.
How the community answered
(18 responses)- A6% (1)
- B11% (2)
- C11% (2)
- D72% (13)
Explanation
The root problem is fetching the entire product catalog into memory on every request. The solution is server-side pagination. Firestore (Datastore mode) cursors allow you to retrieve a fixed-size page of results and receive an opaque cursor pointing to the next position in the result set. On the next request the cursor is submitted to resume exactly where the previous page ended - Datastore only reads the entities on that page, not all preceding ones. This minimises both memory usage (only one page lives in Cloud Run memory) and entity reads (only the requested page is billed). Integer offsets (A) are deceptive: Datastore still internally reads and discards all entities before the offset, so the read count does not decrease. Limits alone (B) without cursors let you retrieve only the first page; there is no way to navigate to subsequent pages without offsets (and the same read-cost problem applies). Increasing memory limits (C) treats the symptom, not the cause, and does not reduce Datastore reads.
Topics
Community Discussion
No community discussion yet for this question.