nebulex icon indicating copy to clipboard operation
nebulex copied to clipboard

When a new node joins, the cache is not copied (Replicated)

Open cospin opened this issue 1 year ago • 2 comments

Hello, I am new to Nebulex and I'm testing the Replicated adapter, but I notice that when a new node joins, it does not copy the cache from the other node. In production, DNSCluster is used to connect the nodes dynamically, but in local tests, I'm simply using Node.connect/1.

This is my setup:

defmodule Core.ReplicatedCache do
  use Nebulex.Cache,
    otp_app: :core,
    adapter: Nebulex.Adapters.Replicated
end
config :core, Core.ReplicatedCache,
  primary: [
    gc_interval: :timer.hours(12),
    gc_memory_check_interval: :timer.seconds(10),
    max_size: 1_000_000,
    allocated_memory: 2_000_000_000
  ]
def start(_type, _args) do
    children = [
      Core.Repo,
      {DNSCluster, query: Application.get_env(:core, :dns_cluster_query) || :ignore},
      {Phoenix.PubSub, name: Core.PubSub},
      {Core.ReplicatedCache, []},
      Core.Telemetry,
      Core.Endpoint
    ]
    opts = [strategy: :one_for_one, name: Core.Supervisor]
    Supervisor.start_link(children, opts)
end

I'm starting one local node and creating a new cache record:

iex --name [email protected] --cookie mycookie -S mix

Core.ReplicatedCache.put("key", "value2")
:ok

Then I join a second node, where it can't access the already created key:

iex --name [email protected] --cookie mycookie -S mix

Node.connect(:"[email protected]")
true

# key not found, so no cache copy?
Core.ReplicatedCache.get("key")
nil

But new writes works fine between the two:

# put newkey from any node
Core.ReplicatedCache.put("newkey", "value2")
:ok

# get works on both nodes:
Core.ReplicatedCache.get("newkey")
"value2"

So, my conclusion is that when a new node joins, it is not copying the cache. Am I overlooking something, or am I doing something wrong?

Thanks!

Elixir 1.17.2 (compiled with Erlang/OTP 27)

cospin avatar Sep 04 '24 17:09 cospin

Ok, I did a local test but this time adding libcluster, and it seems to work well :) I guess the problem was my rudimentary use of the cluster with Node.connect/1 (I'm also new to clustering).

I didn't see any of this in the documentation, so, pretty sure I'm doing something wrong with the cluster formation. Any insights? 😅 Anyways, seems like libcluster is a nice addon.

cospin avatar Sep 04 '24 20:09 cospin

Hey! Yeah, the documentation doesn't mention it, but the distributed adapters rely on "Distributed Erlang/Elixir" and they need the cluster properly set up to work correctly. And yeah, libcluster is the way to go. I agree it would be good to have some documentation about it.

cabol avatar Oct 19 '24 09:10 cabol