Skip to content

Commit

Permalink
feat: Idempotent Consumer Microservice Pattern #2683 (#3082)
Browse files Browse the repository at this point in the history
* added idempotent consumer pattern

* updated doc

* fixed bug in RequestService

* add test converage

* Add test converage to state machine

* renamed module and added code example
  • Loading branch information
dmgodoy authored Nov 11, 2024
1 parent 25bb77e commit 2a2c560
Show file tree
Hide file tree
Showing 13 changed files with 930 additions and 0 deletions.
161 changes: 161 additions & 0 deletions microservices-idempotent-consumer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
---
title: "Idempotent Consumer Pattern in Java: Ensuring Reliable Message Processing"
shortTitle: Idempotent Consumer
description: "Learn about the Idempotent Consumer pattern in Java. Discover how it ensures reliable and consistent message processing, even in cases of duplicate messages."
category: Messaging
language: en
tag:
- Messaging
- Fault tolerance
- Event-driven
- Reliability
---

## Also known as

* Idempotency Pattern

## Intent of Idempotent Consumer Pattern

The Idempotent Consumer pattern is used to handle duplicate messages in distributed systems, ensuring that multiple processing of the same message does not cause undesired side effects. This pattern guarantees that the same message can be processed repeatedly with the same outcome, which is critical in ensuring reliable communication and data consistency in systems where message duplicates are possible.

## Detailed Explanation of Idempotent Consumer Pattern with Real-World Examples

### Real-world Example

> In a payment processing system, ensuring that payment messages are idempotent prevents duplicate transactions. For example, if a user’s payment message is accidentally processed twice, the system should recognize the second message as a duplicate and prevent it from executing a second time. By storing unique identifiers for each processed message, such as a transaction ID, the system can skip any duplicate messages. This ensures that a user is not charged twice for the same transaction, maintaining system integrity and customer satisfaction.
### In Plain Words

> The Idempotent Consumer pattern prevents duplicate messages from causing unintended side effects by ensuring that processing the same message multiple times results in the same outcome. This makes message processing safe in distributed systems where duplicates may occur.
### Wikipedia says

> In computing, idempotence is the property of certain operations in mathematics and computer science whereby they can be applied multiple times without changing the result beyond the initial application.
## When to Use the Idempotent Consumer Pattern

The Idempotent Consumer pattern is particularly useful in scenarios:

* When messages can be duplicated due to network retries or communication issues.
* In distributed systems where message ordering is not guaranteed, making deduplication necessary to avoid repeated processing.
* In financial or critical systems, where duplicate processing would have significant side effects.

## Real-World Applications of Idempotent Consumer Pattern

* Payment processing systems that avoid duplicate transactions.
* E-commerce systems to prevent multiple entries of the same order.
* Inventory management systems to prevent multiple entries when updating stock levels.

## Programmatic example of Idempotent Consumer Pattern
In this Java example, we have an idempotent service that offers functionality to create and update (start, complete, etc.) orders. The service ensures that the **create order** operation is idempotent, meaning that performing it multiple times with the same order ID will lead to the same result without creating duplicates. For state transitions (such as starting or completing an order), the service enforces valid state changes and throws exceptions if an invalid transition is attempted. The state machine governs the valid order status transitions, ensuring that statuses progress in a defined and consistent sequence.
### RequestService - Managing Idempotent Order Operations
The `RequestService` class is responsible for handling the creation and state transitions of orders. The `create` method is designed to be idempotent, ensuring that it either returns an existing order or creates a new one without any side effects if invoked multiple times with the same order ID.
```java
public class RequestService {
// Idempotent: ensures that the same request is returned if it already exists
public Request create(UUID uuid) {
Optional<Request> optReq = requestRepository.findById(uuid);
if (!optReq.isEmpty()) {
return optReq.get(); // Return existing request
}
return requestRepository.save(new Request(uuid)); // Save and return new request
}

public Request start(UUID uuid) {
Optional<Request> optReq = requestRepository.findById(uuid);
if (optReq.isEmpty()) {
throw new RequestNotFoundException(uuid);
}
return requestRepository.save(requestStateMachine.next(optReq.get(), Request.Status.STARTED));
}

public Request complete(UUID uuid) {
Optional<Request> optReq = requestRepository.findById(uuid);
if (optReq.isEmpty()) {
throw new RequestNotFoundException(uuid);
}
return requestRepository.save(requestStateMachine.next(optReq.get(), Request.Status.COMPLETED));
}
}
```
### RequestStateMachine - Managing Order Transitions
The `RequestStateMachine` ensures that state transitions occur in a valid order. It handles the progression of an order's status, ensuring the correct sequence (e.g., from `PENDING` to `STARTED` to `COMPLETED`).
```java
public class RequestStateMachine {

public Request next(Request req, Request.Status nextStatus) {
String transitionStr = String.format("Transition: %s -> %s", req.getStatus(), nextStatus);
switch (nextStatus) {
case PENDING -> throw new InvalidNextStateException(transitionStr);
case STARTED -> {
if (Request.Status.PENDING.equals(req.getStatus())) {
return new Request(req.getUuid(), Request.Status.STARTED); // Valid transition
}
throw new InvalidNextStateException(transitionStr); // Invalid transition
}
case COMPLETED -> {
if (Request.Status.STARTED.equals(req.getStatus())) {
return new Request(req.getUuid(), Request.Status.COMPLETED); // Valid transition
}
throw new InvalidNextStateException(transitionStr); // Invalid transition
}
default -> throw new InvalidNextStateException(transitionStr); // Invalid status
}
}
}
```
### Main Application - Running the Idempotent Consumer Example

