Detailed Component Design
Deep dive into 2-3 key components. Explain how they work, how they scale, discuss tradeoffs, capacity, and any relevant algorithms or data structures.
- Weather service is the core of the design. However, it could be done. We can build a circuit breaker aound it, it has three states, closed, open and half-open. Closed means circuit breaker is not open, traffic is normal. Open means traffic is stopped, Half-open means only a subset of traffic can continue. This would help us to prevent cascading failures as applications won't retry when breaker is open.
- Idepotent writes. Concurrent writes could create duplicate records. At the database level, we need to implement UNIQUE constraint, it is the safety. Most of the use cases in this scenario is read which should be safe. The write scenarios seem to only involve subscription endpoint, which should be enough to use the UNIQUE constraint.
- We can also cache data for popular location ID to avoid overwelming weather service.
- CDN, Cache and RDB serve data to the application. CDN can be deployed at the nearest locations and stores the hottest weather data. For instance, New York City in the next 7 days. If a cache miss, we got to Cache. Many cache services can be used, like Redis. We can configure a TTL to hourly or daily depending on the use case or cities. An LRU eviciton plan should be better as users wouldn't access old weather data most of the time. Cache needs to be deployed in clusters, for instance a Redis cluster, data needs to be persisted, and syn betwee nodes need to be handled. RDB needs to have read-write replicas, data needs to be persisted. Data sharding should also be implemented as data grows. Consistent hashing is a good technique for this task, it makes horizontal scaling easily handled. Virtual nodes on top of that can minimize distruptions on adding or removing nodes.
- When a cache miss occures, it goes to read from RDB, then write back to cache. When an update event occurs, RDB pushes an event to Cache that invalidates the data entry.
- Location service, Weather service are stateless services, they are deployed in clusters and load balanced. They should be easily scaled across AZs.
- Weather data is a time-series data, thus a time-series database would be helpful for queriing, or act as a cache layer. This may overlap with #1, depending on implementation or product design. Time-series database like Influx can be more beneficial in certain cases. Each weather station stores data per second or per microsecond, they aggregate data and push to the main influxdb per minute.
- Push service waits events from event queue and push to certain users.