Spring Boot WebFlux Step 1: Overview

Spring Boot WebFlux Step 1: Overview

Spring Boot is pretty great. It’s especially easy to use if you can find useful documentation and understand how Spring’s magic works under the hood. That may not be much of an obstacle if you’re only new to the framework, but if you’re new to Java as a whole, then learning Spring as your first framework of choice can be a pretty sizable hurdle.

The goal of this blog series is to help web engineers get started with Spring Boot with an updated deep dive into the Spring Boot WebFlux framework. I just went through the whole “learn Java and Spring WebFlux” journey myself and it took me a few months to feel self-sufficient. While I was able to benefit from coworkers who were already knowledgeable in Spring Boot WebFlux, you might not have in-person resources to turn to.

Hopefully, this series will be a resource you can turn to when sources like Baeldung and Stack Overflow come up a bit short.

First, a bit of backstory to give us some context

ActiveCampaign is a leader in the Customer Experience Automation (CXA) space. At ActiveCampaign, we believe businesses should treat their contacts as uniquely as possible. This means not just sending out mass marketing campaigns but also using customized messages and simple automated workflows to provide an exceptional personal experience.

As any software engineer knows, a powerful and flexible platform that provides a simple user experience is a tall order. Luckily, that’s exactly the type of challenge that engineers are drawn to. In that sense, I consider myself blessed. As a computer engineer by education and a software engineer by career, I’ve had a dream run of non-stop challenging problems to tackle.

I’m now part of an ActiveCampaign team in charge of designing and implementing a platform that’s able to analyze and respond to thousands of data points per second. We help push the boundaries of what cutting-edge CXA looks like.

My team uses a tech stack that roughly consists of Java 11, the Spring Boot WebFlux framework, NoSQL databases, Terraform, Kubernetes, and other cloud services including plenty of queues. We can dive into many of these topics in the future, but this series is going to focus on the Spring WebFlux framework.

Spring Boot WebFlux reactive stack

We’re kicking this series off assuming no prior knowledge about Spring, the reactive stack, or Java. There will be links to as much useful documentation as possible, but we won’t go into common coding basics that are similar in many languages, such as declaring variables, for loops, declaring classes, namespaces, constructor methods, basic syntax, etc. If you’d like a rundown of these things I’d recommend taking a look at W3School’s Java Tutorial.

If you feel like something is missing that you can’t find anywhere else, please feel free to post in the comments and we’ll get the article appropriately updated. The goal of this series is to be approachable by the widest breadth of engineers possible — without getting bogged down in generic details that are readily available elsewhere.

What is Spring Boot 2.0?

Even if you’re not familiar with Java, you may have heard of the Spring Framework. It’s one of the most popular Java frameworks in use today. Because Spring Boot makes the Spring framework so easy to get started with, many engineers think of Spring and Spring Boot as synonyms and turn to https://start.spring.io when creating a new Spring application.

Spring and Spring Boot aren’t quite the same thing though. Spring is the base framework that doesn’t come with much out of the box. Most anything you’d want to use you would have to configure and wired up manually. Spring Boot 2.0 takes an opinionated view of the Spring framework. It provides default setting configurations, easy integration with many 3rd party libraries, production-ready metrics and health checks, and much more. In general, here are some of the pros and cons of opinionated frameworks:

Pros of opinionated frameworks:

  • Built-in default functionality makes opinionated frameworks quick to get started with
  • Plenty of documentation and examples available online that typically all point to similar opinionated ways of solving a particular problem

Cons of opinionated frameworks:

  • If you need a feature that the normal opinionated solution doesn’t provide, you’re likely to not find a lot of supporting documentation. Depending on your expertise, you may quickly be in over your head.
  • Opinionated frameworks typically have a lot of “magic.” While this reasonable default functionality is useful for getting started, these frameworks can be difficult to completely understand until you have much more knowledge of the framework itself

WebFlux Reactive high-level overview

In this series, let’s assume you’re looking to build a read-performant API service that interacts with data stores such as Redis and MongoDB. Spring’s reactive stack WebFlux gives you exactly what you need here. If you were looking to integrate with services that do not provide a reactive-compatible interface, you’d be better off choosing Spring’s Servlet (non-reactive) stack. Since you’re using only reactive-compatible infrastructure, Webflux will benefit you greatly — it’ll provide you with about a 50% performance boost over Spring Servlet.

Reactive programming breaks each API request down into a stream of chained functions which create a pipeline that the requests flow through. Within one thread, pipeline stages themselves are still blocking, but on a much smaller level than each request fully blocking each other. This process allows for much more throughput and hence performance than non-reactive services. Check out Spring’s own reactive explanation for more details.

Spring documentation

Finding useful Spring documentation was the biggest hurdle I had when first starting to work with Spring. Spring’s documentation is structured a bit differently than other languages and frameworks you might be used to. Here’s a lay of the land.

Spring’s official website is https://spring.io. From there, you’ll notice the main navigation has a Guides section but no obvious documentation links. The Guides section mainly consists of the most basic examples and can leave a beginner with a lot of unanswered questions.

If you search online, you’ll likely find a mixed bag of tutorials, Stack Overflow posts, and links to Spring API documentation. These resources tend to be:

  • Too brief or too narrow in scope
  • Too specific to a given situation
  • Too detailed and overwhelming

What you’re actually looking for is Spring’s Reference Documentation. Spring is a pretty bare-bones framework on which a lot of other projects are built. Depending on what you’re looking for, you’ll need to reference different documentation for different projects.

Luckily, Spring lists all of these projects here. Over the course of this blog series, you’ll likely find the following projects’ reference documentation useful:

  • Spring Framework: The core Spring Framework contains most of the documentation you’ll need: testing, the framework itself as it applies to Servlet and WebFlux approaches, data layer, caching, internationalization (i18n), beans, etc.
  • Spring Boot: Spring Boot injects a number of opinions into the Spring framework. When looking for documentation on a default construct, it can be useful to check out Spring Boot’s documentation first.
  • Spring Data MongoDB: MongoDB natively supports reactive streams, so it’s a common database to use with services built on WebFlux. Note that MongoDB also supports blocking calls, so when importing a new package into your service, always look for alternative reactive libraries. These reactive libraries are typically mentioned in the documentation, but make sure you keep them in mind — or you might accidentally use a non-reactive component.

Conclusion

You now have everything you need to get started with Spring Boot WebFlux! You know where to go for documentation and understand the basic high-level concepts of Spring WebFlux. In our next post, we’ll dig in and create a basic API using Spring Boot WebFlux.

Never miss an update