A high-performance, event-driven trading system built to ingest, normalize, and analyze real-time cryptocurrency trade data across multiple exchanges (Binance, Coinbase). It detects cross-exchange price divergences and highlights arbitrage opportunities while mitigating alert fatigue.
- Real-Time Event Streaming: Ingests raw websocket streams into Apache Kafka (KRaft mode) to decouple ingestion from downstream processing.
- CQRS-lite Architecture: Separates the high-volume write path (PostgreSQL Archiver) from the high-speed read path (Redis Cache) to optimize database performance.
- Stateful Anomaly Detection: A dedicated stream processor maintains in-memory rolling windows to detect cross-exchange price divergence (e.g., > 0.05% difference).
- Alert Fatigue Mitigation: Implements a 60-second cooldown window for anomalies to prevent downstream system crashes during sustained divergence events.
- Idempotent Processing: Ensures data integrity using database-level
UNIQUEconstraints (Flyway) and Kafka manual offset commits (AckMode.MANUAL), gracefully handling duplicate deliveries (DataIntegrityViolationException). - Resilient Error Handling: Uses Spring Kafka's
@RetryableTopicand Dead Letter Topics (DLT) for non-blocking retry mechanisms.
- Backend: Java 21, Spring Boot 3.4
- Messaging: Apache Kafka (KRaft), Spring Kafka
- Database: PostgreSQL (System of Record), Spring Data JPA
- Caching: Redis (Upstash/Local) for O(1) read performance
- Migrations: Flyway
- Testing: JUnit 5, Mockito, Awaitility, Embedded Kafka, H2 In-Memory DB
- Infrastructure: Docker, Docker Compose
- Ingestion: Exchange WebSockets push trades ->
trades.rawKafka topic. - Archiving (Write Path):
TradeConsumerreadstrades.rawand persists robust, idempotent records to PostgreSQL. - Stream Processing:
AnomalyDetectionConsumerreadstrades.raw, calculates divergence, and emits events toanomalies.detectedtopic (with a cooldown). - Caching (Read Path):
AnomalyCacheConsumerreadsanomalies.detectedand updates Redis. - REST API: Client requests to
/api/anomaliesfetch instantly from the Redis cache.
Ensure you have Docker installed and running, then spin up the required infrastructure:
docker-compose up -d# On Windows
.\mvnw.cmd spring-boot:run
# On Mac/Linux
./mvnw spring-boot:runThe project includes a robust suite of unit and integration tests (including an @EmbeddedKafka integration test for retry logic).
.\mvnw.cmd clean test- Implement CI/CD pipeline using GitHub Actions
- Containerize the Spring Boot application using a
Dockerfile - Integrate Prometheus and Grafana for system observability and metrics