How to Automatically Restart Failed Microservices
In a microservices architecture, failures are inevitable. Whether due to a crash, high memory usage, or an unexpected error, services may go down. To maintain system reliability, failed services should restart automatically. In this blog, we’ll explore how different tools — Kubernetes, systemd, Docker, Consul, and API Gateways — handle failed service recovery.
1️⃣ Restarting Failed Services in Kubernetes
Kubernetes ensures that unhealthy microservices restart automatically using liveness probes.
Kubernetes Configuration for Auto-Restart
Modify your deployment.yaml
file:
livenessProbe:
httpGet:
path: /api/health
port: 8000
initialDelaySeconds: 5
periodSeconds: 10
✅ How It Works:
- If
/api/health
fails, Kubernetes restarts the pod. - It checks every 10 seconds and waits 5 seconds before the first check.
2️⃣ Restarting Failed Services with Systemd
If running Laravel in a VM or bare metal, use systemd to restart it.
Create a systemd Service File
Save this as /etc/systemd/system/laravel-microservice.service
:
[Unit]
Description=Laravel Microservice
After=network.target
[Service]
ExecStart=/usr/bin/php artisan serve --host=0.0.0.0 --port=8000
Restart=always
RestartSec=5
User=www-data
Group=www-data[Install]
WantedBy=multi-user.target
✅ How It Works:
- If Laravel crashes, systemd restarts it in 5 seconds.
- Check status:
systemctl status laravel-microservice
- Restart manually:
systemctl restart laravel-microservice
3️⃣ Restarting Failed Containers in Docker
If Laravel runs inside Docker, use Docker’s restart policies.
Run Container with Restart Policy
docker run --restart=always -d -p 8000:8000 my-laravel-app
✅ How It Works:
--restart=always
→ Restarts even after crashes or reboots.- Alternative options:
on-failure
,unless-stopped
.
4️⃣ Restarting Services with Consul
If Laravel is discovered via Consul, it can be deregistered and restarted.
Register Service with Health Check
{
"service": {
"name": "user-service",
"port": 8000,
"check": {
"http": "http://user-service.local/api/health",
"interval": "10s",
"timeout": "2s"
}
}
}
Then register it with Consul:
curl --request PUT --data @consul.json http://localhost:8500/v1/agent/service/register
✅ How It Works:
- If
/health
fails, Consul removes the service. - The orchestrator (Kubernetes, systemd, etc.) restarts it.
5️⃣ Restarting Failed Services with Kong API Gateway
Kong can remove unhealthy services and redirect traffic.
Set Up Kong Health Checks
curl -X POST http://localhost:8001/upstreams/user-service/healthchecks \
--data "active.http_path=/health" \
--data "active.unhealthy.interval=5"
✅ How It Works:
- If
/health
fails, Kong stops sending requests. - After recovery, it resumes traffic.
🚀 Summary: How Failed Services Restart
MethodHow It WorksBest ForKubernetesAuto-restarts failed podsCloud-native appsSystemdRestarts Laravel if it crashesVM, bare metalDockerAuto-restarts crashed containersContainerized appsConsulDeregisters failed servicesService discoveryKong API GatewayStops routing to failed instancesAPI management
✅ For a Laravel microservices setup, the best approach is:
- Kubernetes for cloud-based deployments
- Systemd for VM-based applications
- Docker restart policies for containerized environments
- Consul and Kong for service discovery and API routing
Would you like help automating service restarts for your Laravel microservices? Let me know in the comments! 🚀