Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm writing a c10k-style service and am trying to evaluate Clojure's performance. Can Clojure agents handle this scale of concurrency with its thread-based agents? Other high performance systems seem to be moving towards async-IO/events/greenlets, albeit at a seemingly higher complexity cost.


Suppose there are 10,000 clients connected, sending messages that should be appended to 1,000 local files--the Clojure service is trying to write to as many files in parallel as it can, while not letting any two separate requests mangle the same single file by writing at the same time.

Clojure agents are extremely elegant conceptually--they would allow separate files to be written independently and asynchronously, while serializing (in the database sense) multiple requests to write to the same file.

My understanding is that agents work by starting a thread for each operation (assume we are IO-bound and using send-off)--so in this case is it correct that it would start 1,000+ threads? Can current-day systems handle this number of threads efficiently? Most of them should be IO-bound and sleeping most of the time, but I presume there would still be a context-switching penalty that is theoretically higher than async-IO/event-based systems (e.g. Erlang, Go, node.js).

If the Clojure solution can handle the performance, it seems like the most elegant thing to code. However if it can't handle the performance then something like Erlang or Go's lightweight processes might be preferable, since they are designed to have tens of thousands of them spawned at once, and are only moderately more complex to implement.

Has anyone approached this problem in Clojure or compared to these other platforms? (Thanks for your thoughts!)

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

agents use an expandable thread pool rather than simply starting a thread per task. so the number of active threads will be cc close to the number of active agents. 1000 threads i seems reasonable for a modern computer. you will likely need to increase the max open file descriptors (assuming Linux)

share|improve this answer
    
Thanks! If you are curious, it appears that modern NPTL threads are quite capable, e.g. see http://stackoverflow.com/a/4058620/37610 –  elliot42 Jan 8 '13 at 3:57
add comment

As of the upcoming Clojure 1.5, you can use custom ExecutorServices (through set-agent-send-executor! and set-agent-send-off-executor!).

Doing so you'll be able to cap your number of threads -- if needed, 1k mostly sleeping threads doesn't seem out of reach for the JVM. Beware of the stack size though.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.