Microservice Best Practices

Development 

  • Single responsibility- One Task per Microservice
  • Strangler Fig Pattern: https://martinfowler.com/bliki/StranglerFigApplication.html 
  • API Gateway: API Gateway should provide Routing, Aggregating, and SSL Offloading 
  • Offload Non-core responsibilities: Non-core responsibilities including – Security, logging, tracking, etc., should be offloaded to Sidecar or Libraries.   

Design for Failure 

  • Fail fast: Patterns like Circuit breaker Pattern, time out Pattern, rate limit Pattern, etc, help applications fail fast  
  • Isolate Failure: A failure should not propagate and impact other services. Bulkhead Pattern helps maintain such a configuration. 
  • Self-healing system: Health checkpoints and scalability settings will help make sure that system is able to manage a server or pod failure. 

Monitoring  

  • Health Monitoring: Applications should expose health endpoint (e.g. actuator in Java), which is used by Load balancers to keep a check.  
  • Golden Signals: Every application should monitor latency, traffic, and error rate https://sre.google/sre-book/monitoring-distributed-systems/
  • Distributed Tracing: Distributed tracing to check downstream and upstream dependencies.
  • Infrastructure Monitoring: Monitor CPU and Memory usage.

Performance 

  • Stateless: Keep APIs stateless
  • Asynchronous: Asynchronous communication wherever possible.
  • Caching: Cache data for better performance wherever possible.
  • Connection Pool: Database and HTTP connection pools should be enabled wherever possible.  

Good to have  

  • Separate Datastores: This is a double-edged sword, we need to be careful in separating data stores.
  • SAGA for Transaction Management: Commonly used pattern for transaction management in microservices. 
  • 12 Factor App: Generic best practices for developing a web application https://12factor.net/