Skip to main content

MongoDB read-write separation router for mongoengine ODM with transaction-aware and consistent hash routing

Project description

Mongoengine Read-Write Router

A MongoDB read-write separation solution for mongoengine ODM, inspired by the django-rw-router architecture.

This library routes write operations to the primary database and read operations to replica set members, with configurable consistency strategies.

Features

  • Automatic read-write routing - Writes go to primary, reads go to replicas
  • Multiple consistency strategies - random, transaction-aware, consistent hashing
  • Read-after-write consistency - Ensures you see your own writes
  • Request context tracking - Thread-safe context using contextvars
  • Web framework integration - Middleware for FastAPI, Flask, and Django
  • Force primary reads - Explicit control when you need fresh data
  • Write operation tracking - Automatic context updates on writes

Installation

pip install mongoengine-rw-router

Quick Start

from mongoengine import connect, Document, StringField, IntField
from mongoengine_rw_router import (
    RwDocumentMixin,
    configure,
    MongoRouterMiddleware,
)

# 1. Set up connections
connect(db="myapp", alias="default", host="mongodb://primary:27017")
connect(
    db="myapp",
    alias="replica1",
    host="mongodb://replica1:27017",
    read_preference=ReadPreference.SECONDARY_PREFERRED,
)

# 2. Configure router
configure(
    primary_alias="default",
    replica_aliases=["replica1"],
    strategy="transaction_aware",
)

# 3. Define model with routing
class User(RwDocumentMixin, Document):
    name = StringField()
    email = StringField()
    age = IntField()

    meta = {"db_alias": "default"}

# 4. Use it!
user = User.objects.first()  # Reads from replica
user.age = 25
user.save()  # Writes to primary

License

MIT

Credits

Inspired by django-rw-router

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mongoengine_rw_router-0.0.1.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mongoengine_rw_router-0.0.1-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file mongoengine_rw_router-0.0.1.tar.gz.

File metadata

  • Download URL: mongoengine_rw_router-0.0.1.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for mongoengine_rw_router-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0fee6ca106eb36c5a5099f505edf4e984f11cecd9d670d97162e23741376b392
MD5 d9ddb369699c2cefbb95505da23fd432
BLAKE2b-256 a601eda91db18cba791949f1544bb8423eb73a86606e3375072b1d42c5be4768

See more details on using hashes here.

File details

Details for the file mongoengine_rw_router-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for mongoengine_rw_router-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9127dd31c449cc3738946fd693c0720ef56fd69486391a3d0c8f225e12719929
MD5 26a592f5562c09440a043017aadfc246
BLAKE2b-256 ff8f94d4a18d830bb7204cef9cd3a405b2f759ce8ac7836a102e31cbe79acaee

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page