Socketelx.Backend.Redis.Topology (socketelx v0.5.2)

Copy Markdown View Source

Finds and follows the Redis Cluster node that owns this app's keys.

In cluster mode every key of an app shares one hash tag (see Socketelx.Config), so they all live in one slot, on one node. That keeps the multi-key Lua scripts working, and it means this process only has to answer one question: which node owns that slot right now?

It asks with CLUSTER SLOTS, connects Redix (commands) and Redix.PubSub (broadcasts) to that node under the usual names, and moves them when the answer changes: a failover, a resharding, or the node going away. It starts from the Redis URL, plus any :redis_cluster_nodes, plus every master it has seen.

Callers never talk to this process on the hot path. Socketelx.Backend.Redis uses the connection directly and only asks for a refresh/1 after a MOVED or similar answer.

Summary

Functions

Returns a specification to start this module under a supervisor.

The hash slot (0..16383) of a key, honouring {hash tags} like Redis does.

Re-reads the slot map and moves the connections if the owner changed. Blocks until done (a few milliseconds normally). A MOVED answer names the new owner; pass it as hint ({host, port}) and the connections move there at once, without waiting for the slot map to catch up (it can lag a moment behind a failover). :ok when the owner is known, :error when no node could be reached.

Same, without waiting. Used after connection errors.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

key_slot(key)

The hash slot (0..16383) of a key, honouring {hash tags} like Redis does.

refresh(name, hint \\ nil)

Re-reads the slot map and moves the connections if the owner changed. Blocks until done (a few milliseconds normally). A MOVED answer names the new owner; pass it as hint ({host, port}) and the connections move there at once, without waiting for the slot map to catch up (it can lag a moment behind a failover). :ok when the owner is known, :error when no node could be reached.

refresh_async(name)

Same, without waiting. Used after connection errors.