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

2

u/yogthos May 20 '16 edited May 20 '16

We ought to be choosing technologies that solve our problems and have longevity.

Sure, my experience is that Ring solves real world problems and because of the huge community around it, it has best chances at longevity.

I would tend to trust conference talks about reach and use more than public downloads from a public repository.

I would certainly trust real world usage more than talks. I guess we'll have to disagree here.

Pedestal has a firm commitment to embracing the community and the host - it can use all of those wonderful Ring middlewares as well as battle-tested Java ServletFilters

I find the opposite. Pedestal has terrible documentation, and it's very difficult to get it up and running compared to Ring. I think those problems need to be addressed before it can be considered a viable option.

If somebody wanted to make a pedestal module for Luminus I would be glad to accept it though. Currently, Luminus supports all the Ring based servers such as Immutant, HTTP Kit, Aleph, and Jetty. If Pedestal is compatible with the Ring model then it should be an easy task to create an adapter that will use it.

I also think there's a common misconception about what Pedestal really is now vs. what it was a few years ago. As others have encouraged in the community, take another look and see for yourself.

I have when the recent version came out, and found it just as poorly documented and difficult to setup as it ever was.

Async and non-blocking behavior become critical when building a service that needs to handle real, production traffic and environments.

That's contrary to my experience using Ring in the enterprise in production for the past 5 years.

It's often the case that one service needs/calls another service. In a thread-per-connection model with blocking I/O, your services will quickly be tied up, and eventually crash.

It's not a thread per connection model, it's a thread pool, and thread pools work very well. Thread pools is how we interact with databases that back large applications, and claiming that this doesn't scale is simply absurd.

The benefits of Pedestal's logging, metrics, router, and interceptor chain shouldn't be unique to Pedestal-- there are lots of other projects/areas where those technologies are advantageous.

Analogs to all of these modules exist as Ring middleware and work equally well in my experience.

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.

1

u/ohpauleez May 20 '16 edited May 20 '16

I find the opposite.

I'm not sure what you mean here. Pedestal allows the use of Ring Middleware and Java ServletFilters. In some cases, Middleware needs to be wrapped to allow it to migrate across threadpools (the main advantage to using the interceptor model, splitting 'enter' and 'leave').

it's very difficult to get it up and running compared to Ring

lein new pedestal-service my-thing; lein run Handlers are just functions, interceptors are just maps/records. Pedestal's documentation does need a lot of help - as we've been greatly refactoring main sections of Pedestal (marching steadily along a roadmap), we've invalidated parts of Pedestal documentation. There is a plan to address that. The best location for information is the "samples" which are updated with each release.

Analogs to all of these modules exist as Ring middleware

That's simply not true. The best and most common router for Ring applications is O(N) for the number of routes, and has redundant copies of data in the route representation. Pedestal's router is O(log32N) in the best case and O(logN) in the worst case. It collapses redundant data and routes into a prefix tree. It's capable of making a routing decision (and applying all constraints) in under 60ns. A typical routing decision for a Ring application takes 3000-8000ns. Once the router module is available, others can use Pedestal's router on Ring applications too!

All available logging and metrics tools for Clojure involve 1-5 extra layers of indirection between the log/metric call, and the backend/implementation call. I don't need that flexibility and I'd prefer a more predictable, performant solution programmed against a single interface (SLF4J for logging, DropWizard Metrics for metrics).

There is no analog for the interceptor chain in Ring. The pattern is perverse in other request-handling/messaging applications. There's no reason why this core piece should be locked in Pedestal -- the new separate module will allow others to apply the pattern wherever it makes sense.

1

u/yogthos May 20 '16

I'm not sure what you mean here.

I'm not talking about the implementation of Pedestal here, I'm referring to usability in practical terms. It's much more work to get it up and running than Ring currently.

lein new pedestal-service my-thing; lein run

Ok now how do we package that for production deployment.

I'm glad you're actively working on improving Pedestal and hopefully one day it will be as easy to use as Ring.

That's simply not true. The best and most common router for Ring applications is O(N) for the number of routes, and has redundant copies of data in the route representation. Pedestal's router is O(log32N) in the best case and O(logN) in the worst case. It collapse redundant data in a prefix tree. It's capable of making a routing decision (and applying all constraints) in under 60ns. A typical routing decision for a Ring application takes 3000-8000ns. Once the router module is available, others can use Pedestal's router on Ring applications too!

Glad to hear it, but I think you're confusing Ring with routing here. You can use a number of routing libraries on top of Ring, and as you yourself point out, even the Pedestal router will be available once it's modularized.

I don't need that flexibility and I'd prefer a more predictable, performant solution programmed against a single interface (SLF4J for logging, DropWizard Metrics for metrics).

I use SLF4J in Luminus and it works perfectly fine...

There is no analog for the interceptor chain in Clojure. The pattern is perverse in other request-handling/messaging applications. There's no reason why this core piece should be locked in Pedestal -- the new separate module will allow others to apply the pattern wherever it makes sense.

That's great to hear.

2

u/ohpauleez May 20 '16

Ok now how do we package that for production deployment.

Depends on your needs. Pedestal's template ships with the ability to package it into a Docker image, OSv unikernel Image, uberjar, or WAR. The first two have dedicated tools (docker and capstan respectively), and the latter two are controlled by either leiningen or the REPL (lein uberjar, lein pedestal war, or using Pedestal's WAR tooling in the pedestal.service-tools tooling).

Glad to hear it, but I think you're confusing Ring with routing here.

Your comment was about applicable Middleware, to which I gave one common example, routing. There are no analogs for some capabilities within Pedestal.

I use SLF4J in Luminus and it works perfectly fine...

We're in the same boat here! :) But logging isn't enough. Real systems are measured in quantiles, and that's why Pedestal has made the jump to support logging and metrics.

2

u/yogthos May 20 '16

So, here's the documentation problem I'm talking about. I don't see clear instructions on how to make an uberjar anywhere here. There are docs on doing a war deployment that talk about hand rolling some web.xml though. Perhaps the main issue with Pedestal is the state of the docs then. :)

2

u/ohpauleez May 20 '16 edited May 20 '16

Yep, as I admitted earlier on, the docs need a lot of work. The community has been great about patching up the glaring differences, but the docs do not reflect the current state of capabilities in master. Often, users are encouraged to use the mailing list and the samples, which receive much more attention. In short order, the docs should get a complete refresh now that the majority of the refactoring work is complete.

I'm always personally more than happy to help users get over any hurdle they hit.