Latches are one of the ways in which the Oracle database serializes access to shared resources. The differences between a latch and a lock:
  • Latches are normally very short lived, and are implemented by the hardware's test-and-set instruction. Locks cover longer-lasting operations, and are implemented in software.
  • Latches are waited for by spinning, testing the status of the lock a number of times in rapid sucession, sleeping, then repeating. The sleep time doubles (called "backing off") each iteration until a maximum is reached, then the process gives up trying to get the latch and reports an error. For this reason, latches are sometimes referred to as "spin locks". While waiting for a lock, the process is placed into a queue, and the code responsible for unlocking also transfers control to the next process waiting for the lock, which can yield all its processor time while waiting.
  • Latches are binary, they are either set or not. Locks have degrees of locked-ness, for example shared or exclusive.
  • Latches are specific to the local node, whereas locks are visible system-wide. An essential part of a VMS or Oracle cluster is the Distributed Lock Manager.

A third technique is the semaphore, devised by Dijkstra. They have two operations, increment and decrement, and can have any value that is positive or zero. Waiting processes attempt to decrement the semaphore, and processes that have finished using the resource increment the semaphore. If decrementing the semaphore would make it take a negative value, the process is blocked until the semaphore has been incremented. Oracle uses semaphores to communicate between processes, and for latch sleeps.