跳到主要内容

Approaches to Rolling Back or Canceling Transactions in Microservice

· 阅读需 7 分钟

Handling rollback or cancellation of transactions in a Microservice Architecture when multiple writes are involved is a complex process because each microservice typically has its own database, and there is no central point of coordination like in monolithic applications. Traditional database transactions (ACID) are difficult to apply in microservices, especially when services span across multiple databases, message brokers, and external APIs. Therefore, rollback in microservices needs a more sophisticated approach.

Key Approaches to Rolling Back or Canceling Transactions in Microservice Architecture​

There are several strategies to handle rollback or cancellation in microservices when multiple services and writes are involved. These include:


1. Saga Pattern​

The Saga Pattern is the most popular strategy for handling distributed transactions in microservices. It breaks a large transaction into a sequence of smaller, independent transactions, each handled by a microservice. If any part of the sequence fails, compensating actions (i.e., rollbacks) are triggered to undo the work done by previous steps.

There are two common approaches to implementing the Saga Pattern:

  • Choreography-based Saga
  • Orchestration-based Saga

a. Choreography-based Saga​

In a Choreography-based Saga, each service involved in the transaction emits an event upon completion of its task. Other services listen to these events and take action accordingly. If an error occurs, the service responsible for the failed transaction emits a failure event, and other services listening for that failure event perform compensating actions.

Example: Imagine an e-commerce system where you place an order that spans multiple microservices:

  1. Order Service: Creates the order.
  2. Inventory Service: Reserves the product.
  3. Payment Service: Charges the customer.
  4. Shipping Service: Ships the product.

If the Payment Service fails after reserving the inventory, it can emit a failure event, and the Inventory Service would listen to this and release the reserved product (compensating action).

Advantages:

  • Fully decentralized and scalable.
  • Easy to add new services without affecting others.

Challenges:

  • Harder to track and manage because there is no central coordinator.
  • More complex error handling logic in each service.

b. Orchestration-based Saga​

In an Orchestration-based Saga, a central coordinator (Orchestrator) is responsible for managing the workflow of the transaction. The Orchestrator sends commands to each service to execute a step in the transaction and handles failures by issuing compensating commands if any service fails.

Example:

  • The Orchestrator sends a command to the Order Service to create an order.
  • After receiving success, the Orchestrator asks the Inventory Service to reserve the product.
  • If the Payment Service fails, the Orchestrator issues a compensating command to the Inventory Service to release the product.

Advantages:

  • Easier to implement because the Orchestrator handles the entire workflow.
  • Centralized error handling and compensations.

Challenges:

  • The Orchestrator can become a single point of failure or bottleneck.
  • Needs to manage state and retries, making it potentially complex.

2. Compensation Mechanism​

The Compensation Mechanism is the core of both the Saga Pattern and other eventual consistency models. A compensation transaction is essentially the "undo" operation that reverses the effects of a previously completed step in case of failure.

How It Works: Each microservice is responsible for defining and implementing compensating actions for its operations. For example:

  • If the Inventory Service reserves stock but the Payment Service fails, the Inventory Service must implement logic to release the stock.
  • If the Payment Service successfully charges a customer but the Shipping Service fails, the Payment Service must issue a refund.

Key Considerations:

  • Compensation is not always straightforward: Rolling back a transaction isn't always as simple as undoing a change. Some operations might be irreversible (e.g., shipping a product), requiring compensating actions that mitigate the issue (e.g., issuing a refund).
  • State-driven compensation: Services must maintain state information to understand which transactions require compensation.

3. Two-Phase Commit (2PC)​

Two-Phase Commit (2PC) is a protocol used for coordinating distributed transactions across multiple systems. It's traditionally used in tightly coupled distributed systems but is less popular in microservice architectures due to performance and complexity concerns.

How 2PC Works:​

  1. Prepare Phase: The transaction coordinator asks each microservice if they are ready to commit the transaction. The services reply with "yes" or "no."
  2. Commit/Rollback Phase: If all services reply with "yes," the coordinator instructs them to commit. If any service replies with "no," the coordinator sends a rollback command to all participating services.

Pros:

  • Strong consistency guarantees.
  • Ensures atomicity across distributed systems.

Cons:

  • Performance overhead: 2PC introduces a blocking wait time for services to prepare and commit, which can hurt performance in a microservices architecture.
  • Risk of blocking: If one service crashes, it can leave other services waiting indefinitely, potentially causing system-wide issues.

Given these challenges, 2PC is rarely used in modern, large-scale microservice architectures.


4. Eventual Consistency​

In distributed microservice architectures, eventual consistency is often the goal rather than strong consistency (ACID). Eventual consistency ensures that after some time, all participants in a distributed transaction will reach a consistent state, even if there are temporary inconsistencies.

How to Handle Rollbacks with Eventual Consistency:​

  • Delayed reconciliation: Some systems may use periodic reconciliation processes to detect inconsistencies and trigger compensating actions to ensure data integrity.
  • State-based updates: Services track their state, and if a failure is detected, each service is responsible for ensuring consistency through compensation.

Example: If a Payment Service charges a customer but the Shipping Service fails to ship the item, the system might temporarily be in an inconsistent state (payment made, no shipment). Over time, a reconciliation job or event listener detects the issue and initiates a refund to bring the system back to consistency.

Pros:

  • High scalability and resilience.
  • Tolerates temporary inconsistencies while ensuring long-term data integrity.

Cons:

  • Complex to reason about, as temporary inconsistencies can confuse users and developers.
  • Some actions (e.g., shipping a product) may be difficult to compensate.

5. Idempotency and Retries​

In microservice architectures, idempotency and retries play a critical role in handling rollbacks and retries, ensuring that repeated operations do not result in unwanted side effects.

Idempotency:​

An operation is idempotent if performing it multiple times has the same result as performing it once. For example, if a Payment Service is asked to process a payment twice, it should charge the customer only once.

Why It Matters:

  • When rolling back a transaction, services may need to retry operations (e.g., issuing a refund). These retries must be idempotent to prevent issues like charging a customer multiple times or reserving inventory repeatedly.
  • Retry policies are often implemented in communication mechanisms (e.g., message brokers) to ensure reliability.

Best Practices for Idempotency:​

  • Use unique transaction IDs for requests so that services can detect duplicate requests.
  • Design APIs and actions to ensure they are safe for retries.

6. Conclusion​

Rolling back or canceling transactions in a Microservice Architecture requires careful design to handle distributed data, asynchronous operations, and eventual consistency. The Saga Pattern (both choreography and orchestration) is the most widely used approach, allowing each service to independently handle parts of the transaction and apply compensating actions in case of failure. Other techniques like compensation mechanisms, eventual consistency, and idempotency ensure that microservices can maintain integrity, even across complex, distributed environments.

When choosing a rollback strategy, it's essential to balance the need for consistency with the performance and scalability requirements of the system.

Considerations on Transactions Across Distributed Systems

· 阅读需 7 分钟

Online Systems: Defining Transactions, Database Transaction Management, and Lock Management​

When working with complex online systems (e.g., e-commerce platforms, distributed systems, microservices architecture), transactions span beyond a single database. This requires a broader understanding of transaction management, involving distributed transactions, data consistency across systems, and sophisticated lock management techniques. Let's break this down with a focus on the key topics: defining transactions in online systems, transaction management (beyond the database), and lock management in such environments.


1. Defining Transactions in Online Systems​

In an online system, a transaction is a sequence of operations performed as a single logical unit, ensuring consistency, correctness, and isolation across different subsystems. These operations could involve:

  • Database operations (e.g., updating inventory, placing an order)
  • Third-party API calls (e.g., payment gateways, external services)
  • Message queue handling (e.g., Kafka, RabbitMQ for event-driven architecture)
  • Microservices interaction (e.g., coordination between an inventory service and an order service)

In simple terms, a transaction in an online system is any series of related actions across multiple components that must either complete successfully together or fail together to maintain a consistent system state.

Key Transaction Properties: ACID vs BASE​

In large-scale systems, transactions must be designed for both strong consistency (ACID) or eventual consistency (BASE), depending on the use case.

  1. ACID (Atomicity, Consistency, Isolation, Durability):

    • Guarantees strong consistency.
    • Used when all subsystems must maintain a single consistent state.
    • Common in single database transactions or within tightly-coupled services.
  2. BASE (Basically Available, Soft-state, Eventually consistent):

    • Sacrifices immediate consistency for availability and scalability.
    • Often used in distributed systems where components may not always be in sync (e.g., eventual consistency in NoSQL databases).

In online systems, distributed transactions often require a mix of ACID and BASE properties, depending on the criticality of the operation.


2. Transaction Management in Complex Online Systems​

Transaction management in online systems can no longer rely solely on single-database transaction managers. These systems often require distributed transaction management. Some of the most critical techniques include:

a. Distributed Transactions (Two-Phase Commit)​

When a transaction spans across multiple services or databases, a Two-Phase Commit (2PC) protocol is commonly used to ensure atomicity.

  • Phase 1 - Prepare: All participating systems are asked to prepare (lock resources and confirm they can commit the transaction).
  • Phase 2 - Commit/Rollback: If all participants agree, the transaction is committed. If any participant fails, the entire transaction is rolled back.

Example:

  • An order is placed in an online store:
    • The Order Service inserts an order into the database.
    • The Payment Service processes the payment.
    • The Inventory Service updates the stock.

All three operations must be successful for the order to be confirmed. The 2PC ensures that if any step fails, all previous operations are rolled back.

However, 2PC can be slow and does not scale well in high-throughput environments due to blocking resources during the "prepare" phase.

b. Saga Pattern​

The Saga Pattern is an alternative to 2PC, often used in microservices architectures where long-lived transactions are undesirable. Instead of blocking resources, the Saga breaks a distributed transaction into a series of smaller transactions.

  • Each service involved in the transaction performs its work independently.
  • If one step fails, the previous steps are compensated by executing defined rollback actions.

Example:

  1. Order Service places an order.
  2. Payment Service charges the customer.
  3. Inventory Service updates the stock.
  4. If the Inventory Service fails, a compensating transaction refunds the payment and cancels the order.

Sagas offer better scalability compared to 2PC and are particularly suited for event-driven architectures where each microservice manages its own transaction.

c. Eventual Consistency and Compensation​

In some distributed systems, strict ACID properties are relaxed for eventual consistency, especially in BASE systems (e.g., NoSQL databases, cloud-native architectures). Instead of locking resources, systems rely on compensation logic:

  • If a downstream service fails or an external API does not respond, the system might retry the operation or initiate compensating actions to restore consistency.

Eventual Consistency Example:

  • In a global payment system, payments made across multiple time zones and currencies may not reflect immediately but will eventually be reconciled to ensure data consistency.

d. Message-Driven Transactions​

In event-driven systems, message queues (e.g., Kafka, RabbitMQ) help decouple services, allowing them to handle transactions asynchronously. Here, services:

  • Emit events for actions like "Order Placed" or "Payment Processed".
  • Listen for events from other services to trigger corresponding actions (e.g., update inventory when an order is placed).

By designing for idempotency (ensuring repeated operations have the same effect as a single operation), these systems can handle eventual consistency and retries gracefully.


3. Lock Management​

In online systems, lock management becomes complex due to multiple participants, different types of data stores, and network latencies. Locking is crucial to prevent data corruption or race conditions during concurrent transactions.

a. Optimistic vs Pessimistic Locking​

  • Pessimistic Locking: A resource is locked for the entire duration of the transaction, preventing any other transaction from accessing it.

    • Used when contention for the resource is high or when updates must be serialized (e.g., bank account transfers).
    • Example: A row in the Inventory table is locked while updating stock.
    SELECT * FROM Products WHERE product_id = 1 FOR UPDATE;
  • Optimistic Locking: Assumes conflicts are rare and does not lock the resource. Instead, it checks for conflicts only at the end of the transaction.

    • Version columns or timestamps are used to detect if another transaction modified the same data. If so, the transaction fails, and a retry is triggered.
    • Example: Update an order only if no other transaction has modified it.
    UPDATE Orders
    SET status = 'SHIPPED'
    WHERE order_id = 101 AND version = 1;

b. Distributed Locks​

In distributed systems, transactions may need to span multiple nodes or databases. To handle this, distributed locks ensure that shared resources are not modified concurrently by different parts of the system.

  1. Redis-Based Locks: A simple solution is to use Redis to acquire distributed locks.

    Example:
    When updating inventory, each microservice tries to acquire a lock for the product:

    SET product_lock:1 my_unique_id NX PX 10000
    • NX ensures the lock is acquired only if it doesn't exist.
    • PX 10000 ensures the lock expires after 10 seconds if not released.
  2. Zookeeper or Etcd: More robust systems like Zookeeper or Etcd can handle distributed coordination, offering leader election, mutexes, and barriers.

    Example: Distributed systems can use Zookeeper to ensure that only one instance of a service performs a critical operation (e.g., process payments in a sharded environment).

c. Deadlock Detection and Prevention​

In systems with complex transactions and lock dependencies, deadlocks can occur, where two transactions are waiting for resources locked by each other.

  • Deadlock Detection: The system detects circular dependencies and aborts one of the transactions to resolve the deadlock.

    Example:

    SET innodb_lock_wait_timeout = 50;
  • Deadlock Prevention: In systems like databases or distributed systems, adopting strategies like acquiring locks in a consistent order can prevent deadlocks.


Conclusion​

In an online system, transaction management and lock management are more complex than in a simple intranet application. The system must account for distributed transactions across multiple services and databases, often relying on protocols like 2PC, Sagas, or message-driven event handling to ensure consistency and reliability. Locks, whether in-database or distributed, are critical for managing concurrency and ensuring data integrity but must be handled with care to avoid performance bottlenecks or deadlocks.

In essence, designing transactions in a modern online system requires a mix of strategies from distributed transaction management, eventual consistency models, and advanced locking mechanisms to maintain data reliability, availability, and scalability.

Data Integrity and Consistency in Databases

· 阅读需 6 分钟

Data Integrity and Consistency in Databases


Introduction​

To understand data integrity and consistency, let’s break them down with an example, illustrating how they work in a transactional database environment.


Definitions​

  • Data Integrity: Refers to the accuracy and reliability of the data. Data integrity ensures that the data is correct, complete, and consistent throughout its lifecycle. It is often enforced through rules such as constraints (e.g., primary keys, foreign keys, unique constraints).

  • Consistency: In the context of transactions, consistency ensures that the database transitions from one valid state to another. This means that after every transaction, the database adheres to all rules and constraints that maintain its correctness.


Example Scenario: E-commerce System​

Imagine an e-commerce system where users place orders for products. We have two important tables:

  1. Products table: Stores information about products, including product_id, name, price, and quantity_in_stock.
  2. Orders table: Stores orders placed by customers, including order_id, product_id, quantity_ordered, and status.

Products Table​

product_idnamepricequantity_in_stock
1Laptop100010
2Smartphone80020
3Headphones15030

Orders Table​

order_idproduct_idquantity_orderedstatus
10112PENDING
10221SHIPPED

Part 1: Data Integrity​

Data Integrity ensures that the data adheres to business rules and constraints. Let's define some integrity constraints in this system:

  1. Foreign Key Constraint: Ensures that every product_id in the Orders table refers to a valid product_id in the Products table. This prevents orders from being placed for non-existent products.

    ALTER TABLE Orders
    ADD CONSTRAINT fk_product_id
    FOREIGN KEY (product_id) REFERENCES Products(product_id);

    Example Violation:
    If someone tries to insert an order with product_id = 999 (which does not exist in the Products table), the foreign key constraint would reject the transaction.

    INSERT INTO Orders (order_id, product_id, quantity_ordered, status)
    VALUES (103, 999, 1, 'PENDING');

    Result: Integrity violation. The order cannot be placed because product_id = 999 does not exist in the Products table.

  2. Check Constraint: Ensures that no negative quantity can be ordered or stored in the inventory.

    ALTER TABLE Products
    ADD CONSTRAINT chk_positive_quantity CHECK (quantity_in_stock >= 0);

    Example Violation:
    If an update tries to set quantity_in_stock to a negative value, it will be rejected.

    UPDATE Products
    SET quantity_in_stock = -5
    WHERE product_id = 1;

    Result: Integrity violation. Negative stock values are not allowed.

  3. Unique Constraint: Ensures no duplicate product_id exists in the Products table.

    ALTER TABLE Products
    ADD CONSTRAINT unique_product_id UNIQUE (product_id);

    Example Violation:
    If we try to insert another product with the same product_id:

    INSERT INTO Products (product_id, name, price, quantity_in_stock)
    VALUES (1, 'Tablet', 500, 15);

    Result: Integrity violation. Duplicate product IDs are not allowed.

