coaster
coaster copied to clipboard
Support master/replica databases
For performance reasons, apps like Hasjob in production should have separate master and replica databases, running on different nodes:
- All writes happen on the master database
- All reads happen on the replica database a. …unless we just wrote to master, in which case read from master
Here are a couple of gists showing how this can be achieved transparently with Flask-SQLAlchemy:
- https://gist.github.com/trustrachel/6828122
- https://gist.github.com/adhorn/b84dc47175259992d406
We'll need a variation of the second gist to handle scenario 2a: record a dirty flag in application context (_app_ctx_stack.top
) and send all queries to master for as long as the flag remains. We also need to debug and resolve the problem with self._flushing
, as reported in the various comments on the first gist.
Coaster provides a copy of Flask-SQLAlchemy's SQLAlchemy
object in the coaster.db
module (not the db
instance that is also provided). Our version of the subclass could reside here and be available for importing to apps.