arc-swap — atomic Arc swaps vs RwLock for config

Contents

  1. 1. RwLock
  2. 2. arc-swap

In Rust, config that threads read while another thread updates it often shows up as Arc<RwLock<T>>. You can also reach for arc-swap. What’s the difference?

Typical shape: one thread updates config while others keep reading—they should see the new snapshot without restarting the process.

RwLock

First implementation with RwLock:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use std::sync::{Arc, RwLock};
use std::thread;
use std::time::Duration;

fn main() {
let config = Arc::new(RwLock::new("Initial Config".to_string()));
let shared_config = Arc::clone(&config);

let reader_thread = {
thread::spawn(move || loop {
// 1. read config
let current_config = shared_config.read().unwrap();
println!("Reader thread: {}", *current_config);
thread::sleep(Duration::from_secs(2));
})
};

// Update the configuration after 1s
thread::sleep(Duration::from_secs(1));

println!("Updating config");
// 2. write config
let mut current_config = config.write().unwrap();
*current_config = "Updated Config".to_string();
println!("Updating config done");
reader_thread.join().unwrap();
}

Run it and you may see the reader stuck on the old config — and "Updating config done" never prints. Why?

This is a deliberate footgun: a read guard keeps the lock held. More readers can pile on, but no writer can take the lock until every read guard is dropped.

Scope the guards so they release before you write:

1
2
3
4
5
6
7
8
9
10
11
12
13
// 1. read config
{
let current_config = shared_config.read().unwrap();
println!("Reader thread: {}", *current_config);
}

...
// 2. write config
{
let mut current_config = config.write().unwrap();
*current_config = "Updated Config".to_string();
println!("Updating config done");
}

Then you get what you expect:

1
2
3
4
5
Reader thread: Initial Config
Updating config
Updating config done
Reader thread: Updated Config
...

arc-swap

With RwLock, you have to think about how long you hold locks. Reads and writes are not one atomic step, so the code is easier to get wrong and deadlock.

arc-swap is aimed at that pain: swap the whole Arc atomically so readers and writers do not fight over a lock guard lifecycle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use arc_swap::ArcSwap;
use std::sync::Arc;
use std::thread;
use std::time::Duration;

fn main() {
let config = Arc::new("Initial Config".to_string());
let shared_config = Arc::new(ArcSwap::new(config));

let reader_thread = {
let shared_config = Arc::clone(&shared_config);
thread::spawn(move || loop {
let current_config = shared_config.load();
println!("Reader thread: {}", current_config);
thread::sleep(Duration::from_secs(1));
})
};

thread::sleep(Duration::from_secs(2));

let new_config = Arc::new("Updated Config".to_string());
shared_config.store(new_config);

reader_thread.join().unwrap();
}

The flow is more direct. For read-mostly, occasionally-updated data, arc-swap is often worth trying.

Questions? Leave a comment below or email: newbvirgil@gmail.com