Maybets is a high-performance Go application designed for real-time processing of large volumes of betting transactions while providing analytical insights. It efficiently ingests transaction data, stores it, and exposes APIs for retrieving key betting analytics.
- Data Ingestion: Accepts betting transactions in JSON format from a file (
bets.json
) or an API endpoint. - Processing & Storage: Leverages Go's in-memory data structures and SQLite for efficient transaction handling.
- Analytics APIs: Provides insights into user betting statistics and detects anomalies.
- Performance Optimization: Uses goroutines for concurrent processing, ensuring a throughput of at least 10,000 bets per second.
- CLI Support: Includes a command-line interface for batch processing.
The approach below was adopted when implementing the solution
Instead of inserting bets individually, transactions are grouped into batches (e.g., 1,000 bets per batch) and inserted in a single transaction. This significantly reduces database writes and improves performance.
A connection pool manages database connections efficiently, ensuring the system can handle multiple concurrent requests without overwhelming SQLite.
Goroutines process bets concurrently, enabling the system to handle multiple batches of bets simultaneously and improving throughput.
Frequently queried columns (e.g., user_id
) are indexed to optimize SQL query performance and speed up retrieval times.
Batch inserts require temporarily storing transactions in memory before writing to the database, leading to higher memory consumption, especially with large datasets.
SQLite is lightweight and not designed for extremely high write loads. While batching and connection pooling mitigate some limitations, SQLite may still struggle under very high transaction volumes.
- Go 1.20+
- Redis (MANDATORY)
git clone [email protected]:KathurimaKimathi/maybets.git
cd maybets
go mod tidy
Ensure Redis is installed and running:
redis-server
export ENVIRONMENT="LOCAL"
export REDIS_URL="redis://localhost:6379/0"
export JAEGER_ENDPOINT="localhost:4318"
export PORT="8080"
export SQLITE_URL="/path/to/your/sqlite/file"
Method 1: Using CLI
cd cmd
go run cmd.go runserver
Method 2: Direct Execution
go run server.go
Generate test betting data:
cd cmd
go run cmd.go generate --betdata 10000 bets.json
Load test data into SQLite:
go run cmd.go process bets.json
Each betting transaction follows this JSON structure:
{
"bet_id": "string",
"user_id": "string",
"amount": "float64",
"odds": "float64",
"outcome": "win" | "lose",
"timestamp": "RFC3339 format"
}
curl --location '<BASEURL>:<PORT>/api/v1/analytics/total_bets?user_id={user_id}'
curl --location '<BASEURL>:<PORT>/api/v1/analytics/total_winnings?user_id={user_id}'
curl --location '<BASEURL>:<PORT>/api/v1/analytics/top_users'
curl --location '<BASEURL>:<PORT>/api/v1/analytics/anomalies'
Run the following command to start the tracing service:
docker compose up
Then, navigate to Jaeger UI to view request traces across various points in the application.
Contributions are welcome! Feel free to submit issues, feature requests, or pull requests to improve the project.
This project is licensed under the MIT License.