Open In App

Designing Instagram | System Design

Last Updated : 17 Jan, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Designing Instagram is an important topic for system design interview questions. Instagram follows strong Architecture. In this article, we will discuss the architecture of posting photos and videos, following and unfollowing users, liking and disliking posts, searching photos and videos, and generating news feeds.

design-instagram-banner

1. What is Instagram?

Instagram is an American photo and video-sharing social networking service owned by Meta Platforms. It allows users to upload media that can be edited with filters, be organized by hashtags, and be associated with a location via geographical tagging. Posts can be shared publicly or with preapproved followers.

2. Requirements for Instagram System Design

2.1 Functional Requirements for Instagram System Design

In functional Requirements, we will not discuss the login or signup page of Instagram. Login and Signup architecture is the same for everyone. We will look for further like posting photos, etc.

  • Post photos and videos: The users can post photos and videos on Instagram.
  • Follow and unfollow users: The users can follow and unfollow other users on Instagram.
  • Like or dislike posts: The users can like or dislike posts of the accounts they follow.
  • Search photos and videos: The users can search photos and videos based on captions and location.
  • Generate news feed: The users can view the news feed consisting of the photos and videos (in chronological order) from all the users they follow.

2.2 Non-Functional requirements for Instagram System Design

  • Scalability: The system should be scalable to handle millions of users in terms of computational resources and storage.
  • Latency: The latency to generate a news feed should be low.
  • Availability: The system should be highly available.
  • Durability: Any uploaded content (photos and videos) should never get lost.
  • Consistency: We can compromise a little on consistency. It is acceptable if the content (photos or videos) takes time to show in followers’ feeds located in a distant region.
  • Reliability: The system must be able to tolerate hardware and software failures.

3. Capacity Estimation for Instagram System Design

We have 1 billion users, with 500 million as daily active users. Assume 60 million photos and 35 million videos are shared on Instagram per day. We can consider 3 MB as the maximum size of each photo and 150 MB as the maximum size of each video uploaded on Instagram.On average, each user sends 20 requests (of any type) per day to our service.

3.1 Storage Per Day

Photos: 60 million photos/day * 3 MB = 180 TeraBytes / day
Videos: 35 million videos/day * 150 MB = 5250 TB / day
Total content size = 180 + 5250 = 5430 TB
The Total Space required for a Year:
5430 TB/day * 365 (days a year) = 1981950 TB = 1981.95 PetaBytes

3.2 Bandwidth Estimation

5430 TB/(24 * 60* 60) = 5430 TB/86400 sec ~= 62.84 GB/s ~= 502.8 Gbps
Incoming bandwidth ~= 502.8 Gbps
Let’s say the ratio of readers to writers is 100:1.
Required outgoing bandwidth ~= 100 * 502.8 Gbps ~= 50.28 Tbps

4. Use Case Diagram for Instagram System Design

uml
Use Case Diagram Instagram

In the above Diagram we have discussed about the use case diagram of Instagram:

  • If the user is new, they will register firstly it will be store in database, they will verifiy the profile.
  • If user is already signup, they will provide the email-Id and Password.
  • On the home page they will get the photos and videos, as well as the story page.
  • The post which is posted now, it will come at the top. User can follow or unfollow the person. User can get live. It's all depend on them.
  • There will be setting, in which user can see there past story or the post which has been archive. User can unblock the person they can get verified account, after paying.

5. Low-Level Design(LLD) for Instagram System Design

design-instagram-lld

