
Answer-first summary for fast verification
Answer: Set the memcache service level to dedicated. Create a key from the hash of the query, and return database values from memcache before issuing a query to Cloud SQL.
Option A is correct because it uses a dedicated memcache service level for better performance and reliability, and implements a proper cache-aside pattern by creating unique keys from query hashes and checking memcache before querying the database. This minimizes database queries by serving cached results when available. Option B is inefficient due to unnecessary cron-based cache population every minute, which doesn't align with actual query patterns. Options C and D use shared memcache (less reliable) and store all queries under a single key 'cached_queries', which violates memcache best practices as it doesn't allow for efficient retrieval of individual query results and exceeds size limits. The community discussion strongly supports A with 100% consensus and 31 upvotes for the main reasoning comment.
Author: LeetQuiz Editorial Team
Ultimate access to all questions.
You are deploying a PHP application on App Engine standard environment with a Cloud SQL backend. Your goal is to minimize the number of database queries. What should you do?
A
Set the memcache service level to dedicated. Create a key from the hash of the query, and return database values from memcache before issuing a query to Cloud SQL.
B
Set the memcache service level to dedicated. Create a cron task that runs every minute to populate the cache with keys containing query results.
C
Set the memcache service level to shared. Create a cron task that runs every minute to save all expected queries to a key called ג€cached_queriesג€.
D
Set the memcache service level to shared. Create a key called ג€cached_queriesג€, and return database values from the key before using a query to Cloud SQL.
No comments yet.