Distributed InMemory Database

Following code builds a consistent hashing based Get/Put requests backed by an ConcurrentHashMap in memory.

package in.ashwanthkumar.suuchi.example

import java.nio.ByteBuffer

import in.ashwanthkumar.suuchi.client.SuuchiClient
import in.ashwanthkumar.suuchi.router.ConsistentHashingRouting
import in.ashwanthkumar.suuchi.rpc.Server.whoami
import in.ashwanthkumar.suuchi.rpc.{Server, SuuchiPutService, SuuchiReadService}
import in.ashwanthkumar.suuchi.store.InMemoryStore
import io.grpc.netty.NettyServerBuilder

object DistributedKVServer extends App {
  val port = args(0).toInt
  val PARTITIONS_PER_NODE = 100
  val REPLICATION_FACTOR = 2

  val routingStrategy = ConsistentHashingRouting(REPLICATION_FACTOR, PARTITIONS_PER_NODE, whoami(5051), whoami(5052), whoami(5053))

  val store = new InMemoryStore
  val server = Server(NettyServerBuilder.forPort(port), whoami(port))
    .routeUsing(new SuuchiReadService(store), routingStrategy)
    .withParallelReplication(new SuuchiPutService(store), REPLICATION_FACTOR, routingStrategy)
  server.start()

  server.blockUntilShutdown()
}

Let's break down the above code step by step.

To see this recipe in action, you might also want to look into the client which can talk to this service - DistributedKVClient.