Here's a breakdown of the key components and interactions for Instagram's low-level design:

  • User Service:
    • Handles user registration, login, authentication, and profile management.
    • Stores user data like username, email, bio, profile picture, etc.
    • Integrates with social authentication providers (e.g., Facebook, Google).
  • Post Service:
    • Handles photo and video uploads, editing, and deletion.
    • Stores post metadata like caption, hashtags, location, timestamp, etc.Processes uploaded media for resizing, filtering, and thumbnail generation.
    • Manages photo and video transcoding for different devices and resolutions.
  • Feed Service:
    • Generates personalized news feeds for each user based on their follows, likes, activity, and engagement.
    • Leverages a distributed system like Apache Kafka or RabbitMQ for real-time updates and notifications.
    • Utilizes a cache layer like Redis for fast feed retrieval and reduced database load.
  • Storage Service:
    • Stores uploaded photos and videos efficiently and reliably.
    • Utilizes a scalable object storage solution like Amazon S3, Google Cloud Storage, or Azure Blob Storage.
    • Implements redundancy and disaster recovery mechanisms for data protection.
  • Search Service:
    • Enables searching for users, hashtags, and locations.
    • Indexes users, posts, and hashtags based on relevant parameters.
    • Employs efficient indexing and search algorithms for fast and accurate results.
  • Comment Service:
    • Handles adding, editing, and deleting comments on posts.
    • Tracks comment threads and parent-child relationships.
    • Notifies users of new comments on their own posts or comments they participated in.
  • Notification Service:
    • Informs users about relevant events like likes, comments, mentions, and follows.
    • Pushes notifications to mobile devices through platforms like Firebase Cloud Messaging or Amazon SNS.
    • Leverages a queueing system for asynchronous notification delivery.
  • Analytics Service:
    • Tracks user engagement, post performance, and overall platform usage.
    • Gathers data on views, likes, comments, shares, and clicks.
    • Provides insights to improve user experience, optimize content recommendations, and target advertising.

Why we need caching for storing the data?

  • Cache the data to handle millions of reads. It improves the user experience by making the fetching process fast. We’ll also opt for lazy loading, which minimizes the client’s waiting time.
  • It allows us to load the content when the user scrolls and therefore save the bandwidth and focus on loading the content the user is currently viewing. It improves the latency to view or search a particular photo or video on Instagram.

6. High-Level Design(HLD) for Instagram System Design

Our system should allow us to upload, view, and search images and videos at a high level. To upload images and videos, we need to store them, and upon fetching, we need to retrieve the data from the storage. Moreover, the users should also be allowed to follow each other.

At a high level, Instagram can be viewed as a system with the following key components and interactions:

design-instagram-hld

Components:

  • Client: Mobile apps, web app, and APIs providing interface for users to interact with the system.
  • Authentication & Authorization: Handles user login, registration, and access control.
  • Content Management: Manages user-generated content like photos, videos, live streams, stories, and messages.
  • Feed Generation: Personalizes news feeds for each user based on their follows, activity, and engagement.
  • Social Graph: Tracks relationships between users (follows, followers, friends).
  • Discovery & Search: Enables searching for users, hashtags, locations, and content.
  • Notifications: Informs users about relevant events like likes, comments, mentions, and follows.
  • Analytics & Reporting: Tracks user engagement, content performance, and overall platform usage.

Interactions:

  • User creates content:
    • Client uploads photo/video.
    • Content Management stores media and metadata.
    • Feed Generation updates user's and relevant followers' feeds.
    • Notifications inform interested users.
  • User interacts with content:
    • Client sends like/comment/share actions.
    • Content Management and Social Graph update relevant data.
    • Feed Generation potentially reshuffles feeds based on new interactions.
    • Notifications inform interested users.
  • User discovers new content:
    • Client uses search functionalities.
    • Discovery & Search identifies relevant content.
    • Client displays search results.
  • User manages connections:
    • Client sends follow/unfollow requests.
    • Social Graph updates connections.
    • Feed Generation adjusts based on changed relationships.
  • User monitors activity:
    • Client checks notifications feed.
    • Notifications provide updates on relevant events.

Key Design Considerations:

  • Scalability: System should handle millions of users and massive data volumes.
  • Performance: Deliver fast response times for user interactions and content delivery.
  • Reliability: Ensure high availability and prevent data loss.
  • Security: Protect user data and privacy.
  • Engagement: Design features that encourage user interaction and content creation.

7. API Design for Instagram System Design

7.1 Post photos and videos

Here's a potential API design for uploading photos and videos to Instagram:

Endpoints:

  • POST /media: Submits a new photo or video.
  • PUT /media/{media_id}: Updates existing metadata for a media item.
  • DELETE /media/{media_id}: Deletes a media item.
Request
import requests

url = 'https://api.instagram.com/media'  # Replace with the actual API endpoint URL
access_token = 'your_access_token'  # Replace with the user's valid access token

