How to handle concurrent adds on the same key in Last Write Wins map?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Last Write Wins (LWW) maps are data structures commonly used in distributed systems to resolve conflicts among concurrent operations. As the name suggests, the LWW strategy prioritizes the most recent write operation. This approach is particularly useful when multiple systems or agents modify the same data concurrently.
Understanding Last Write Wins (LWW)
A Last Write Wins map holds key-value pairs where each key stores its associated value and the timestamp of the last update. When multiple writes to the same key occur, the system evaluates the timestamps of the updates; the write with the latest timestamp wins, irrespective of the actual sequence of operations.
Technical Underpinnings
To implement a LWW map, you generally need:
- Map Data Structure: Typically a hashmap or dictionary.
- Time-Stamping Mechanism: A reliable clock to timestamp each write operation.
An example of how an LWW map can be structured in a programming context (using Python) is shown below:
In this implementation:
- Each key in
storemaps to a tuple of(value, timestamp). - The
addmethod checks if the new write has a more recent timestamp than the existing one before updating the value. - The
getmethod simply retrieves the value for any given key.
Handling Concurrency
Concurrency issues arise when multiple processes attempt to write to the same key around the same time. Sometimes, due to network delays or clock drifts, timestamps might not precisely reflect the order of operations as they happened in real-time. Here’s how to handle such situations:
Centralized Timestamping: Use a central server or service to generate timestamps when an operation is initiated. This can help reduce discrepancies due to different clock times in distributed systems.
Synchronization Mechanism: Implement locking mechanisms or synchronized blocks to manage accesses to the same key more safely. This ensures that a write operation is completed by one process before another can start.
Logical Clocks: Use logical clocks (like Lamport timestamps) instead of relying solely on physical time, which can reduce the problems caused by clock skew.
Example Scenario
Suppose two users are updating the configuration settings (stored in LWW map) of a distributed application from different locations:
- User 1 (from New York) sends an update with timestamp 16:05:00.
- User 2 (from London) sent an update at 16:05:03, but due to a delay, it arrives at the server at 16:05:02.
A physical clock-based LWW map would use the timestamps as they appear even if they might not reflect the exact sequence of events.
Additional Considerations
Expiry Mechanism: This could be integrated to prevent the storage from being overwhelmed with outdated entries.
Conclusion
Handling concurrent writes on the same key using the LWW strategy in a map involves ensuring that the most recent write (based on the timestamp) is what gets stored. This helps maintain consistency across distributed systems even when multiple updates to the same data occur concurrently.
Summary Table
Here is a summary of key points related to implementing and managing a Last Write Wins map:
| Key Aspect | Description |
| Map Data Structure | Use hashmap or similar for storing key-value pairs. |
| Time-Stamping | Essential for determining the "last write". Use reliable sources. |
| Concurrency Handling | Implement locks, use centralized timestamping, or logical clocks. |
| Conflict Resolution | Latest timestamp wins, regardless of actual sequence of operations. |
| Additional Mechanisms | Consider adding data expiry or cleanup systems. |
By taking these strategies into account, developers can effectively manage data consistency in environments with high concurrency.
Related reading
- How to implement 3 stacks with one array?
- How to implement a better sliding window algorithm?
- How to implement a binary tree?
- How to implement a double linked list with only one pointer?
- How to handle errors from setTimeout in JavaScript?
- How to handle exceptions raised in other threads when unit testing?
- How to implement a Map with multiple keys?
- How to implement a Median-heap

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.