Skip to content

cliff-de-tech/EventFlow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

EventFlow πŸš€

Python 3.11+ FastAPI Redis Streams License: MIT

EventFlow is a production-grade event-driven backend system built with Python, providing real-time visibility into user actions and system events. It demonstrates modern event streaming architecture with support for high-throughput message processing, reliable delivery, and comprehensive observability.


πŸ—οΈ Architecture Overview

                                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                    β”‚                      EventFlow System                        β”‚
                                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                                              β”‚
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚                                         β”‚                                         β”‚
                    β–Ό                                         β–Ό                                         β–Ό
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚   Event Producers   β”‚                   β”‚   Message Broker    β”‚                   β”‚   Event Consumers   β”‚
         β”‚   ────────────────  β”‚                   β”‚   ──────────────    β”‚                   β”‚   ────────────────  β”‚
         β”‚ β€’ REST API          β”‚                   β”‚ β€’ Redis Streams     β”‚                   β”‚ β€’ Event Workers     β”‚
         β”‚ β€’ Batch Processing  β”‚      ─────────▢   β”‚ β€’ Consumer Groups   β”‚   ─────────▢     β”‚ β€’ Retry Handler     β”‚
         β”‚ β€’ Event Factory     β”‚                   β”‚ β€’ Message Ordering  β”‚                   β”‚ β€’ DLQ Handler       β”‚
         β”‚ β€’ CloudEvents Spec  β”‚                   β”‚ β€’ Persistence       β”‚                   β”‚ β€’ Webhook Delivery  β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                    β”‚                                         β”‚                                         β”‚
                    β”‚                                         β”‚                                         β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                       β”‚                                         β”‚
                                       β–Ό                                         β–Ό
                            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                            β”‚  Persistence Layer  β”‚                   β”‚    Observability    β”‚
                            β”‚  ─────────────────  β”‚                   β”‚    ────────────     β”‚
                            β”‚ β€’ PostgreSQL        β”‚                   β”‚ β€’ Structured Logs   β”‚
                            β”‚ β€’ Event Store       β”‚                   β”‚ β€’ Prometheus Metricsβ”‚
                            β”‚ β€’ Query Patterns    β”‚                   β”‚ β€’ Health Checks     β”‚
                            β”‚ β€’ Data Retention    β”‚                   β”‚ β€’ Grafana Dashboardsβ”‚
                            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

✨ Key Features

Event Processing

  • CloudEvents-compliant schema design for interoperability
  • Multiple event types: User, System, Business, Analytics events
  • Event batching with automatic flushing for optimized throughput
  • Producer pool for high-concurrency scenarios

Message Broker

  • Redis Streams for durable, ordered message delivery
  • Consumer groups for horizontal scaling
  • Message acknowledgment with automatic retry
  • Dead Letter Queue (DLQ) for failed message handling

Reliability

  • Exponential backoff retry mechanism with jitter
  • Circuit breaker pattern for fault tolerance
  • Graceful shutdown with in-flight message completion
  • Idempotent processing via unique event IDs

Observability

  • Structured logging with JSON output (structlog)
  • Prometheus metrics for real-time monitoring
  • Health check endpoints for container orchestration
  • Request tracing with correlation IDs

API

  • RESTful endpoints built with FastAPI
  • Auto-generated OpenAPI documentation
  • Event replay capabilities
  • Subscription management for webhooks

πŸ“ Project Structure