These constraints enforce data integrity by ensuring that only valid data can be inserted or updated in the database.


Part 2: Data Consistency in a Transaction​

Data Consistency means that after a transaction, all defined integrity rules are maintained, and the system remains in a valid state. Let's explore this with a transactional scenario.

Transaction Scenario: Placing an Order​

When a user places an order, several things happen as part of a single transaction:

  1. The system decreases the quantity of the product in the Products table.
  2. A new order is inserted into the Orders table.
  3. If the order cannot be completed for some reason (e.g., insufficient stock), the entire transaction is rolled back.

Let’s simulate this with an example where a customer places an order for 3 laptops (product_id = 1).

  1. Initial State:

    • Products table shows 10 laptops in stock.
    • No new order is placed yet.
  2. Transaction Start:

    • Begin the transaction:

      BEGIN TRANSACTION;
  3. Check Stock:
    First, the system checks if there are enough laptops in stock.

    SELECT quantity_in_stock FROM Products WHERE product_id = 1;
    -- Result: 10
  4. Decrease Stock:
    If there is enough stock, decrease the stock by the ordered quantity.

    UPDATE Products
    SET quantity_in_stock = quantity_in_stock - 3
    WHERE product_id = 1;

    After the update, the stock of laptops is reduced to 7.

  5. Insert Order:
    Insert the new order into the Orders table.

    INSERT INTO Orders (order_id, product_id, quantity_ordered, status)
    VALUES (103, 1, 3, 'PENDING');
  6. Commit Transaction:
    If everything succeeds (no constraint violations, no failures), commit the transaction.

    COMMIT;
  7. Final State:

    • The Products table now shows 7 laptops in stock.
    • A new order has been added to the Orders table.

Data Consistency is maintained because the system follows the ACID properties. The database's state transitions from a valid state to another valid state (the stock is updated, and the order is recorded correctly).


Example of Inconsistent State Without Transactions​

Now let’s imagine what would happen if transactions were not used, leading to inconsistent data.

  1. Decrease Stock:
    The system updates the stock to 7 but fails to insert the order due to a system error.

    UPDATE Products
    SET quantity_in_stock = quantity_in_stock - 3
    WHERE product_id = 1;
    -- Success: stock reduced to 7
  2. Insert Order Fails:
    The order insertion fails (due to a network issue or other error).

    INSERT INTO Orders (order_id, product_id, quantity_ordered, status)
    VALUES (103, 1, 3, 'PENDING');
    -- Error: Failure to insert the order

Since the database was not in a transaction, the stock was reduced but the order was never recorded. This results in an inconsistent state where the stock is reduced but no corresponding order exists, violating the system’s expected behavior.


Conclusion​

  • Data Integrity ensures that only valid and accurate data can be entered or modified in the database by using constraints like foreign keys, check constraints, and unique constraints.
  • Data Consistency ensures that after a transaction, the database remains in a valid state, where all integrity rules are maintained and the system behaves as expected.

By using proper transaction management and enforcing data integrity rules, we can prevent inconsistent states and maintain the overall reliability of an online system.

ElasticSearch Index Lifecycle Policy (ILM) – Deep Dive

· 阅读需 5 分钟

Introduction to Index Lifecycle Management (ILM)​


ElasticSearch’s Index Lifecycle Management (ILM) allows you to define and automate how indices are managed throughout their lifecycle. This is critical for controlling the size, performance, and cost of your Elasticsearch cluster by ensuring data is stored in appropriate storage tiers as it ages.

An ILM policy divides an index’s lifecycle into phases, during which specific actions are applied to the data. These actions include closing, freezing, moving to different nodes, or deleting indices.


ElasticSearch ILM Phases​

1. Hot Phase​

  • The hot phase is for indexing and querying fresh data.
  • Data is stored on high-performance nodes for rapid access.
  • Indices in this phase are actively written and read.

Operations:

  • Force merge segments
  • Shrink the index
  • Roll over to a new index when the current one is too large

Command:

{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "30d"
}
}
}
}
}
}

2. Warm Phase​

  • The warm phase is for data that is no longer being actively written but still queried.
  • Data is stored on less powerful nodes.
  • The index can be optimized by reducing the number of replicas and relocating shards to nodes with less CPU and memory resources.

Operations:

  • Shrink index to fewer shards
  • Reduce the number of replicas
  • Move the index to nodes with less powerful resources

Command:

{
"policy": {
"phases": {
"warm": {
"actions": {
"allocate": {
"require": {
"box_type": "warm"
}
}
}
}
}
}
}

3. Cold Phase​

  • The cold phase is for data that is rarely accessed but needs to be retained.
  • Data is stored on the least powerful nodes, with a focus on cost-effectiveness over performance.
  • Indices can be marked as read-only.

Operations:

  • Set the index as read-only
  • Move the index to cold storage nodes
  • Freeze the index (lowers resource consumption even further)

Command:

{
"policy": {
"phases": {
"cold": {
"actions": {
"allocate": {
"require": {
"box_type": "cold"
}
},
"freeze": {}
}
}
}
}
}

4. Delete Phase​

  • The delete phase removes indices once they are no longer needed, preventing unnecessary resource usage.

Operations:

  • Delete the index after a specific time period or based on certain criteria.

Command:

{
"policy": {
"phases": {
"delete": {
"actions": {
"delete": {}
}
}
}
}
}

Creating an Index Lifecycle Policy​

To create a custom ILM policy, use the following steps:

  1. Define the policy by specifying the different phases (hot, warm, cold, delete).
  2. Apply the policy to your indices using an alias or direct index management.

Command Example:

PUT _ilm/policy/my_policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "7d"
}
}
},
"delete": {
"min_age": "30d",
"actions": {
"delete": {}
}
}
}
}
}

