First-Write-Wins (Examples)
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:
- The first write operation to reach the system is accepted
- Subsequent conflicting writes are rejected
- The client receives a response indicating success or failure
-- Example of potential write conflict-- Transaction 1 (arrives first)UPDATE sensor_readingsSET temperature = 23.5WHERE sensor_id = 'A123'AND timestamp = '2024-01-01 10:00:00';-- Transaction 2 (arrives second - will be rejected)UPDATE sensor_readingsSET temperature = 23.7WHERE 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 dataCREATE TABLE sensor_readings (timestamp TIMESTAMP,sensor_id SYMBOL,temperature DOUBLE,receive_time TIMESTAMP) TIMESTAMP(timestamp);-- Create a materialized view that implements FWWCREATE MATERIALIZED VIEW sensor_hourly_fww ASWITH deduplicated AS (SELECTtimestamp,sensor_id,temperature,row_number() OVER (PARTITION BY timestamp, sensor_idORDER BY receive_time) as row_numFROM sensor_readings)SELECTtimestamp,sensor_id,avg(temperature) as avg_tempFROM deduplicatedWHERE row_num = 1SAMPLE BY 1h;
This materialized view:
- Uses to identify the first reading for each timestamp/sensor combinationrow_number()
- Keeps only the first write () for each data pointrow_num = 1
- Computes hourly averages using the deduplicated data
Optimizing First-Write-Wins implementations
To maximize the effectiveness of FWW:
- Use precise timestamps to minimize ambiguity
- Implement appropriate error handling for rejected writes
- Monitor rejection rates to identify potential system bottlenecks
- Consider buffer strategies for high-frequency write scenarios
-- Example of timestamp-based write with buffer windowINSERT INTO sensor_readings (sensor_id, timestamp, value)SELECT 'A123', '2024-01-01 10:00:00', 23.5WHERE NOT EXISTS (SELECT 1FROM sensor_readingsWHERE 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.