Microservices Mimarisi: Monolith'ten Microservices'e Geçiş
zafer ak
Yazar
Microservices Nedir?
Microservices, uygulamayı küçük, bağımsız servislere bölen bir mimari yaklaşımdır. Her servis kendi database'ini ve business logic'ini yönetir.
Monolith vs Microservices
Monolith
- Tek deployment unit
- Shared database
- Basit geliştirme başlangıcı
- Scaling tüm uygulama için
Microservices
- Bağımsız deployment
- Service başına database
- Technology agnostic
- Granular scaling
Ne Zaman Microservices?
Uygun Senaryolar
- Büyük, karmaşık sistemler
- Farklı takımlar farklı alanlar
- Farklı scaling gereksinimleri
- Farklı teknoloji ihtiyaçları
Uygun Olmayan Senaryolar
- Startup MVP
- Küçük takımlar
- Basit uygulamalar
- DevOps maturity düşük
Service Design Principles
Single Responsibility
Her servis tek bir iş yapmalı:
- User Service: Kullanıcı yönetimi
- Order Service: Sipariş işleme
- Payment Service: Ödeme işleme
- Notification Service: Bildirimler
Bounded Context (DDD)
Domain-Driven Design ile servis sınırlarını belirleyin.
API Gateway Pattern
Client → API Gateway → Services
API Gateway görevleri:
- Routing
- Authentication
- Rate limiting
- Load balancing
- Request/Response transformation
- Caching
Araçlar: Kong, AWS API Gateway, Traefik
Service Communication
Synchronous (REST/gRPC)
// Order Service → User Service
$user = Http::get("http://user-service/api/users/{$userId}");
// gRPC (daha hızlı)
$client = new UserServiceClient($channel);
$response = $client->GetUser($request);
Asynchronous (Message Queue)
// Order created event
$event = new OrderCreated($order);
Queue::push($event);
// Notification Service dinler
class OrderCreatedListener
{
public function handle(OrderCreated $event)
{
// Email gönder
}
}
Event-Driven Architecture
Order Service:
- OrderCreated event publish
Inventory Service:
- OrderCreated subscribe → Stock azalt
Payment Service:
- OrderCreated subscribe → Ödeme işle
Notification Service:
- OrderCreated subscribe → Email gönder
Data Management
Database per Service
Her servis kendi database'ine sahip. Join yerine API çağrısı.
Saga Pattern
Distributed transaction yönetimi:
1. Order Service: Order oluştur
2. Payment Service: Ödeme al
3. Inventory Service: Stok düş
4. Hata olursa: Compensating transaction
Monitoring & Observability
- Distributed Tracing: Jaeger, Zipkin
- Logging: ELK Stack (Elasticsearch, Logstash, Kibana)
- Metrics: Prometheus + Grafana
- Health Checks: Her servis /health endpoint
Sonuç
Microservices güçlü bir mimari ama karmaşıklık getirir. "Start with monolith, evolve to microservices" yaklaşımı önerilir.