Real-Life Scenarios of ILM​

Scenario 1: Logs Retention Policy​

In a logging system (e.g., centralized logging with ElasticSearch), the hot phase may consist of daily log ingestion, followed by moving the logs to the warm phase after 30 days. Finally, after 90 days, the logs are deleted.

Policy Example:

PUT _ilm/policy/log_retention_policy
{
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_size": "50gb",
"max_age": "1d"
}
}
},
"warm": {
"min_age": "30d",
"actions": {
"allocate": {
"include": {
"box_type": "warm"
}
},
"shrink": {
"number_of_shards": 1
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {}
}
}
}
}
}

Scenario 2: Time-Based Data Management​

For applications dealing with time-based data (e.g., financial transactions or event logs), ILM helps archive data efficiently. You might retain recent data in the hot phase for instant querying, move older data to warm nodes, and eventually store data in cold nodes before deleting it.


ILM Best Practices​

  1. Define Clear Retention Policies: Tailor lifecycle policies to the nature of your data.
  2. Monitor Resource Usage: Ensure you have enough storage in warm and cold nodes.
  3. Test Policy Changes: Roll out new policies incrementally to avoid production outages.
  4. Leverage Aliases: Use index aliases to manage indices during rollovers without affecting search queries.

Useful Commands and Operations​

  • Create a policy:
    PUT _ilm/policy/my_policy
  • Get details of an existing policy:
    GET _ilm/policy/my_policy
  • Delete a policy:
    DELETE _ilm/policy/my_policy
  • Apply a policy to an index:
    PUT /my_index/_settings
    {
    "index.lifecycle.name": "my_policy"
    }

Conclusion​

ElasticSearch's Index Lifecycle Management (ILM) is a powerful tool that helps organizations manage their indices' lifecycle by automating transitions between different storage phases (hot, warm, cold, and delete). By optimizing storage and performance, ILM policies improve the overall cost-effectiveness and scalability of your ElasticSearch cluster. Proper use of ILM ensures your data remains accessible and performant while controlling operational costs effectively.

Reactive Programming in Spring WebFlux Series - I

· 阅读需 4 分钟

Overview and Fundamentals

What is Reactive Programming?​

Reactive programming is a programming paradigm oriented around data flows and the propagation of changes. Unlike traditional imperative programming, reactive programming allows you to declare data dependencies, and changes are automatically propagated when the data source changes. The core idea of reactive programming involves asynchronous data stream processing and non-blocking operations, making applications more efficient in utilizing system resources.


Why Choose Spring WebFlux?​

Spring WebFlux is an asynchronous, non-blocking web framework introduced in Spring 5 that handles non-blocking asynchronous requests. Unlike traditional Spring MVC, Spring WebFlux is built on the Reactive Streams specification and supports a fully non-blocking reactive programming model. Some key reasons to choose Spring WebFlux include:

  • Non-blocking I/O: Allows handling of more concurrent requests, improving system throughput and response times.
  • Reactive Programming Model: Offers reactive programming support based on Project Reactor, making the code more concise and maintainable.
  • Flexible Programming Model: Supports both annotation-driven and functional programming models, catering to different developer needs.
  • High Scalability: Ideal for microservices and distributed systems, making it easy to scale and integrate.

Core Concepts and Components​

1. Reactive Streams​

Reactive Streams is the foundation of reactive programming, defining the standards for asynchronous stream processing. It consists of four core interfaces:

  • Publisher: Responsible for producing data.
  • Subscriber: Consumes data.
  • Subscription: Manages the relationship between publishers and subscribers.
  • Processor: Acts as both a publisher and a subscriber.

2. Project Reactor​

Project Reactor is the underlying implementation for Spring WebFlux, providing support for reactive streams. It has two main types:

  • Mono: Represents an asynchronous sequence of 0 or 1 element.
  • Flux: Represents an asynchronous sequence of 0 to N elements.

3. WebFlux​

Spring WebFlux is an asynchronous, non-blocking web framework based on Reactive Streams API and Project Reactor. The main components include:

  • Router Functions: Define mappings between requests and handlers in a functional style.
  • Handler Functions: Handle HTTP requests and return responses.

Practical Example: Building a Simple WebFlux Application​

1. Project Setup​

Start by creating a Spring Boot project and adding the necessary dependencies:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
</dependencies>

2. Define Data Model​

Define a simple user data model:

public class User {
private String id;
private String name;

// Constructors, Getters and Setters
}

3. Create Handler and Router​

Create a handler function and a router function:

@Component
public class UserHandler {
public Mono<ServerResponse> getUser(ServerRequest request) {
User user = new User("1", "John Doe");
return ServerResponse.ok().body(Mono.just(user), User.class);
}
}

@Configuration
public class RouterConfig {
@Bean
public RouterFunction<ServerResponse> route(UserHandler handler) {
return RouterFunctions.route(RequestPredicates.GET("/user"), handler::getUser);
}
}

Conclusion​

In this article, we introduced the basic concepts and core components of Spring WebFlux and demonstrated how to build a simple reactive application using WebFlux.

Reactive Programming in Spring WebFlux Series - II

· 阅读需 4 分钟

Index​


In-depth Understanding of Reactor and Asynchronous Data Streams


Introduction to Reactor​

Reactor is at the core of Spring WebFlux, providing powerful reactive programming support based on the Reactive Streams standard. Reactor mainly offers two core classes: Mono and Flux, which are used to represent asynchronous sequences of one or multiple elements, respectively.


Mono and Flux​

Mono​

Mono represents an asynchronous sequence of 0 or 1 element. It is typically used when dealing with operations that might return a single result or no result at all.

Flux​

