Middle+
What microservice patterns do you know?
sobes.tech AI
Answer from AI
- Service Discovery: Allows services to find each other. Implemented through a centralized registry (e.g., ZooKeeper, Consul, etcd).
- API Gateway: A single entry point for clients, routing requests to the appropriate services. Provides authentication, authorization, load balancing.
- Circuit Breaker: Prevents cascading failures by temporarily disabling calls to failing services.
- Bulkhead: Isolates resources of different services to prevent mutual impact during failures.
- Saga: Manages distributed transactions, ensuring data consistency across multiple services through a sequence of local transactions and compensating actions.
- Event Sourcing: Stores all data changes as a sequence of events, providing a complete history of states.
- CQRS: Separates read and write logic into different models, optimizing performance for each operation type.
- Database per Service: Each service has its own database to ensure independence and decentralization.
- Strangler Fig: Gradually replaces a monolithic application with microservices by redirecting parts of the traffic.
Example implementation of Circuit Breaker in PHP (pseudo-code):
<?php
// Circuit Breaker class
class CircuitBreaker
{
private $threshold; // Failure threshold
private $timeout; // Time the breaker remains open
private $failures = 0; // Current failure count
private $lastFailureTime = 0; // Last failure time
private $state = 'CLOSED'; // Current state: CLOSED, OPEN, HALF_OPEN
public function __construct(int $threshold, int $timeout)
{
$this->threshold = $threshold;
$this->timeout = $timeout;
}
public function execute(callable $operation)
{
switch ($this->state) {
case 'OPEN':
if (time() - $this->lastFailureTime > $this->timeout) {
$this->state = 'HALF_OPEN';
// Attempt to execute in HALF_OPEN state
try {
$result = $operation();
$this->reset(); // Success, reset
return $result;
} catch (\Exception $e) {
$this->recordFailure(); // Failure in HALF_OPEN, go back to OPEN
throw $e;
}
} else {
throw new \Exception("Circuit breaker is open"); // Breaker is open
}
break;
case 'HALF_OPEN':
// Logic for HALF_OPEN already above
break;
case 'CLOSED':
default:
try {
$result = $operation();
$this->reset(); // Success, reset
return $result;
} catch (\Exception $e) {
$this->recordFailure(); // Failure, increment counter
throw $e;
}
break;
}
}
private function recordFailure(): void
{
$this->failures++;
$this->lastFailureTime = time();
if ($this->failures >= $this->threshold) {
$this->state = 'OPEN'; // Transition to OPEN on threshold
}
}
private function reset(): void
{
$this->failures = 0;
$this->lastFailureTime = 0;
$this->state = 'CLOSED'; // Transition to CLOSED on success
}
}
// Usage
$breaker = new CircuitBreaker(3, 60); // Threshold 3 failures, timeout 60 seconds
try {
$data = $breaker->execute(function() {
// External service call code
if (rand(0, 1) == 0) { // Simulate failure
throw new \Exception("Service unavailable");
}
return ['status' => 'ok'];
});
echo "Success: " . json_encode($data) . "\n";
} catch (\Exception $e) {
echo "Failure: " . $e->getMessage() . "\n";
}