Skip to content

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:

Reader 1
Reader 2
Reader 3

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.

Sequence Counter

Even Number
No Writer Active

Odd Number
Writer Active

Readers use the sequence counter to determine whether a consistent snapshot was obtained.


Reader Flow

Reader operation:

Read Sequence
Read Data
Read Sequence Again

If:

Sequence Unchanged
AND
Sequence Is Even

the read is valid.

Otherwise:

Retry

Reader Pseudo Code

do {
    seq_start = read_seqbegin();

    copy_shared_data();

} while (read_seqretry(seq_start));

Writer Flow

Writer acquires exclusive access.

Before modifying data:

Sequence++

Sequence becomes odd.

After modification:

Sequence++

Sequence becomes even again.


Writer Example

Initial
seq = 10

Writer Start
seq = 11

Update Data

Writer Complete
seq = 12

Readers observing an odd sequence know that a write is in progress.


Consistent Snapshot

Seqlock guarantees readers receive a consistent snapshot.

Example:

value_a = 100
value_b = 200

Writer updates:

value_a = 101
value_b = 201

A reader should never observe:

value_a = 101
value_b = 200

Such inconsistent reads are detected through the sequence counter and automatically retried.


Advantages

Very Fast Readers

Readers:

No Lock Acquisition
No Blocking
No Waiting

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:

Spinlock
Mutex

to serialize updates.


Unsuitable for Pointer Lifetime Management

Seqlock protects data consistency.

It does not protect object lifetime.

Example:

Pointer Publication
Object Reclamation

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:

Data Consistency

Goal:

Readers obtain a consistent snapshot.

RCU

Focus:

Object Lifetime Management

Goal:

Readers safely access published objects.

Example

Seqlock protects:

struct statistics {
    uint64_t rx_packets;
    uint64_t tx_packets;
};

RCU protects:

struct device_entry *dev;

where readers may hold references to older objects.


Typical Use Cases

Statistics

Network statistics
Driver counters
System monitoring

Timekeeping

Kernel time structures
Clock data

Configuration Snapshots

Read-mostly configuration tables
System state 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:

RWLock
Seqlock
RCU

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.