Flux represents an asynchronous sequence of 0 to N elements. It is suitable for cases where multiple results need to be handled, such as querying a database that returns multiple records.


Basic Operations​

Reactor provides a rich set of operators for manipulating data streams, such as transformations, filtering, and aggregations. Some of the commonly used operators include:

  • map: Transforms each element in the data stream.
  • filter: Filters elements in the data stream, retaining only those that meet the criteria.
  • flatMap: Maps each element to a new Publisher and merges these Publishers into a single Flux.
  • concatMap: Similar to flatMap but maintains the order of the elements.
  • zip: Combines multiple Publishers by pairing elements based on their indices.

Practical Example: Building a Reactive Application with Reactor​

1. Project Setup​

Ensure your project contains the following dependencies:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
</dependencies>

2. Define Data Model​

Define a simple user data model:

public class User {
private String id;
private String name;

// Constructors, Getters and Setters
}

3. Simulated Data Source​

Create a simulated data source to generate user data:

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

public class UserRepository {
private static final List<User> users = Arrays.asList(
new User("1", "John Doe"),
new User("2", "Jane Doe"),
new User("3", "Sam Smith")
);

public Mono<User> findById(String id) {
return Mono.justOrEmpty(users.stream()
.filter(user -> user.getId().equals(id))
.findFirst());
}

public Flux<User> findAll() {
return Flux.fromIterable(users);
}
}

4. Handlers and Router Configuration​

Create handler functions and router configurations to handle HTTP requests and return responses:

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import reactor.core.publisher.Mono;

@Component
public class UserHandler {
private final UserRepository userRepository;

public UserHandler(UserRepository userRepository) {
this.userRepository = userRepository;
}

public Mono<ServerResponse> getUser(ServerRequest request) {
String userId = request.pathVariable("id");
return userRepository.findById(userId)
.flatMap(user -> ServerResponse.ok().body(Mono.just(user), User.class))
.switchIfEmpty(ServerResponse.notFound().build());
}

public Mono<ServerResponse> getAllUsers(ServerRequest request) {
return ServerResponse.ok().body(userRepository.findAll(), User.class);
}
}

@Configuration
public class RouterConfig {
@Bean
public RouterFunction<ServerResponse> route(UserHandler handler) {
return RouterFunctions.route()
.GET("/user/{id}", handler::getUser)
.GET("/users", handler::getAllUsers)
.build();
}
}

Conclusion​

In this article, we took a deep dive into Reactor and its application in Spring WebFlux. By understanding Mono and Flux and how to handle asynchronous data streams with these tools, we can build more efficient reactive applications.

Reactive Programming in Spring WebFlux Series - III

· 阅读需 4 分钟

Error Handling and Debugging


Error Handling​

Error handling in reactive programming is different from traditional synchronous programming due to the asynchronous nature of reactive streams. In Reactor, errors are handled in various ways:

1. onErrorReturn​

This operator allows returning a default value when an error occurs. Example:

Flux<String> flux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception occurred")))
.onErrorReturn("Default");

flux.subscribe(System.out::println);

In this example, when an error occurs, the default value "Default" is returned.

2. onErrorResume​

This operator allows switching to another Publisher when an error occurs. Example:

Flux<String> flux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception occurred")))
.onErrorResume(e -> {
System.out.println("Error: " + e.getMessage());
return Flux.just("D", "E");
});

flux.subscribe(System.out::println);

Here, when an error occurs, it switches to another Flux sequence to continue processing new data.

3. onErrorMap​

This operator allows converting an error into a different type of error. Example:

Flux<String> flux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception occurred")))
.onErrorMap(e -> new CustomException("Custom message", e));

flux.subscribe(System.out::println, e -> System.out.println("Error: " + e.getMessage()));

In this case, a RuntimeException is converted into a custom CustomException.


Global Error Handling in WebFlux​

In addition to handling errors in streams, Spring WebFlux also allows defining a global error handler for managing errors across the application.

1. Defining a Custom Exception Class​

public class CustomException extends RuntimeException {
public CustomException(String message) {
super(message);
}
}

2. Creating an Exception Handler​

import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.function.server.ServerResponse.BodyBuilder;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebExceptionHandler;
import reactor.core.publisher.Mono;

@Component
@Order(-2)
public class GlobalErrorHandler implements WebExceptionHandler {

@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
BodyBuilder response = ServerResponse.status(HttpStatus.INTERNAL_SERVER_ERROR)
.contentType(MediaType.APPLICATION_JSON);

if (ex instanceof CustomException) {
response = ServerResponse.status(HttpStatus.BAD_REQUEST);
}

return response.bodyValue(new ErrorResponse(ex.getMessage()))
.flatMap(resp -> resp.writeTo(exchange, new Context()));
}

private static class ErrorResponse {
private final String message;

public ErrorResponse(String message) {
this.message = message;
}

public String getMessage() {
return message;
}
}
}

Debugging Reactive Applications​

Debugging reactive applications can be more challenging than debugging traditional synchronous applications. Here are some debugging techniques:

1. Using the log Operator​

Reactor provides the log operator to output log information for each step. Example:

Flux<String> flux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception occurred")))
.log();

flux.subscribe(System.out::println);

2. Breakpoint Debugging​

Setting breakpoints in an IDE allows you to step through the execution. Be mindful of thread switching and asynchronous execution details when debugging reactive code.

3. Using Reactor Debugger​

Reactor provides a special debug tool Hooks.onOperatorDebug to help understand and debug operations in reactive streams.

Hooks.onOperatorDebug();

Flux<String> flux = Flux.just("A", "B", "C")
.concatWith(Flux.error(new RuntimeException("Exception occurred")));

flux.subscribe(System.out::println);

Conclusion​

