Search code examples
javamultithreadingclojureimmutant

How does a multi-threaded web server affect a Clojure program?


I know that you can build multi-threaded Clojure program explicitly using the tools in java.util.concurrent.Executors and language constructs like agents and atoms. However, if I write a Ring app, how will a multi-threaded server (like Jetty or Immutant) change the way it runs? If I do some initialization (e.g. loading configuration, setting up a database connection pool) before I start serving requests, will I end up with duplicated, possibly inconsistent work in different threads? Will each request be handled by one thread?


Solution

  • Your Clojure code will lie dormant w/o any thread until a web request arrives (essentially you are writing a library/subroutine, not a main program). Jetty et al will assign a thread to invoke your Clojure code, supplying the relevant parameters. If you make any DB requests, they typically go out over a socket to the DB machine, which will assign a thread to handle your request and return the relevant results. Your thread in Jetty will receive the data from the DB and continue processing from there.

    The important part is that each piece of information is a treated like an immutable message. The message can be copied and passed from one system to another without losing its meaning. It doesn't matter how many copies are made and deleted, only that one copy gets to the final destination.

    A simple webapp typically doesn't need to do any multithreading of it's own, since the thread handling & synchronization provided by Jetty and the DB are usually all that is needed. If you do your own multithreading for something, you mostly just need to worry about your own internal synchronization.

    You can see some good information online at The Clojure Cookbook.

    I can also highly recommend the book Web Development with Clojure.