Seize the deal!
Caching Best Practices
Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!
In this article, I’m going to explain how the logical and physical clock versioning strategies work, and why you should prefer using logical clocks for concurrency control.
Optimistic locking is a viable solution for prevening lost updates when running application-level transactions.
Optimistic locking requires a version column that can be represented as:
This article will demonstrate why logical clocks are better suited for optimistic locking mechanisms.
The system time is provided by the internal clock of the current operating system. The programmable interval timer periodically sends an interrupt signal (with a frequency of 1.193182 MHz). The CPU receives the time interruption and increments a tick counter.
Both Unix and Window record time as the number of ticks since a predefined absolute time reference (an epoch). The operating system clock resolution varies from 1ms (Android) to 100ns (Windows) and to 1ns (Unix).
To order events, the version must advance monotonically. While incrementing a local counter is a monotonic function, system time might not always return monotonic timestamps.
Java has two ways of fetching the current system time. You can either use:
This method doesn’t give you monotonic time results because it returns the wall clock time which is prone to both forward and backward adjustments (if NTP is used for system time synchronization).
This method tries to use the current operating system monotonic clock implementation, but it falls back to wall clock time if no monotonic clock could be found.
System time is not always monotonically incremented.
The SQL-92 standard defines the TIMESTAMP data type as YYYY-MM-DD hh:mm:ss. The fraction part is optional and each database implements a specific timestamp data type:
MySQL 5.6.4 added microseconds precision support for
Prior to 5.6.4, MySQL discards the fractional part of all temporal types.
When it comes to persisting timestamps, most database servers offer at least 6 fractional digits. MySQL users have long been waiting for a more precise temporal type and the 5.6.4 version had finally added microsecond precision.
On a pre-5.6.4 MySQL database server, updates might be lost during the lifespan of any given second. That’s because all transactions updating the same database row will see the same version timestamp (which points to the beginning of the currently running second).
Prior to the 5.6.4 version, MySQL only supported second precision timestamps.
Incrementing a local version number is always safer because this operation doesn’t depend on any external factors. If the database row already contains a higher version number your data has become stale. It’s as simple as that.
On the other hand, time is one of the most complicated dimension to deal with. If you don’t believe me, check the for daylight saving time handling considerations.
Optimistic locking is all about event ordering, so naturally we’re only interested in the happened-before relationship.
In distributed computing, logical clocks are favored over physical ones (system clock), because networks time synchronization implies variable latencies.
Sequence number versioning is similar to Lamport timestamps algorithm, each event incrementing only one counter.
While Lamport timestamps was defined for multiple distributed nodes event synchronization, database optimistic locking is much simpler, because there is only one node (the database server) where all transactions are synchronized (coming from concurrent client connections).
Distributed computing favors logical clock over physical ones because we are only interested in event ordering anyway.
If you enjoyed this article, I bet you are going to love my upcoming Online Workshops!
- Caching Best Practices with JPA and Hibernate (2.5 hours) on the 30th of September
- High-Performance SQL (4 hours) on the 6th of October in collaboration with Voxxed Days Ticino
- High-Performance SQL (12 hours) starting on the 28th of October in collaboration with Bouvet
Using physical time might seem convenient at first, but it turns out to be a naive solution. In a distributed environment, perfect system time synchronization is most unlikely. All in all, you should always prefer logical clocks when implementing an optimistic locking mechanism.