Instagram feed- WIP
3 min readOct 10, 2024
--
¡ Subtopic
¡ Requirements
¡ Data access pattern
¡ Data schema
â How to get number of likes for comment/post?
â Following/Follower feature
¡ High Level Design
¡ TODO
¡ Resources
Subtopic
- generate feed
- celebrity problem
- image upload and processing flow + CDN integration
Requirements
- Store/Get Images
- Like + Comment + Like a comment on Image (Can we comment on a comment? i.e. recursive comments?)
- Follow someone
- Publish a news feed
Data access pattern
Data access pattern --
* Get all the posts made by a user + sort by timestamp
* Get all the comments made on a post + sort by timestamp
* Get the number of likes on a post/comment
* Get list of users who have liked a post
* Get all posts made by a user's followers and sort by timestamp
Data schema
TODO â what database to use? and did you decide on the same?
- Likes Table: ID, Type, Active, ParentID, UserID, timestamp
[ParentID â can be a comment or a post,
Active â means is active or deleted
Type â comment or a post] - Post Table: UserID, PostID, text, Image URL, timestamp
- Comment Table: ID, text, timestamp, PostID
How to get number of likes for comment/post?
Approach # 1
- select (*) from like where PostID = âabcâ;
- very slow
Approach # 2
- hold âlikesâ in a column in likes/posts table
Post Table: UserID, PostID, text, Image URL, timestamp, likes
Comment Table: ID, text, timestamp, PostID, likes
- whenever there is a like on the post insert in like table and do +1 in likes column
- this column is an aggregated value and doesnât belong with posts/comments table data.
Approach #3
- Create an Activity table [ParentID, Likes]
Following/Follower feature
Data access pattern --
* Who follows user Hawking?
Select * from following where followeeId = 'Hawking';
* Which users does user Hawking follow?
Select * from following where followerId = 'Hawking';
High Level Design
Design 1 â Not scalable at posts service level
Design 2 â Precompute
- Problem here is that when a celerity creates a post there will be lot of cache update + lot of notifications
Design 3 â Precompute with celebrity problem
TODO
- More detailed read to resolve celebrity problem â push vs pull vs hybrid? â algorists
- Image upload and processing flow
- Image render flow + CDN integeration
Resources
- Arpit bhiyani
- Interview ready
- Alex xu template
- scaler videos