Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use a concurrenthashmap that is shared with 100K actors?

Tags:

scala

akka

I have about 100K products that I want to keep in memory. These products can change at a high rate per hour, but at the same time reads will be the majority of the calls, and there can be a delay with getting the most recent version.

I want to create 1 actor per product, but have a shared in-memory store of the product.

Is it safe to use a concurrenthashmap and pass it as a prop to 100K actors?

val products = new ConcurrentHashMap[ProductId, Product](initialCapacity)

So in my actor I will have something like:

def recieve = {
  case GetProduct(id: ProductId) =>
    // lookup in products concurrenthashmap, if not there, read from datastore and return 
    //
  case UpdateProduct(id: ProductId) => ???
}

When there are updates, there could be 100-500K per hour. There can be a delay in the update, that's not an issue. So to distribute the updates and prevent locks, 1 actor per product is what I am thinking. I just want to make the cache global across all actors.

Is this design sound?

like image 542
Blankman Avatar asked Jan 24 '26 13:01

Blankman


1 Answers

ConcurrentHashMap is safe to be shared, the number of threads does not matter. But why do you create 100k actors when you keep the state elsewhere anyway? An Actor exists because it owns its state; if that is not the case then the Actor should not exist.

So either you keep your data in a ConcurrentHashMap, but then you need only as many threads (actors) as you have CPU cores to make the accesses. Or you ditch the CHM and store each of your products in its own designated Actor. The decision depends on the nature of the operations you want to perform: if you want to interact only with a single product at a time, then Actors are a great way of modelling that. If you want to interact with a collection of products at a time, then use something else.

like image 58
Roland Kuhn Avatar answered Jan 27 '26 03:01

Roland Kuhn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!