Designing with Topics in Clojure

Designing systems that obey the Open/Closed principle is tough. Here’s a nugget of wisdom about using topics to create a system that’s more fluid in terms of extensibility. I’ll demonstrate in Clojure.

A topic is a type of message queue where all subscribers consume every message. An example of this in the small is Clojure’s juxt function.

What happens when your functions in juxt are computationally expensive? What happens if you constantly add new functions? A topic makes great sense for both of these cases. It allows you to add new functions to the computation without having to modify the innards of any existing code. Just add a new subscriber, and away you go.

The following example is rather contrived, but I wanted to keep it as simple as possible to get the point across. I used Clamq ontop of ActiveMQ. As you read, visualize how simple it is to add another computation.