# Define file path, caption, hashtags, and location (adjust as needed)
file_path = 'path/to/your/photo_or_video.jpg'  # Or .mp4 for videos
caption = 'Your caption for the media'
hashtags = 'photography,nature,beautiful'
location = {"latitude": 37.421998, "longitude": -122.084269}

# Prepare headers and files for the request
headers = {
    'Authorization': f'Bearer {access_token}',
    'Content-Type': 'multipart/form-data'
}
files = {
    'file': open(file_path, 'rb'),
    'caption': caption,
    'hashtags': hashtags,
    'location': str(location)  # Location needs to be serialized as a string
}

# Send the POST request
response = requests.post(url, headers=headers, files=files)

# Handle the response
if response.status_code == 201:
    data = response.json()
    print('Media uploaded successfully!')
    print('Media ID:', data['media_id'])
    print('URL:', data['url'])
else:
    print('Upload failed:', response.text)
    print('Error details:', response.json())  # Display any error details
Response
{
  "media_id": "1234567890abcdef",
  "url": "https://instagram.com/p/12345678",
  "created_at": "2024-01-09T18:34:00Z",
  "updated_at": "2024-01-09T18:34:00Z"
}

7.2 Follow and unfollow users

Here's a potential API design for following and unfollowing users on Instagram:

Endpoints:

  • POST /users/{user_id}/follow: Follows the specified user.
  • DELETE /users/{user_id}/follow: Unfollows the specified user.
  • GET /users/{user_id}/following: Retrieves a list of users followed by the specified user (paginated).
  • GET /users/{user_id}/followers: Retrieves a list of users following the specified user (paginated).
Response
 {
   "message": "User followed successfully"
 }
Requests
import requests

url = f'https://api.instagram.com/users/{user_id_to_follow}/follow'
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.post(url, headers=headers)

if response.status_code == 200:
    print('User followed successfully!')
else:
    print('Follow failed:', response.text)
    print('Error details:', response.json())

7.3 Like or Dislike posts

Designing an API for liking and disliking posts involves multiple considerations. Here's a breakdown of key aspects to think about:

API Endpoints:

  • GET /posts/{post_id}: Retrieves details of a post, including the number of likes/dislikes.
  • POST /posts/{post_id}/like: Registers a like for the post by the authenticated user.
  • POST /posts/{post_id}/dislike: Registers a dislike for the post by the authenticated user.
  • DELETE /posts/{post_id}/like: Removes the like for the post by the authenticated user (if previously done).
  • DELETE /posts/{post_id}/dislike: Removes the dislike for the post by the authenticated user (if previously done).
Request
import requests

url_like = f"https://api.instagram.com/media/likes/{media_id}"
url_unlike = f"https://api.instagram.com/media/likes/{media_id}"
headers = {'Authorization': f'Bearer {access_token}'}

# Like a post
response_like = requests.post(url_like, headers=headers)

# Unlike a post
response_unlike = requests.delete(url_unlike, headers=headers)
Response
// Request
POST /posts/123/like
Content-Type: application/json

{
  "user_id": 456
}

// Response (if successful)
Status: 200 OK
{
  "message": "Post liked successfully"
}

// Response (if already liked)
Status: 400 Bad Request
{
  "error": "Post already liked"
}

7.4 Search photos and videos

  • Search Endpoint:
    • Typically a POST request to a /search endpoint.
    • Query parameters include:query: The search term(s), media_type: Filters results by photo, video, or both, Additional filters: date range, location, people, etc. (if supported)
Request
import requests

url = "https://photoslibrary.googleapis.com/v1/mediaItems:search"
headers = {
    "Authorization": "Bearer YOUR_ACCESS_TOKEN"  # Replace with your access token
}
data = {
    "filters": {
        "mediaTypes": ["PHOTO", "VIDEO"]  # Search for both photos and videos
    }
}
response = requests.post(url, headers=headers, json=data)

if response.status_code == 200:
    results = response.json()
    # Process the search results
else:
    print("Error:", response.status_code, response.text)
