NikhilWanpal
NikhilWanpal

Reputation: 3000

Akka: Is this the correct way to build REST web service with Actors?

What I am doing:

I am using play 2.5.7 (java) and trying to build a REST application. When I get a call on my controller I ask the first actor, this actor can only solve part of the problem (getting additional data), which needs to be forwarded to another actor which uses the request data and additional data to update some more data, send an async void call (tell) to another actor and respond to the controller. All these (4) actors are @Injected in other actors or controller with Guice.

Flow of calls:

controller --(Patterns.ask)--> actor1 --(actor.forward)--> actor2 --(actor.forward)--> actor3 (-tell-> actor4) and --(sender().tell)--> controller.

Issue:

This works for first 4 calls. Then on actor1.forward keeps failing on every consecutive request; Patterns.ask times out. System.out on the line before actor1.forward works but not the actual forward. No matter the timeout value (tried even 20s). No change done in the request; I just hit the send button in postman every time.

I have two questions:

  1. Why 4? Why does it fail after 4th request? Is it some config? What should I look for in config?
  2. Is what I am doing with actors correct way to build a REST web service?

Update: I found the issue; it was caused due to consumption of Redis connections through the pool and never freeing them. But the second question I had still remains, is what I am doing here advisable?

Upvotes: 0

Views: 147

Answers (1)

acjay
acjay

Reputation: 36551

Sure, this could be a reasonable design. But I would consider though whether it would be more maintainable to work with Future returning methods, unless your workflow requires some complex protocol between multiple moving pieces or internal state. It may also be worth considering Akka Streams, if your processing doesn't map well to async method calls.

Basically, actors are a pretty low-level tool. To the extent that you need them, I would try to minimize the surface area of your application where they are being directly used. Higher-level abstractions are better, where possible.

Upvotes: 1

Related Questions