3

Why Microservices Should use Event Sourcing

 2 years ago
source link: https://blog.bitsrc.io/why-microservices-should-use-event-sourcing-9755a54ebfb4
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Why Microservices Should use Event Sourcing

6 Reasons where you need to use Event Sourcing in Micro-services

1*ea0SaTnK1Zp-sxk032ZO-Q.jpeg

Microservices play a vital role when migrating from monolithic applications. They help improve applications’ scalability, manageability, agility, or speed of delivery.

However, there are some challenges in using microservices, such as state handling. As developers, it is indispensable for us to know how to overcome these issues to get the maximum out of microservices.

Using event sourcing is a great solution for most of these issues. So, in this article, I will discuss why we should use event sourcing with microservices and the advantages of using it.

What is Event Sourcing

Event Sourcing is an alternative way to persist data. Compared to other state persistence methods like state-oriented persistence, event sourcing stores all the state mutations as a separate record called an event.

For example, when a customer puts items to a cart in an online buying platform, event sourcing saves all the state changes in the cart as a list of state-changing events. Whenever the state of an entity changes, a new event is appended to the list of events.

1. Smooth Transition from Domain-Driven Design

When it comes to the designing stage of a project, developers and designers analyze the domain and do event storming/event modeling. The objective here is to identify the project from a pure design standpoint.

However, a part of the conversation is highly event-oriented. So the outcome of the effort is microservices that developers will build. Therefore, the commands and events go into these different microservices.

Event sourcing comes in the stage of implementation. First, the code and the system’s data flow are implemented using event sourcing as the design has been done with the event mindset.

2. Reducing Coupling

Assume a scenario where two microservices loosely couple. If a design, implementation, or behavior change happens in one microservice, it won’t cause another change.

1*TGgCI6xrjj0cHjp0Ogq0gg.png

However, when it comes to microservices, the coupling can happen. For example, if a change is made to a microservice, it will cause all other microservices to collaborate with the first one directly or indirectly.

3. Optimizes the Read vs. Write performance Bottleneck

Imagine a typical system with many operations that are probably built over the years and managed by a single database. However, the database can be optimized for reads and writes when performing CRUD operations.

But it’s always a trade-off. If you optimize for reads, it comes with the expense of writes. If you optimize for writes, it comes with expensive reads. So there are obvious, significant trade-offs that are going on.

However, event sourcing solves the problem. When it comes to event sourcing, it uses segregated persistence. That is to say, we have got the event store that is inserting events. All the CRUD operations become just storage and events. All those events aggregate to this current operation state that developers need to execute.

The real advantage of the event store here is the expense of the user, and the bottleneck of read-write operations will decrease.

4. Elevate the concurrency barrier

There is a greater probability of getting a traffic spike in large enterprise applications. Such scenarios can affect the application database and solving them can be painful.

Yes, these issues can be solved by spending three or four weeks. But, you can still run into a situation where the database is just pushed to its limit.

1*WdiYLsKTwz6dTkzWpTeP-w.png

This is another reason why event sourcing is increasingly becoming a popular approach. Event sourcing handles this issue by writing and inserting records in the spike into the event log and not letting the read-side wait.

5. Simplify and harden messaging

The semantics of messaging in microservices can be broken down into three categories.

  • In most once — Cases where a message might not get delivered, and you will lose messages under certain circumstances. When thinking about the microservice we build, this is not an acceptable feature.
  • At least once — The category is the easiest to be implemented. In this case, a message can be delivered at least once. On the other hand, a certain message can be delivered more than twice.
  • Exactly once — A reasonably mythical method and hard to implement but still doable from the consumer perspective.
1*AWrb1e8LBKuWLu0in5oLvg.png

There are some scenarios where messages do not deliver the first time. So the solution will be implementing a retry logic.

When developers implement a retry logic, they have to start thinking about cases like producer services trying to publish data. It has to be capable of being in that mode where some messages haven’t been delivered.

Also, cases like the service go down. And when that service comes back, can it pick up where it left off and continue to retry sending those messages? Such situations bring much complexity to the producer.

1*96RGwqE8CXZIPSi7yNTIsw.png

Also, imagine a scenario where a service does a database transaction first, and then it makes a call to Kafka. Since these are two separate transactions, first the database transaction will occur. However, the message in Kafka will not pass. So that’s a leak in the pipe, and the consumers have a potential vulnerability. When the service goes down during that exposure period, the messages will be lost.

When using event sourcing, developers do not need to worry about failing messages. So here, the messages aren’t pushed to Kafka consumers. Kafka consumers pull, and it is easier to implement this pull pattern.

6. Eliminate service coupling

Imagine a situation where we have customer service helping a few other services. If the customer service goes down, additional services will also go down.

1*-CGkGUtUwP-C9b01UyC4rg.png

When using event sourcing, the approach is that the customer publishes and the other services consume. In such a case, if the customer service goes out, there is no harm until it gets back.

Conclusion

Microservices architecture is one of the commonly used architectures in backend development. However, it is crucial to know how to use it in a much optimized and hassle-free manner.

In this article, I have explained 06 reasons why you should use event soucing with microservice to avoid common pitfalls.

I hope this article will help you develop microservices in a much more optimized way. Thank you for reading.

Build composable web applications

Don’t build web monoliths. Use Bit to create and compose decoupled software components — in your favorite frameworks like React or Node. Build scalable and modular applications with a powerful and enjoyable dev experience.

Bring your team to Bit Cloud to host and collaborate on components together, and speed up, scale, and standardize development as a team. Try composable frontends with a Design System or Micro Frontends, or explore the composable backend with serverside components.

Give it a try →

0*-bOLd_JKeI1L-87M.gif

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK