Skip to main content
adaptive distributed systems intent-based dynamic consistency in java 21

Semantic Error Handling and Conflict Resolution

3 min read Chapter 20 of 25
Summary

Railway Oriented Programming (ROP) and Semantic Result Type...

Railway Oriented Programming (ROP) and Semantic Result Type for error handling in distributed systems, with Java 21 and Kafka examples.

Semantic Error Handling and Conflict Resolution

Introduction to Railway Oriented Programming (ROP)

Railway Oriented Programming (ROP) is a functional programming pattern designed to handle errors by modeling computation as a two-track pipeline—one for success and one for failure. This approach is particularly useful in distributed systems where partial failures are common. By using ROP, developers can write more robust and maintainable code that explicitly handles errors and exceptions.

Semantic Result Type

A key concept in ROP is the Semantic Result Type, which is a generic container (e.g., Result<T, E>) that explicitly represents the outcome of an operation. This type replaces generic exceptions with domain-specific failure types, making it easier to handle errors in a meaningful way. In Java 21, this can be achieved using sealed interfaces and records, as shown in the following code example:

public sealed interface Result<T, F> permits Success, Failure {
    record Success<T, F>(T value) implements Result<T, F> {}
    record Failure<T, F>(F error) implements Result<T, F> {}

    default <R> Result<R, F> map(Function<T, R> mapper) {
        return this instanceof Success<T, F> s ? new Success<>(mapper.apply(s.value())) : (Result<R, F>) this;
    }

    default <R> Result<R, F> flatMap(Function<T, F> mapper) {
        return this instanceof Success<T, F> s ? mapper.apply(s.value()) : (Result<R, F>) this;
    }
}

This implementation provides a robust way to handle errors and exceptions in a distributed system.

Monadic Bind and Error Handling

The Monadic Bind is a process of chaining operations where each step depends on the success of the previous one. This is often implemented via flatMap. By using monadic bind, developers can write concise and expressive code that handles errors in a elegant way.

Consistency Floor and Conflict Resolution

In distributed systems, consistency is a critical aspect of data handling. The Consistency Floor is a metadata attribute in an intent that defines the minimum consistency level required for its execution. Conflict resolution strategies, such as Last-Write-Wins (LWW) and Compare-And-Swap (CAS), are used to manage concurrency and ensure data consistency.

Last-Write-Wins (LWW) and Compare-And-Swap (CAS)

LWW is a conflict resolution strategy where the update with the highest monotonically increasing token (e.g., timestamp or offset) prevails. CAS is an atomic operation used to manage concurrency by updating a value only if it matches an expected ‘original’ state, typically via versioning.

Kafka and PostgreSQL

Kafka partitions guarantee sequential processing order for intents sharing the same message key. PostgreSQL provides a robust way to implement LWW using ‘INSERT … ON CONFLICT’ or ‘UPDATE … WHERE’ with freshness tokens.

PACELC Theory and Linearizable Consistency

PACELC theory dictates that systems incur a latency penalty for consistency even when no network partition exists. Linearizable consistency is triggered when Intent Criticality is ‘High’ and System Health is ‘Good’.

Java 21 Virtual Threads and Structured Concurrency

Java 21 Virtual Threads are optimized for thread-per-request architectures and high-throughput blocking I/O. Structured concurrency in Java 21 provides a way to cancel subtasks if any single subtask fails.

Idempotence in Kafka

Idempotence in Kafka requires ‘acks=all’ or ‘acks=-1’ to ensure exactly-once write semantics per message.

Compacted Kafka Topics

Compacted Kafka topics use Tombstones (null payloads) to signal the deletion of an entity key.

Optimistic Locking

Optimistic locking assumes conflicts are rare and only verifies state integrity at the moment of commitment.

Vector Clocks

Vector clocks provide a mechanism for determining causal ordering in distributed systems without relying on physical wall-clock time.

Sources

[1] https://www.mycompiler.io/view/FV5jT7Obkhn [2] https://livebook.manning.com/book/event-streams-in-action/chapter-7 [3] https://www.geeksforgeeks.org/computer-networks/exception-handling-in-distributed-system/