forte
Does Phoenix PubSub respect the order of messages received
Hi, I have a question regarding the Phoenix PubSub process as it’s used by Channel sockets. Are the sockets that receive broadcast! messages via the PubSub process guaranteed to receive and handle messages sent to the PubSub process (via broadcast!) in the order that the PubSub process received them?
I’m assuming that since there’s only one PubSub process then there’s no opportunity for, say, 2 messages each sent via broadcast! in 2 seperate socket processes, one at time = T1 and the other at T2, to be received by sockets subscribed to the PubSub out of order (say T2 and then T1).
Thanks.
Marked As Solved
benwilson512
it 100% can happen in not the order you expect, particularly if A and D are on different servers.
No, this is a distributed state issue. You either need to bring your own coordinating entity, or use something like a CRDT. This latter thing is actually what Phoenix Presence does, although it is by no means simple.
Also Liked
benwilson512
There is no global pubsub process. The VM guarantees message order for any messages sent between two process. So, if process A sends process B, :foo and then :bar, process B is guaranteed to receive them in that order. However if A sends :foo to B and then C sends :bar to B, it’s possible it could see them in any order, depending on how close together those sends happened.
When you broadcast!("some topic", value) you are basically acting as Process A, and you are sending to all processes that are subscribed to that topic. So, if you broadcast :foo and then broadcast :bar, that will arrive in the correct order to everyone.
benwilson512
Exacerbated, but it’s 100% possible even with one server for a variety of reasons.
For one thing, this ^ is a flawed assumption. Subscribers are stored in Registry, which uses a sharded set of :ets tables in :duplicate_bag mode. There is no real order guarantee here at all between any two invocations, plus the list itself can change between those invocations in a way that may effect the order.
Even if the order was a simple list somewhere, the BEAM simply doesn’t provide the guarantee you’re looking for. If the time between A and D messaging C is sufficiently small, then small differences like which scheduler each process is currently on can cause them to arrive out of order. Message order is only guaranteed between one process and another.
forte
Thanks @benwilson512. I saw a PubSub.local0 genserver process in the erlang observer process tree and thought maybe it was “sending” messages to the socket processes I saw it was monitoring. But that’s not very elixir-ey. If anything, that PubSub process (or some other entity) would probably keep a registry of socket genserver pids within which broadcast! calls would ultimately call functions.
All that aside, I think I get it. Thanks a lot.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