In the main application, we demonstrate how the `RequestService` can be used to perform idempotent operations. Whether the order creation or state transition is invoked once or multiple times, the result is consistent and does not produce unexpected side effects.

```java
Request req = requestService.create(UUID.randomUUID());
// Try creating the same Request again with the same UUID (idempotent operation)
requestService.create(req.getUuid());
// Again, try creating the same Request (idempotent operation, no new Request should be created)
requestService.create(req.getUuid());
LOGGER.info("Nb of requests : {}", requestRepository.count()); // 1, processRequest is idempotent
// Attempt to start the Request (the first valid transition)
req = requestService.start(req.getUuid());
// Try to start the Request again, which should throw an exception since it's already started
try {
req = requestService.start(req.getUuid());
} catch (InvalidNextStateException ex) {
// Log an error message when trying to start a request twice
LOGGER.error("Cannot start request twice!");
}
// Complete the Request (valid transition from STARTED to COMPLETED)
req = requestService.complete(req.getUuid());
// Log the final status of the Request to confirm it's been completed
LOGGER.info("Request: {}", req);
```
Program output:
```
19:01:54.382 INFO [main] com.iluwatar.idempotentconsumer.App : Nb of requests : 1
19:01:54.395 ERROR [main] com.iluwatar.idempotentconsumer.App : Cannot start request twice!
19:01:54.399 INFO [main] com.iluwatar.idempotentconsumer.App : Request: Request(uuid=2d5521ef-6b6b-4003-9ade-81e381fe9a63, status=COMPLETED)
```
## Benefits and Trade-offs of the Idempotent Consumer Pattern

### Benefits

* **Reliability**: Ensures that messages can be processed without unwanted side effects from duplicates.
* **Consistency**: Maintains data integrity by ensuring that duplicate messages do not cause redundant updates or actions.
* **Fault Tolerance**: Handles message retries gracefully, preventing them from causing errors.

### Trade-offs

* **State Management**: Requires storing processed message IDs, which can add memory overhead.
* **Complexity**: Implementing deduplication mechanisms can increase the complexity of the system.
* **Scalability**: In high-throughput systems, maintaining a large set of processed messages can impact performance and resource usage.

## Related Patterns in Java

* [Retry Pattern](https://java-design-patterns.com/patterns/retry/): Works well with the Idempotent Consumer pattern to handle failed messages.
* [Circuit Breaker Pattern](https://java-design-patterns.com/patterns/circuitbreaker/): Often used alongside idempotent consumers to prevent repeated failures from causing overload.

## References and Credits

* [Enterprise Integration Patterns: Designing, Building, and Deploying Messaging Solutions](https://amzn.to/4dznP2Y)
* [Designing Data-Intensive Applications](https://amzn.to/3UADv7Q)
126 changes: 126 additions & 0 deletions microservices-idempotent-consumer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
The MIT License
Copyright © 2014-2022 Ilkka Seppälä
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.26.0-SNAPSHOT</version>
</parent>

<artifactId>microservices-idempotent-consumer</artifactId>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<type>pom</type>
<version>3.2.3</version>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.4.4.Final</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring Boot Dependencies -->

<!-- Spring Boot Data JPA for database access -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- Testing Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- JUnit Jupiter Engine for testing -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<!-- Mockito for mocking in tests -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<!-- H2 for mocking database -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

</dependencies>


<build>
<plugins>
<!-- Maven Assembly Plugin for creating a single distributable JAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- Define the main class for the executable JAR -->
<mainClass>com.iluwatar.idempotentconsumer.App</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>

<!-- Other plugins as needed -->

</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.idempotentconsumer;

import java.util.UUID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/**
* The main entry point for the idempotent-consumer application.
* This application demonstrates the use of the Idempotent Consumer
* pattern which ensures that a message is processed exactly once
* in scenarios where the same message can be delivered multiple times.
*
* @see <a href="https://en.wikipedia.org/wiki/Idempotence">Idempotence (Wikipedia)</a>
* @see <a href="https://camel.apache.org/components/latest/eips/idempotentConsumer-eip.html">Idempotent Consumer Pattern (Apache Camel)</a>
*/
@SpringBootApplication
@Slf4j
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
/**
* The starting point of the CommandLineRunner
* where the main program is run.
*
* @param requestService idempotent request service
* @param requestRepository request jpa repository
*/
@Bean
public CommandLineRunner run(RequestService requestService, RequestRepository requestRepository) {
return args -> {
Request req = requestService.create(UUID.randomUUID());
requestService.create(req.getUuid());
requestService.create(req.getUuid());
LOGGER.info("Nb of requests : {}", requestRepository.count()); // 1, processRequest is idempotent
req = requestService.start(req.getUuid());
try {
req = requestService.start(req.getUuid());
} catch (InvalidNextStateException ex) {
LOGGER.error("Cannot start request twice!");
}
req = requestService.complete(req.getUuid());
LOGGER.info("Request: {}", req);
};
}
}

Loading

0 comments on commit 2a2c560

Please sign in to comment.