Optimizing Django Performance with AWS RDS and Read Replica

Anupam kushwaha
6 min readJun 9, 2024

--

Image source

What would you learn?

You will be able to understand the below topics in detail with practical screenshots

  1. How to create RDS in AWS.
  2. What is Read Replica.
  3. How to create Read replica from RDS Instance.
  4. How to Connect RDS with Dbeaver.
  5. How to configure RDS Inbound rule.
  6. How to Configure 2 Databases in Django.
  7. 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:

  1. 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

Selecting RDS Service

Step 2:

Create the RDS database

RDS Instance list

Step 3:

Click on Create Database

  1. Select “Standard Create” from database creation method .
  2. Select “PostgreSQL” in Engine type.
Creating Database

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.

Configuring credentials for Database

5. Select “Yes” under “Public access”.

6. Click on Create database and wait until the instance has status “Available”.

Created Database

Step 4:

Click on Created Database and copy endpoint of RDS, we will use it while connecting with dbeaver

RDS Connectivity and Security

Step 5:

Scroll down to the security group and click on Edit Inbound rules

Configuring security group inbound rules

Step 6:

Click on Add Rule button and add Inbound rule and then click on save

Editing inbound rule

Step 7:

Now let’s connect RDS with dbeaver for viewing the data of RDS

Connecting RDS Database with Dbeaver

Step 8:

Now let’s create a table via executing an Query on dbeaver

Creating a table by running Query

Step 9 :

Now let’s add few records into the table

Adding records into table

Step 10:

Now let’s create database Read Replica.

Select the RDS Instance >> Click on Actions >> Click on Create Read Replica

Creating Read replica of database

Step 11:

Select source Replica and Give name to your Read DB Instance

configuring Read replica of database

Step 12:

select Single DB Instance in Availability and select IPV4 in connectivity and click on Create Read replica.

Configuring single DB Instance

Step 13:

After Clicking on Create Read Replica, you will be able to see your Read replica like below

Created Read Replica

Step 14:

Let’s connect Read Replica with DBeaver and see whether both the DB has same data or not.

Connecting RDS Database with Dbeaver

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”

Checking records of Replica DB

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.

  1. We can not delete the table directly from Replica DB. if we will try then we will get below error
Error while deleting table in read-replica DB

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”

Replica Table

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

Example of Configuring the databases in Django

Step 19:

Now, After running “makemigrations” and “migrate” command all Django default tables will get created in both Database.

Data in RDS DB and it’s Replica Read DB

Step 20:

Now, Let’s Add some records into phone_book model and check whether data is reflecting in both the tables or not.

Django Admin panel of Phone Book

Step 21:

We can see, both the table are having exactly same data.

Phone_book table data in both DB

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.

  1. 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

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.

--

--

Anupam kushwaha
Anupam kushwaha

No responses yet