Server-Side

Microservices


  • The Architecture
  • TCP Protocol
  • TCP Services

Version


v1.2 23 September 2021 Split presentation in 2. Part II, here.
v1.1 21 September 2021 Revision to TCP Services Section
v1.0 14 September 2021 Initial Release

Acknowledgments


Thanks to:
  • Hamzeh Roumani, who has shaped EECS-4413 into a leading hands-on CS course at EECS and who generously shared all of his course materials and, more importantly, his teaching philosophy with me;
  • Parke Godfrey, my long-suffering Master’s supervisor and mentor; and
  • Suprakash Datta for giving me this opportunity to teach this course.

Download PDF

Monolithic vs Microservices

Monolithic

An overview layout of a monolithic application with each function
as one of a variety of shapes. All of the functions reside within the same
outer container. Each function is connected to the rest from within the
container.

VS

Microservices

An overview layout of a microservices application with each
function as one of a variety of shapes. Each functions reside within
its own container, and each container is connected to the
rest.

Monolithic Architecture

Monolithic architecture is considered to be a traditional way of building applications. A monolithic application is built as a single and indivisible unit. Usually, such a solution comprises a client-side user interface, a server side-application, and a database. It is unified and all the functions are managed and served in one place.

Strengths
  • Less cross-cutting concerns:
    • Logging,
    • Handling,
    • Caching,
    • Performance monitoring, and
    • Security.
  • Easier debugging and testing
  • Simple to deploy
  • Simple to develop
Weaknesses
  • Understanding and complexity
  • Making changes
  • Scalability
  • New technology barriers

An overview layout of the monolithic architecture. All components
are under a single stack. With the user interface at the top, then the
business logic, and the data interface. The data interface interacts
with the database.

Microservice Architecture

A microservices architecture breaks the application down into a collection of smaller independent units. These units carry out every application process as a separate service. So all the services have their own logic and access the database separately to perform specific functions. They are multithreaded, pooled, containerized, and each on separate nodes.

Functionality is divided into independently deployable modules that communicate with each other through APIs over technology-agnostic protocols such as HTTP. Each service has its own scope and can be updated, deployed, and scaled independently.

Strengths
  • Independent components
  • Easier understanding
  • Better scalability
  • Load-balancing
  • Flexibility with technology
  • The higher level of agility
Weaknesses
  • Extra complexity
  • System distribution
  • Testing
  • Cross-cutting concerns:
    • Configurations,
    • Logging,
    • Metrics,
    • Health checks,
    • Security, and
    • Others.

An example layout of a microservice architecture. There are several microservices.
Each microservice interacts with a shared or its own database. Some microservices rely
on other microservices. The user interface interacts directly with the
microservices.

TCP


The Protocol

What is TCP/IP?

TCP/IP stands for Transmission Control Protocol / Internet Protocol.

  • TCP/IP is a set of standardized rules that allow computers to communicate on a network such as the Internet.
  • IP is machine-to-machine. TCP is process-to-process. Each process listens to a socket. Each socket has an assigned Port number.
  • TCP and IP are two separate network protocols. IP is the part that obtains the address to which data is sent. TCP is responsible for data delivery once that IP address has been found.
  • TCP provides reliable, ordered, and error-checked delivery of a stream of bytes between applications running on hosts communicating via an IP network.
  • TCP is a text- and binary-based protocol, and custom or standard protocol built on top of it, such as HTTP, SMTP, FTP, SSH, etc.
  • The loopback (127.0.0.1) or localhost is a hostname that refers to the current computer used to access it. It is used to access the network services that are running on the host via the loopback network interface.
  • There are private and public IPs. Private IPs are only accessible within the network and public IPs are accessible to everyone on the Internet.
  • The Domain Name System (DNS) is a hierarchical and decentralized naming system for computers, services, or other resources connected to the Internet or a private network. It maps numeric IP addresses to human-readable domain names or hostnames of specific computers.
  • A router maps public IPs to private IPs via Network address translation (NAT), a method of mapping an IP address space into another by modifying network address information in the IP header of packets while they are in transit across a traffic routing device.

Private IPs

Reserved private IPv4 network ranges:

Name CIDR Block Address Range Number of Addresses
24-bit block 10.0.0.0/8 10.0.0.0 – 10.255.255.255 16,777,216
20-bit block 172.16.0.0/12 172.16.0.0 – 172.31.255.255 1,048,576
16-bit block 192.168.0.0/16 192.168.0.0 – 192.168.255.255 65,536

Telnet

  • Telnet is a client-server protocol based on text-oriented data exchange over TCP connections.
  • Telnet enables remote communication with a TCP server via text-based inputs and outputs.

TCP


Services

Sockets

Socket connection between the server and the client.

TCP Client

  • Specify the server’s host IP address and TCP socket port number that it is listening to.

  • Get an output stream to issue the request to the server.

  • Get an input stream to read the response from the server.

  • Close the connection with the server.

  • Or use a try-with-resources block to automatically close the connection and the input and output streams.

TCP Server

  • A server must be able to accept multiple clients at the same time. To support concurrency, we extend the Thread class. There are two ways to create a new thread of execution. This is the first.

  • To start the thread running, we call the start method which invokes the run method. We override the run method with our own implementation. More later.

  • Create a server instance on a host and port. We will use the local host’s own IP address. Alternatively, we could use the loopback address. However, this would restrict connections to the server to clients on the same host. See next slide for detail about ServerSocket constructor.

  • Query the server’s host and port for logging.

  • Accept the next client connection. Blocks until a request arrives. Then, create a new thread and starts it running. Loops until the server is shut down.

ServerSocket

Create a server with the specified port, listen backlog, and local IP address to bind to.

port
Must be between 0 and 65535 (16-bits), inclusive.
A port number of 0 means that the port number is automatically allocated.
backlog
Is the requested maximum number of pending connections on the socket. The value provided should be greater than 0. If it is less than or equal to 0, then an implementation-specific default will be used.
bindAddr
Can be used on a multi-homed host for a ServerSocket that will only accept connect requests to one of its addresses. If bindAddr is null, it will default accepting connections on any/all local addresses.

TCP Server run

In microservices, run would typically: compute, use APIs, lookup a database, use HTTP, etc.

  • Query the client socket:
    • client.getPort
    • client.getInetAddress
    • client.getInputStream
    • client.getOutputStream
  • Get an input stream to read the request from the client.

  • Validate the request, process it and form a respond.

  • Send the respond via an output stream to the client.

  • Close the connection with the client. or let Java automatically close it along the input and output streams at the end of the try-with-statement.

TCP Client & Server

Client start connection to the server.

Client forms a request and sends it to the server.

Client receives the response from the server.

Client closes connection with the server.

Server starts and listens for new connections.

Server accepts the connection from the client.

Server reads the request from the client.

Server validates and processes request and forms a response.

Server sends the response to the client.

Server closes connection with the client.

Server continues accepting new connection from other clients.

This slide is intentionally left blank.

Return to Course Page or Part II.