EventFlow/
β”œβ”€β”€ src/eventflow/
β”‚   β”œβ”€β”€ __init__.py              # Package initialization
β”‚   β”œβ”€β”€ app.py                   # FastAPI application entry
β”‚   β”œβ”€β”€ cli.py                   # Command-line interface
β”‚   β”œβ”€β”€ config.py                # Configuration management
β”‚   β”‚
β”‚   β”œβ”€β”€ api/                     # REST API layer
β”‚   β”‚   β”œβ”€β”€ dependencies.py      # FastAPI dependencies
β”‚   β”‚   β”œβ”€β”€ schemas.py           # Request/response models
β”‚   β”‚   └── routes/
β”‚   β”‚       β”œβ”€β”€ events.py        # Event CRUD endpoints
β”‚   β”‚       β”œβ”€β”€ health.py        # Health check endpoints
β”‚   β”‚       β”œβ”€β”€ dlq.py           # Dead letter queue management
β”‚   β”‚       β”œβ”€β”€ subscriptions.py # Webhook subscriptions
β”‚   β”‚       └── metrics.py       # Prometheus metrics endpoint
β”‚   β”‚
β”‚   β”œβ”€β”€ core/                    # Core business logic
β”‚   β”‚   β”œβ”€β”€ broker.py            # Redis Streams broker
β”‚   β”‚   └── producer.py          # Event producer with batching
β”‚   β”‚
β”‚   β”œβ”€β”€ consumers/               # Event processing
β”‚   β”‚   β”œβ”€β”€ consumer.py          # Event consumer with retry
β”‚   β”‚   └── worker.py            # Background workers
β”‚   β”‚
β”‚   β”œβ”€β”€ models/                  # Database models
β”‚   β”‚   └── database.py          # SQLAlchemy models
β”‚   β”‚
β”‚   β”œβ”€β”€ persistence/             # Data access layer
β”‚   β”‚   β”œβ”€β”€ database.py          # Database connection
β”‚   β”‚   └── repository.py        # Repository pattern
β”‚   β”‚
β”‚   β”œβ”€β”€ schemas/                 # Domain schemas
β”‚   β”‚   └── events.py            # CloudEvents schemas
β”‚   β”‚
β”‚   └── observability/           # Monitoring & logging
β”‚       β”œβ”€β”€ logging.py           # Structured logging
β”‚       β”œβ”€β”€ metrics.py           # Prometheus metrics
β”‚       └── retry.py             # Retry & circuit breaker
β”‚
β”œβ”€β”€ tests/                       # Test suite
β”‚   β”œβ”€β”€ conftest.py              # Pytest fixtures
β”‚   β”œβ”€β”€ test_api.py              # API integration tests
β”‚   β”œβ”€β”€ test_schemas.py          # Schema validation tests
β”‚   └── test_retry.py            # Retry mechanism tests
β”‚
β”œβ”€β”€ scripts/
β”‚   └── generate_demo_data.py    # Demo data generator
β”‚
β”œβ”€β”€ docker/                      # Docker configuration
β”‚   β”œβ”€β”€ prometheus.yml           # Prometheus config
β”‚   └── grafana/                 # Grafana provisioning
β”‚
β”œβ”€β”€ Dockerfile                   # API server image
β”œβ”€β”€ Dockerfile.worker            # Worker image
β”œβ”€β”€ docker-compose.yml           # Full stack orchestration
β”œβ”€β”€ pyproject.toml               # Project configuration
β”œβ”€β”€ .env.example                 # Environment template
└── README.md                    # This file

πŸš€ Quick Start

Prerequisites

  • Python 3.11+
  • Docker & Docker Compose
  • Redis 7.0+
  • PostgreSQL 15+

1. Clone and Setup

git clone https://github.com/yourusername/eventflow.git
cd eventflow

# Create environment file
cp .env.example .env

2. Start with Docker Compose

# Start all services (API, Worker, PostgreSQL, Redis, Monitoring)
docker-compose up -d

# View logs
docker-compose logs -f eventflow-api

# Check health
curl http://localhost:8000/health

3. Access Services

Service URL Credentials
EventFlow API http://localhost:8000 -
API Documentation http://localhost:8000/docs -
Prometheus http://localhost:9090 -
Grafana http://localhost:3000 admin/admin
pgAdmin http://localhost:5050 [email protected]/admin
Redis Commander http://localhost:8081 -

4. Send Your First Event

# Create a user event
curl -X POST http://localhost:8000/api/v1/events \
  -H "Content-Type: application/json" \
  -d '{
    "type": "user.created",
    "source": "api",
    "data": {
      "user_id": "usr_12345",
      "email": "[email protected]",
      "username": "johndoe"
    }
  }'

πŸ’» Local Development

Setup Virtual Environment

# Create and activate virtual environment
python -m venv .venv
.venv\Scripts\activate  # Windows
source .venv/bin/activate  # Linux/Mac

# Install dependencies
pip install -e ".[dev]"

Run Services Locally

# Start dependencies
docker-compose up -d postgres redis

# Run database migrations
python -m eventflow.cli db upgrade

# Start API server
uvicorn eventflow.app:create_app --factory --reload

# In another terminal - Start worker
python -m eventflow.cli worker start

Generate Demo Data

# Generate 500 realistic events
python scripts/generate_demo_data.py

πŸ“– API Reference

Events API

Create Event

POST /api/v1/events
Content-Type: application/json

{
  "type": "user.login",
  "source": "web-app",
  "data": {
    "user_id": "usr_123",
    "ip_address": "192.168.1.1"
  },
  "metadata": {
    "priority": "high",
    "partition_key": "usr_123"
  }
}

Query Events

GET /api/v1/events?event_type=user.login&limit=50&offset=0

Get Event by ID

GET /api/v1/events/{event_id}

Replay Events

POST /api/v1/events/replay
Content-Type: application/json

