A real-time market data service that fetches, processes, and stores stock market data using FastAPI, PostgreSQL, and Kafka.
- Real-time stock price data fetching
- Multiple data provider support (currently Alpha Vantage)
- Automated price polling at configurable intervals
- Data persistence in PostgreSQL
- Message queuing with Kafka
- RESTful API with Swagger documentation
- Database management with pgAdmin
- Docker containerization
- Docker and Docker Compose
- Python 3.11+
- Alpha Vantage API key
- Clone the repository:
git clone <repository-url>
cd market_data_service- Create a
.envfile in the project root:
DATABASE_URL=postgresql://postgres:postgres@db:5432/market_data
KAFKA_BOOTSTRAP_SERVERS=kafka:9092
ALPHA_VANTAGE_API_KEY=your_api_key_here
APP_ENV=development
DEBUG=true
LOG_LEVEL=INFO- Start the services:
docker-compose up --build -d- Access the services:
- API Documentation: http://localhost:8000/docs
- pgAdmin: http://localhost:5050 (admin@admin.com/admin)
GET /prices/latest?symbol=AAPLParameters:
symbol: Stock symbol (e.g., AAPL, MSFT, GOOGL)provider: Data provider (default: alpha_vantage)
POST /prices/poll?symbols=["AAPL","MSFT"]&interval=60Parameters:
symbols: List of stock symbolsinterval: Polling interval in seconds
- Access pgAdmin at http://localhost:5050
- Login with:
- Email: admin@admin.com
- Password: admin
- Add a new server:
- Name: Market Data
- Host: db
- Port: 5432
- Database: market_data
- Username: postgres
- Password: postgres
- FastAPI: REST API framework
- PostgreSQL: Database for data persistence
- Kafka: Message queue for data processing
- pgAdmin: Database management interface
- Docker: Containerization
- API receives request for stock data
- Service fetches data from Alpha Vantage
- Raw data is stored in PostgreSQL
- Processed data is published to Kafka
- Consumers process and store final data
market_data_service/
├── alembic/ # Database migrations
│ └── migrations/ # Migration files
├── app/
│ ├── db/ # Database models and connection
│ ├── routers/ # API endpoints
│ ├── schemas/ # Pydantic models
│ ├── services/ # Business logic
│ └── main.py # Application entry point
├── tests/ # Test suite
├── .env # Environment variables
├── docker-compose.yml # Docker services
└── Dockerfile # Application container
docker-compose run app pytest# Create a new migration
docker-compose run app alembic revision --autogenerate -m "description"
# Apply migrations
docker-compose run app alembic upgrade head-
Database Connection Issues
- Check if PostgreSQL container is running
- Verify database credentials in .env
- Check database logs:
docker-compose logs db
-
API Not Responding
- Check if FastAPI container is running
- Verify API logs:
docker-compose logs app - Check if Alpha Vantage API key is valid
-
Kafka Connection Issues
- Check if Kafka and Zookeeper are running
- Verify Kafka logs:
docker-compose logs kafka
# View all logs
docker-compose logs -f
# View specific service logs
docker-compose logs -f app
docker-compose logs -f db
docker-compose logs -f kafka- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.