DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Data Engineering
  3. Databases
  4. Understanding Spring Reactive: Client-to-Server Communication

Understanding Spring Reactive: Client-to-Server Communication

Want to learn more about Spring Reactive? Check out this post where we explore client-to-server communication using the Client and Server containers.

Naveen Katiyar user avatar by
Naveen Katiyar
·
Sep. 27, 18 · Presentation
Like (16)
Save
Tweet
Share
21.27K Views

Join the DZone community and get the full member experience.

Join For Free

The purpose of this blog series is to take readers through the journey of Java servlet and Spring from thread per the request model to the single thread model in Spring Reactive (Spring Webflux to be more precise).

When we talk about a Servlet/Spring application, the flow of data takes place in two phases:

i) Client (e.g browser) to ServletContainer (e.g. Tomcat) and vice-versa
ii) ServletContainer to Servlet/Spring and vice-versa.

Client-to-Server Communication

In HTTP 1.1, all connections between the browser and the server are considered persistent unless declared otherwise. Persistence, in this context, means to use a single TCP connection to send and receive multiple HTTP requests/responses, as opposed to opening a new connection for every single request/response pair.

Until Tomcat 6, which is the implementation of Java EE 5 (and hence servlet 2.5), the default HTTP connector is asynchronous blocking and follows a one thread per connection model. This means that, in order to serve 100 concurrent users, it requires 100 active threads. We end up wasting resources (the thread), because connections may not be used heavily, but just enough to avoid a timeout.

In order to configure tomcat to use the non-blocking NIO connector instead of the default asynchronous blocking BIO, one simply needs to change the value of the protocol attribute of the connector tag in the server.xml from HTTP/1.1 to org.apache.coyote.http11.Http11NioProtocol.

One Thread Per Connection Model

Opposed to this is the relatively new NIO or non-blocking connector (based on J2SE 1.4). This connector has a couple of poller threads (usually depends upon the number of cores) used to keep the connection alive for all connected users while worker threads are called whenever data (a new HTTP request) is available. Threads can be allocated to connections only when requests are being processed. When a connection is idle between requests, the thread can be recycled and the connection is placed in a centralized NIO select set to detect new requests without consuming a separate thread. This model, called a thread per request, potentially allows web servers to handle a growing number of user connections with a fixed number of threads. This model leads to a much better sharing of resources (threads) and a larger number of concurrent users can be served from the same server. Java NIO API can be referenced for a better understanding of Channel, Selectors, and other related stuff.

From Tomcat 8.x, NIO is the default connector, and BIO support is dropped altogether from 8.5x.

NIO implementation (One Thread Per Request)

So, with the introduction of NIO connector, client-to-server communication was non-blocking, but the server-to-servlet connection was still blocking, which effectively meant that a thread would be blocked for every request leading to the One Thread per Request Model. So, as Servlet containers have evolved, there was a need for non-blocking support in Servlet API. Let's explain this with the following table.


Blocking connector

Non-Blocking Connector

ClassName

Http11Protocol

Http11NioProtocol

Tomcat Version

3.x 4.x 5.x 6.x

6.x 7.x 8.x

Read Http Request

Blocking

Non-Blocking

Read Http Body

Blocking

Simulated Blocking

Write Http Response

Blocking

Simulated Blocking

In the above table, I wrote simulated blocking for NIO connectors, this means a blocking API (server-to-servlet and vice-versa) wrapped around the underlying non-blocking NIO API (browser-to-server and vice versa).

In the next article, we will be focusing on server-to-servlet/Spring communication. Stay tuned!

Spring Framework Connection (dance) Blocking (computing) Requests Connector (mathematics) Database

Published at DZone with permission of Naveen Katiyar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • DevSecOps: The Future of Secure Software Development
  • [DZone Survey] Share Your Expertise and Take our 2023 Web, Mobile, and Low-Code Apps Survey
  • Java Bytecode: Journey to the Wonderland (Part 3)
  • Steel Threads Are a Technique That Will Make You a Better Engineer

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: