Use exclusive queue as distributed lock?

petegehrman
petegehrman Member Posts: 43 ✭✭

I was wondering if I could use an exclusive queue as a rudimentary distributed "headship" mechanism. If there are multiple queue consumers that create a flow to the queue, my thought is that whoever is the active consumer would considered the head node, and if they shut down then headship would automatically be transferred to a different node. Is there any way via JCSMP for a consumer to know if a flow is active or passive?

Comments

  • petegehrman
    petegehrman Member Posts: 43 ✭✭

    Just thought of an additional way to implement this with an exclusive queue, without needing to know explicitly that you're active or passive. If I prepopulate the queue with a single message, and each connected consumer creates a flow but never acks any messages, then receiving that message would indicate that you're the active consumer. If the active (head) consumer shuts down then another consumer will become active and receive that same message, indicating that it's the head and can perform headship tasks. Does that sounds reasonable?

  • amackenzie
    amackenzie Member, Employee Posts: 268 Solace Employee

    There is an activeFlowIndication for a consumer to determine (or set) the state of the flow.

  • marc
    marc Member, Administrator, Moderator, Employee Posts: 959 admin

    Hey @petegehrman, interesting thoughts for sure! As @amackenzie mentioned we do have the concept of ActiveFlowIndication that allows a consumer to know the state of their flow.

    Are you essentially thinking about using this as a mechanism for leader election across multiple apps? If so I'd suggest checking out what @GreenRover contributed to the SolaceCommunity. He created the Spring Solace Leader Election project which allows for leader election for spring integration using Solace exclusive queues :). He also took the time to create some great docs and covered a few use cases that the project might be useful.

  • Aaron
    Aaron Member, Administrator, Moderator, Employee Posts: 636 admin

    @petegehrman yes, this is a super useful pattern in Solace, essentially a network mutex. We use this heavily in various production scenarios where you want to ensure only one active participant... e.g. HA publishers: have multiple publishers available to publish some stream of data onto Solace, but only the guy who has the ACTIVE_FLOW_NOTIFICATION from their shared exclusive queue is the live publisher. (they're only using the queue to determine who's active, not subscribing to anything).

    Just did some Googling, and I'm surprised that I don't see a Solace blog about this well-known pattern. Huh! Maybe one of us should go write one..! 🙃

  • petegehrman
    petegehrman Member Posts: 43 ✭✭

    Cool, thanks everyone for the information. I guess I was on the right track with the idea, but I missed the documentation on the FlowEventHandler. This is going to be very useful for us!