A simple but functional service registry implementation for understanding service discovery in distributed systems.
Python 3.8 or higher
- Clone the repository:
git clone https://github.com/ranjanr/ServiceRegistry.git
cd ServiceRegistry- Create a virtual environment (Python 3.13+):
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt# Make sure virtual environment is activated
source venv/bin/activate
# Start the registry
python3 service_registry_improved.pyYou should see:
Service Registry starting on port 5000...
Heartbeat timeout: 30s
Cleanup interval: 10s
A Service Registry is a database of available service instances in a distributed system. It enables:
- Service Registration: Services register themselves when they start
- Service Discovery: Services can find and communicate with each other
- Health Monitoring: Track which services are alive and healthy
- Load Balancing: Distribute requests across multiple service instances
βββββββββββββββ βββββββββββββββββββ βββββββββββββββ
β Service A ββββββββββΆβ Service Registry βββββββββββ Service B β
β (Port 8001) β Registerβ (Port 5000) β Discoverβ (Port 8002) β
βββββββββββββββ βββββββββββββββββββ βββββββββββββββ
β β β
βββββββββ Heartbeat ββββββββ β
βββββββββ Heartbeat ββββββββ
The basic implementation you provided - simple but functional.
Pros:
- β Simple and easy to understand
- β Core functionality works
Cons:
- β No error handling
- β No health checks
- β No way to remove services
- β Services stay registered forever (even if they crash)
Enhanced version with enterprise features.
New Features:
- β Error Handling: Proper validation and error responses
- β Health Checks: Heartbeat mechanism to detect dead services
- β Deregistration: Services can unregister gracefully
- β Auto Cleanup: Removes stale services automatically
- β Thread Safety: Uses locks for concurrent access
- β Detailed Responses: Rich JSON responses with metadata
- β Service Listing: View all registered services
Demo client showing how services interact with the registry.
- Dockerfile - Container image for the registry
- k8s/ - Kubernetes manifests for deployment
- KUBERNETES.md - Complete Kubernetes deployment guide
- deploy-minikube.sh - Automated deployment script
- consul_client.py - Consul service discovery client
- CONSUL.md - Production-grade service registry guide
- Compare custom implementation with industry-standard Consul
Choose your learning path:
Python 3.8 or higher
- Clone the repository:
git clone https://github.com/ranjanr/ServiceRegistry.git
cd ServiceRegistry- Create a virtual environment:
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txtBasic Version:
python3 service_registry.pyImproved Version (Recommended):
python3 service_registry_improved.pyThe registry will start on http://localhost:5001
# One-command deployment
./deploy-minikube.shThis will:
- Start Minikube (if not running)
- Build Docker image
- Deploy registry and example services
- Show access URLs and test commands
# Start Minikube
minikube start
# Build image
eval $(minikube docker-env)
docker build -t service-registry:latest .
# Deploy
kubectl apply -f k8s/registry-deployment.yaml
kubectl apply -f k8s/example-service-deployment.yaml
# Access
minikube ip # Get IP
curl http://<MINIKUBE_IP>:30001/healthSee KUBERNETES.md for complete guide.
Terminal 1: Start the Registry
python service_registry_improved.pyTerminal 2: Start User Service
python example_service.py user-service 8001Terminal 3: Start Payment Service
python example_service.py payment-service 8002Terminal 4: Run Discovery Demo
python example_service.py demoPOST /register
Content-Type: application/json
{
"service": "user-service",
"address": "http://localhost:8001"
}Response:
{
"status": "registered",
"message": "Service user-service registered at http://localhost:8001"
}GET /discover/user-serviceResponse:
{
"service": "user-service",
"instances": [
{
"address": "http://localhost:8001",
"uptime_seconds": 45.2
}
],
"count": 1
}POST /heartbeat
Content-Type: application/json
{
"service": "user-service",
"address": "http://localhost:8001"
}POST /deregister
Content-Type: application/json
{
"service": "user-service",
"address": "http://localhost:8001"
}GET /servicesResponse:
{
"services": {
"user-service": {
"total_instances": 2,
"active_instances": 2
},
"payment-service": {
"total_instances": 1,
"active_instances": 1
}
},
"total_services": 2
}GET /healthWhen a service starts, it tells the registry:
- Who am I? (service name)
- Where am I? (address/port)
# Service registers itself
requests.post("http://registry:5001/register", json={
"service": "user-service",
"address": "http://localhost:8001"
})When a service needs to call another service:
- Ask the registry for available instances
- Get list of addresses
- Choose one (round-robin, random, etc.)
# Discover payment service
response = requests.get("http://registry:5001/discover/payment-service")
instances = response.json()['instances']
payment_url = instances[0]['address'] # Use first instanceServices periodically send "I'm alive" signals:
- Prevents stale entries
- Detects crashed services
- Registry removes services that stop sending heartbeats
# Send heartbeat every 10 seconds
while True:
requests.post("http://registry:5001/heartbeat", json={
"service": "user-service",
"address": "http://localhost:8001"
})
time.sleep(10)When a service stops, it should deregister:
- Prevents clients from calling dead services
- Keeps registry clean
# On shutdown
requests.post("http://registry:5001/deregister", json={
"service": "user-service",
"address": "http://localhost:8001"
})Netflix uses a similar pattern with their Eureka service registry:
- Microservices register on startup
- Other services discover them dynamically
- Handles thousands of service instances
Kubernetes has built-in service discovery:
- Services register via DNS
- Load balancing across pods
- Health checks and auto-restart
Production-grade service registry with:
- Health checking
- Key-value store
- Multi-datacenter support
- Persistence: Save registry to disk/database
- Load Balancing: Return instances in round-robin order
- Service Metadata: Store version, tags, capabilities
- Authentication: Secure the registry endpoints
- Monitoring: Add metrics and logging
- Clustering: Multiple registry instances for high availability
- Service Mesh: Integrate with Istio or Linkerd
# Register a service
curl -X POST http://localhost:5001/register \
-H "Content-Type: application/json" \
-d '{"service": "test-service", "address": "http://localhost:9000"}'
# Discover services
curl http://localhost:5001/discover/test-service
# List all services
curl http://localhost:5001/services
# Send heartbeat
curl -X POST http://localhost:5001/heartbeat \
-H "Content-Type: application/json" \
-d '{"service": "test-service", "address": "http://localhost:9000"}'
# Deregister
curl -X POST http://localhost:5001/deregister \
-H "Content-Type: application/json" \
-d '{"service": "test-service", "address": "http://localhost:9000"}'| Feature | Original | Improved |
|---|---|---|
| Registration | β | β |
| Discovery | β | β |
| Error Handling | β | β |
| Heartbeats | β | β |
| Deregistration | β | β |
| Auto Cleanup | β | β |
| Thread Safety | β | β |
| Service Listing | β | β |
| Health Endpoint | β | β |
| Uptime Tracking | β | β |
- Microservices Patterns by Chris Richardson
- Building Microservices by Sam Newman
- Martin Fowler's Blog: https://martinfowler.com/articles/microservices.html
This is a learning project - feel free to use and modify as needed!
This is an educational project. Feel free to:
- Add new features
- Improve documentation
- Create additional examples
- Share your learnings!