This repo was created to simulate a Django project using a write/read replica database (PSQL).
As you can see on the project root, this project uses docker-compose, so simply run docker-compose up app
and that's
it!
The scenario that will be explained uses the route POST /api/v1/message
(this is the view).
I also used PyCharm for debug purposes, with an remote interpreter of app
(from docker-compose.yml)
This scenario is before applying the DB Router (to be more specifically, the test was made on this commit).
As we can see, using a single database replica for read/write, even before the transaction being commited, the data is available to be retrieved:
The object was created on line 19 and, just after that, it's possible to perform a .get
and retrieve it.
As we can see, using a database replica specific for read and another one for write, the data will only be created after the transaction commit is finished.
The first image simulates the same scenario that was performed before: inside the same transaction, right after performing the .create
, on line 19.
But, since tthe transaction wasn't commited yet, thus the new data isn't on read replica, it raise a Message.DoesNotExist
, which is exactly the scenario that we're looking for!
And, right after the transaction is commited, the data was found on read replica.
There are some TODOs, such as:
- Create an integration test, to make it easier to debug;
- Some docstrings to help understand the code;
But the initial purpose of the project was achieved, so other people can benefit from it.