System Architecture

Comprehensive overview of the Beas Rule Engine architecture and design patterns

Architecture Overview

The Beas Rule Engine follows a modern, scalable architecture designed for enterprise-grade rule processing. The system is built using Spring Boot with a layered architecture that promotes separation of concerns, maintainability, and extensibility.

System Architecture Layers

Presentation Layer

REST Controllers, DTOs, Validation, Exception Handling

Business Layer

Services, Rule Engine Manager, Cache Controller, Cryptography

Data Layer

Repositories, MongoDB, Cache Stores, RSQL Queries

Layered Architecture

Presentation Layer

The presentation layer handles all external communication and provides a clean REST API interface.

Key Components:
  • Controllers: REST endpoints for rule operations
  • DTOs: Data transfer objects for request/response
  • Validation: Input validation and sanitization
  • Exception Handling: Global exception handling and error responses
Example Controller:
@RestController
@RequestMapping("/beasre/v1/rule-engine")
public class RuleEngine {
    
    @PostMapping("/evaluate")
    public ResponseEntity<RuleResponse> evaluateRule(
        @RequestBody RuleRequest request,
        @RequestParam Map<String, Object> parameters
    ) {
        // Rule evaluation logic
    }
}

Business Layer

The business layer contains the core business logic and orchestrates rule processing.

Key Components:
  • Services: Business logic implementation
  • Rule Engine Manager: Core rule processing engine
  • Cache Controller: Caching strategy management
  • Cryptography: Data encryption and security
Example Service:
@Service
public class RuleLibraryService extends BaseService<RuleLibrary> {
    
    public RuleLibrary createRuleLibrary(RuleLibrary ruleLibrary) {
        // Business logic for creating rule library
        validateRuleLibrary(ruleLibrary);
        return repository.save(ruleLibrary);
    }
}

Data Layer

The data layer manages data persistence and provides data access abstractions.

Key Components:
  • Repositories: Data access layer
  • MongoDB: Primary data store
  • Cache Stores: In-memory caching
  • RSQL Queries: Advanced querying capabilities
Example Repository:
@Repository
public interface RuleLibraryRepository extends MongoRepository<RuleLibrary, String> {
    
    List<RuleLibrary> findByContainerName(String containerName);
    
    @Query("{'name': {$regex: ?0, $options: 'i'}}")
    List<RuleLibrary> findByNameContainingIgnoreCase(String name);
}

Core Components

Rule Engine Manager

The Rule Engine Manager is the heart of the system, responsible for rule evaluation and processing.

Responsibilities:
  • Rule compilation and validation
  • Parameter binding and context creation
  • Rule execution and result processing
  • Error handling and logging
Key Features:
public class RuleEngineManager {
    
    public Object evaluateRule(String ruleName, Map<String, Object> parameters, Object payload) {
        // 1. Load rule from cache or database
        // 2. Compile and validate rule
        // 3. Create execution context
        // 4. Execute rule with parameters
        // 5. Return result
    }
}

Cache Controller

The Cache Controller manages multiple caching layers for optimal performance.

Cache Types:
  • Function Cache: Compiled function storage
  • Instance Cache: Rule instance caching
  • Result Cache: Evaluation result caching
Cache Strategy:
@Component
public class CacheController {
    
    public void syncCaches() {
        // Synchronize all cache layers
        functionCache.sync();
        instanceCache.sync();
        resultCache.sync();
    }
}

Cryptography Service

The Cryptography Service provides data encryption and security features.

Security Features:
  • TripleDES encryption for sensitive data
  • Configurable encryption keys
  • Secure key management
  • Data integrity validation
Encryption Example:
@Service
public class Cryptography {
    
    public String encrypt(String data) {
        // TripleDES encryption
        return cryptoState.encrypt(data);
    }
    
    public String decrypt(String encryptedData) {
        // TripleDES decryption
        return cryptoState.decrypt(encryptedData);
    }
}

Data Flow

Request Processing Flow

  1. Request Reception: REST controller receives HTTP request
  2. Authentication: JWT token validation
  3. Input Validation: Request parameter validation
  4. Business Logic: Service layer processing
  5. Rule Evaluation: Rule engine execution
  6. Cache Update: Result caching
  7. Response: Formatted response return
Flow Diagram:
Client Request
    ↓
REST Controller
    ↓
Authentication Filter
    ↓
Validation Layer
    ↓
Service Layer
    ↓
Rule Engine Manager
    ↓
Cache Controller
    ↓
Database/Repository
    ↓
Response Formatter
    ↓
Client Response

Security Architecture

Security Layers

Security Components:
  • OAuth2/JWT: Token-based authentication
  • Role-Based Access: User permission management
  • Data Encryption: Sensitive data protection
  • Input Validation: Request sanitization
Security Configuration:
security:
  oauth2:
    resource:
      jwt:
        key-value: ${JWT_PUBLIC_KEY}
  jwt:
    secret: ${JWT_SECRET}
    expiration: 3600

cryptography:
  key: ${CRYPTO_KEY}
  iv: ${CRYPTO_IV}

Caching Strategy

Multi-Layer Caching

Cache Layers:
  • L1 Cache: In-memory function cache
  • L2 Cache: Rule instance cache
  • L3 Cache: Result cache
Cache Configuration:
cache:
  function:
    ttl: 3600
    max-size: 1000
  instance:
    ttl: 1800
    max-size: 500
  result:
    ttl: 900
    max-size: 2000

Deployment Architecture

Deployment Options

Deployment Models:
  • Standalone: Single instance deployment
  • Cluster: Multi-instance load balancing
  • Docker: Containerized deployment
  • Kubernetes: Orchestrated deployment
Docker Configuration:
FROM openjdk:17-jdk-slim
COPY target/beas-rule-engine.jar app.jar
EXPOSE 8070
ENTRYPOINT ["java", "-jar", "/app.jar"]