Skip to main content
https://img.shields.io/pypi/v/flask_replicated.svg

SUMMARY

Flask replicated is a Flask extension, designed to work with SqlAlchemy. It’s purpose it to provide more or less automatic master-slave replication. On each request, extension determines database usage intention (to read or to write into a database). Then, it picks right database url inside overriden db.get_engine() whenever request handler tries to access database depending on REST verb used.

Flask replicated comes with a security kill-switch to enable developper mode to easily keep control on the feature AUTO_READ_ON_SLAVE true by default, once deactivated only master database will be used unless explicit use of the decorators.

INSTALLATION

  1. Install flask replicated distribution using pip install flask_replicated. Or add flask-replicated==1.4 in requirements.txt and requirements pip3 install -r requirements.txt.

  2. Import library from flask_replicated import FlaskReplicated or import flask_replicated depending on how you want to call the functions or decorators

  3. In flask app.config configure your database bindings a standard way:

    AUTO_READ_ON_SLAVE = True
    SQLALCHEMY_DATABASE_URI = '%(schema)s://%(user)s:%(password)s@%(master_host)s/%(database)s'
    SQLALCHEMY_BINDS = {
        'master': SQLALCHEMY_DATABASE_URI,
        'slave': '%(schema)s://%(user)s:%(password)s@%(slave_host)s/%(database)s'
    }
  4. Register app extension:

    app = Flask(...)
    ...
    FlaskReplicated(app)

USAGE

Flask replicated routes SQL queries into different databases based on request method. If method is one of READONLY_METHODS which are defined as set([‘GET’, ‘HEAD’]) and config AUTO_READ_ON_SLAVE has not been set

While this is usually enough there are cases when DB access is not controlled explicitly by your business logic. Good examples are implicit creation of sessions on first access, writing some bookkeeping info, implicit registration of a user account somewhere inside the system. These things can happen at arbitrary moments of time, including during GET requests.

To handle these situations wrap appropriate view function with @flask_replicated.use_master_database decorator. It will mark function to always use master database url.

Conversely, wrap the view function with the @flask_replicated.use_slave_database decorator if you want to ensure that it always uses the slave replica.

GET after POST

There is a special case that needs addressing when working with asynchronous replication scheme. Replicas can lag behind a master database on receiving updates. In practice this mean that after submitting a POST form that redirects to a page with updated data this page may be requested from a slave replica that wasn’t updated yet. And the user will have an impression that the submit didn’t work.

Release files for flask-replicated 2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-replicated 2.1
File Size Uploaded
flask_replicated-2.1.tar.gz 3.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask-replicated 2.1
File Interpreter ABI Platform
flask_replicated-2.1-py3-none-any.whl Python 3 none any Details

Total release size: 6.6 kB

Release files / flask_replicated-2.1.tar.gz

Download URL flask_replicated-2.1.tar.gz
Size 3.1 kB
Tags Source
SHA-256 checksum
How to use checksums
52313a8ac28088d076e1cbb0971493ec08ed8e29089a3b03639b86496b3f3d0e
BLAKE2b-256 checksum
How to use checksums
80804d84ae859fafee2706dd344abda17da7822d36967a4f1533e21feda6c86b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.8

Release files / flask_replicated-2.1-py3-none-any.whl

Download URL flask_replicated-2.1-py3-none-any.whl
Size 3.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b393d23e0360ef57e58fb04aeb22273df2c8d756e517883f4419b0d6ce143d97
BLAKE2b-256 checksum
How to use checksums
ae84558814bb1ce39e40f9c004ab26eadd2065ed6b8e1b8e3c0cfe856cc5c619
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.8

Release history Release notifications | RSS feed

This release

2.1 This release

2 release files

2.0

3 release files

1.4

2 release files

1.3

2 release files

1.2

2 release files

1.1

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page