Table of Contents
- 7. HTTP
- 7.1. HTTP Works
- 7.2. HTTP Scales
- 7.3. REST
- 7.4. Spring WebFlux: a Net-New Reactive Web Runtime
- 7.5. Long-Lived Client Connections
- 7.6. Server-Sent Events (SSE)
- 7.7. Websockets
- 7.8. Reactive Views with Thymeleaf
- 7.9. A Reactive Servlet Container
- 7.10. The Reactive Client
- 7.11. Security
- 7.12. Next Steps
Frontmatter
Reactive Springby Josh Long
2020 Josh Long. All rights reserved.
ISBN: 978-1-7329104-2-3Version 1.0.
Reactive Spring logo (as used in the cover design and elsewhere) is used with license and permission.
While the author has used reasonable faith efforts to ensure that the information and instructions in this work are accurate, the author disclaims all responsibility for errors or omissions, including without limitation, responsibility for damages resulting from the use of or reliance on this work. The use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
Dedication
To my "bears:" my partner Tammie and her daughter Kimly, who have become my family, and with whom I dare to dream of a better tomorrow: I love you both so much.
To my late father, Clark Long, who passed away in December 2019: we miss you and love you. Rest in peace, dad.
To Dr. Fauci and the men and women of the world who labor (hopefully by the time youre reading this, well be able to use the past tense: labored) tirelessly to save lives during the COVID-19 pandemic: thank you.
Preface
I am terrible at writing books. I am good at iterating on books. But, getting them done? So that theyre in the past? In the rearview mirror? Thats a different thing altogether. Im terrible at writing books.
I wrote this book thinking I could take a reasonably large 70+ page, multipart article that I had co-authored by with my buddy and Java industry legend Matt Raible, flesh it out, and turn it into a book. That was mid-2018, and here we finally are! Its been years. Ive traveled more than a million miles since 2018. Ive been to every continent and dozens of countries many times since then. And its only 2020 as I write this. I knew writing something would be difficult with my commitments to work and my growing family, but I still wanted to try.
There was one more problem: Spring. You see, Spring grows and evolves and does more and more exciting things day by day. Its a whirlwind! I wish that I could write fast enough to capture the state of the art, and then publish, but I just cant.
This book represents my best attempt at keeping up. Its filled with focused analyses on the various things youll need to understand when building reactive services and systems with Spring.
I wrote this because this is the book I wish I could have read when I first started with reactive programming. Its not easy, but here I am on the other side of the conceptual chasm, and it was worth the effort.
Reality informs architecture, and reactive programming is a sea-tide change in architecture representing the changing, cloudy realities of production. Reactive programming emerged in its current form in research in academia and then in the industry at organizations like Microsoft, Netflix, VMware, and Lightbend. As you read this, were more than a decade into the evolution of those applied practices. Its no accident than reactive programming emerged in the industry around the same time as microservices.
While I know what production looks like without reactive programming, I dont like to think about it. I cant imagine what the architecture of production looks like without reactive programming. By the time you finish this book, dear reader, you too will be able to and want to build production-optimized software and services. Production is the happiest place on Earth. Youll love it there.
Josh Long
Summer, 2020
Foreword
Dear Reader,
Welcome to the Reactive Revolution! Its been a few years since the concept of Reactive Systems was first floated as an answer to all sorts of scalability and resiliency problems. With the maturity of the ecosystem, standards, and practices around Reactive, there has never been a better time to start learning about it.
During the last decade, I dedicated most of my energy to exploring and crafting "non-blocking" solutions, including Project Reactor. It allowed me to meet with many engineers from different horizons.
One thing Ive learned is that writing more efficient software is rarely the focus of engineering groups. One possible cause is the endless race to market new features faster than our competition to improve our productivity.
But what if you could both have efficiency and productivity? Here you have it in a nutshell: the mission of the Spring Reactive stack.
I cant think of a better mentor than Josh to guide you, and I hope you will enjoy reading him as much as I did! In this book, Josh has depicted pragmatic use cases and code samples to demonstrate Reactive Programmings value.
You will learn everything about the game-changing aspect of "flow control," something your container resources will never stop loving. You will start transforming your blocking services into non-blocking ones and understanding its ramifications. You will get more vertical as you start diving into the web layer and the data layer. In the process, you will accumulate more and more knowledge of Project Reactor, the reactive library of choice for building Spring applications.
Finally, once you have mastered designing Reactive microservices, the book will invite you to its penultimate software design lesson: Distributed Reactive Systems with RSocket.
My friend has worked extra hard to make this journey fun and rewarding for the curious mind. I am incredibly proud that someone as talented as Josh took the time to introduce to the world the collective effort I had the chance to be part of. You will soon understand he is one of those humans who love sharing their in-depth knowledge whenever you need it. He will do so in a concise way because he respects your time.
Dear Reader, I must warn you. Do not devour the book too quickly, or you might feel as overwhelmed as I do, and you will eagerly wait for your next chance to learn from Josh Long.
Stephane Maldini
Project Reactor co-founder
Senior Software Engineer | Productivity Engineering - Runtime Java Platform
Netflix Los Gatos
1. Introduction
Welcome to the reactive revolution! You hold in your hands a guided tour to the wide and wonderful reactive world of Spring.
Reactive programming intends to help you build more efficient, more resilient services to respond to cloud-native, distributed, microservices realities. Todays architectures are different. They must contend with several demands that are more exacerbated than ever, if not entirely novel. Todays architectures must support agility, so they are typically decomposed into smaller, singly focused, bounded-contexts, or microservices, to better align with team sizes. Microservices enable continuous delivery. Microservices also imply more network communication as services are distributed. That decomposition also implies more risk, more volatility. Services may change more readily. They may disappear; they may suffer outages, etc.
Reactive programming is a way to address these demands. It gives you a pattern and paradigm that embraces the volatile nature of distributed systems. It introduces a new way to think about building services keeping in mind that computers have finite resources, even in the cloud. Reactive programming changes how we think about interactions between actors in a system, moving from push-based to a pull-based approach.