In this article, we explored how to handle errors and debug reactive applications in Spring WebFlux, providing techniques for both stream-level and global error handling.

Reactive Programming in Spring WebFlux Series - IV

· 阅读需 4 分钟

Data Stream Transformation and Operations


Data Stream Transformation​

In reactive programming, transforming and operating on data streams is a core aspect. Using different operators, we can handle data streams flexibly, applying transformations, filtering, aggregating, and more. Below are some common data stream operations and examples.

1. map Operator​

The map operator transforms each element in a data stream. It takes a function as an argument and applies it to each element.

Flux<Integer> numbers = Flux.just(1, 2, 3, 4, 5)
.map(n -> n * n);

numbers.subscribe(System.out::println); // Output: 1, 4, 9, 16, 25

2. flatMap Operator​

The flatMap operator maps each element to a new Publisher and merges them into a single Flux. It is often used for asynchronous processing.

Flux<String> ids = Flux.just("1", "2", "3");
Flux<User> users = ids.flatMap(id -> userRepository.findById(id));

users.subscribe(System.out::println);

3. filter Operator​

The filter operator filters data streams, keeping only elements that meet a certain condition.

Flux<Integer> numbers = Flux.just(1, 2, 3, 4, 5)
.filter(n -> n % 2 == 0);

numbers.subscribe(System.out::println); // Output: 2, 4

4. reduce Operator​

The reduce operator aggregates all elements in a data stream into a single value. It takes an accumulator function, combining the previous result with the current element.

Flux<Integer> numbers = Flux.just(1, 2, 3, 4, 5);
Mono<Integer> sum = numbers.reduce((a, b) -> a + b);

sum.subscribe(System.out::println); // Output: 15

Practical Example: Building a Complex Data Stream Processing Application​

Below is an example showing how to use these operators to build a complex reactive application in Spring WebFlux.

1. Project Setup​

Ensure that the following dependencies are included in your project:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
</dependencies>

2. Data Model​

Define a simple user data model:

public class User {
private String id;
private String name;

// Constructors, Getters and Setters
}

3. Mock Data Source​

Create a mock data source:

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

public class UserRepository {
private static final List<User> users = Arrays.asList(
new User("1", "John Doe"),
new User("2", "Jane Doe"),
new User("3", "Sam Smith")
);

public Mono<User> findById(String id) {
return Mono.justOrEmpty(users.stream()
.filter(user -> user.getId().equals(id))
.findFirst());
}

public Flux<User> findAll() {
return Flux.fromIterable(users);
}
}

4. Handler Functions and Route Configuration​

Set up handler functions and route configuration:

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import reactor.core.publisher.Mono;

@Component
public class UserHandler {
private final UserRepository userRepository;

public UserHandler(UserRepository userRepository) {
this.userRepository = userRepository;
}

public Mono<ServerResponse> getUser(ServerRequest request) {
String userId = request.pathVariable("id");
return userRepository.findById(userId)
.flatMap(user -> ServerResponse.ok().body(Mono.just(user), User.class))
.switchIfEmpty(ServerResponse.notFound().build());
}

public Mono<ServerResponse> getAllUsers(ServerRequest request) {
return ServerResponse.ok().body(userRepository.findAll(), User.class);
}
}

@Configuration
public class RouterConfig {
@Bean
public RouterFunction<ServerResponse> route(UserHandler handler) {
return RouterFunctions.route()
.GET("/user/{id}", handler::getUser)
.GET("/users", handler::getAllUsers)
.build();
}
}

Conclusion​

In this article, we explored how to transform and operate on data streams in Spring WebFlux. Using different operators, we can flexibly handle data streams, building complex reactive applications.

Reactive Programming in Spring WebFlux Series - IX

· 阅读需 4 分钟


Test


Introduction

In previous articles, we explored the foundational concepts of Spring WebFlux, Reactor, error handling, data flow transformation, reactive database access, performance optimization, complex scenarios, and security. This article delves into how to conduct testing in Spring WebFlux to ensure that our reactive applications run reliably in both development and production environments.

Why Testing is Necessary​

Testing is an indispensable part of the software development process. Through testing, we can validate code correctness, ensure individual modules function as expected, and promptly identify and resolve potential issues. For reactive applications like those built with Spring WebFlux, testing is particularly important because reactive programming introduces asynchronous and non-blocking operations, which increase the complexity of debugging and validation.

Types of Testing in Spring WebFlux​

In Spring WebFlux, there are typically three main types of tests:

  • Unit Testing: Testing individual modules or components to ensure their behavior matches expectations.
  • Integration Testing: Testing the interactions between multiple modules to ensure they work together correctly.
  • End-to-End Testing: Simulating real user actions and verifying the behavior of the entire system.

1. Unit Testing​

Unit testing is the smallest test scope, focusing on individual functionality or components. In Spring WebFlux, we commonly test controllers, services, and data access layers.

1.1 Testing Controllers with WebTestClient​

WebTestClient is a powerful tool for testing the web layer of Spring WebFlux applications.

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.test.web.reactive.server.WebTestClient;

@WebFluxTest(UserHandler.class)
public class UserHandlerTest {

@Autowired
private WebTestClient webTestClient;

@Test
void testGetUser() {
webTestClient.get().uri("/user/1")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.name").isEqualTo("John Doe");
}

@Test
void testGetAllUsers() {
webTestClient.get().uri("/users")
.exchange()
.expectStatus().isOk()
.expectBodyList(User.class).hasSize(3);
}
}

This example shows how to test a controller using WebTestClient, focusing on the GET /user/1 and GET /users endpoints.

1.2 Testing Services with Mockito​

Mockito is a popular mocking framework used to create and manage dependencies in tests.

