Add configurable :keys option for the underlying Registry#210
Add configurable :keys option for the underlying Registry#210studzien wants to merge 1 commit intophoenixframework:mainfrom
Conversation
| one of `:unique`, `:duplicate`, `{:duplicate, :pid}`, or | ||
| `{:duplicate, :key}` (defaults to `:duplicate`, which is equivalent | ||
| to `{:duplicate, :pid}`). With `:unique`, subscribing the same | ||
| process to the same topic twice returns |
There was a problem hiding this comment.
I thought unique would only allow a single process per topic, no? It probably doesn't make sense? Perhaps the option should be group_by: :pid | :key?
There was a problem hiding this comment.
Yes, that's right. It doesn't make much sense, but I can think of scenarios where it does (like limiting to one subscriber per user topic).
The main reason I went with just passing the option directly to Registry was that we can't use the {:duplicate, _} form in Elixir < 1.19 (it crashes in CI runs).
Would making an Elixir-dependent test be the way to go here?
There was a problem hiding this comment.
Default to :duplicate and use the tuple only of group_by is given. Let’s not support unique :)
There was a problem hiding this comment.
Changed. Also added a test dependent on the Elixir version, but maybe we don't need that
Selects how the underlying Registry partitions subscriptions. Defaults
to :pid (current behavior). Accepts :pid or :key.
- :pid (default) groups entries by subscriber pid; best when topics
have many subscribers each. Translates to keys: :duplicate.
- :key groups entries by topic so key-based lookups touch a single
partition; best when there are many topics with few subscribers
each. Translates to keys: {:duplicate, :key}, which requires
Elixir 1.19+ (see elixir-lang/elixir#14654).
Invalid values raise ArgumentError at supervisor start.
Refs: phoenixframework#198
81a7706 to
971441a
Compare
Summary
Adds a
:keysoption toPhoenix.PubSub, forwarded to the underlyingRegistry. Defaults to:duplicate(no behavior change). Accepts:unique,:duplicate,{:duplicate, :pid}, or{:duplicate, :key}.Motivation
{:duplicate, :key}, added in elixir-lang/elixir#14654 (which cites #198 as motivation), shards by topic key — better for workloads with many topics and few subscribers per topic, since key-based lookups touch a single partition. The default:duplicate(≡{:duplicate, :pid}) is still best when topics have many subscribers each.See
Registry.start_link/1for trade-offs.Notes
:uniqueis accepted but rarely useful for pub/sub (caps each topic at one subscriber).:uniquerejection,:duplicatedefault delivery, and:uniquebroadcast.