RwLock in Actix Data state

Hi! :slightly_smiling_face:

I have an Actix state like this:

struct Store {
  resource_a: HashMap<String, A>,
  resource_b: HashMap<String, B>,

// Before to start the server
let store = Store::new();
let store_data = actix_web::web::Data::new(RwLock::new(store));

Furthermore, the store is updated periodicaly (10 seconds) by tokio tasks who will get back a writer on the store: store.write()

In each endpoints of the app, the store is read and sometimes updated.

With RwLock my code compile and run like a charm on my computer. But I'm a bit stressed about when the app will be thousands requests per second. I'm wondering if each requests could read the store (not locked).

What do you tkink about RwLock? Is there a good choice?

I tried with Mutex but he doesn't make the difference between read and write.

The main danger with an RwLock is that write locks can be starved. You might want to consider something like dashmap instead of using your own lock.

1 Like

Thanks again @alice for you answer!
Just for my knowlege, what do you mean by write locks can be starved?

There's a sample rust code block at the end of the article.

Starved means that it is kept locked for a long time. This can happen if the number of read locks stays positive due to the lock often being locked and unlocked, but there always being at least one read lock left.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.