Skip to content

Commit 5dcaead

Browse files
authored
Add read replica (#115)
* Use a custom NestedSensorSerializer since sensor_type isn't being used at any place * Update dev postgres to version 13.7 which is what we are using in production * Add a read replica for the primary database * Add a Router for determining which database to be used for which database operation. * Use NestedSensorSerializer from feinstaub * Update database environment variable naming * Use the default database for user authentication queries * Adds a Postgresql data volume
1 parent 4f6b7f2 commit 5dcaead

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

docker-compose.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@ services:
1212
- RABBITMQ_USERNAME=sensorsafrica
1313
- RABBITMQ_PASSWORD=sensorsafrica
1414
postgres:
15-
image: postgres:9.6
15+
image: postgres:13.7
1616
ports:
1717
- "54321:5432"
1818
environment:
1919
- POSTGRES_USER=sensorsafrica
2020
- POSTGRES_PASSWORD=sensorsafrica
2121
- POSTGRES_DB=sensorsafrica
22+
volumes:
23+
- postgres_data:/var/lib/postgresql/data/
2224
api:
2325
build:
2426
context: .
2527
environment:
2628
SENSORSAFRICA_DATABASE_URL: postgres://sensorsafrica:sensorsafrica@postgres:5432/sensorsafrica
29+
SENSORSAFRICA_READ_DATABASE_URL: postgres://sensorsafrica:sensorsafrica@postgres:5432/read_replica
2730
SENSORSAFRICA_RABBITMQ_URL: amqp://sensorsafrica:sensorsafrica@rabbitmq//
2831
SENSORSAFRICA_FLOWER_ADMIN_USERNAME: admin
2932
SENSORSAFRICA_FLOWER_ADMIN_PASSWORD: password
@@ -38,3 +41,6 @@ services:
3841
ports:
3942
- "8000:8000"
4043
- "5555:5555"
44+
45+
volumes:
46+
postgres_data:

sensorsafrica/router.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class ReplicaRouter:
2+
route_app_labels = {'auth', 'sessions',}
3+
4+
def db_for_read(self, model, **hints):
5+
if model._meta.app_label in self.route_app_labels:
6+
return 'default'
7+
return "read_replica"
8+
9+
def db_for_write(self, model, **hints):
10+
return "default"
11+
12+
def allow_relation(self, obj1, obj2, **hints):
13+
"""
14+
Relations between objects are allowed if both objects are
15+
in the primary/replica pool.
16+
"""
17+
db_set = {'default', 'read_replica'}
18+
if obj1._state.db in db_set and obj2._state.db in db_set:
19+
return True
20+
return None

sensorsafrica/settings.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,16 @@
109109
"SENSORSAFRICA_DATABASE_URL",
110110
"postgres://sensorsafrica:sensorsafrica@localhost:5432/sensorsafrica",
111111
)
112-
DATABASES = {"default": dj_database_url.parse(DATABASE_URL)}
112+
READ_DATABASE_URL = os.getenv(
113+
"SENSORSAFRICA_READ_DATABASE_URL", DATABASE_URL
114+
)
115+
116+
DATABASES = {
117+
"default": dj_database_url.parse(DATABASE_URL),
118+
"read_replica": dj_database_url.parse(READ_DATABASE_URL),
119+
}
113120

121+
DATABASE_ROUTERS = ["sensorsafrica.router.ReplicaRouter", ]
114122

115123
# Password validation
116124
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

0 commit comments

Comments
 (0)