Thegretia
Back to All Projects
Software EngineeringRole: Software (Python) Engineer•4 Months • 2024

Distributed Python Async Task Engine

Resilient worker pool architecture with priority queueing and telemetry

A lightweight, fault-tolerant distributed background job orchestrator written in modern Python with Redis persistence, intelligent retry exponential backoff, dead-letter queuing, and OpenTelemetry distributed tracing.

Python 3.12AsyncioRedisFastAPIOpenTelemetryDockerPytest

Performance Metrics & Quantified Impact

Task Throughput25,000+ / minProcessed concurrent jobs across lightweight async worker nodes
Memory Footprint< 45 MBBase memory usage per worker process compared to Celery (>120MB)
Retry Reliability99.999%Zero lost tasks during unexpected worker node terminations
Phase 01

The Challenge & Core Problem

Off-the-shelf distributed queue frameworks (such as heavy Celery configurations) introduced significant memory bloat, fragile state recovery, and high debugging complexity for microservice workloads.

Core Architectural Bottleneck:The application required an ultra-lean, asyncio-native Python task engine with fine-grained priority queues, graceful cancellation, and transparent distributed tracing.

Key Engineering Objectives

  • Design an asyncio-first worker execution model with priority levels.
  • Implement reliable message acknowledgments using Redis streams and visibility timeouts.
  • Support automated retry policies with jittered exponential backoffs.
  • Provide an intuitive developer decorator API (`@task(priority='high', retries=3)`).
Phase 02

System Architecture & Pipeline Design

Clients dispatch tasks through a type-safe Python SDK or REST API into Redis priority streams. Async worker nodes pull available jobs, acquire distributed locks, execute non-blocking coroutines, and publish telemetry spans.

DATA FLOW TOPOLOGY

FastAPI Producer → Redis Streams (Priority Buckets) → Worker Fleet (Asyncio Event Loops) → Heartbeat & Health Monitor → Dead Letter Queue / Success Result Store → Jaeger Tracing UI

01

Task Serialization & Dispatch

Created a zero-copy Msgpack serializer and decorator interface for registering asynchronous task handlers with runtime type-checking via Pydantic V2.

Python 3.12Pydantic V2Msgpack
02

Redis Consumer Group Architecture

Implemented consumer groups with `XREADGROUP` and `XACK` guarantees, along with a claim loop (`XAUTOCLAIM`) to reassign orphaned tasks if a worker crashes.

Redis 7.2Redis-pyAsyncio
03

Distributed Tracing & Metrics

Integrated OpenTelemetry hooks to inject and extract trace context across asynchronous process boundaries, visualizing latency bottlenecks in Jaeger and Grafana.

OpenTelemetryPrometheusJaeger
Phase 03

Technical Obstacles & Engineering Solutions

Obstacle #1

Worker starvation when long-running blocking CPU-bound tasks froze the asynchronous event loop.

Engineered Solution

Dynamically offloaded synchronous or CPU-intensive execution blocks to a bounded `ProcessPoolExecutor` while keeping I/O network operations on the native asyncio loop.

Phase 04

Impact & Key Takeaways

Engineered an enterprise-grade async queueing engine that provides extreme performance, sub-second error recovery, and clear developer ergonomics.

60% reduction in worker memory overhead compared to legacy frameworks.
Adopted across 12 microservices with 0 critical production regressions.
Achieved 100% test coverage with comprehensive pytest-asyncio integration suites.

Interested in similar architectures for your team?

Let's discuss custom Data Pipelines, AI deployment, or high-throughput Python backends.

Start a Conversation