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

The Reader Path: Intent-Based Projection

3 min read Chapter 19 of 25
Summary

Intent-Based Projection serves reads based on client-requested consistency...

Intent-Based Projection serves reads based on client-requested consistency levels, ensuring Read-Your-Writes consistency through routing logic and freshness tokens.

The Reader Path: Intent-Based Projection

Introduction to Intent-Based Projection

Intent-Based Projection is a read-path strategy where the query mechanism is determined by the declarative intent of the client. This approach maps requested consistency levels to specific infrastructure components, ensuring that the client’s requirements are met. For instance, a client may request a consistent-read to ensure that the data returned is the most recent version, or an eventual-read to prioritize horizontal scalability.

Read-Your-Writes Consistency

Read-Your-Writes consistency is a crucial aspect of Intent-Based Projection. It guarantees that a client can immediately see the effects of its own successful writes, even if the system is globally eventual. This is achieved through the use of a freshness token, such as a commit token, which is generated after a write and used by the client to verify if a replica has reached a specific state.

Implementing Read-Your-Writes

The implementation of Read-Your-Writes consistency involves routing reads based on the client’s requested consistency level. The following Java code example demonstrates how to achieve this:

public class ReaderPathService {
    private final DataSource primaryDS;
    private final DataSource replicaDS;

    public State readState(String entityId, ConsistencyLevel intent, Long requiredToken) {
        if (intent == ConsistencyLevel.CONSISTENT_READ) {
            return readFromPrimary(entityId);
        }

        // Read-Your-Writes Logic
        if (requiredToken != null) {
            long currentReplicaOffset = getReplicaLagOffset(entityId);
            if (currentReplicaOffset < requiredToken) {
                // Force primary read if replica is too stale for client's own write
                return readFromPrimary(entityId);
            }
        }

        return readFromReplica(entityId);
    }

    private State readFromPrimary(String id) { /* Implementation */ return null; }
    private State readFromReplica(String id) { /* Implementation */ return null; }
    private long getReplicaLagOffset(String id) { /* Implementation */ return 0L; }
}

Routing Decision Matrix

The routing decision matrix is a critical component of Intent-Based Projection. It determines the physical route of a read request based on the client’s requested consistency level and the system’s health. The following table illustrates a sample routing decision matrix:

Requested IntentSystem HealthPhysical RouteConsistency Model
consistent-readGoodPrimary NodeLinearizable
eventual-readGoodReplica PoolEventual
read-your-writesDegradedPrimary (Fallback)Session Consistent
eventual-readDegradedReplica (Async)Weak Eventual

Conclusion

In conclusion, Intent-Based Projection is a powerful strategy for serving reads in a distributed system. By mapping requested consistency levels to specific infrastructure components, it ensures that the client’s requirements are met. The implementation of Read-Your-Writes consistency and the routing decision matrix are critical components of this strategy.

Sources

[1] https://docs.oracle.com/database/bdb181/html/gsg_db_rep/C/rywc.html [2] https://en.wikipedia.org/wiki/Consistency_model