Estimate the scale of the system. Consider daily active users, read/write ratio, storage requirements, bandwidth, and any relevant QPS calculations...
given 1B total users, since the db is sql, assuming the user schema is made of 6 fields of type string and 2 ids (primary + foreign key), each row is about 250 bytes in average, therefore 250 * 1B = 250GB of storage. This is absolutely fine for a single sql database purely in term of storage, but there will be max connection hard limit. We need an in-memory database for reading the users. We could use a distrubuted rack of in memory db, read replicas to maintain the most accessed profiles (like famous people for example). We can do it randomly across the globe.
For tweets, the required space for all of them is bigger, like 10-50 times bigger -> 125TB of data, because the row takes more space (the message section is longer) and the number if tweets in bigger, plus it is ever increasing. We probably want to shard by creation time, every year, and keep the other old tweets for around for data retention. We will be moving them in a cheaper storage because they will not be actively read.
We also need a rack of redis replicas to read the tweets. every user can rely on a read replica where they can find their homes feed. When a profile is written, the single table is updated, as well as tweets. We will rely on a message queue to write both the tweets and the profiles asyncronusly, so we don't need to horizontally scale the main database, simplifying our design. We still need a couple of database backup, for high availability.
Client
load balancer
web service
sql database
redis for friends (read replica)
cdn
kafka to handle writes into the db, like following operation
because the tweets and users have known schema.
tweet -> id, message, owner_id (foreign key on user.id), likes
user -> id, name, lastname, email, picture_url, friend_id (foreign key on user.id)
we need some sharding given the volume of data. We could shard the users by region, so clients in one region access their info from a closer database in their region. It is also a legal requirement for the GDPR regulation in EU. To handle friend relationship (followers and followed) without fetching data across multiple db shards, we need a table owned by every user, where we store the following relationship. For that we can use a graph database, which is quite good for modelling these types of relationship