Created
March 14, 2026 15:12
-
-
Save mohashari/2906ab214d1e67848fb2ba58cfa21bcf to your computer and use it in GitHub Desktop.
Code snippets — Microservices Architecture Patterns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type OrderSaga struct{} | |
| func (s *OrderSaga) Execute(orderID string) error { | |
| steps := []SagaStep{ | |
| {Execute: paymentService.Charge, Compensate: paymentService.Refund}, | |
| {Execute: inventoryService.Reserve, Compensate: inventoryService.Release}, | |
| {Execute: shippingService.Schedule, Compensate: shippingService.Cancel}, | |
| } | |
| var completed []SagaStep | |
| for _, step := range steps { | |
| if err := step.Execute(orderID); err != nil { | |
| // Rollback in reverse order | |
| for i := len(completed) - 1; i >= 0; i-- { | |
| completed[i].Compensate(orderID) | |
| } | |
| return err | |
| } | |
| completed = append(completed, step) | |
| } | |
| return nil | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Client → Order Service → [HTTP] → Inventory Service → [HTTP] → Payment Service |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type CircuitBreaker struct { | |
| failures int | |
| threshold int | |
| lastFailed time.Time | |
| timeout time.Duration | |
| state string // "closed", "open", "half-open" | |
| } | |
| func (cb *CircuitBreaker) Call(fn func() error) error { | |
| if cb.state == "open" { | |
| if time.Since(cb.lastFailed) < cb.timeout { | |
| return errors.New("circuit breaker open") | |
| } | |
| cb.state = "half-open" | |
| } | |
| err := fn() | |
| if err != nil { | |
| cb.failures++ | |
| cb.lastFailed = time.Now() | |
| if cb.failures >= cb.threshold { | |
| cb.state = "open" | |
| } | |
| return err | |
| } | |
| cb.failures = 0 | |
| cb.state = "closed" | |
| return nil | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Order Service → [Kafka] → Payment Service | |
| → Inventory Service | |
| → Notification Service |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ┌─────────────────┐ | |
| Clients ──────────→ │ API Gateway │ | |
| └────────┬────────┘ | |
| │ | |
| ┌──────────────┼──────────────┐ | |
| ↓ ↓ ↓ | |
| user-service order-service product-service |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1. Order Service: Creates order → publishes "order.created" | |
| 2. Payment Service: Charges card → publishes "payment.processed" | |
| (if fails) → publishes "payment.failed" | |
| 3. Inventory Service: Reserves stock → publishes "stock.reserved" | |
| (if fails) → publishes "stock.insufficient" | |
| 4. Order Service: | |
| - On "payment.failed" → cancels order | |
| - On "stock.insufficient" → refunds payment, cancels order |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Producer | |
| func PlaceOrder(order Order) error { | |
| if err := db.Save(order); err != nil { | |
| return err | |
| } | |
| event := OrderPlacedEvent{ | |
| OrderID: order.ID, | |
| UserID: order.UserID, | |
| Items: order.Items, | |
| Total: order.Total, | |
| Timestamp: time.Now(), | |
| } | |
| return kafka.Publish("orders.placed", event) | |
| } | |
| // Consumer (in payment-service) | |
| func HandleOrderPlaced(event OrderPlacedEvent) error { | |
| return paymentService.ChargeUser(event.UserID, event.Total, event.OrderID) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Bad (technical layers) | |
| frontend-service | |
| business-logic-service | |
| database-service | |
| # Good (business capabilities) | |
| user-service | |
| payment-service | |
| notification-service | |
| inventory-service | |
| order-service |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment