|
| 1 | +# Tiny Queue |
| 2 | + |
| 3 | +A lightweight, production-ready job queue system with worker threads, automatic retries, dead letter queue (DLQ), and real-time monitoring - all in exactly 100 lines of Python! |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +Tiny Queue demonstrates how to build a resilient background job processing system with minimal code. It includes: |
| 8 | + |
| 9 | +- **Multi-threaded Workers**: Concurrent job processing with configurable worker count |
| 10 | +- **Automatic Retries**: Exponential backoff retry mechanism for failed jobs |
| 11 | +- **Dead Letter Queue (DLQ)**: Failed jobs after max retries are moved to DLQ for inspection |
| 12 | +- **Real-time Monitoring**: Live dashboard showing queue size, completed jobs, and failures |
| 13 | +- **Performance Metrics**: Visual latency distribution and success rate statistics |
| 14 | + |
| 15 | +Perfect for understanding concurrency, job queues, and error handling patterns in Python! |
| 16 | + |
| 17 | +## Features |
| 18 | + |
| 19 | +✨ **Key Highlights**: |
| 20 | +- 4 worker threads processing jobs concurrently |
| 21 | +- Automatic retry with exponential backoff (2^retries seconds) |
| 22 | +- Dead letter queue for permanently failed jobs |
| 23 | +- Real-time metrics tracking (success/failure rates) |
| 24 | +- Latency distribution analysis |
| 25 | +- Visual ASCII-based result charts |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +No external dependencies required! Uses only Python standard library: |
| 30 | + |
| 31 | +```bash |
| 32 | +# Clone the repository |
| 33 | +git clone https://github.com/josharsh/100LinesOfCode.git |
| 34 | +cd 100LinesOfCode/Tiny-Queue |
| 35 | + |
| 36 | +# Run the project (Python 3.6+) |
| 37 | +python main.py |
| 38 | +``` |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +### Basic Execution |
| 43 | + |
| 44 | +Simply run the script: |
| 45 | + |
| 46 | +```bash |
| 47 | +python main.py |
| 48 | +``` |
| 49 | + |
| 50 | +### Example Output |
| 51 | + |
| 52 | +``` |
| 53 | +[MONITOR] queue=987 done=12 dlq=0 |
| 54 | +[MONITOR] queue=854 done=134 dlq=2 |
| 55 | +[MONITOR] queue=721 done=256 dlq=5 |
| 56 | +... |
| 57 | +
|
| 58 | +=== RESULTS === |
| 59 | +Total jobs: 1000 |
| 60 | +Success : 856 |
| 61 | +DLQ : 144 |
| 62 | +
|
| 63 | +Success vs DLQ |
| 64 | +SUCCESS | ########################################## |
| 65 | +DLQ | ####### |
| 66 | +
|
| 67 | +Latency distribution |
| 68 | +<0.5s | ############################ |
| 69 | +0.5-2s | ########## |
| 70 | +>2s | #### |
| 71 | +``` |
| 72 | + |
| 73 | +### Customization |
| 74 | + |
| 75 | +Modify the code to fit your needs: |
| 76 | + |
| 77 | +```python |
| 78 | +# Change number of workers (line 68) |
| 79 | +for _ in range(8): # 8 workers instead of 4 |
| 80 | + threading.Thread(target=worker, daemon=True).start() |
| 81 | + |
| 82 | +# Adjust job volume (line 73) |
| 83 | +for _ in range(5000): # Process 5000 jobs |
| 84 | + submit(random.choice(JOB_TYPES)) |
| 85 | + |
| 86 | +# Add custom job types (line 15-22) |
| 87 | +def handle(job): |
| 88 | + if job["type"] == "your_custom_job": |
| 89 | + # Your job logic here |
| 90 | + time.sleep(0.1) |
| 91 | +``` |
| 92 | + |
| 93 | +## How It Works |
| 94 | + |
| 95 | +1. **Job Submission**: Jobs are added to the queue with unique IDs and trace identifiers |
| 96 | +2. **Worker Processing**: Multiple workers pull jobs from the queue and execute them |
| 97 | +3. **Error Handling**: Failed jobs are retried with exponential backoff (up to 3 retries) |
| 98 | +4. **DLQ Transfer**: Jobs exceeding retry limit are moved to the Dead Letter Queue |
| 99 | +5. **Monitoring**: Background thread displays real-time queue statistics every second |
| 100 | +6. **Results**: Final visualization shows success rates and latency distribution |
| 101 | + |
| 102 | +## Job Types |
| 103 | + |
| 104 | +The demo includes three job types: |
| 105 | + |
| 106 | +- **send_email**: Fast job (50ms) - always succeeds |
| 107 | +- **resize_image**: Medium job (150ms) - always succeeds |
| 108 | +- **charge_card**: Fast job with 40% failure rate - demonstrates retry logic |
| 109 | + |
| 110 | +## Technologies |
| 111 | + |
| 112 | +- **Language**: Python 3.6+ |
| 113 | +- **Core Libraries**: |
| 114 | + - `queue` - Thread-safe queue implementation |
| 115 | + - `threading` - Concurrent worker threads |
| 116 | + - `time` - Timing and delays |
| 117 | + - `uuid` - Unique job identifiers |
| 118 | + - `random` - Job type selection and failure simulation |
| 119 | + |
| 120 | +## Use Cases |
| 121 | + |
| 122 | +This pattern is useful for: |
| 123 | + |
| 124 | +- Background job processing (emails, notifications) |
| 125 | +- Image/video processing pipelines |
| 126 | +- Payment processing with retries |
| 127 | +- Data ingestion and ETL tasks |
| 128 | +- Webhook delivery systems |
| 129 | +- Batch processing workflows |
| 130 | + |
| 131 | +## Learning Outcomes |
| 132 | + |
| 133 | +By studying this code, you'll learn: |
| 134 | + |
| 135 | +- Thread-safe queue operations in Python |
| 136 | +- Worker thread pool patterns |
| 137 | +- Exponential backoff retry strategies |
| 138 | +- Dead letter queue implementation |
| 139 | +- Real-time monitoring and metrics collection |
| 140 | +- Concurrent programming best practices |
| 141 | + |
| 142 | +## Code Structure |
| 143 | + |
| 144 | +``` |
| 145 | +Tiny-Queue/ |
| 146 | +├── main.py # Complete queue system (100 lines) |
| 147 | +└── README.md # This file |
| 148 | +``` |
| 149 | + |
| 150 | +## Author |
| 151 | + |
| 152 | +Contributed to [100 Lines of Code](https://github.com/josharsh/100LinesOfCode) |
| 153 | + |
| 154 | +## License |
| 155 | + |
| 156 | +This project is part of the 100 Lines of Code repository, licensed under the [GNU General Public License v3.0](../LICENSE). |
| 157 | + |
| 158 | +--- |
| 159 | + |
| 160 | +⭐ If you find this helpful, please star the [100 Lines of Code repository](https://github.com/josharsh/100LinesOfCode)! |
0 commit comments