A Production Level Social Media Backend Implementation with Django
Explore the docs Β»
Report Bug
Β·
Request Feature
This project provides a system for building scalable Twitter-liked social media backend systems and implementing robust system designs using Python, Django, and various modern technologies. The system is aimed at developers who want to enhance their backend development skills or need to implement real-world system architecture. It is inspired by industrial-grade backend systems designed to handle millions of users and offers practical tools and techniques for scalable system design.
- Backend Development with Python and Django: Scalable web backend architecture with Django.
- System Design Patterns: Implementing system design principles like Push/Pull models, Denormalization, and Asynchronous Processing.
- Cloud Integration: Cloud deployment and scaling using AWS. In this project, we mainly use OCI for free deployment and Blob Storage.
- Caching: Efficient caching mechanisms using Redis and Memcached.
- Message Queuing: Implementing message queues using Celery & Redis to handle asynchronous tasks.
- Database Management: Support for SQL (MySQL) and NoSQL (HBase) databases for scalable data storage solutions.
- RESTful APIs: Build and manage scalable REST APIs for microservices architecture.
This system is built with the following components. Before you use, please also get prepared for:
-
Clone the repository:
git clone https://github.com/ljyou001/django-twitter-backend.git cd system-design-backend -
Set up the virtual environment:
python3 -m venv venv source venv/bin/activate -
Install the required dependencies:
pip install -r requirements.txt
-
Configure the environment variables:
Create a
.envfile for environment variables and provide your database and cloud credentials:SECRET_KEY=<your-secret-key> DATABASE_URL=mysql://user:password@localhost:3306/db_name AWS_ACCESS_KEY_ID=<your-access-key> AWS_SECRET_ACCESS_KEY=<your-secret-key>
-
Run the project:
python manage.py migrate python manage.py runserver
Celery is used to handle background tasks in this project, such as sending notifications or processing large data. Here's how to set it up:
-
Install Celery:
Celery should already be included in the
requirements.txt. If it's missing, install it using:pip install celery
-
Configure Celery:
In your Django project's
settings.py, add the following configurations:CELERY_BROKER_URL = 'redis://localhost:6379/0' # Using Redis as the broker CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
-
Start Celery Worker:
After configuring Celery, start the worker by running:
celery -A your_project_name worker --loglevel=info
Memcached is used for caching frequently accessed data and reducing database load.
-
Install Memcached:
If Memcached is not already installed on your system, you can install it via:
sudo apt-get install memcached
Or, for macOS, use Homebrew:
brew install memcached
-
Install the Python Memcached Client:
Install the Python client
pymemcache:pip install pymemcache
-
Configure Memcached:
In
settings.py, add the following configuration for Django to use Memcached:CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache', 'LOCATION': '127.0.0.1:11211', } }
-
Start Memcached:
Start Memcached using:
memcached -d -m 64 -p 11211 -u memcache
HBase is used for managing NoSQL data, particularly for write-heavy tables.
-
Install HBase and Thrift:
Install HBase and enable Thrift support:
sudo apt-get install hbase thrift
-
Start HBase and Thrift Server:
Once HBase is installed, start HBase and the Thrift server with the following commands:
start-hbase.sh hbase thrift start
-
Install the Python Thrift Client:
To interact with HBase via Thrift, install the Python client:
pip install happybase
-
Configure HBase in the Project:
In
settings.py, configure your HBase connection:import happybase HBASE_HOST = 'localhost' HBASE_PORT = 9090 # Example connection to HBase connection = happybase.Connection(HBASE_HOST, port=HBASE_PORT)
You can use this system to implement a Twitter-like News Feed. The project includes examples of how to:
- Handle user interactions such as Tweets, Friendships, Likes, and Comments.
- Use Redis & Memcached to cache data and reduce database load.
- Implement Message Queues with Celery and Redis for handling background tasks like sending notifications.
This project includes integration with AWS S3 for media storage and Elastic Load Balancer for scalability. You can deploy the application on EC2 or EKS for production use.
Contributions are welcome! Here's how you can help:
- Fork this repository.
- Create a new branch (
git checkout -b feature-branch). - Make your changes.
- Submit a pull request with detailed description of the feature or fix.
If you encounter any issues or have suggestions for improvements, feel free to open an issue in the issues tracker.
This project is licensed under the MIT License - see the LICENSE file for details.
- Oliver Lyu - Lead Developer
This project is based on the course - Python in Real World - Twitter Backend System
This project is inspired by professional backend system designs for large-scale applications. We aim to make scalable backend architectures accessible to everyone in the open-source community.