# Django create clone objects and keep them in replica database

**URL:** <https://forum.djangoproject.com/t/django-create-clone-objects-and-keep-them-in-replica-database/12109>\
**Category:** Show & Tell\
**Created:** [February 12, 2022, 8:29pm UTC](https://forum.djangoproject.com/t/django-create-clone-objects-and-keep-them-in-replica-database/12109 "2022-02-12T20:29:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [February 12, 2022, 8:29pm UTC](https://forum.djangoproject.com/t/django-create-clone-objects-and-keep-them-in-replica-database/12109/1 "2022-02-12T20:29:02Z")

</div>

After reading [django multiple database documentation](https://docs.djangoproject.com/en/4.0/topics/db/multi-db/) we can use multiple database but can’t keep clone object in our database. Let explain little bit assume you have two model teacher and student. You can use two separate database for teacher and student but you can’t use another database for keep clone objects of your teacher and student model. So here we will use django siganls for keep clone objects in our replica\_database. Signals will be triggered and create clone object whenever any objects create in our model. Here is my code:

**settings.py**

```auto
'default': { 
        'NAME': 'primary_database',
        'ENGINE': 'django.db.backends.mysql',
        'HOST': 'localhost',
        'USER': 'root',
        'PASSWORD': '',
    },
    'replica1_database': {
        'NAME': 'replica1_database',
        'ENGINE': 'django.db.backends.mysql',
        'HOST': 'localhost',
        'USER': 'root',
        'PASSWORD': '', },

```

**models.py:**

```auto
from django.db.models.signals import post_save
from django.dispatch import receiver

class Contact(models.Model):
      name = models.CharField(blank=True, null=True, max_length=100)
    
    
@receiver(post_save, sender=Contact, dispatch_uid="clone_objects")
def replica1_databse(sender, instance, created, **kwargs):
   if created: #cerate clone object
        obj = instance
        obj.save(using='replica1') 
    else: #updating clone object
        obj = Contact.objects.using('replica1').update(
        name=instance.name)

```

Here I am triggering signals for create an clone objects in my replica1\_database.

Now run `python manage.py makemigrtions contact` and `python manage.py migrate contact` this two migrations applied in your default database. This is the most important step **----\>** You have to run `python manage.py migrate --database=replica1` This migrate applied for your **replica1 database**.

I Think it also an good idea to keep an backup database for avoid any unexcepted situation such as server down.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 17, 2022, 4:47am UTC](https://forum.djangoproject.com/t/django-create-clone-objects-and-keep-them-in-replica-database/12109/2 "2022-02-17T04:47:46Z")

</div>

First, there are no guarantees that the two databases will remain _identical_. There are any number of race conditions existing that would cause the primary keys to get out of sync with each other.

This really isn’t something that should be handled within an application. Both PostgreSQL and MySQL/MariaDB have database-layer replication options that are going to be far more robust than what can be done within Django.

---

<div class="post-metadata">

**Author:** ![MDFARHYN](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/mdfarhyn/32/4519_2.png) [@MDFARHYN](https://forum.djangoproject.com/u/MDFARHYN)\
**Post date:** [February 17, 2022, 5:52am UTC](https://forum.djangoproject.com/t/django-create-clone-objects-and-keep-them-in-replica-database/12109/3 "2022-02-17T05:52:25Z")

</div>

Thanks for your suggestions. I really don’t know that those database have own replication method.

---

<div class="post-metadata">

**Author:** ![plopidou](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/plopidou/32/5148_2.png) [@plopidou](https://forum.djangoproject.com/u/plopidou)\
**Post date:** [March 14, 2022, 11:30am UTC](https://forum.djangoproject.com/t/django-create-clone-objects-and-keep-them-in-replica-database/12109/4 "2022-03-14T11:30:19Z")

</div>

yes, nothing anyone will do at application level will work as well as database-level replication (master/slave, master/master, reference/replica, use the new parlance or the old one – principle is the same).

one master - n slaves is very often used and as Ken pointed out, will ensure consistency across all instances.

Now this will enable you to scale for _read_ operations.  
Scaling for write operations will be a little more involved and can (should?) come later.

In either case, you can start by using database routers at django level – or the equivalent provided by you database vendor (whichever it may be).

At python/django level, it is a surprisingly easy thing to do: [Multiple databases | Django documentation | Django](https://docs.djangoproject.com/en/4.0/topics/db/multi-db/#automatic-database-routing)

`db_for_write` will most likely be your master  
`db_for_read` will most likely be one of your slaves.

Setting up a mysql master/slave is not too hard. Easy to test on a local installation. If you intend to go that route (pun intended haha), though that is beyond django in general, and you use Mysql:

1. use gtids
2. use semi sync
3. do not store sessions in the database – use another storage backend or deactivate their replication.

Have fun! 🙂
