Why should i use Mount or Component

Hi everyone, i’m learning basic web development with clojure. I have some question.
In luminus template, they use mount or component library to manage db connection and I see many people use it too.
Why should i use mount and what is common scenarios to use it.

Thanks

Using mount or one of the other libraries for stateful components is not strictly necessary. What they give you is a way to tear down and rebuild the stateful parts of your application without restarting the REPL. You might have seen this old blog post by Stuart Sierra on the reloaded workflow, which inspired these systems: http://thinkrelevance.com/blog/2013/06/04/clojure-workflow-reloaded

I usually encapsulate the database connection pool in one component, the web server in another, and declare the database component a dependency of the web server component.

I think I saw another topic here on Clojureverse with some discussion on the pros and cons of component systems. Some alternatives were also highlighted in that topic, e.g. using delay for encapsulating components.

Edit:

Found the topic: How to manage database connection in Clojure? . Look at the comments by @didibus for a description of using delay and promise in this context.

1 Like
  • When to use: When you’re keeping state (a db connection, a server binding a port)
  • Why to use:
    • When you write the code, you’re able to reload your system without resetting the REPL with tools like integrant.repl
    • When you run the code, you have a standard way of managing stateful references.

I’m linking to Integrant because I prefer it, but Mount and Component approach the same problem.

1 Like

First of all, I’d use integrant. I’ve worked in production with all three and integrant is the most capable and overall pleasing to work with.
Yes, these state management libs are not strictly necessary, but they let you define each state independently and then combine them somewhere else into a system of dependencies. They take care of the order of starting and stopping the dependencies.
Integrant lets you define that system in a configuration file, and integrant-repl lets you read that configuration on REPL restart so you can quickly try different system topologies. For example you could define channels as states and pipe them with integrant, changing the piping, the buffer types, the processing functions etc.

2 Likes