Seqlock¶
Overview¶
Seqlock is a synchronization mechanism optimized for read-mostly workloads.
Unlike traditional locks, readers do not acquire a lock.
Instead, readers verify whether a write operation occurred while data was being read.
If a write is detected, the reader retries the operation.
This design minimizes reader overhead and provides highly scalable read performance.
Motivation¶
Traditional reader-writer locks allow multiple readers:
to access shared data concurrently.
However, every reader still performs locking operations.
For workloads containing frequent reads and infrequent writes, reader-side locking overhead may become significant.
Seqlock addresses this issue by allowing lockless readers.
Basic Idea¶
A sequence counter tracks writer activity.
Readers use the sequence counter to determine whether a consistent snapshot was obtained.
Reader Flow¶
Reader operation:
If:
the read is valid.
Otherwise:
Reader Pseudo Code¶
Writer Flow¶
Writer acquires exclusive access.
Before modifying data:
Sequence becomes odd.
After modification:
Sequence becomes even again.
Writer Example¶
Readers observing an odd sequence know that a write is in progress.
Consistent Snapshot¶
Seqlock guarantees readers receive a consistent snapshot.
Example:
Writer updates:
A reader should never observe:
Such inconsistent reads are detected through the sequence counter and automatically retried.
Advantages¶
Very Fast Readers¶
Readers:
Only sequence verification is required.
Excellent Read Scalability¶
Reader performance scales well as CPU count increases.
This makes seqlock suitable for frequently accessed shared data.
Limitations¶
Reader Retry Required¶
Readers may need to retry repeatedly during heavy write activity.
Writers Still Require Exclusion¶
Multiple writers cannot update shared data simultaneously.
Writers typically use:
to serialize updates.
Unsuitable for Pointer Lifetime Management¶
Seqlock protects data consistency.
It does not protect object lifetime.
Example:
remain unsafe without additional mechanisms.
Seqlock vs RWLock¶
| Feature | RWLock | Seqlock |
|---|---|---|
| Multiple readers | Yes | Yes |
| Reader locking | Required | Not required |
| Reader blocking | Possible | No |
| Reader retry | No | Yes |
| Writer exclusivity | Yes | Yes |
| Consistent snapshot | Yes | Yes |
Seqlock vs RCU¶
These mechanisms solve different problems.
Seqlock¶
Focus:
Goal:
RCU¶
Focus:
Goal:
Example¶
Seqlock protects:
RCU protects:
where readers may hold references to older objects.
Typical Use Cases¶
Statistics¶
Timekeeping¶
Configuration Snapshots¶
Relationship to RCU¶
Seqlock improves read scalability by removing reader-side locking.
However, it does not solve object lifetime management.
RCU extends lockless-read design further by allowing safe publication and reclamation of shared objects.
Conceptually:
Summary¶
Seqlock is a lockless-read synchronization mechanism designed for read-mostly workloads.
Readers obtain consistent snapshots without acquiring locks.
The trade-off is that readers may need to retry when concurrent writes occur.
Seqlock solves data consistency problems but does not solve object lifetime management problems.
Those problems are addressed by RCU.