{
  "stream": "events:user",
  "start_time": "2024-01-01T00:00:00Z",
  "end_time": "2024-01-02T00:00:00Z"
}

Dead Letter Queue API

List DLQ Messages

GET /api/v1/dlq?limit=100

Retry Failed Message

POST /api/v1/dlq/{message_id}/retry

Delete DLQ Message

DELETE /api/v1/dlq/{message_id}

Subscriptions API

Create Subscription

POST /api/v1/subscriptions
Content-Type: application/json

{
  "name": "order-notifications",
  "event_types": ["order.created", "order.completed"],
  "webhook_url": "https://your-service.com/webhook",
  "secret": "your-webhook-secret"
}

Health Check

GET /health           # Basic health
GET /health/ready     # Readiness probe
GET /health/live      # Liveness probe

Metrics

GET /metrics          # Prometheus metrics

πŸ“Š Event Types

User Events

Event Type Description
user.created New user registration
user.updated User profile update
user.deleted User account deletion
user.login User login
user.logout User logout

System Events

Event Type Description
system.health_check Service health report
system.error System error occurred
system.config_changed Configuration update

Business Events

Event Type Description
order.created New order placed
order.updated Order status change
payment.completed Payment processed
notification.sent Notification delivered

Analytics Events

Event Type Description
page.view Page view tracked
feature.used Feature usage logged
conversion.tracked Conversion recorded

βš™οΈ Configuration

Environment Variables

Variable Default Description
APP_NAME eventflow Application name
APP_ENV development Environment (development/staging/production)
DEBUG false Enable debug mode
API_HOST 0.0.0.0 API server host
API_PORT 8000 API server port
DATABASE_URL postgresql+asyncpg://... Database connection URL
REDIS_URL redis://localhost:6379 Redis connection URL
LOG_LEVEL INFO Logging level
LOG_FORMAT json Log format (json/console)

Event Processing

Variable Default Description
BATCH_SIZE 100 Events per batch
BATCH_TIMEOUT_MS 1000 Batch flush timeout
CONSUMER_GROUP eventflow-workers Redis consumer group
MAX_RETRIES 3 Max retry attempts
RETRY_BASE_DELAY 1.0 Base retry delay (seconds)

πŸ§ͺ Testing

Run Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=eventflow --cov-report=html

# Run specific test file
pytest tests/test_api.py -v

# Run tests matching pattern
pytest -k "test_event" -v

Test Categories

  • Unit Tests: Schema validation, utility functions
  • Integration Tests: API endpoints, database operations
  • Reliability Tests: Retry mechanisms, circuit breaker

🐳 Docker Deployment

Production Deployment

# Build images
docker-compose build

# Start in production mode
docker-compose -f docker-compose.yml up -d

# Scale workers
docker-compose up -d --scale eventflow-worker=3

Health Checks

The containers include health checks for orchestration:

healthcheck:
  test: ["CMD", "curl", "-f", "http://localhost:8000/health"]
  interval: 30s
  timeout: 10s
  retries: 3

Resource Management

# View resource usage
docker stats

# View logs
docker-compose logs -f --tail=100

# Restart services
docker-compose restart eventflow-api eventflow-worker

πŸ“ˆ Monitoring

Prometheus Metrics

Key metrics exported:

Metric Type Description
eventflow_events_published_total Counter Total events published
eventflow_events_consumed_total Counter Total events consumed
eventflow_events_failed_total Counter Failed event processing
eventflow_event_processing_seconds Histogram Processing latency
eventflow_dlq_size Gauge Dead letter queue size
eventflow_circuit_breaker_state Gauge Circuit breaker status

Grafana Dashboards

Pre-configured dashboards for:

  • Event throughput and latency
  • Error rates and DLQ growth
  • Consumer lag and processing time
  • System health metrics

πŸ”§ Reliability Patterns

Retry with Exponential Backoff

from eventflow.observability import retry_async, RetryConfig

config = RetryConfig(
    max_retries=3,
    base_delay=1.0,
    max_delay=60.0,
    exponential_base=2.0,
    jitter=True,
)

@retry_async(config)
async def process_event(event):
    # Processing logic
    pass

Circuit Breaker

from eventflow.observability import CircuitBreaker

breaker = CircuitBreaker(
    failure_threshold=5,
    recovery_timeout=30.0,
    half_open_max_calls=3,
)

async with breaker:
    await call_external_service()

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines

  • Follow PEP 8 style guide
  • Write tests for new features
  • Update documentation as needed
  • Use conventional commit messages

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ™ Acknowledgments


Built with ❀️ for scalable event-driven systems

About

Event-Driven Analytics & Observability Pipeline

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published