Designing Instagram's Backend in Django

Jul 26, 2024

Lecture: Designing Instagram's Backend in Django

Lecture Flow

  1. Discuss design
  2. Database schemas
  3. Dive into coding

System Design

  • Server: One Django server running with default SQLite DB
  • Clients: Multiple clients interacting with the server

Core Features

  • Models: Post and User (primary models)
    • Post: Includes images, text, and descriptions
    • User: Authenticated users
    • PostComment: Comments on posts
    • UserFollow: Tracks followers and followees
    • PostLike: Tracks who liked which posts

Initial Setup

  • Create a Python environment: source insta/bin/activate
  • Install Django and Django Rest Framework: pip install Django djangorestframework
  • Create Django project: django-admin startproject Instagram
  • Create a new app: python manage.py startapp app
  • Modify settings.py to include app and rest_framework
  • Modify urls.py to include app's URLs

Database Schema

User Table

  • userID (Primary Key)
  • email
  • username
  • password
  • first_name
  • last_name
  • bio (optional)
  • created_at
  • updated_at

Post Table

  • postID (Primary Key)
  • title
  • description
  • created_at
  • updated_at
  • user (Foreign Key)

PostComment Table

  • commentID (Primary Key)
  • comment_text
  • created_at
  • updated_at
  • userID (Foreign Key)
  • postID (Foreign Key)

PostLike Table

  • likeID (Primary Key)
  • userID (Foreign Key)
  • postID (Foreign Key)

UserFollow Table

  • followID (Primary Key)
  • userID (Foreign Key)
  • followeeID (Foreign Key)

Django Model Configurations

User Model

  • Extend Django's default user model using AbstractUser
  • Configure custom fields and default values (is_staff, is_active, is_superuser)
  • Add custom manager class (UserManager) with methods to create users and superusers
  • Settings update: Specify custom user model in settings.py

Post Model

  • Define basic fields: title, description, created_at, updated_at, user

PostLike Model

  • Define fields: userID, postID
  • Ensure a user cannot like the same post more than once using unique_together

PostComment Model

  • Define fields: comment_text, created_at, updated_at, userID, postID

UserFollow Model

  • Define fields: userID, followeeID
  • Ensure unique combinations of user and followee using unique_together

Serializers

  • Create serializers for each model to handle validation and transformation of data

Views and APIs

User APIs

  • Registration: Register a new user
  • Login: Authenticate user and return a token
  • Get User: Retrieve user by ID
  • Update User: Edit user details
  • Delete User: Remove a user from the database
  • Follow User: Follow another user
  • Unfollow User: Unfollow a user
  • List User Posts: Retrieve all posts made by a user

Post APIs

  • Create Post: Add a new post
  • Retrieve Post: Get details of a post by ID
  • Update Post: Modify an existing post
  • Delete Post: Remove a post
  • Like Post: Like or unlike a post
  • Get Post Likes: Get all users who liked a post
  • Add Comment: Comment on a post
  • Get Comments: Retrieve all comments on a post

Testing

  • Various test cases for each API using Django's development server and Django shell
  • Validations for user creation, password checking, user token generation for login, and CRUD operations for posts and comments

Potential Enhancements

  • Media handling by uploading media to S3 and storing URLs in the database
  • Optimize using caching for follower/following counts on large scale