Response
{
  "data": [
    {
      "media_type": "IMAGE",
      "media_url": "https://www.instagram.com/p/abc123/",
      "thumbnail_url": "https://scontent-amt2-1.cdninstagram.com/v/t51.2885-15/e35/s1080x1080/275469546_546545454546_54554655465654654.jpg?_nc_ht=scontent-amt2-1.cdninstagram.com&_nc_cat=100&_nc_ohc=xyz123&edm=ABfd0MgBAAAA&ccb=7-4&oh=00_AT854x5465465465454654465546554656546545&oe=62BD5465&_nc_sid=7bff83",
      "caption": "My adorable cat!",
      "timestamp": "2024-01-10T13:40:00+0000"
    },
    {
      "media_type": "VIDEO",
      "media_url": "https://www.instagram.com/tv/xyz987/",
      "thumbnail_url": "https://scontent-amt2-1.cdninstagram.com/v/t51.2885-15/e35/s1080x1080/246545465546544546454654465454655465.jpg?_nc_ht=scontent-amt2-1.cdninstagram.com&_nc_cat=101&_nc_ohc=abc123&edm=ABfd0MgBAAAA&ccb=7-4&oh=00_AT854x546546546544654465546554656546554&oe=62BD5465&_nc_sid=7bff83",
      "caption": "Amazing sunset timelapse!",
      "timestamp": "2024-01-09T20:30:00+0000"
    }
  ]
}

8. Database Design for Instagram Database Design

We need the following tables to store our data:

8.1 User:

Table to store user data - Users

User
{
userId: string[HashKey]
name: string
emailId: string
creationDateInUtc: long
}

8.2 User_Follows:

Table to store follower data - User_follows

Follow_User_Table
{
followingUserId_followerUserId: string [HashKey]
followingUserId: string [RangeKey]
followerUserId: string
creationDateInUtc: long
}

8.3 User Uploads

Table to store user uploads - User_uploads

Upload Image Table
{
uploadId: string[Hashkey]
userId: string[RangeKey]
imageLocation: string
uploadDateInUtc: long
caption: string
}

8.4 User Feed

Table to store the user feed data - User_feed

Upload Video or Image
{
userId: string[Hashkey]
uploadId: string
creationDateInUtc: long[RangeKey]
}

8.5 Which Database we should select for Data storing?

It is essential to choose the right kind of database for our Instagram system, but which is the right choice — SQL or NoSQL? Our data is inherently relational, and we need an order for the data (posts should appear in chronological order) and no data loss even in case of failures (data durability). Moreover, in our case, we would benefit from relational queries like fetching the followers or images based on a user ID. Hence, SQL-based databases fulfill these requirements.

Image and Feed generation service used as microservice architecture.

9. Microservices for Instagram System Design

Image and Feed generation service used as microservice architecture.

Microservices - also known as the microservice architecture - is an architectural style that structures an application as a collection of services that are:

The microservice architecture enables an organization to deliver large, complex applications rapidly, frequently, reliably and sustainably - a necessity for competing and winning in today’s world.

10. Scalability for Instagram System Design

Scalability refers to the ability of an organization (or a system, such as a computer network) to perform well under an increased or expanding workload. A system that scales well will be able to maintain or increase its level of performance even as it is tested by larger and larger operational demands.

We can add more servers to application service layers to make the scalability better and handle numerous requests from the clients. We can also increase the number of databases to store the growing users’ data.

These requirements has been covered:

11. Conclusion

In this article we have discussed about instagram design in which how discussed about the save post and videos, like and dislike the post, show feeds, posting the post or videos, how we can put the hashtags. nstagram's system design is a complex and sophisticated architecture that prioritizes scalability, availability, security, and user experience. The platform's success is not only attributed to its user-friendly interface but also to the robust backend infrastructure that supports its massive user base and dynamic content.


Similar Reads

