I’ve recently started working with Spring Boot in a new project that has the following code (simplified):
@RestController
@RequestMapping("/users")
public class MyController {
@Autowired
UserService service;
@GetMapping
public Mono<ResponseEntity<User>> getUser() {
// note, the non-reactive call is wrapped into Mono.just
return Mono.just(new ResponseEntity<User>(service.getUser(), ...);
}
}
// both service and dao (below) are non reactive
@Service
public class UserService {
@Autowired
UserDao dao;
User getUser() {
// some logic (everything is in memory, nothing is io-bound)
return dao.getUser();
}
}
@Repository
public class UserDao {
public User getUser() {
// call the non-reactive relational db and get the user
// it takes 99% of the time I believe
// return this user;
}
}
Now I’ve never really used reactive approach but I understand that under-the-hood this spring boot application must run netty and use webflux
I’ve used a database as an example of I/O bound operation, but sometimes in the code there are also calls to remote services (REST), and also they’re not reactive…
These DB operations/remote rest calls I believe take like 99% of the execution time, so this makes the application to be a good candidate for reactive implementation.
However, I’m confused by the current situation with the code:
- The controller is reactive
- The service / DAO / Remote Calls are not
My question is – whether such a usage of mixture of non-reactive and reactive approaches is ok or it is wrong?
In my understanding if I want to go with reactive web-flux, all my code must be reactive all the way down to the database/remote rest call, otherwise I’ll effectively keep the event-loop threads of netty busy and this is something I can’t do… So it seems wrong to me in a nutshell, But I’m not sure what happens actually in this code. Is my understanding correct or I’m missing something?