A flexible and scalable rule engine implementation using gRPC for microservices architecture. This project demonstrates how to build a rule-based pricing system that can be easily extended and deployed across different services.
- gRPC-based Architecture: High-performance, language-agnostic communication
- JSON-based Rule Configuration: Easy to modify rules without code changes
- Flexible Rule Engine: Support for complex conditions and actions
- Scalable Design: Built for microservices and distributed systems
- Type Safety: Protocol Buffers ensure type-safe communication
- Concurrent Processing: Multi-threaded server for handling multiple requests
RuleEngine_gRPC/
├── client.py # gRPC client implementation
├── server.py # gRPC server implementation
├── rule_engine.proto # Protocol Buffer definition
├── rule_engine_pb2.py # Generated Python protobuf classes
├── rule_engine_pb2_grpc.py # Generated Python gRPC classes
├── rules.json # Rule configuration file
├── README.md # This file
└── LICENSE # MIT License
- Python 3.7+
- pip (Python package manager)
Install the required dependencies:
pip install grpcio grpcio-toolsIf you modify the .proto file, regenerate the Python files:
python -m grpc_tools.protoc --python_out=. --grpc_python_out=. rule_engine.protopython server.pyThe server will start on localhost:50051 and display:
RuleEngine gRPC Server is running on port 50051...
In a new terminal window:
python client.pyThis will send sample user data to the server and display the rule evaluation results.
Rules are defined in rules.json with the following structure:
[
{
"condition": "membership == 'gold' and age > 18",
"action": "base_price * 0.80",
"message": "Gold adult member"
}
]- condition: Python expression that evaluates to True/False
- action: Python expression that calculates the final price
- message: Descriptive message for the applied rule
The current configuration includes:
- Gold Adult Members: 20% discount for gold members over 18
- Gold Young Members: 15% discount for gold members 18 and under
- Silver EU Members: 5% discount for silver members in EU region
- Default Rule: Regular pricing for all other cases
The client sends user data with the following fields:
age: User's age (integer)membership: Membership type (string: "gold", "silver", "platinum")region: Geographic region (string: "EU", "US", etc.)base_price: Base price before discounts (float)
Service: RuleEngineService
Method: Evaluate
Request (UserRequest):
message UserRequest {
int32 age = 1;
string membership = 2;
string region = 3;
double base_price = 4;
}Response (RuleResponse):
message RuleResponse {
string message = 1;
double final_price = 2;
}User: {'age': 25, 'membership': 'gold', 'region': 'EU', 'base_price': 100}
→ Message: Gold adult member, Final Price: $80.00
User: {'age': 17, 'membership': 'gold', 'region': 'US', 'base_price': 120}
→ Message: Gold young member, Final Price: $102.00
User: {'age': 30, 'membership': 'silver', 'region': 'EU', 'base_price': 80}
→ Message: Silver EU member, Final Price: $76.00
eval() for rule evaluation, which can be a security risk in production environments. For production use, consider:
- Using a safer expression evaluator like
ast.literal_eval() - Implementing a custom rule parser
- Using a dedicated rule engine library like
durable_rulesorbusiness-rules
- Edit
rules.jsonto add new rule conditions and actions - Restart the server to load new rules
- Test with the client
- Update
rule_engine.prototo add new fields toUserRequest - Regenerate protobuf files:
python -m grpc_tools.protoc --python_out=. --grpc_python_out=. rule_engine.proto - Update server and client code to handle new fields
- Modify rules in
rules.jsonto use new fields
Since this uses gRPC and Protocol Buffers, you can easily create clients in other languages:
- Go: Use
protocwith Go plugins - Java: Use
protocwith Java plugins - C++: Use
protocwith C++ plugins - Node.js: Use
@grpc/proto-loader
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Ertugrul Kra
- GitHub: @ErtugrulKra
- gRPC team for the excellent framework
- Protocol Buffers for efficient serialization
- Python community for the rich ecosystem
⭐ If you found this project helpful, please give it a star!