| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
High-performance streaming pipeline using Python, Redis Streams, and Postgres. No Kafka needed.
Everyone thinks you need Kafka for streaming. Not really.
Python Can Handle Streaming — Here's How:
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
│ Producers │─────▶│ Redis Stream │─────▶│ Consumers │
│ (async) │ │ (buffer) │ │ (5 workers)│
└─────────────┘ └──────────────┘ └──────┬──────┘
│
│ Batch Insert
│ (500 events or 2s)
▼
┌─────────────┐
│ Postgres │
│ (JSONB cols)│
└─────────────┘
Key Components:
It runs 10K events/sec on one box.
If it breaks, you've earned enough to hire a team for Kafka.
Install uv:
curl -LsSf https://astral.sh/uv/install.sh | shcd python-redis-streaming
cp .env.example .envThe run.sh script handles everything:
# Start Redis and Postgres
./run.sh
# Start the streaming engine
./run.sh startIn another terminal:
# Produce 1000 sample events
./run.sh produce 1000
# Or produce 5000 events
./run.sh produce 5000In another terminal:
# Monitor with 5 second refresh
./run.sh monitor
# Or with 10 second refresh
./run.sh monitor 10You'll see:
============================================================
STREAMING PIPELINE DASHBOARD
============================================================
Redis Streams:
Stream Length: 1523
Pending Messages: 0
DLQ Length: 0
Postgres:
Total Events: 8477
Events (last minute): 1000
DLQ Count: 0
Table Size: 1256 kB
Events by Type:
order_placed: 1243
user_created: 1189
payment_processed: 1156
product_viewed: 1098
order_shipped: 1067
============================================================
# Make sure services are running first
./run.sh
# Run tests
./run.sh testTest the throughput:
# Run at 10K events/sec for 60 seconds
./run.sh benchmark 10000 60
# Run at 5K events/sec for 30 seconds
./run.sh benchmark 5000 30Expected output:
============================================================ STREAMING BENCHMARK ============================================================ Target Rate: 10000 events/sec Duration: 60 seconds Expected Total: 600000 events ============================================================ Produced 1000 events... Produced 2000 events... ... Produced 600000 events... Completed: 600000 total events produced ============================================================ BENCHMARK RESULTS ============================================================ Total Events: 600000 Elapsed Time: 60.02 seconds Actual Rate: 9996.67 events/sec Target Rate: 10000 events/sec Accuracy: 99.97% ============================================================
python-redis-streaming/ ├── src/ │ ├── config.py # Configuration management │ ├── producer.py # Event producer │ ├── consumer.py # Event consumer with batching │ ├── monitor.py # Observability utilities │ └── main.py # Main application ├── tests/ │ ├── test_producer.py │ └── test_consumer.py ├── scripts/ │ ├── produce_sample.py # Produce sample events │ ├── monitor.py # Standalone monitoring │ └── benchmark.py # Benchmark script ├── docker-compose.yml ├── Dockerfile ├── init.sql # Postgres schema ├── pyproject.toml ├── run.sh # Main entry script └── README.md
Edit .env to customize:
# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
# Postgres
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_DB=streaming
POSTGRES_USER=streaming_user
POSTGRES_PASSWORD=streaming_pass
# Streaming config
NUM_WORKERS=5 # Consumer workers
BATCH_SIZE=500 # Events per batch
BATCH_TIMEOUT_SECONDS=2 # Max wait time for batch
XREAD_COUNT=100 # Events to read per XREADGROUP
XREAD_BLOCK_MS=5000 # Block time for XREADGROUP# Produce single event
await producer.produce('user_created', {
'user_id': 123,
'email': 'user@example.com'
})
# Produce batch
events = [
('order_placed', {'order_id': 'ORD-123', 'amount': 99.99}),
('payment_processed', {'txn_id': 'TXN-456'})
]
await producer.produce_batch(events)Consumers automatically:
# Get stream length
stream_len = await monitor.get_stream_length()
# Get pending messages
pending = await monitor.get_pending_messages()
# Get Postgres stats
stats = await monitor.get_postgres_stats()# Start everything with Docker Compose
docker-compose up -d
# View logs
docker-compose logs -f streaming-app
# Stop everything
docker-compose down# Install dependencies
uv sync
# Start Redis and Postgres only
docker-compose up -d redis postgres
# Run locally
uv run python -m src.main
# Run tests
uv run pytest tests/ -v# Check Redis stream length
docker-compose exec redis redis-cli XLEN events
# Check Postgres data
docker-compose exec postgres psql -U streaming_user -d streaming -c "SELECT COUNT(*) FROM events;"
# View consumer groups
docker-compose exec redis redis-cli XINFO GROUPS events
# Clear all data
./run.sh cleanConsumers can't keep up with producers. Solutions:
Check Postgres performance:
SELECT * FROM pg_stat_statements
WHERE query LIKE '%INSERT INTO events%';Check dead letter queue:
docker-compose exec postgres psql -U streaming_user -d streaming -c "SELECT * FROM dead_letter_queue ORDER BY failed_at DESC LIMIT 10;"| Back | FazBrowse Home | New Git URL |