Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8996745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:47:45+00:00 2026-06-15T23:47:45+00:00

As I have been developing with Play 2 for the first time, I find

  • 0

As I have been developing with Play 2 for the first time, I find I do something like this a lot in my controllers (one of the simpler examples):

 val promUser = Akka.future(UserService.findByUsername(access.username))
  Async(
    promUser.map {
      _.map{
        user => {
          val promService = Akka.future(ServiceService loadOnlyWithUser (id,user.id.get))
          Async(
            promService.map { _.map { service =>
              Ok(toJson(service))
            }.getOrElse(BadRequest("not accessible"))}
          )
        }
      }.getOrElse {
        BadRequest("unauthorised")
      }
    }
  )

Would it be better to have a single future? eg:

val promService = Akka.future{
    val userOption = UserService.findByUsername(access.username)
    userOption.map( user => {
      ServiceService loadOnlyWithDeveloper (id,user.id.get)
    }).getOrElse(None)

  }
  Async(
    promService.map { _.map { service =>
      Ok(toJson(service))
    }.getOrElse(BadRequest("unauthorised"))}
  )

I’m thinking on the one hand many futures/returns to the controller may add overhead, on the over hand group the calls into one future will be more readable but lead to larger “threads” running in the Akka system. For much larger jobs I have an extra Akka system, so these would only encompass maybe max of 4 SQL transactions. As far as I can work out from apache bench, there isn’t any difference between the examples above…. Is there anything Im missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:47:46+00:00Added an answer on June 15, 2026 at 11:47 pm

    There isn’t any difference in performance with your examples because they are equivalent.

    When you write:

    val promUser = Akka.future(UserService.findByUsername(access.username))
    

    It does not start to evaluate that Future, it is only executed when you then map it.

    As I understand it, you need to think about futures composition. This is a really nice documentation that I would recommend: http://docs.scala-lang.org/sips/pending/futures-promises.html

    To answer your question then, I don’t think it makes a difference if you have multiple Futures, one for each DB access, if you need to perform a bunch of queries in sequence. E.g. retrieve a record, then delete it.

    Where you can take advantage of Futures (besides making your app non-blocking) is to take advantage of the for-comprehension syntax so that you can run two or more asynchronous Futures at the same time, e.g.

    for {
      authUser <- User.findById(request.authUserId)
      otherUser <- User.findById(id)
    } yield (authUser, otherUser)
    

    Also, I would recommend using flatMap to compose your Futures together, rather than having multiple Async{} blocks. This way you will effectively flatten your multiple Futures, Future[Future[Result]] into a single Future that can be an AsyncResult.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been developing test builds of an app for some time and have
I have been developing a game for Game Center. Description of game :- One
I have been developing an Android app. I would like to hide the OK
I have been developing a web app using play framework for a while (using
I have been developing one Map project. In all Activity classes i have one
I have been developing iPhone apps on my iPad for a long time and
I have been developing for iPhone from last 1-2 months and all the time
I have been developing my first large (for me) MVC project for a couple
Ok, I have been thinking of developing a web application using the play framework
I have been developing an user interface with Java Swing, and I have met

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.