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

Materialized Views and Freshness Tokens

3 min read Chapter 18 of 25
Summary

Materialized views maintain data consistency with freshness tokens...

Materialized views maintain data consistency with freshness tokens and LWW semantics

Materialized Views and Freshness Tokens

Materialized views are a crucial component in maintaining data consistency within distributed systems. By physically storing the results of a query, these views improve performance while requiring a refresh strategy to remain consistent with the source data. A key aspect of managing materialized views is the implementation of freshness tokens, which are monotonically increasing identifiers used to track the causal order of updates. This can be achieved through various means, including Kafka offsets, vector clocks, or high-resolution timestamps.

Last-Write-Wins (LWW) Semantics

LWW semantics provide a conflict resolution strategy where the update with the highest token value or latest timestamp is preserved, and older updates are discarded or ignored. This approach is particularly useful in distributed systems where out-of-order writes can occur due to network jitter or partitioning. To implement LWW semantics, a unique, monotonically increasing token is assigned to each update. The token with the highest value prevails, ensuring that the most recent update is reflected in the materialized view.

Causal Time and Out-of-Order Writes

Causal time refers to the mechanism of ordering events based on their cause-and-effect relationship rather than physical wall-clock time. This is often implemented using logical clocks, which help maintain the causal order of updates. Out-of-order writes, on the other hand, occur when a logically later update arrives at the storage layer before an earlier update. To address this issue, freshness tokens such as vector clocks or version tokens are used to determine causality and ordering in distributed systems.

Implementing Materialized Views with LWW Logic

The implementation of materialized views with LWW logic involves creating a table that stores the entity’s current state along with freshness metadata. This can be achieved using PostgreSQL, which supports conditional updates using the INSERT ... ON CONFLICT or UPDATE ... WHERE syntax. The following SQL code snippet demonstrates how to create a materialized view with LWW freshness tracking based on Kafka offsets:

CREATE TABLE materialized_entity_state (
    entity_id UUID PRIMARY KEY,
    state_data JSONB NOT NULL,
    last_offset BIGINT NOT NULL,
    last_updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);

-- Atomic Last-Write-Wins Update Logic
UPDATE materialized_entity_state
SET 
    state_data = :new_data,
    last_offset = :incoming_offset,
    last_updated_at = NOW()
WHERE 
    entity_id = :entity_id
    AND :incoming_offset > last_offset;

Decision Matrix for Freshness Token Comparison Logic

The decision matrix for freshness token comparison logic is crucial in determining the outcome of updates based on their token values. The matrix considers scenarios such as standard updates, out-of-order writes, duplicate updates, and partition resets. The following table illustrates the decision matrix:

ScenarioIncoming TokenStored TokenOutcomeReason
Standard Update105100SuccessIncoming token is greater (fresher)
Out-of-Order98100RejectIncoming token is lower (stale)
Duplicate100100RejectIncoming token is equal (idempotency)
Partition Reset1100RejectCausal safety maintained despite replay

Sources

[1] PostgreSQL Documentation: Conditional Updates [2] Kafka Documentation: Offset Management