import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import reactor.core.publisher.Mono;

public class UserServiceTest {

@Mock
private UserRepository userRepository;

@InjectMocks
private UserService userService;

public UserServiceTest() {
MockitoAnnotations.openMocks(this);
}

@Test
void testGetUser() {
User user = new User("1", "John Doe");
when(userRepository.findById("1")).thenReturn(Mono.just(user));

Mono<User> result = userService.getUser("1");
assertEquals("John Doe", result.block().getName());
}
}

In this example, we use Mockito to mock the UserRepository and verify that the UserService correctly retrieves the user.

2. Integration Testing​

Integration tests validate the interactions between multiple modules. In Spring WebFlux, we usually use the @SpringBootTest annotation to load the entire application context and use WebTestClient to perform tests.

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApplicationIntegrationTest {

@Autowired
private WebTestClient webTestClient;

@Test
void testGetUser() {
webTestClient.get().uri("/user/1")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.name").isEqualTo("John Doe");
}

@Test
void testGetAllUsers() {
webTestClient.get().uri("/users")
.exchange()
.expectStatus().isOk()
.expectBodyList(User.class).hasSize(3);
}
}

In this example, the entire application is loaded and tested for correctness across modules using WebTestClient.

3. End-to-End Testing​

End-to-end testing simulates real user actions and verifies the behavior of the entire system. These tests are generally more complex as they involve starting the whole application and mimicking various user interaction scenarios.

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EndToEndTest {

@Autowired
private WebTestClient webTestClient;

@Test
void testUserFlow() {
// Create a new user
User newUser = new User(null, "Alice");
webTestClient.post().uri("/user")
.bodyValue(newUser)
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.name").isEqualTo("Alice");

// Get all users
webTestClient.get().uri("/users")
.exchange()
.expectStatus().isOk()
.expectBodyList(User.class).hasSize(4);
}
}

This example tests the flow of creating a new user and then retrieving all users to ensure the system works end-to-end.

Conclusion​

In this article, we discussed how to perform testing in Spring WebFlux. By conducting unit tests, integration tests, and end-to-end tests, you can ensure that every part of your application functions as expected. Additionally, understanding common vulnerabilities like CSRF and XSS is crucial for building secure applications.

Reactive Programming in Spring WebFlux Series - V

· 阅读需 3 分钟

Reactive Database Access


Why Choose Reactive Database Access?​

Traditional database access methods are usually blocking, meaning that when a query is executing, the thread is blocked until the query is completed. This can lead to performance bottlenecks in highly concurrent environments. Reactive database access uses non-blocking I/O operations, allowing applications to handle more concurrent requests, improving system throughput and response times.


Spring Data R2DBC​

Spring Data R2DBC (Reactive Relational Database Connectivity) is a project in the Spring ecosystem that provides reactive programming support for relational databases. R2DBC is a new database connection specification designed to support reactive programming models.

1. Project Setup​

First, create a Spring Boot project and include the necessary dependencies:

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-h2</artifactId>
</dependency>
</dependencies>

2. Database Connection Configuration​

Configure the H2 database in application.properties:

spring.r2dbc.url=r2dbc:h2:mem:///testdb
spring.r2dbc.username=sa
spring.r2dbc.password=
spring.r2dbc.generate-unique-name=true

spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true

3. Data Model​

Define a simple User entity:

import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;

@Table("users")
public class User {
@Id
private Long id;
private String name;

// Getters and Setters
}

4. Creating a Repository​

Create a reactive UserRepository interface:

import org.springframework.data.repository.reactive.ReactiveCrudRepository;

public interface UserRepository extends ReactiveCrudRepository<User, Long> {
}

5. Data Initialization​

Use an initializer class to preload some data:

import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import reactor.core.publisher.Flux;

@Configuration
public class DataInitializer {

@Bean
CommandLineRunner init(UserRepository repository) {
return args -> {
Flux<User> userFlux = Flux.just(
new User(null, "John Doe"),
new User(null, "Jane Doe"),
new User(null, "Sam Smith"))
.flatMap(repository::save);

userFlux
.thenMany(repository.findAll())
.subscribe(user -> System.out.println("Saving " + user.toString()));
};
}
}

6. Handler Functions and Route Configuration​

Create handler functions and route configurations to handle HTTP requests and return responses:

import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import reactor.core.publisher.Mono;

@Component
public class UserHandler {
private final UserRepository userRepository;

public UserHandler(UserRepository userRepository) {
this.userRepository = userRepository;
}

public Mono<ServerResponse> getUser(ServerRequest request) {
Long userId = Long.valueOf(request.pathVariable("id"));
return userRepository.findById(userId)
.flatMap(user -> ServerResponse.ok().body(Mono.just(user), User.class))
.switchIfEmpty(ServerResponse.notFound().build());
}

public Mono<ServerResponse> getAllUsers(ServerRequest request) {
return ServerResponse.ok().body(userRepository.findAll(), User.class);
}

public Mono<ServerResponse> createUser(ServerRequest request) {
Mono<User> userMono = request.bodyToMono(User.class);
return userMono.flatMap(userRepository::save)
.flatMap(user -> ServerResponse.ok().body(Mono.just(user), User.class));
}
}

@Configuration
public class RouterConfig {
@Bean
public RouterFunction<ServerResponse> route(UserHandler handler) {
return RouterFunctions.route()
.GET("/user/{id}", handler::getUser)
.GET("/users", handler::getAllUsers)
.POST("/user", handler::createUser)
.build();
}
}

Conclusion​

In this article, we explored how to perform reactive database access in Spring WebFlux. With Spring Data R2DBC, we can easily achieve non-blocking access to relational databases, greatly improving the performance and concurrency handling of applications.