A comprehensive Ballerina application showcasing production-ready features and best practices for building cloud-native microservices.
This repository demonstrates enterprise-grade Ballerina applications with:
- RESTful APIs with proper error handling
- Database integration with ACID transactions
- Concurrent processing with Ballerina workers
- Health checks and observability
- Containerization and cloud deployment
- Security best practices
- Comprehensive testing
- Features
- Architecture
- Getting Started
- Services
- Configuration
- Development
- Testing
- Deployment
- API Documentation
- Contributing
- Resources
- β RESTful API Server - Full CRUD operations with validation
- β Database Service - Transaction management and data integrity
- β Game Server - AI-integrated game backend with player management
- β AWS Lambda Function - Serverless deployment example
- β DAG Implementation - Advanced data structure with algorithms
- π Concurrent Processing - Worker-based parallelism
- π Transaction Management - ACID compliance with rollback support
- π Observability - Metrics, logging, and tracing
- π‘οΈ Error Handling - Structured error responses
- βοΈ Configuration Management - Environment-based config
- π³ Containerization - Docker and Docker Compose support
- βΈοΈ Cloud Native - Kubernetes deployment ready
- π§ͺ Testing - Comprehensive test suite
- π GraphQL - Modern API with subscriptions
- π WebSocket - Real-time bidirectional communication
- π gRPC - High-performance RPC framework (proto definitions included)
ballerina-study/
βββ main.bal # Main production server (Port 9090)
βββ database_service.bal # Database/transaction service (Port 9091)
βββ graphql_service.bal # GraphQL API service (Port 4000)
βββ websocket_service.bal # WebSocket chat service (Port 9092)
βββ src/main/server/
β βββ server_ai.bal # Game server with AI (Port 8080)
β βββ aws/
β βββ lambda/ballerina.bal # AWS Lambda function
β βββ dag/dag.bal # DAG implementation
βββ tests/ # Test files
βββ proto/ # gRPC protocol definitions
βββ k8s/ # Kubernetes manifests
βββ .github/workflows/ # CI/CD pipelines
βββ Ballerina.toml # Package configuration
βββ Cloud.toml # Cloud deployment config
βββ Dockerfile # Container image
βββ docker-compose.yml # Multi-service orchestration
βββ DEPLOYMENT.md # Production deployment guide
βββ OBSERVABILITY.md # Monitoring and observability
βββ SECURITY.md # Security best practices
βββ CONTRIBUTING.md # Contributing guidelines
- Ballerina 2201.8.0 or later (Download)
- Java 11 or later
- Docker (optional, for containerized deployment)
- Clone the repository:
git clone https://github.com/smaruf/ballerina-study.git
cd ballerina-study- Run the main production server:
bal run main.bal- Test the health endpoint:
curl http://localhost:9090/api/v1/health- Create a user:
curl -X POST http://localhost:9090/api/v1/users \
-H "Content-Type: application/json" \
-d '{"name":"John Doe","email":"john@example.com"}'Port: 9090
A comprehensive REST API demonstrating production-ready patterns:
- Health & Readiness:
/api/v1/health,/api/v1/ready - User Management: CRUD operations with validation
- Metrics:
/api/v1/metrics - Concurrent Processing: Worker-based parallel execution
- Transactions: Atomic operations with rollback
Port: 9091
E-commerce backend with transaction management:
- Product Management: Create, read, update products
- Order Processing: Transaction-safe order creation
- Stock Management: Automatic inventory updates
- Order Lifecycle: Status updates and cancellations
Port: 8080
AI-integrated game backend:
- Player Management: Join, track actions and reactions
- AI Integration: Multiple AI provider fallback logic
- RAG Models: Create and store RAG models
- Real-time State: In-memory game state management
Port: 4000
GraphQL API for book management:
- Queries: Get books, search, filter
- Mutations: Create, update, delete books
- Subscriptions: Real-time updates
- GraphiQL: Interactive GraphQL playground at
/graphiql
Port: 9092
Real-time chat application:
- Real-time Messaging: Instant message delivery
- Typing Indicators: Show when users are typing
- User Presence: Track online users
- Chat History: Maintain message history
- Connection:
ws://localhost:9092/chat
Port: 8080
Serverless function example:
- Lambda Handler: AWS-compatible request/response
- Async Processing: Background task execution
- Health Checks: Container health monitoring
Standalone directed acyclic graph implementation:
- Graph Operations: Add nodes and edges
- Cycle Detection: DFS-based cycle detection
- Topological Sort: Dependency ordering
- Visualization: Console-based graph printing
# Main Production Server
export serverPort=9090
export environment=production
export enableMetrics=true
export requestTimeout=30000
# Game Server
export gameServerPort=8080
# Database Service
export dbHost=localhost
export dbPort=3306
export dbName=ballerina_store
# Lambda Function
export lambdaPort=8080[ballerina_study.production_server]
serverPort = 9090
environment = "production"
enableMetrics = true
requestTimeout = 30000See DEPLOYMENT.md for complete configuration options.
bal build# Production server
bal run main.bal
# Database service
bal run database_service.bal
# GraphQL service
bal run graphql_service.bal
# Then visit: http://localhost:4000/graphiql
# WebSocket chat service
bal run websocket_service.bal
# Connect using: ws://localhost:9092/chat
# Game server
cd src/main/server
bal run server_ai.bal
# DAG standalone
cd src/main/server/aws/dag
bal run dag.bal# Format code
bal format
# Run linter (if available)
bal lintbal testbal test tests/main_test.bal
bal test tests/database_test.balTests cover:
- β Health and readiness endpoints
- β User CRUD operations
- β Input validation
- β Error handling
- β Product and order management
- β Transaction rollback scenarios
- β Concurrent processing
# Build image
docker build -t ballerina-study:1.0.0 .
# Run with Docker Compose
docker-compose up -d
# Check status
docker-compose ps
# View logs
docker-compose logs -f# Generate K8s artifacts
bal build --cloud=k8s
# Deploy to cluster
kubectl apply -f target/kubernetes/production_server/
# Check deployment
kubectl get pods
kubectl get servicesSee DEPLOYMENT.md for detailed Lambda deployment instructions.
GET /api/v1/healthResponse:
{
"status": "UP",
"environment": "production",
"timestamp": "2024-01-26T05:30:00Z",
"version": "1.0.0"
}POST /api/v1/users
Content-Type: application/json
{
"name": "John Doe",
"email": "john@example.com"
}Response (201 Created):
{
"success": true,
"message": "User created successfully",
"data": {
"id": "uuid-here",
"name": "John Doe",
"email": "john@example.com",
"createdAt": "2024-01-26T05:30:00Z"
}
}GET /api/v1/users/{id}GET /api/v1/users?limit=10&offset=0PUT /api/v1/users/{id}
Content-Type: application/json
{
"name": "Jane Doe",
"email": "jane@example.com"
}DELETE /api/v1/users/{id}POST /api/v1/process/concurrent
Content-Type: application/json
{
"data": "test data"
}POST /store/products
Content-Type: application/json
{
"name": "Product Name",
"price": 99.99,
"quantity": 100
}POST /store/orders
Content-Type: application/json
{
"userId": "user-id",
"productId": "product-id",
"quantity": 2
}POST /store/orders/{id}/cancelSee full API documentation in DEPLOYMENT.md.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Ballerina team for the excellent language and documentation
- Community contributors and examples
- Cloud native computing foundation for best practices
Built with β€οΈ using Ballerina