Crypto and the event loop

178 views
Skip to first unread message

Sanjeev Koranga

unread,
Aug 29, 2014, 8:02:25 PM8/29/14
to nod...@googlegroups.com
Hi 

When I use the node crypto module, does it block the event loop while processing or spawn a different thread in C++?


-sanjeev

mscdex

unread,
Aug 30, 2014, 7:29:28 PM8/30/14
to nod...@googlegroups.com
On Friday, August 29, 2014 8:02:25 PM UTC-4, Sanjeev Koranga wrote:
When I use the node crypto module, does it block the event loop while processing or spawn a different thread in C++?

Currently there are no non-blocking counterparts to the existing functions in the built-in crypto module for node, except for `randomBytes()`/`pseudoRandomBytes()` which run in a separate thread if a callback is passed to those functions.

Sanjeev Koranga

unread,
Aug 31, 2014, 2:28:09 PM8/31/14
to nod...@googlegroups.com
So if an app is doing lot of crypto, would you advice using child processes/fibers/webworkers for running crypto functionality?

Floby

unread,
Sep 1, 2014, 4:45:10 AM9/1/14
to nod...@googlegroups.com
Yes, your best bet is probably to use a dedicated worker and communicate with it with worker.send()

Fedor Indutny

unread,
Sep 2, 2014, 12:39:08 PM9/2/14
to nod...@googlegroups.com
This totally depends on what kind of crypto are you going to be using.

For example the 2048bit RSA decryption takes 1ms on my MBP, so using it on every request will limit your RPS to around 700-800.


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/0980ed05-2d1b-4430-bfbd-e5e011c48410%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Matt

unread,
Sep 2, 2014, 12:39:13 PM9/2/14
to nod...@googlegroups.com

On Sun, Aug 31, 2014 at 2:28 PM, Sanjeev Koranga <sanjeev...@gmail.com> wrote:
So if an app is doing lot of crypto, would you advice using child processes/fibers/webworkers for running crypto functionality?

It shouldn't really be necessary. What APIs are you going to use?

Alex Kocharin

unread,
Sep 2, 2014, 12:39:18 PM9/2/14
to nod...@googlegroups.com
 
Yes, child processes or webworkers (which usually implemented using child processes anyway).
 
Fibers run in the same thread, so this isn't really a solution.
 
 
It depends on the app though. Sometimes you can just spawn a lot of instances in a cluster using something like pm2, and while one blocks, clients can use others.
 
 
01.09.2014, 06:45, "Sanjeev Koranga" <sanjeev...@gmail.com>:
--

Sanjeev Koranga

unread,
Sep 2, 2014, 1:36:59 PM9/2/14
to nod...@googlegroups.com, fe...@indutny.com
I am using createCipheriv, createHash api's and on an average doing 4-5 crypt-decrypt per request. Major consumer of crypto in my app is session (SHA1 on session size ≈20-30k). What I am seeing is only 50-100 rps max and 90+% CPU in perf testing and I was suspecting heavy crypto use for it. Let me move crypto functionality to child processes and compare the result.

-sanjeev
Reply all
Reply to author
Forward
0 new messages