Implementing High-Performance Caching with Memorystore for Redis
Short answer
D Create a Memorystore for Redis instance, and use this database to store the most accessed stock data. Query this instance first when user requests are received, and fall back to the internal database.
Question
Key Takeaways
Memorystore for Redis provides the necessary in-memory speed for real-time stock data APIs. It wins by caching frequently accessed data to reduce latency and offload the internal database. This pattern ensures high scalability while maintaining data availability through a fallback mechanism.
Expert Analysis
The requirement for real-time stock data retrieval with high scalability necessitates a caching architecture that decouples the read path from the underlying internal database, which is typically optimized for transactional integrity rather than high-concurrency low-latency reads. Option D correctly implements this by introducing Memorystore for Redis as an in-memory cache layer, ensuring that frequent requests are served at microsecond latency without overwhelming the backend system. The fallback mechanism ensures data consistency and availability when cache misses occur, making it the only viable solution among the choices provided for this specific performance profile.
Cache Layer Necessity Over Raw Database Scaling
Introducing an alternative such as migrating directly to BigQuery (Option A) or relying solely on Cloud Tasks (Option C) would fundamentally alter the application's operational model in ways that contradict the prompt's constraints. BigQuery is designed for analytical processing and batch queries, not for serving individual, real-time user requests with sub-second latency; its query execution time is orders of magnitude slower than in-memory lookups. Similarly, using Cloud Tasks (mentioned in Option C's distractor logic) is intended for asynchronous background job distribution, not for synchronous API responses where the user expects immediate feedback. The condition that would make Option A correct is if the requirement were for historical trend analysis or bulk reporting, but for real-time individual stock quotes, the I/O bottleneck of a relational database or the latency of a data warehouse makes them unsuitable. The scenario explicitly demands speed, which mandates bypassing disk-based storage for the hot path.
In-Memory Performance as the Decisive Factor
The decisive condition in this scenario is the need for "real-time" delivery with "high scalability." Memorystore for Redis operates entirely in RAM, providing single-digit millisecond response times regardless of the number of concurrent users, up to the instance's capacity limits. This architectural choice allows the API to handle massive spikes in traffic without linearly increasing backend load. By storing the most accessed stock data in Redis, the system leverages temporal locality—users frequently check the same major tickers repeatedly. This approach transforms a potential O(N) database scan or lock contention issue into an O(1) hash lookup. The claim here is not merely about speed, but about the ability to scale horizontally by adding more API instances that all point to a centralized, high-throughput memory cache, thereby decoupling compute scaling from data access scaling.
Cost and Latency Implications of Remaining Options
Choosing Option A incurs significant latency costs per query, as BigQuery must parse, optimize, and execute SQL against large datasets, leading to poor user experience for real-time apps. Option B creates a single point of failure and potential consistency issues by attempting to store all market data in memory, which is economically unfeasible given the volume of stock data, and lacks a fallback mechanism if the cache becomes stale or full. Option C introduces unnecessary complexity by queuing user requests via Pub/Sub, which turns a synchronous API call into an asynchronous event stream, violating the expectation of immediate response. Furthermore, Bigtable (also in Option C) is excellent for wide-column workloads but requires more complex client-side logic for simple key-value lookups compared to Redis, and the added Pub/Sub layer adds network hops and processing overhead that degrade real-time performance.
Mechanism of the Cache-Aside Pattern
The chosen action takes effect through the cache-aside pattern, also known as lazy loading. When a user request arrives, the API first checks the Memorystore instance for the requested stock data. If the data is present (cache hit), it is returned immediately. If not (cache miss), the API queries the internal database, stores the result in Memorystore for future requests, and then returns the data to the user. This mechanism ensures that the internal database is only touched for initial loads or updates, drastically reducing its load. The fallback to the internal database ensures that the system remains functional even if the cache is cleared or during peak load when memory limits are reached, providing a robust balance between performance and reliability.
Official Reference
https://cloud.google.com/memorystore/docs/redis
This link opens the official vendor documentation in a new tab.
Exam Strategy
When a question emphasizes 'real-time', 'low latency', or 'high concurrency' for read-heavy workloads, prioritize in-memory caching solutions like Memorystore for Redis over traditional databases or data warehouses. Always look for options that include a fallback strategy to ensure data consistency and availability during cache misses or failures, as this demonstrates a production-ready design.
Related Analysis
Practice All PCD Questions
Access 84 questions with complete answers and detailed explanations.
View Full PCD Practice Test →