liveBook · Manning

Synchronization

As explained in Latency

A synchronization primitive is an execution mechanism that coordinates concurrent operations and controls access to shared resources.

Synchronization coordinates concurrent operations on shared data so that clients or threads do not read stale values or corrupt data. Its cost matters in latency-sensitive systems: coordinating access can add overhead and increase tail latency. section 5.1 section 8.0

A lock controls access to a shared resource. Distributed locks can require additional network round trips; mutexes and spinlocks can also be expensive when threads share data. Atomic operations manipulate shared memory in a way that appears instantaneous to other threads or processors. They are building blocks for higher-level mechanisms such as mutexes, read–write locks, and spinlocks. section 5.1 section 8.0 section 8.3

Reduce the need for coordination before replacing a synchronization mechanism. Partitioning separates data into independently accessible buckets, making concurrent writes less likely to conflict. Partitions can also run on different CPU cores or machines. When data must remain shared and locks are too costly, wait-free synchronization offers an alternative, often isolated in low-level components such as request processing or job queues. section 5.1 section 8.0

Partitioning users by location

A large user dataset can be divided into geographic partitions, such as New York and Sydney. Clients accessing different partitions can work independently, reducing the coordination needed for concurrent updates. section 5.1

Synchronization also propagates optimistic updates between clients. An application can show changes immediately, store them in a local shadow write queue, and synchronize asynchronously through a server or peer-to-peer protocol. If multiple clients have changed the same data, their updates must be reconciled without blocking the UI. section 11.3.2

Figure 11.4 Optimistic views build on a base database and a shadow write queue. In this example, we have a database table of tasks with ID, task, and completion status. The application writes locally to a shadow write queue, which is combined into an optimistic view, shown on the right.

The figure shows how a shadow write queue combines with a base database to form an optimistic view. section 11.3.2

Last-writer-wins resolves a conflict by selecting the update ordered last. Physical clock drift can make that ordering inaccurate. A logical clock, such as a Lamport timestamp or vector clock, can order events, with a node ID breaking ties. For Lamport timestamps, each process maintains a counter, sends its value with messages, and on receipt sets its counter to the greater of the local and received values, then increments it. section 11.3.2

How Latency covers it

How deep it goes
Defines atomic operations as synchronization building blocks and explains Lamport counter updates for reconciling optimistic writes.
Examples
Uses geographically partitioned user data, request queues, and a task database with a shadow write queue.
What you can do after
Evaluate coordination costs and choose between partitioning, locks, wait-free techniques, and asynchronous reconciliation for the described scenarios.
How it connects
Connects synchronization to mutual exclusion, atomic operations, partitioning, optimistic updates, and logical clocks.

Also explained in

Compare how 13 books explain synchronization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Latency. The section links point to the parts of the book it draws on.