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.
Cache
We will be using Redis Cache to provide caching support to this application. Following are the key concepts
- Cache invalidation
- Every type of data is allocated a Time to live so that we avoid stale data.
- When data such as vital stats or workout data is being updated, we look up that key in cache and invalidate it as it's being updated and the old data is no longer valid
- Cache miss
- For the get data, we will attempt to read from cache and if there is a miss we will retrive and update in our cache. Following data will be read from cache -> vitals history, workout history, exercise list.
Database
We will be using a MariaDB or MySQL database or any relational database as we don't have any unstructured data. We will be maintaining 3 nodes with 2 nodes replication.
We will shard on the basis of user id to ensure even distribution. Distribution of keys on nodes will be done using consistent hashing to ensure that when nodes are added or removed, it's done with minimal impact
Scenarios
- What happens when user tries to upload activity twice
- Every activity has an idempotency key consisting of the start time and the user id. Since we cannot start two activities at the same time, this idempotency key should be good enough to identify that workout and remove the duplicates. Along with the idempotency key, we also send the timestamp when the upload is attempted. We take the more recent timestamp. Reason is , since the data is originating from the user's mobile, the more recent one needs to be picked
- Do we face any issues with large uploads
- We don't deal with very large amount of data. Even a 2 hour workout with around 25 exercises is not bound to be more than around 5MB. This is bound to be the exception as only top body builders are likely to do this much
- Even if we assume that everyone works out this way, we can implement a weighted rate limiter, that ensures that the system handles only a certain amount of load.
- What happens to the other requests that get rejected? they will retry. Uploaded data is primarily present on the user's phone and it's only getting synced to our database periodically. So if not in the original request, we will do it during the retry
- How to handle large uploads?
- We will ensure that the client does multiple patch requests. Data for a single exercise should not be greater than a few KBs
- We will also do rate limiting at the API gateway to ensure that no rogue upload is allowed inside
This app does not support cycling based workouts