Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array vs In-Memory database

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.

  • Arrays reserve a contiguous block of memory. The data access might be faster. However, if the data grows bigger, finding a contiguous block of memory might be difficult.
  • In functional programming, if a function adds/removes elements in Array/collection, it is changing the application state or in other words, it produces a side effect. This is not desirable as parallelizing this function might be difficult.
  • Concurrent updates: If multiple requests are updating the array, we might end up having inconsistent data or some updates might be lost, due to race conditions. Thus, one needs to implement locking mechanism for updates. In database(relational), you can use transactions that should solve this problem. On the other side, relational databases support transactions, which should take care of inconsistency problem.

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.

like image 403
Muhammad Avatar asked Sep 23 '26 21:09

Muhammad


1 Answers

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?

like image 148
Mario Galic Avatar answered Sep 26 '26 17:09

Mario Galic



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!