|
| 1 | +# FairQueue Project |
| 2 | + |
| 3 | +## Project Overview |
| 4 | +**fairque** is a production-ready fair queue implementation using Redis/Valkey with work stealing and priority scheduling. |
| 5 | + |
| 6 | +## Key Features |
| 7 | +- Fair Scheduling: Round-robin user selection with work stealing |
| 8 | +- Priority Queues: Type-safe priority system (1-6) with critical/normal separation |
| 9 | +- Atomic Operations: Lua scripts ensure consistency and performance |
| 10 | +- Configuration-Based: No Redis state for user management, fully configurable |
| 11 | +- Pipeline Optimization: Batch operations for high throughput |
| 12 | +- Comprehensive Monitoring: Built-in statistics and alerting |
| 13 | +- Production Ready: Error handling, graceful shutdown, health checks |
| 14 | +- Cron-Based Scheduling: Task scheduling with croniter for recurring tasks |
| 15 | +## Architecture |
| 16 | +- **Redis/Valkey as persistent storage** (not in-memory cache) |
| 17 | +- **Lua scripts for server-side atomic operations** with integrated statistics |
| 18 | +- **Configuration-based user management** (no all_users Redis key) |
| 19 | +- **Dual implementation**: Synchronous and Asynchronous versions |
| 20 | +- **Type-safe Priority system** using IntEnum |
| 21 | +- **Work stealing strategy** for load balancing |
| 22 | + |
| 23 | +## Technology Stack |
| 24 | +- Python 3.10+ |
| 25 | +- Redis 7.2.5+ / Valkey 7.2.6+ / Amazon MemoryDB |
| 26 | +- Package Manager: uv |
| 27 | +- Type Annotations: Required (Full Typing) |
| 28 | +- Language: English only |
| 29 | + |
| 30 | +## Project Structure |
| 31 | +``` |
| 32 | +fairque/ |
| 33 | +├── fairque/ # Main package |
| 34 | +│ ├── __init__.py |
| 35 | +│ ├── core/ # Core models and types |
| 36 | +│ │ ├── __init__.py |
| 37 | +│ │ ├── models.py # Task, Priority, DLQEntry |
| 38 | +│ │ ├── config.py # Configuration classes |
| 39 | +│ │ └── exceptions.py # Exception classes |
| 40 | +│ ├── queue/ # Queue implementation |
| 41 | +│ │ ├── __init__.py |
| 42 | +│ │ ├── fairqueue.py # Main FairQueue class |
| 43 | +│ │ └── async_fairqueue.py # Async implementation |
| 44 | +│ ├── worker/ # Worker implementation |
| 45 | +│ │ ├── __init__.py |
| 46 | +│ │ ├── worker.py # Sync worker |
| 47 | +│ │ └── async_worker.py # Async worker |
| 48 | +│ ├── scripts/ # Lua scripts |
| 49 | +│ ├── scheduler/ # Task scheduling |
| 50 | +│ │ ├── __init__.py |
| 51 | +│ │ ├── models.py # ScheduledTask model |
| 52 | +│ │ └── scheduler.py # TaskScheduler implementation |
| 53 | +│ ├── scripts/ # Lua scripts |
| 54 | +│ │ ├── common.lua # Shared functions |
| 55 | +│ │ ├── push.lua # Push operation |
| 56 | +│ │ ├── pop.lua # Pop operation |
| 57 | +│ │ └── stats.lua # Statistics operations |
| 58 | +│ └── utils/ # Utilities |
| 59 | +│ ├── __init__.py |
| 60 | +│ ├── stats.py # Statistics formatting |
| 61 | +│ └── monitoring.py # Monitoring and alerting |
| 62 | +├── tests/ # Test suite |
| 63 | +├── examples/ # Usage examples |
| 64 | +├── docs/ # Documentation |
| 65 | +├── pyproject.toml # Project configuration |
| 66 | +└── README.md # Project documentation |
| 67 | +``` |
| 68 | + |
| 69 | +## Implementation Status |
| 70 | +- [x] Project setup and structure |
| 71 | +- [x] Core models implementation (Priority, Task, DLQEntry) |
| 72 | +- [x] Configuration system (RedisConfig, WorkerConfig, QueueConfig, FairQueueConfig) |
| 73 | +- [x] Exception handling system |
| 74 | +- [x] Lua scripts implementation (common.lua, push.lua, pop.lua, stats.lua) |
| 75 | +- [x] FairQueue core implementation (FairQueue class, LuaScriptManager) |
| 76 | +- [x] Worker implementation (TaskHandler, Worker classes with work stealing) |
| 77 | +- [x] Basic testing suite (unit tests and integration tests) |
| 78 | +- [x] **Async implementation (AsyncTaskQueue, AsyncWorker, AsyncTaskHandler)** |
| 79 | +- [x] Task Scheduler implementation (Cron-based scheduling with distributed locking) |
| 80 | +- [x] **Performance testing suite** (throughput, worker, Redis operations, async comparison) |
| 81 | +- [ ] Complete documentation |
| 82 | + |
| 83 | +## Current Phase |
| 84 | +**Phase 7: Performance Testing** - ✅ **PERFORMANCE TESTING COMPLETED** |
| 85 | + |
| 86 | +**Status**: **🎉 COMPREHENSIVE PERFORMANCE TESTING SUITE COMPLETE 🎉** |
| 87 | + |
| 88 | +### Performance Testing Details |
| 89 | +- **Queue Throughput Tests**: Single/batch operations, concurrent pushes, work stealing |
| 90 | +- **Worker Performance Tests**: Single/concurrent workers, processing efficiency |
| 91 | +- **Redis Operations Tests**: Lua script performance, pipeline optimization, memory efficiency |
| 92 | +- **Async vs Sync Comparison**: Throughput, concurrency, resource usage comparisons |
| 93 | +- **Benchmarking Utilities**: Reusable performance metrics and reporting |
| 94 | +- **Run Script**: `python tests/performance/run_benchmarks.py` for easy execution |
| 95 | + |
| 96 | +### Async Implementation Details |
| 97 | +- **AsyncTaskQueue**: Full async version of TaskQueue using `redis.asyncio` |
| 98 | +- **AsyncWorker**: Async worker with `asyncio.Task` based concurrency |
| 99 | +- **AsyncTaskHandler**: Abstract base class for async task processing |
| 100 | +- **AsyncLuaScriptManager**: Async version of Lua script management |
| 101 | +- **Concurrent Operations**: `push_batch_concurrent()` for high-throughput scenarios |
| 102 | +- **Async Context Managers**: Full support for `async with` statements |
| 103 | +- **Health Checks**: Async health monitoring and statistics |
| 104 | +- **Graceful Shutdown**: Proper async task cleanup and resource management |
| 105 | + |
| 106 | +### Dual Architecture Benefits |
| 107 | +- **Synchronous Version**: Thread-based concurrency, familiar patterns |
| 108 | +- **Asynchronous Version**: Event-loop based, higher throughput potential |
| 109 | +- **Shared Core**: Same Lua scripts, models, and configuration system |
| 110 | +- **API Compatibility**: Similar interfaces for easy migration |
| 111 | +- **Performance Options**: Choose based on use case and environment |
| 112 | + |
| 113 | +## Current Phase |
| 114 | +**Phase 5: Testing and Documentation** - ✅ **CORE TESTING COMPLETED** - Comprehensive test suite covering all major functionality. |
| 115 | + |
| 116 | +**Status**: **🎉 PRODUCTION-READY CORE SYSTEM COMPLETE 🎉** |
| 117 | + |
| 118 | +## Next Steps |
| 119 | +1. Implement core models (Priority, Task, Configuration) |
| 120 | +2. Create Lua scripts with common functions |
| 121 | +3. Implement basic FairQueue functionality |
| 122 | +4. Add comprehensive error handling |
| 123 | + |
| 124 | +## Development Guidelines |
| 125 | +- All code and documentation in English |
| 126 | +- Full type annotations required |
| 127 | +- Comprehensive docstrings |
| 128 | +- Follow PEP 8 style guidelines |
| 129 | +- Use uv for package management |
| 130 | +- Incremental implementation with checkpoints |
0 commit comments