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

Strategy Options

策略 配置 一致性
random 基础读写分离 最终一致 (Eventual Consistency)
transaction_aware 事务内 + 写后读主库 Read Your Writes
consistent_hash 同一 user_id 固定从库 Monotonic Reads / Consistent Prefix

一致性场景 (DDIA)

django-rw-router 一致,支持 DDIA 第四章复制滞后导致的四种读一致性问题:

场景 含义 推荐配置
Read Your Writes 写入后立即读可见 strategy="transaction_aware" + read_after_write_consistency=True + transaction_read_primary=True
Monotonic Reads 同一用户不出现时光倒流 strategy="consistent_hash" + 中间件注入 user_id
Consistent Prefix 有序读取不出现乱序 strategy="consistent_hash"(同 Monotonic Reads)
Eventual Consistency 接受复制滞后 strategy="random"

配置示例

# Read Your Writes(推荐,默认)
configure(
    primary_alias="default",
    replica_aliases=["replica1", "replica2"],
    strategy="transaction_aware",
    read_after_write_consistency=True,
    transaction_read_primary=True,
)

# Monotonic Reads / Consistent Prefix(需中间件注入 user_id)
configure(
    strategy="consistent_hash",
    hash_virtual_nodes=40,
)
app.add_middleware(MongoRouterMiddleware, user_id_attr="user.id")

# Eventual Consistency
configure(strategy="random")

强制主库读

# 方式 1:primary_queryset
user = User.primary_queryset().first()

# 方式 2:force_primary 链式调用
user = User.objects.force_primary().first()

# 方式 3:force_primary 上下文
from mongoengine_rw_router import force_primary
with force_primary():
    user = User.objects.first()

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.2.tar.gz (37.7 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.2-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: mongoengine_rw_router-0.0.2.tar.gz
  • Upload date:
  • Size: 37.7 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.2.tar.gz
Algorithm Hash digest
SHA256 49272161ded05a7413a76a275dc13c133e73ab8f696313aa9a4c87eb5c70f6b5
MD5 9e7b7971ad900399294c208dc677160f
BLAKE2b-256 c4608c0333d23e3984d68faf6676ba60d0f7c98d6930b149cb5d4e4cecaebe80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mongoengine_rw_router-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8bd9473443d1c5dabf85943a835554fa250bab16b250732234ff7ac1b781ffe4
MD5 44bb7eea70e7064612a4ba5c05d6a6e7
BLAKE2b-256 9b20be0bf85f348a51ba72a90926443d052209fc640ff528e3ad78e06133d8e7

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