First-Write-Wins (Examples)

RedditHackerNewsX
SUMMARY

First-Write-Wins (FWW) is a concurrency control mechanism used in distributed databases to handle conflicting write operations. When multiple processes attempt to write to the same data simultaneously, FWW accepts the first write operation that arrives and rejects subsequent conflicting writes.

How First-Write-Wins works

First-Write-Wins implements a simple but effective strategy for maintaining data consistency in distributed systems. When multiple write requests arrive:

  1. The first write operation to reach the system is accepted
  2. Subsequent conflicting writes are rejected
  3. The client receives a response indicating success or failure
-- Example of potential write conflict
-- Transaction 1 (arrives first)
UPDATE sensor_readings
SET temperature = 23.5
WHERE sensor_id = 'A123'
AND timestamp = '2024-01-01 10:00:00';
-- Transaction 2 (arrives second - will be rejected)
UPDATE sensor_readings
SET temperature = 23.7
WHERE sensor_id = 'A123'
AND timestamp = '2024-01-01 10:00:00';

Next generation time-series database

QuestDB is an open-source time-series database optimized for market and heavy industry data. Built from scratch in Java and C++, it offers high-throughput ingestion and fast SQL queries with time-series extensions.

Implementation in time-series databases

In time-series databases, First-Write-Wins is particularly important for handling high-frequency data ingestion. The mechanism helps maintain data integrity when dealing with:

  • Multiple data sources sending measurements for the same timestamp
  • Duplicate records from redundant sensors
  • Network delays causing out-of-order arrivals

Benefits and limitations

Benefits

  • Simple to implement and understand
  • Predictable behavior
  • Low overhead compared to more complex concurrency mechanisms
  • Suitable for append-only workloads common in time-series data

Limitations

  • May discard valid updates in high-contention scenarios
  • Doesn't support complex transaction patterns
  • Network latency can influence which write "wins"

Real-world applications

First-Write-Wins is commonly used in:

Industrial monitoring

Manufacturing plants with multiple sensors measuring the same parameters benefit from FWW's ability to handle duplicate readings without complex resolution logic.

Financial market data

When multiple sources report the same trade or quote, FWW ensures only the first observation is recorded, preventing duplicate entries in market data archives.

IoT deployments

Industrial IoT (IIoT) Data systems use FWW to manage concurrent updates from distributed sensor networks while maintaining data consistency.

Time-series analytics

When processing time-series data, FWW is often used to handle duplicate readings. Consider a practical example using a materialized view to implement FWW for sensor readings:

-- First, create a table for raw sensor data
CREATE TABLE sensor_readings (
timestamp TIMESTAMP,
sensor_id SYMBOL,
temperature DOUBLE,
receive_time TIMESTAMP
) TIMESTAMP(timestamp);
-- Create a materialized view that implements FWW
CREATE MATERIALIZED VIEW sensor_hourly_fww AS
WITH deduplicated AS (
SELECT
timestamp,
sensor_id,
temperature,
row_number() OVER (
PARTITION BY timestamp, sensor_id
ORDER BY receive_time
) as row_num
FROM sensor_readings
)
SELECT
timestamp,
sensor_id,
avg(temperature) as avg_temp
FROM deduplicated
WHERE row_num = 1
SAMPLE BY 1h;

This materialized view:

  1. Uses
    row_number()
    to identify the first reading for each timestamp/sensor combination
  2. Keeps only the first write (
    row_num = 1
    ) for each data point
  3. Computes hourly averages using the deduplicated data

Optimizing First-Write-Wins implementations

To maximize the effectiveness of FWW:

  1. Use precise timestamps to minimize ambiguity
  2. Implement appropriate error handling for rejected writes
  3. Monitor rejection rates to identify potential system bottlenecks
  4. Consider buffer strategies for high-frequency write scenarios
-- Example of timestamp-based write with buffer window
INSERT INTO sensor_readings (sensor_id, timestamp, value)
SELECT 'A123', '2024-01-01 10:00:00', 23.5
WHERE NOT EXISTS (
SELECT 1
FROM sensor_readings
WHERE sensor_id = 'A123'
AND timestamp = '2024-01-01 10:00:00'
);

Next generation time-series database

QuestDB is an open-source time-series database optimized for market and heavy industry data. Built from scratch in Java and C++, it offers high-throughput ingestion and fast SQL queries with time-series extensions.

Best practices for First-Write-Wins

When implementing FWW in distributed systems:

  • Ensure accurate clock synchronization across nodes
  • Implement clear error handling for rejected writes
  • Monitor system performance and conflict rates
  • Document the behavior for application developers
  • Consider combining with batch processing for efficiency

First-Write-Wins provides a pragmatic approach to handling write conflicts in distributed systems, particularly valuable for time-series data where simplicity and performance are crucial considerations.

Subscribe to our newsletters for the latest. Secure and never shared or sold.