7 Database Patterns for Microservices Architecture

7 Database Patterns for Microservices Architecture

Data Storage and Management in Microservices

Introduction to Microservices Data Storage

  • The speaker, Countin the Jen, introduces the topic of data storage and management within microservices, emphasizing the need for a standardized approach.
  • The presentation will cover seven different microservices data storage patterns including database by service, shared database, API composition, CQRS (Command Query Responsibility Segregation), Saga pattern, event sourcing, and more.

Database by Service Pattern

  • In this pattern, each microservice maintains its own private database accessible only through its API. This ensures that services do not directly access each other's databases.
  • Alternatives to full-fledged databases include using private tables or schemas per service to minimize overhead costs while maintaining data privacy.
  • A barrier must be established to enforce modularity; for instance, assigning unique database user IDs can prevent unauthorized access between services.

Example Scenario

  • An online store application illustrates how different services (e.g., order service and customer service) manage their respective databases but may need to interact via APIs when accessing related data.

Benefits and Drawbacks

  • Benefits include loose coupling of services where changes in one service's database do not affect others. Each service can choose the most suitable type of database for its needs.
  • Drawbacks involve challenges with distributed transactions across multiple services and complexities in managing various SQL/NoSQL databases.

Shared Database Pattern

  • This pattern allows multiple services to access a single shared database. Services can perform local asset transactions ensuring atomicity and consistency across operations.

Example Scenario

  • In an online store context, both order and customer services share a common database which simplifies transaction management but increases inter-service dependencies.

Benefits and Drawbacks

  • Benefits include straightforward implementation of asset transactions due to a unified schema.
  • However, drawbacks consist of tight coupling among developers needing coordination on schema changes which could slow down development time.

Saga Pattern

  • The Saga pattern coordinates transactions that span multiple services without relying on distributed transactions. It uses a sequence of local transactions that publish events upon completion.

Implementation Approaches

  • Two approaches exist: choreography (where events trigger local transactions across services automatically), and orchestration (where an orchestrator directs the flow of operations).

Advantages and Disadvantages

  • Sagas maintain data consistency without distributed transactions but introduce complexity in programming models due to compensating actions required for failed operations.

API Composition Pattern

  • This method addresses query complexities arising from multiple microservices by creating an API composer that aggregates results from various sources into a single response.

Benefits and Drawbacks

  • While it simplifies handling queries across microservices, it may lead to inefficient memory usage during large dataset joins as all processing occurs in-memory.

Command Query Responsibility Segregation (CQRS)

  • CQRS separates read operations from write operations by utilizing read-only replicas designed specifically for query support while keeping main event-driven architecture intact.

Advantages

  • It enhances performance through separation of concerns but increases complexity due to potential code duplication across systems.

Event Sourcing

  • Event sourcing captures state changes as sequences of events rather than storing current states directly. This allows reconstruction of past states based on historical events.

Benefits

  • It provides reliable event-driven architecture facilitating consistent updates while avoiding object-relational impedance mismatch issues commonly found in traditional systems.

This structured markdown file encapsulates key insights from the transcript regarding various data storage patterns used in microservices architecture along with their benefits and drawbacks.

Video description

This video explains the 7 Database Patterns for Microservices. 00:55 - Database per Service 07:29 - Shared database 11:20 - Saga 19:00 - API Composition 21:08 - CQRS 24:36 - Domain event 26:37 - Event sourcing To learn more about how to build microservices in Spring, see links below. Microservices Step by Step Procedure here: https://bit.ly/2I6gCDD Microservices Source Codes here: https://bit.ly/2It8fkp Find all the Tutorials below. Subscribe to get updates 1. Introduction to Microservices https://youtu.be/bXEBK6hhZXk 2. The Microservices We Build https://youtu.be/OEl0te9fPBI 3. Create the Applications in Spring Initializr https://youtu.be/8coumjuILpc 4. Import the projects into Sprint Tool Suite https://youtu.be/aE-aH6bnPSc 5. Set port and application name in application.properties file https://youtu.be/5sn3HDcz5_I 6. Create the Admissions Controller and Patient class https://youtu.be/I5dzQtIV2bY 7. Add RequestMappings https://youtu.be/qx2ol1EUGHk 8. Write getPatientById method https://youtu.be/QJFbI-wtP8w 9. Build the Hr Service https://youtu.be/nfaUZ_R_a8g 10. Build the Pathology Service https://youtu.be/X-sJJNZZc4g 11. Test all the three Service https://youtu.be/Onm69PluCqc 12. Using RestTemplate Bean https://youtu.be/rEZKo-CNisY 13. Making REST Calls With RestTemplate https://youtu.be/mdoEkr6YYtY 14. List of Objects vs ObjectList Object https://youtu.be/K5Q6TGYrjaA 15. Class Duplication in Microservices https://youtu.be/yv9ktGjVg84 16. Test the Services Interaction https://youtu.be/G1-AORVXAsk 17. Call pathology service from admissions service https://youtu.be/7iwgfLdrV_E 18. Debugging Skills in Microservices https://youtu.be/M76lY2Ms4ZY 19. What is Eureka https://youtu.be/DqW2xqWDv4k 20. Building the Eureka Server https://youtu.be/hKQY2QYoqU8 21. Create Eureka Clients https://youtu.be/HpvnC2w4nis 22. Publish Clients to the Eureka Server https://youtu.be/kpWePQl9bYY 23. Consuming Microservices https://youtu.be/8ZTJmTIiVTs 24. Summary and Thank You https://youtu.be/roZmOZy-bLw Subscribe Kindson The Tech Pro Youtube: https://bit.ly/2PpJd8Q Join my group ICS on Facebook: https://bit.ly/2UULiQj Follow me on Instagram - https://www.instagram.com/kindsonm/ Connect with me on LinkedIn: https://www.linkedin.com/in/kindson/ Follow me on Twitter: https://twitter.com/KindsonM Learn about me: http://www.kindsonthegenius.com Microservices Tutorial Step by Step Procedure here: https://bit.ly/2I6gCDD Microservices Tutorial Source Codes here: https://bit.ly/2It8fkp How to Build Microservices in Java Using Spring Tool Suite and Eureka