DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • How To Get Closer to Consistency in Microservice Architecture
  • Spring Microservice Tip: Abstracting the Database Hostname With Environment Variable
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 1: Concepts

Trending

  • When Production Stops Moving: Running Claude Code Across a Distributed Enterprise Integration Team
  • Treating Web Scrapers Like Production Services
  • Every Acquisition Promises One Company. Most Deliver Two.
  • Microsoft Fabric Throttling: Diagnose Before You Scale

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Three Important Patterns for Building Microservices

Three Important Patterns for Building Microservices

We look into how event sourcing, sagas, and CQRS patterns effect the development of microservices.

By 
Saurabh Dashora
DZone Core CORE ·
Jun. 03, 19 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
41.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

The microservices architectural style is gaining a significant amount of popularity in the industry right now. More and more organizations want to move to a microservices architecture.

However, building microservices is not easy. 

In this post, we will look at three important patterns that can help you create microservices.

Event Sourcing

Event sourcing tries to solve the problem around atomically updating the database and also publishing an event.

What this means is that when you do something on a Domain Entity, it can be treated like a Domain Event. And the state of the Domain Entity is stored as a sequence of these Domain Events. In other words, a new event is created whenever a new record is inserted or something is updated on an existing record. An event store keeps track of all the events occuring on the entity.

Now, let's assume the following transactions occur on a particular account:

  • Creation of the account with an initial balance of $100 USD.
  • Account set to ACTIVE status.
  • A friend paid some money back for sharing a meal. Deposit of $55 USD in the account.
  • You bought ice cream for your significant other. Withdrawal of $20 USD.
  • Your significant other didn't like the ice cream and so you had to buy another one. Withdrawal of $50 USD.
  • You forgot to pay the rent. Your landlord imposed a penalty. Withdrawal of $100 USD.
  • Account set to HOLD status because of a negative balance.
  • Deposit $100 USD to the account. Breath a sigh of relief.
  • Account set to ACTIVE status.

Below is how it will be stored in an event sourcing way:

Aggregate_Id Event Event Payload
ABCD567823 AccountActivatedEvent {…}
ABCD567823 AccountBalanceUpdatedEvent {…}
ABCD567823 AccountHeldEvent {…}
ABCD567823 AccountBalanceUpdatedEvent {…}
ABCD567823 AccountBalanceUpdatedEvent {…}
ABCD567823 AccountBalanceUpdatedEvent {…}
ABCD567823 AccountBalanceUpdatedEvent {…}
ABCD567823 AccountActivatedEvent {…}
ABCD567823 AccountBalanceUpdatedEvent {…}
ABCD567823

AccountCreatedEvent    

Whenever there is a request for the current state of the domain object, the events are replayed and the state is constructed.

Event Sourcing implementation can be done using a combination of standard Java frameworks such as Spring Boot and Axon.

CQRS 

CQRS stands for Command-Query Responsibility Segregation. Often, CQRS is implemented in conjunction with Event Sourcing.

The main use of CQRS is to get around the problem arising from API Composition.

In a typical Event Sourcing and CQRS setup, a CQRS application listens to domain events from multiple applications. Using these updates, it creates and maintains a query database. Depending on the business case, this query database can aggregate complex queries and serve them whenever a request for such queries comes up in the application.

Saga Pattern

Saga Pattern is a direct solution to implementing distributed transactions in a microservices architecture. 

Typically, in a microservice based application, each microservice will have its own database. However, certain business processes will require communication between multiple microservices. That's where Saga Pattern comes in. A typical Saga implementation can be seen as a sequence of local transactions where each transaction does a small part of the overall work of the Saga. In other words, a Saga Pattern is pretty much a necessity in a microservices architecture.

Let's see a simple example in a typical food delivery app flow.

When a user places an order, the sequence of actions that could happen are:

  • The food ordering service creates an order. At this point, the order is in a PENDING state. A saga manages the chain of events.
  • The saga contacts the restaurant via the restaurant service.
  • The restaurant service attempts to place the order with the chosen restaurant. After getting a confirmation, it sends back a reply.
  • The saga receives the reply. And, depending on the reply, it can approve the order or reject the order.
  • The food order service then changes the state of the order. If the order was approved, it would inform the customer and provide any necessary details. If rejected, it will also inform the customer with an apology message.

Sagas can also be of different types such as choreography-based or orchestration-based. An orchestration-based Saga can be implemented by using the Spring Boot and Axon frameworks.

microservice Database Event Spring Framework

Opinions expressed by DZone contributors are their own.

Related

  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • How To Get Closer to Consistency in Microservice Architecture
  • Spring Microservice Tip: Abstracting the Database Hostname With Environment Variable
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 1: Concepts

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook