r/Clojure • • May 16 '16

Ask /r/Clojure: Secure web frameworks for building REST Api?

I'm evaluating Pedestal and I guess I like it's built-in security features although I'm not sure whether this is just a fluff piece of somebody has actually done a thorough auditing.

I'm working in fintech space and since I'm using datomic I'm pondering what my backend endpoint api stack is going to be. I like the appeal of keeping everything clojure to save the cognitive cost of switching between backend server / datomic, and all in all I've gone through the clojure essentials yesterday and was just blown away by it's concise and straight forwardness, no ambiguity, and see it as a powerful language for dealing with lot of data, and more importantly it feels like the first time I began learning programming, but I digress, I'm turning to the community to get some advice preferably from people who work in fintech.

So should I use Pedestal + Datomic?

I'm sort of wary about using "collection of libraries" that people seem to advocate, and this arises from my experience of using "microframeworks" like Flask in the Python world and ending up googling "how to get stuff I got for free in full MVC frameworks but in Flask + 3rd party libraries that seem sporadically updated by individuals". It left a very bad taste and I've made some pre-judgement here coming to clojure....

However, if I could attain a better security feature by employing the "collection of libraries from the shelf" to build backend API, I might consider it seeing how tight the community is and not flaky like Flask....but again if Pedestal or some full framework on clojure lets me achieve higher productivity and security features then I'd much much prefer the full framework approach.

Anyways I'm going through the more advanced topics on clojure and will come back time to time to check on people's replies.

Thanks y'all.

7 Upvotes

57 comments sorted by

View all comments

Show parent comments

3

u/ohpauleez May 20 '16

Your point about thread pools is well heard, but it's leaving out a lot of subtle and important details. It's common to create thread pools based upon workload and resource utilization - a thread pool for accepting connections, a worker thread pool, a thread pool for I/O bound tasks, and so on. In the Ring model, you're forced to synchronously process your task, always holding a thread in the initial pool that you start in (regardless if you start tasks on the other thread pools).

In the interceptor model, your request can migrate across thread pools, freeing up the thread in one pool, while you occupy/use a thread in another pool. What's more, when your response is bigger than the HTTP buffer, your Ring handler must occupy a critical server thread until the client finishes consuming all of the data, often 100ms-1000ms worth of idle time. With NIO-based responses, you can free that thread and have the Container signal you when it's ready to send more data.

In Pedestal, that's as simple as returning a core.async channel, ByteBuffer, or ReadableByteChannel as your response body from your handler function.

1

u/yogthos May 20 '16

At the end of the day the work still has to be done. I agree that in some situations the async model can lead to higher responsiveness. However, in the vast majority of situations the thread pool approach works perfectly fine. The advantage is that it's much easier to reason about and debug.

3

u/ohpauleez May 20 '16

My point is that Pedestal's async model is that thread pool model.

What may be unclear to readers is that Ring's processing model holds onto threads within critical pools while you process your middleware and application logic, and that's still true in the "async" containers that are support Ring. Pedestal allows you to free up threads as you migrate to a different pool.

If your response is larger than the HTTP response buffer, a Ring application will hold onto that thread until the client is done receiving the data. Pedestal will free up the thread until the client signals it's ready for more data -- this is all handled at the container, you don't need to do anything special in your Pedestal application, just return NIO or Channel body in your Ring Response map.

That is, you could always write a Ring-style application in Pedestal (with similar debugging and runtime behavior), but you could never write a Pedestal-style application in Ring.

This is the fundamental trade-off of Ring, and the motivating factor for Pedestal, to utilize a pattern that is common across many processing applications.

I might edit the original comment to make that a bit clearer.

3

u/yogthos May 20 '16

I think that's a fair summary of the differences. My main points are that the Pedestal model only becomes relevant in specific situations. If you have an application that requires async all the way down, then Pedestal is the right choice. Most applications work fine with the Ring model however.

I think the win for Ring is in terms of better packaging and better documentation. Should the story for Pedestal improve there, then it would become a more attractive alternative for the general case.

And of course, I'm very much open to having a Pedestal adapter in Luminus if you're interested in contributing one. That would definitely provide an easier way for people to try and use Pedestal.

2

u/yogthos May 21 '16

Also, it's worth noting that with a server like Immutant, the worker thread is dispatched before the Ring handler is invoked. So, the request accepting threads are not tied up in any way. The only thread that's tied up is the one actually doing the work handling the request.

1

u/ohpauleez May 21 '16

There are two points here that should be clarified. While Immutant will dispatch to the next pool, Ring is still synchronous and you're stuck in that pool - you can not perform any async operations, migrate to any other pools -- that is, Ring on Immutant still ties up a thread for the lifetime of processing, just not a connection thread. When you exhaust handling threads within the pool, back pressure is applied to the connection/acceptor pool. The results are the same.

The other point occurs when proxying results back to the client of the original call or when the response is bigger than the HTTP response buffer. In that case a "join" operation will need to occur, blocking a critical server thread until the client completes.

2

u/yogthos May 21 '16

Ring on Immutant still ties up a thread for the lifetime of processing, just not a connection thread. When you exhaust handling threads within the pool, back pressure is applied to the connection/acceptor pool. The results are the same.

Right, the key is that the thread is only tied up for the duration of the work that needs to be performed by the server. If you use the async approach you're not gaining much over this in most situations from my experience.

Regarding blocking for things large responses, again in my experience the problem is somewhat overblown. However, my understanding is that Aleph handles it just as well as Pedestal while being Ring compatible. So, it's still perfectly possible to do this in the context of Ring.