I program a backend web application in Clojure using, among other things:
I know about the performance benefits generated by events, non-blocking stacks, such as the ones you find in NodeJS, and the Play Framework ( this question helped me), and how it gives a much better payload. For this reason, I am considering the possibility of creating a backend asynchronously using core.async.
My question is: Can you recreate the performance benefits of non-blocking web stacks using core.async on top of locking client / driver libraries?
developed:
What I am doing now is regular synchronous calls:
(defn handle-my-request [req] (let [data1 (db/findData1) data2 (db/findData2) data3 (s3/findData3) result (make-something-of data1 data2 data3)] (ring.util.response/response result)) )
What I plan to do is transfer any call using IO to the block threadand synchronize it inside the block go,
thread
go
(defn handle-my-request! [req resp-chan] ;; resp-chan is a core.async channel through which the response must be pushed (go (let [data1-ch (thread (db/findData1)) ;; spin of threads to fetch the data (involves IO) data2-ch (thread (db/findData2)) data3-ch (thread (s3/findData3)) result (make-something-of (<! data1-ch) (<! data2-ch) (<! data3-ch))] ;; synchronize (->> (ring.util.response/response result) (>! resp-chan)) ;; send response )))
Does it make sense to do it this way?
I do this because such best practices that I have found, but their performance advantages are still a mystery to me. I thought the problem with synchronous stacks is that they use one thread for each request. Now it seems that they are using more than one.
Thank you in advance for your help, you have a wonderful day.
- , . 100 , 300 " " 3- . , , , . , , -, , 100 .
, , , 300/2 = 150 .
, , , - -, / , , .. 200 , 100 3 , 100 6 . , , 5-6 . . , , .
, , - -, -, . Clojure , , , , , , .
, findData1,2 3 , .
, , findData2 findData1, findData3 findData2, , , core.async