StockTrader is an educational and entertainment platform that allows users to simulate stock trading with virtual money. The application helps users learn the basics of stock trading, portfolio management, and analytics without financial risk.
-
Backend: Java with Spring Boot
-
Database: PostgreSQL (relational DB)
-
Authentication: JWT via Spring Security, passwords encrypted with BCrypt
-
Logging: SLF4J + Logback (centralized, standard format, sensitive data protection)
-
Caching: Caffeine
-
Containerization: Docker (separate containers for app and DB)
-
Messaging: Apache Kafka (for events between microservices, event-driven architecture)
-
Web Services: REST
-
Design Patterns:
- Strategy (Portfolio-service)
- Factory (Stock-service)
- State Machine (Transaction-service)
- Iterator (Transaction-service)
- Chain of Responsibility (Transaction-service)
- Facade (Stock-service)
Microservices-based architecture with Kafka integration
The application is built using a microservices architecture, where each service is independently deployable and encapsulates its own business logic. Services communicate asynchronously via Kafka, and synchronously via REST APIs, ensuring loose coupling and high scalability.
-
Kafka provides independent processing of events (e.g., transactions and notifications).
-
The Stock Service manages stock data. It provides CRUD operations for stocks, updates prices automatically and through external APIs, and publishes price update events to Kafka. Other services use it to fetch up-to-date stock information.
-
The Transaction Service is responsible for handling buy and sell operations. It validates requests, interacts with the User Service to update balances, and with the Stock Service to fetch stock data. It also publishes transaction events to Kafka for further processing.
-
The Portfolio Service manages users’ holdings. It updates positions after transactions, reacts to stock price updates, and evaluates portfolio value using different strategies (conservative, aggressive, balanced).
-
The User Service handles authentication and user management. It issues JWT tokens for secure access and maintains user account balances, which are used by other services during transactions.
-
User Registration and Authentication
- Register using username, email, and password (encrypted with BCrypt)
- Authentication via JWT with token expiration and refresh support
- Initial balance: $10,000 in virtual funds
-
Stock Operations
- View a list of available stocks with current prices (updated every 60 seconds)
- Buy and sell stocks with a 0.1% commission (balance and portfolio validation)
- Transactions are processed and broadcasted via Kafka events
-
Portfolio Management
- View portfolio details: name, quantity, average price, current value
- View transaction history (buy/sell operations)
-
Administrative Functions
- Add or remove stocks from the list (admin only)
-
Security
- Password encryption (BCrypt)
- JWT-based authentication and role-based access control
- Sensitive data is excluded from logs (e.g., passwords)
-
Performance
- Stock prices update at least every 60 seconds
-
Reliability
- All user and transaction data is stored in the database
- Error handling and retry logic for Kafka events
-
Scalability
- Supports up to 1,000 concurrent users
- Possibility of horizontal service scaling
-
Logging
- Centralized logging (SLF4J/Logback, standard format: timestamp, service name, log level, traceId)
-
Deployability
- Docker Compose setup, separate containers for app, database, Kafka
-
Maintainability
- Modular structure for ease of maintenance and feature extension
git clone <your-repository>Set up database, Kafka connection settings in .env or via command-line/config files.
docker-compose up --buildThe following services will be started via docker-compose:
- Spring Boot application
- PostgreSQL
- Kafka
The application will be available at http://localhost:<8080>
docker-compose downor
CTRL+C
in the terminal where the application is running.
When the application starts for the first time, all necessary database schemas for each microservice are created automatically:
- user-service
- portfolio-service
- stock-service
- transaction-service
When starting the user-service, the application checks if an ADMIN user exists in the database. If no administrator is found, the application automatically creates a default admin account:
- Username:
admin - Email:
admin@stocktrader.local - Password:
admin123
You can change these credentials by setting the following environment variables:
| Variable | Default Value |
|---|---|
| ADMIN_USERNAME | admin |
| ADMIN_EMAIL | admin@stocktrader.local |
| ADMIN_PASSWORD | admin123 |
After deployment, you can log in as the administrator using the following endpoint:
POST /api/auth/loginRequest body:
{
"username": "admin",
"password": "admin123"
}- Logging: All important actions in both stock and portfolio services (including data changes and queries) are logged in a standardized format at the controller and service levels.
- Data protection: Sensitive data (such as passwords) is excluded from logs.