Reputation: 527
How do you think Scala can help us to write a distributed memory service like Redis (but multi-thread)?
Scala programming encourages us to use immutable data in order to avoid shared data and race conditions, however in this kind of data storage immutable data can't help us (I think).
Blocking threads would be the solution?
Upvotes: 0
Views: 157
Reputation: 23105
Scala deals with immutable data and mutable data equally well.
Sure it's highly encouraged to use immutable data - for any data which can be shared across threads/actors.
But it's also highly encouraged to use mutable data dedicated to individual threads/actors - i.e. in a context which can never be shared across threads/actors.
A simple high-level concurrency pattern is:
Actor
:
Actor
s
Actor
in a message to Another Actor
!
and !!
)Future[T]
as part of the initiation. Then specify response handling code via Future.onComplete
(or even better via Future.flatMap/Map/withFilter/Recover
and their for
-comprehension equivalents - which are handy 'monadic'-wrappers around onComplete
).Upvotes: 3