Optimizing Django Performance with AWS RDS and Read Replica
What would you learn?
You will be able to understand the below topics in detail with practical screenshots
- How to create RDS in AWS.
- What is Read Replica.
- How to create Read replica from RDS Instance.
- How to Connect RDS with Dbeaver.
- How to configure RDS Inbound rule.
- How to Configure 2 Databases in Django.
- how to Reduce load from Primary Database and shift it to read DB.
What is Read Replica?
Read replica allows us to have “non-editable copies” of our database. This Replica is created by copying data from primary database asynchronous.
Advantages of the Read Replicas:
- Increased Read Capacity: Read Replicas allow you to distribute read traffic across multiple RDS instances, thereby increasing the read throughput and reducing the load on the primary database instance.
Step 1:
Login into your AWS account. search for “RDS” in search bar and select RDS
Step 2:
Create the RDS database
Step 3:
Click on Create Database
- Select “Standard Create” from database creation method .
- Select “PostgreSQL” in Engine type.
3. In Templates section, select free tier if you are following that article just for learning purpose.
4. Name your database, and set username and password. In this example, we are giving “Django-phone-book-project-database” for database.
5. Select “Yes” under “Public access”.
6. Click on Create database and wait until the instance has status “Available”.
Step 4:
Click on Created Database and copy endpoint of RDS, we will use it while connecting with dbeaver
Step 5:
Scroll down to the security group and click on Edit Inbound rules
Step 6:
Click on Add Rule button and add Inbound rule and then click on save
Step 7:
Now let’s connect RDS with dbeaver for viewing the data of RDS
Step 8:
Now let’s create a table via executing an Query on dbeaver
Step 9 :
Now let’s add few records into the table
Step 10:
Now let’s create database Read Replica.
Select the RDS Instance >> Click on Actions >> Click on Create Read Replica
Step 11:
Select source Replica and Give name to your Read DB Instance
Step 12:
select Single DB Instance in Availability and select IPV4 in connectivity and click on Create Read replica.
Step 13:
After Clicking on Create Read Replica, you will be able to see your Read replica like below
Step 14:
Let’s connect Read Replica with DBeaver and see whether both the DB has same data or not.
Step 15:
We can see our “django-phone-book-project-database-read-replica” database is also have exactly same data as “django-phone-book-project-database”
Step 16:
Now let’s delete our Testing table “mytable” and then connect it with Django.
To delete a table, you need to first delete the table in the primary database. This will ensure that the corresponding table in the replica database is also deleted.
- We can not delete the table directly from Replica DB. if we will try then we will get below error
2. So we have to delete the table from primary DB.
3. After Deleting “mytable” Database will become empty and now it is ready to connect with Django application.
4. our Replica Database, After deleting “mytable”
Step 17:
Configuring multiple databases in Django settings with environment variable for better security
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': env('POSTGRES_PASSWORD'),
'HOST': env('POSTGRES_HOST'),
'PORT': '5432'
},
'read_db': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': env('POSTGRES_PASSWORD'),
'HOST': env('POSTGRES_HOST'),
'PORT': '5432'
},
}
Step 18:
In this example, I have directly assigned values. but it Real project it is always recommended to use Environment variables
Step 19:
Now, After running “makemigrations” and “migrate” command all Django default tables will get created in both Database.
Step 20:
Now, Let’s Add some records into phone_book model and check whether data is reflecting in both the tables or not.
Step 21:
We can see, both the table are having exactly same data.
Step 22:
Now, we can fetch the data from Read DB or Replica DB as per our requirement and we can reduce the load from primary DB.
- Code snippet for getting the data from read_db
>>> from app.models import PhoneBook
>>> # accessing data from Primary DB
>>> contacts = PhoneBook.objects.all()
>>> contacts
<QuerySet [<PhoneBook: PhoneBook object (1)>, <PhoneBook: PhoneBook object (2)>, <PhoneBook: PhoneBook object (3)>, <PhoneBook: PhoneBook object (4)>]>
>>> # now accessing instances from readdb or Reaplica DB
>>> contacts = PhoneBook.objects.using('read_db').all()
>>> contacts
<QuerySet [<PhoneBook: PhoneBook object (1)>, <PhoneBook: PhoneBook object (2)>, <PhoneBook: PhoneBook object (3)>, <PhoneBook: PhoneBook object (4)>]>
2. Example of fetching data from primary DB and Read DB
Ref
Conclusion
Finally, We have successfully learn the concept of Read Replica and how to Implement it in Django.