Is there any alternative of dispatcher?
We cannot set headers from CQ5 with using dispatcher. Handling cookies is also tricky, we can store cookies, only with POST request with using dispatcher. (storing cookies on client side is a bad practice).
So is there any better alternative for dispatcher, like caching in filter chain?
Thanks!
Well, it's not really an alternative but a possible extension:
https://www.varnish-cache.org/
Related
I want to perform two POST requests to two different endpoints, is it possible to do this in a transactional way?
I want to perform two POST requests to two different endpoints, is it possible to do this in a transactional way?
No.
There's no rule that says that the effects of a POST will be scoped to precisely one resource. On the web, we can submit one HTML form that changes multiple pages. Implementations have a lot of freedom when processing a request (HTTP constrains message meaning, not handler implementations).
POST /replace/these/resources/with/empty/documents
Content-Type: text/plain
/A
/B/C
/D/E/F?g
A piece that is missing, however, is the ability to communicate to general purpose components which resources have been changed when handling the POST request. The mechanism described by RFC 7234 handles only a few simple cases, and does not (as far as I can tell) extend generally.
You get at most three
effective request uri
Location
Content-Location
and both Location and Content-Location mean something, so you may end up with a mess if you try to force them out of their normal semantics.
You can, of course, send back response that includes in the payload a list of all of the resources that were changed by your transaction (if that's practical), but you don't have any way to lift that information into the metadata where general purpose components can use it.
I have the following clear algorithm:
Client sends a request to my spray application.
Spray receives a request and I see spray receiving load as multiple requests come in.
If loading is high, spray returns HTTP 503; otherwise it starts processing the request.
How can I manage current spray loading?
Also, as I understand spray uses akka internally which can be extended with adding additional nodes, so how can I manage the load with additional nodes?
Spray itself uses reactive I/O and can handle very high loads, probably higher than any custom code "protecting" it could handle. So don't worry about trying to protect the spray system itself. If you've got complex processing logic that might take a while to handle certain requests, it might make sense to put a protective throttle around that processing logic, using something like http://letitcrash.com/post/28901663062/throttling-messages-in-akka-2 . And in the case where the queue is full you can simply complete(StatusCodes.ServiceUnavailable).
I'm using spray-routing to build a simple HTTP server. This server calls out to a number of services that take a while to respond (seconds). We would like to reject requests when the number of concurrent requests becomes to large. Otherwise a large number of concurrent requests bogs down the system to nobody's advantage.
There are a number of layers where this might be solved. I'm not sure how to do any of them precisely, or which is the best one.
I could supply an execution context for spray-routing that has a bounded queue and a rejection policy.
I could limit the mailbox size of my spray http server since it is also an actor.
I could configure a setting in application.conf that addresses this directly for spray.
What is a simple an effective way of implementing such a policy?
I don't know what solution would be the best for your case (I would go for creating my own execution context) but I believe that maybe you should rethink how you want to process your requests.
What do you do with your request? Do you try to handle them in Spray directly? With some help from Futures?
I would suggest creating additional actors, passing the request context to them and then deciding what to do. If you want to process it or maybe you should put it down immediately. This will give you much flexibility in future. You can attach additional servers with now support for clustering in Akka without changing the spray part adding more processing power easily.
I know this doesn't answer your question but I think akka was designed to handle this kind of problems differently and cutting on mailboxes or anything else is not the right choice.
I am making a transition from SOAP to REST, and I would like to convince my colleagues that this is a good move. We don't need the extra security mechanisms that SOAP can provide. For us the overhead of SOAP and WSDL has only proven to be a headache over the years.
Apart from the obvious simplifications, one really valuable advantage for our system would be the HTTP caching mechanisms. I have read things on the subject, but I still don't fully understand why these caching mechanisms can't be applied to SOAP messages.
Is it simply because REST by convention encodes all the parameters in the url? Since a GET call can also have a body with parameters, I understand that it's not restricted for REST, but the caching mechanisms don't work if you do so?
SOAP, when using HTTP as the transfer mechanism, is sent via HTTP POST requests. As HTTP POST is non-idempotent, it is not cached at the HTTP level.
REST may be cached, provided the requests in question are idempotent ones: GET, PUT and (theoretically) DELETE. Any POST requests still won't be cached. That said, if you are planning to do a lot of work with this, you should look into how caches check whether they are still valid. In particular, the ETag header will be a good way to implement caching providing you've got a cheap way to compute what the value should be.
Is it simply because REST by convention encodes all the parameters in the url? Since a GET call can also have a body with parameters, I understand that it's not restricted for REST, but the caching mechanisms don't work if you do so?
REST does not prescribe any particular mechanism for how to encode request parameters in the URL. What is recommended is that clients never do any URL synthesis: let the server do all the URL creation (by whatever mechanism you want, such as embedding within the path or as query parameters). What you definitely shouldn't do is have a GET where the client sends a body to the server! Any cache is likely to lose that. Instead, when that request doesn't correspond to a simple fetch of some resource, you POST or PUT that complex document to the server and GET the result of the operation as a separate stage.
A POST of a complex document that returns the result of the operation as another complex document is pretty much how SOAP is encoded. If you're tempted to do that, you might as well use SOAP directly as that's got much more mature tooling in many languages. If you want to run REST that way, you're probably doing it wrong somewhere…
Stating that POST requests are non-idempotent in general is not correct. POST should be used for non-idempotent operations but SOAP is not really using the HTTP correctly and as the result of that it can happened, that POST is used for idempotent operation - for example for fetching an entity by ID (which you would normally use GET for).
Also according to this: Is it possible to cache POST methods in HTTP? (check the answer of reBoot), POST requests is possible to cache according to RFC. It may not be implemented very well by browsers and proxies though. Anyway if you add appropriate HTTP headers, SOAP over HTTP messages should be possible to cache using HTTP caching mechanisms.
Another reason could be that SOAP URI is always the soap server, its not determining which resource needs to be cached or what is the resource. So Cache server cannot perform caching of something it don't know. However, REST has URI for each resource (can be multiple URIs for same resource), which helps caching server to perform its job.
On top of this, as mentioned in other answers only some of HTTP verbs are used for caching like GET, PUT etc (mostly GET).
I have a REST API where I would like to cache the JSON response of the index (GET /foo) and the read actions (GET /foo/1) to significantly increase the performance. When there is a POST or a PUT on a resource the cache entries for the index and read results need to be expired, so no old content is served.
Is this a scenario that's best done with a Reverse proxy like Squid / Varnish or would you choose memcache(d)?
Using a reverse proxy that sits on the HTTP layer is more transparent. That means that it's possible to see what's going on over the wire. The bad thing is that few of these support caching authenticated responses, so their efficiency may drop to 0 if your resources require authentication. Reverse proxies also don't usually automatically expire resource A (/foo) when this completely unrelated resource B (/foo/1) is modified. That's correct behaviour that you'd have to add to your solution somehow.
Both of these problems can be solved if you use memcached, since it doesn't have the transparency requirement.
I would go for a reverse proxy like varnish because you can implement (and test) your service without involving cache logic, and add caching as a separate layer. You can upgrade/restart your service while varnish serves old results for GET request (great for availability), and it's easy to setup rules in varnish to invalide (purge) existing cache results based on specific GET/POST actions.
If you want to employ distributed memory memcached is a good solution. https://github.com/cpatni/middleman is a reverse proxy which uses memcached for caching.