Designing Authentication System | System Design
Keeping your important digital information safe is like building a strong foundation, and the key to that is a good security system. This article will help you understand how to make a robust security system step by step. We'll start with figuring out what you want to protect and what you want to achieve. Then, we'll talk about the detailed design
10 min read
Designing Parking Lot (Garage) System | System Design
Parking garage plays an important role in today's world, as the number of vehicles is increasing every day, so it has become a major concern to manage them. The parking garage provides the solution to this problem of limited space. It offers to book a Parking garage using any method whether online or offline. In this article, we will explore all th
11 min read
How does the process of developing a system differ from designing a system?
System Development vs. System DesignSystem DevelopmentSystem DesignHow Does The Process Of Developing A System Differ From Designing A System?ConclusionSystem DevelopmentSystem development is a structured process of designing, creating, and implementing software or hardware solutions to address specific needs or problems within an organization. It
7 min read
Complete Reference to Databases in Designing Systems - Learn System Design
Previous Parts of this System Design Tutorial What is System DesignAnalysis of Monolithic and Distributed SystemsImportant Key Concepts and TerminologiesWhat is Scalability and How to achieve itWhat is a Database? When we store data, information, or interrelated data, in an organized manner in one place, it is known as Database. Databases are respo
14 min read
Designing Google Maps | System Design
A web mapping tool called Google Maps offers a range of geographic data, such as street maps, satellite photos, streets' aerial views, real-time traffic reports, and directions. There are several versions of it available, such as web, mobile, etc. In this article, we will see the system design of Google Maps Important Topics for the Google Maps Sys
11 min read
Designing Online Code Editor | System Design
Online code editors are now a crucial part of modern software development. They allow collaboration, real-time coding, and the ability to access development tools from anywhere in the world. In this article, we'll explore how to design a system to create a useful and scalable online code editor. Important Topics for the Online Code Editor System De
11 min read
Designing TikTok | System Design
TikTok, the globally acclaimed video-sharing platform, enchants audiences with its short, captivating content. Behind this phenomenon lies a sophisticated system meticulously designed to handle vast user-generated videos, likes, and personalized recommendations. From video uploads to tailored feeds, TikTok's design weaves together smart technologie
15 min read
Designing Amazon Prime Video | System Design
Amazon Prime Video is a popular streaming provider that offers a wide library of movies, TV shows, and unique content to hundreds of thousands of users worldwide. Designing such a complex system involves addressing diverse challenges related to scalability, reliability, and overall performance. In this article, we're going to discover the High-leve
14 min read
Designing Content Delivery Network (CDN) | System Design
In the digital age, where online content material is king, the importance of delivering a seamless and rapid user experience can not be overstated. Content Delivery Networks (CDNs) play a vital role in attaining this aim by distributing content material strategically throughout servers globally. This article will guide you through the process of de
15 min read
Designing Zoom | System Design
Creating an app like Zoom may seem simple from the user's perspective, but in reality, it's a complex task involving hundreds of software engineers working for years. Zoom, like other similar apps, requires careful planning and design to provide seamless video conferencing services worldwide. This article explains how Zoom works and how it handles
14 min read
Designing Whatsapp Messenger | System Design
Ever thought about how this widely-used messaging app actually works behind the scenes? This article is your guide to the system design of WhatsApp. From handling tons of messages to making sure your chats are secure, we'll explore the technical aspects that keep this app running smoothly and managing things like managing data, keeping your message
7 min read
Designing Facebook Messenger | System Design Interview
We are going to build a real-time Facebook messaging app, that can support millions of users. All the chat applications like Facebook Messenger, WhatsApp, Discord, etc. can be built using the same design. In this article, we will discuss the high-level architecture of the system as well as some specific features like building real-time messaging, g
9 min read
Designing UPI - System Design
Designing a Unified Payments Interface (UPI) system involves creating an architecture that enables real-time inter-bank transactions through a seamless and secure platform. Key components include user interfaces, a central UPI switch managed by the National Payments Corporation of India (NPCI), backend systems of participating banks, and third-part
10 min read
Designing Twitter - A System Design Interview Question
Designing Twitter (or Facebook feed or Facebook search..) is a quite common question that interviewers ask candidates. A lot of candidates get afraid of this round more than the coding round because they don't get an idea of what topics and tradeoffs they should cover within this limited timeframe. Important Topics for Designing Twitter How Would Y
15+ min read
Design a system that counts the number of clicks on YouTube videos | System Design
Designing a Click Tracking System for YouTube Videos involves architecting a comprehensive and efficient solution to monitor and analyze user interactions with videos on the YouTube platform. Important Topics for Counting the number of Clicks on Youtube Videos System DesignRequirementsCapacity EstimationLow-Level Design (LLD)Database Design-High-Le
15+ min read
Design Restaurant Management System | System Design
In the modern restaurant industry, delivering exceptional dining experiences requires more than just good cuisine. Restaurant Management Systems have emerged as the secret ingredient for efficient operations and customer satisfaction. In this article, we are going to design a restaurant management system where users can effortlessly discover nearby
15 min read
Design a Picture-Sharing System - System Design
In the present day, the need for good tools to exchange and organize images has never been this much higher. As for social networking, e-shopping, personal branding, and many other related purposes, users anticipate that the sites they visit provide them with fast and dependable environments with or without loads of images that enable them to inter
11 min read
Google Cloud Platform - Designing an Issues Notification System using Cloud Run
Problem Statement: Consider your run a Software company. And you have a huge client list who frequently use your software. You host your software on Github so that its users can send information regarding bugs in them. You being a lead developer in the team, how will you design a system that notifies the development team on Slack whenever an issue
3 min read
Iterative Incremental Model in Designing System
The Iterative Incremental Model is a smart way to design complex systems. Instead of trying to figure everything out at the beginning, this approach breaks the project into smaller parts. Each part is then developed, tested, and added to the system, bit by bit. This method is great for projects where things might change or if the requirements aren'
8 min read
Agile Model In Designing System
The Agile model in the context of system design is a flexible and adaptive approach to designing systems that can respond to changing requirements and customer needs. The key objective is to create a functioning system as soon as possible and to refine it depending on input from stakeholders. The agile model also promotes customer and developer inv
9 min read
Difference between System Design and System Architecture
When it comes to software development and engineering there can be some confusion, between the two terms "System Design" and "System Architecture." Although they are related they actually refer to stages and responsibilities in creating scalable and efficient systems. It is important to grasp the differences, between System Design and System Archit
3 min read
Distributed Messaging System | System Design
In our fast-paced world, how we share information matters more than ever. Old-school messaging setups sometimes struggle to keep up with today's tech demands. That's where distributed messaging systems step in. They're like a breath of fresh air, changing the game and making sure our messages get where they need to go, no matter what. Important Top
8 min read
System Analysis | System Design
In the areas of science, information technology, and knowledge, the difficulty of systems is of much importance. As systems became more complicated, the traditional method of problem-solving became inefficient. System analysis is to examine a business problem, identify its objectives and requirements, and then design the most optimal solution to fu
6 min read
System Design | Online Banking System
In the digital era, the layout of a Online Banking System is a critical element of any commercial enterprise, it not only affects user experience but also the growth of a service-providing company. This article explores the comprehensive guide to designing a Online Banking System for its efficient use. From practical and non-useful requirements to
12 min read
Resilient System - System Design
Imagine you're building a castle out of blocks. If you design it so that removing one block doesn't make the whole castle collapse, you've made something resilient. hen we talk about creating a resilient system, we're essentially doing the same thing but with computer systems. These systems are designed to handle problems like errors, crashes, or e
10 min read
Blob vs. File System in System Design
In system design, choosing the right method for storing and managing data is crucial for performance, scalability, and maintenance. Two common approaches are using Blobs (Binary Large Objects) and traditional file systems. Each has its own set of advantages and challenges, making them suitable for different use cases. This article explores the key
7 min read
Differences between System Analysis and System Design
System Analysis and System Design are two stages of the software development life cycle. System Analysis is a process of collecting and analyzing the requirements of the system whereas System Design is a process of creating a design for the system to meet the requirements. Both are important stages as it helps to create an effective system with all
4 min read
System Development Life Cycle vs. System Design Life Cycle
The concepts of System Development Life Cycle (SDLC) and System Design Life Cycle (SDLC) are fundamental in the field of software engineering and system architecture. Both frameworks play crucial roles in the creation and implementation of complex systems, but they address different aspects of system creation and management. Important Topics for Sy
6 min read
System Design of Uber App | Uber System Architecture
Getting the hassle-free transportation service(Uber, Ola) is very easy but is it also simple to build these gigantic applications that have hundreds of software engineers working on them for a decade? surely not. These systems have much more complex architecture and there are a lot of components joined together internally to provide riding services
13 min read
System Design - Design Google Calendar
Google Calendar is like a special calendar that we can use on our computer or phone. We can use it to remember important things, like birthdays, appointments, and events. We can also set reminders to help us remember when something is coming up soon. We can also share our calendars with our family or friends. It's like having our own special schedu
8 min read
three90RightbarBannerImg