Intelligent task routing with data locality for distributed systems
Project description
AffinityRouter
Intelligent task routing with data locality for distributed systems.
Overview
AffinityRouter is a Python library for smart task routing in distributed systems. Instead of distributing work agnostically (e.g., Round-Robin), it prioritizes Data Locality using consistent hashing, ensuring tasks for the same resource always reach the same worker — maximizing in-memory cache hits and minimizing database pressure.
Key Features
- Consistent Hashing — Tasks for the same resource always go to the same worker
- Dynamic Cluster Management — Workers register/deregister with heartbeats
- Native Idempotency — Built-in duplicate execution prevention
- Pluggable Architecture — Swap transport, registry, and state backends
- Async-First — Built on
asynciowith automatic sync function wrapping
Installation
pip install --pre affinity-router
Note: The
--preflag is required to install this alpha pre-release.
Quick Start
from affinity_router import Router, Worker
# --- Router side ---
router = Router.from_redis(redis_url="redis://localhost:6379")
await router.submit(
routing_key="ticket:12345",
task_name="process_payment",
payload={"amount": 100, "currency": "EUR"},
)
# --- Worker side ---
worker = Worker.from_redis(redis_url="redis://localhost:6379", worker_id="worker-1")
@worker.task("process_payment")
async def process_payment(task):
return {"status": "paid"}
await worker.start()
Transport Backends
AffinityRouter supports multiple transport backends:
Redis Streams (default)
Best for production deployments with resilience requirements.
from affinity_router import Router, Worker
from affinity_router.transport import RedisStreamTransport
router = Router.from_redis(redis_url="redis://localhost:6379")
Direct TCP
Best for maximum throughput in controlled environments where workers have direct network access.
from affinity_router.transport import DirectTcpTransport
transport = DirectTcpTransport(registry=registry, host="0.0.0.0", port=8000)
Batched TCP
Best for high-throughput scenarios with batching to reduce syscall overhead.
from affinity_router.transport import BatchedTcpTransport
transport = BatchedTcpTransport(
registry=registry,
batch_size=100,
flush_interval_ms=10
)
| Transport | Throughput | Latency | Resilience | Use Case |
|---|---|---|---|---|
| Redis Streams | Medium | Medium | High | Production, distributed |
| Direct TCP | High | Low | Low | Controlled networks |
| Batched TCP | Highest | Medium | Low | High-throughput batch jobs |
⚠️ Disclaimer
This software is provided "AS IS", without warranty of any kind, express or implied. The authors and contributors shall not be held liable for any damages, data loss, service disruptions, or any other issues arising from the use of this software. Users are solely responsible for evaluating the suitability of this software for their intended use case.
By using this software, you acknowledge that you have read and understood the MIT License under which it is distributed.
🚧 Alpha Status
This is an alpha release (
0.1.0a1).This version may contain incomplete implementations and undocumented critical bugs. The API surface is subject to breaking changes without prior notice.
It is strongly recommended to avoid using this package in real development or production environments.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file affinity_router-0.1.0a1.tar.gz.
File metadata
- Download URL: affinity_router-0.1.0a1.tar.gz
- Upload date:
- Size: 989.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc771b6b92cb4ccef1404f5beaaf90496e99206ba8ff4f85873282536d7119c3
|
|
| MD5 |
613ebc9b5125b2a9fd31fd6ff7c6329a
|
|
| BLAKE2b-256 |
671cb86a6b52e287c97cb6db5b247b8ae8d5af039298da170f7f7c2e7ff17cfd
|
Provenance
The following attestation bundles were made for affinity_router-0.1.0a1.tar.gz:
Publisher:
pypi-publish.yml on marcbadiam/affinityRouter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
affinity_router-0.1.0a1.tar.gz -
Subject digest:
bc771b6b92cb4ccef1404f5beaaf90496e99206ba8ff4f85873282536d7119c3 - Sigstore transparency entry: 1065659225
- Sigstore integration time:
-
Permalink:
marcbadiam/affinityRouter@28ff0f93ed869e7bdf6cf82f002e12580d94e9a2 -
Branch / Tag:
refs/tags/0.1.0a1 - Owner: https://github.com/marcbadiam
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@28ff0f93ed869e7bdf6cf82f002e12580d94e9a2 -
Trigger Event:
release
-
Statement type:
File details
Details for the file affinity_router-0.1.0a1-py3-none-any.whl.
File metadata
- Download URL: affinity_router-0.1.0a1-py3-none-any.whl
- Upload date:
- Size: 32.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fe6f2ccb873943b357bffc83c5ea80e2795d418126abf0c855aa31a7adfc12f0
|
|
| MD5 |
c542db1a9936fb379ce3918ea8d1ec4f
|
|
| BLAKE2b-256 |
9d083b5a2ce48aca6061ddd562378c6b58bdd1abbc672031507b7008da801016
|
Provenance
The following attestation bundles were made for affinity_router-0.1.0a1-py3-none-any.whl:
Publisher:
pypi-publish.yml on marcbadiam/affinityRouter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
affinity_router-0.1.0a1-py3-none-any.whl -
Subject digest:
fe6f2ccb873943b357bffc83c5ea80e2795d418126abf0c855aa31a7adfc12f0 - Sigstore transparency entry: 1065659232
- Sigstore integration time:
-
Permalink:
marcbadiam/affinityRouter@28ff0f93ed869e7bdf6cf82f002e12580d94e9a2 -
Branch / Tag:
refs/tags/0.1.0a1 - Owner: https://github.com/marcbadiam
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@28ff0f93ed869e7bdf6cf82f002e12580d94e9a2 -
Trigger Event:
release
-
Statement type: