I am building a REST API where I need to save 2D data (x: Double, y: Double) in application memory (no need to persist the data on disk). The no of records/data objects can grow/shrink in future. The user can add/remove data objects, but can't edit them. I have 2 choices. Either save the data ain n array/collection or use an in memory database such as H2.
The question is, which approach should I choose and why? Here is what I know already or have found until this point. Feel free to disagree and share your thoughts.
What am I missing here, does saving the data in database bring any other advantage? Creating a database which will contain only 1 table with 2-3 columns seams to be an overkill though.
Thanks in advance.
Consider concurrent Maps which provides atomic operations and
can be accessed by several threads at once
for example
import java.util.concurrent.ConcurrentHashMap
import scala.jdk.CollectionConverters._
case class User(id: Int, name: String)
val chm: collection.concurrent.Map[Int, User] = new ConcurrentHashMap[Int, User]().asScala
chm.addOne(1 -> User(1, "Picard"))
Another option is to wrap mutable state with Akka Actor which guarantees
processing of one message happens before processing of the next message by the same actor
perhaps something like so
class MyActor() extends Actor {
private val _mutableSate = mutable.Map[Int, User]()
def insertUser(u: User): Unit = _mutableSate.addOne(u.id, u)
}
Regarding whether one should use collection or in-memory database, IMO, this is an engineering decision which has tradeoffs but no clear answers. For example, one could apply principle of minimal power reasoning and say if collections solve the problem adequately then there is no need to go with more powerful database solution. On the other hand, one should also consider does the solution scale. For example, will collections have sufficiently expressive querying mechanism once there are multiple tables which need to be joined?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With