Beyond REST API’s – An overview about modern API technologies by Lars Duelfer @ Spring I/O 2022

Beyond REST API’s – An overview about modern API technologies by Lars Duelfer @ Spring I/O 2022

Introduction

In this section, the speaker introduces himself and the topic of his talk. He also gives an overview of what he will cover in the next 50 minutes.

  • The speaker welcomes everyone to the conference and introduces himself.
  • He talks about the topic of his talk, which is alternative technologies to REST.
  • The speaker asks some warm-up questions to the audience about their experience with REST, services, events, and websockets.
  • He explains that when he refers to REST in this talk, he means any kind of HTTP JSON APIs.
  • The speaker gives an overview of what he will cover in the next 50 minutes.

Why Look for Alternative API Technologies?

In this section, the speaker discusses why someone might look for alternative API technologies instead of using REST.

  • The speaker acknowledges that REST is cool and has many benefits such as widespread knowledge and support in various languages and frameworks.
  • He mentions some advantages of using hypermedia links with REST such as transferring information about permissions and possible state changes.
  • The speaker notes that with HTTP/2, some limitations related to concurrent connections from browser-based applications have become less tricky to handle.
  • However, there may be challenges that cannot be solved with REST alone.

Example Domain Model

In this section, the speaker presents a simple example domain model for a task management solution that will be used throughout his talk.

  • The speaker shows a slide with a simple domain model for a task management solution consisting of three contexts: users, companies/customers (paying customers), and projects/tasks (core domain).
  • He explains how tasks can be assigned to users within projects and tracked accordingly.
  • The example domain model will be used later on when comparing different API technologies.

REST API Architecture

In this section, the speaker presents an example architecture for a task management solution using REST.

  • The speaker shows a slide with an example architecture for a task management solution using three microservices bundled together with Spring Cloud Gateway.
  • He notes that different clients such as mobile apps, single-page applications, web browser-based apps, or third-party backends can interact with the microservices.
  • The speaker provides examples of what endpoints might look like for fetching and manipulating tasks.

Conclusion

In this section, the speaker concludes his talk by summarizing what he covered and encouraging the audience to explore alternative API technologies.

  • The speaker summarizes that he discussed some experiences providing and consuming REST APIs and introduced three alternative technologies: GraphQL, gRPC, and RSocket.
  • He notes that there are two more talks later in the day that will provide more information on these technologies and how they are implemented in Spring.
  • The speaker encourages the audience to explore these alternative technologies when facing new API challenges.

Introduction to GraphQL, gRPC and RSocket

In this section, the speaker introduces three technologies: GraphQL, gRPC, and RSocket. The speaker explains that these technologies are alternatives to REST APIs and can be used in situations where REST APIs are not ideal.

Technologies for Alternatives to REST APIs

  • GraphQL is an open-source technology founded by Facebook that has gained support in the community. It is now a first-class citizen in the Spring world with Spring GraphQL.
  • gRPC is a library from the community that offers an alternative to REST APIs.
  • RSocket was originally developed by Netflix and is also supported by the Spring framework natively as part of Spring messaging.

Experience with GraphQL

In this section, the speaker asks if anyone has experience working with GraphQL.

Experience with GraphQL

  • A few people have worked with GraphQL before.

Issues with REST APIs

In this section, the speaker discusses some issues with using REST APIs.

Issues with Using REST APIs

  • Fetching data for different views can result in cascading sequences of requests that are slow and take time.
  • There are limited ways to approach this issue when using REST APIs. One option is to cut resources differently or use custom expansion mechanisms offered by some public APIs.

How Does GraphQL Work?

In this section, the speaker explains how GraphQL works and its basic idea.

Basic Idea of How GraphQL Works

  • With Spring's new GraphQL library, it is just another endpoint where all requests are received and handled. HTTP is only used for transportation of requests to the server, and HTTP status codes are not used for reporting errors.
  • GraphQL is a language that describes the payloads being sent over to the single endpoint and how they are interpreted on the server side.

Using Websockets with GraphQL

In this section, the speaker explains how websockets can be used with GraphQL.

Using Websockets with GraphQL

  • Besides HTTP, websockets can also be used for transportation of requests. It offers request stream communication style so you can make subscriptions to endpoints and get a constant stream of data back from the server all with the same technology.

Conclusion

In this section, the speaker concludes by summarizing what was covered in this video.

Summary

  • The speaker introduced three technologies: GraphQL, gRPC, and RSocket as alternatives to REST APIs.
  • The basic idea behind GraphQL is that it has a schema that describes the data that your API is exposing.
  • With Spring's new GraphQL library, it is just another endpoint where all requests are received and handled.
  • Besides HTTP, websockets can also be used for transportation of requests.

Understanding GraphQL

In this section, the speaker explains how GraphQL works and how it differs from traditional REST APIs.

How GraphQL Works

  • GraphQL allows clients to specify exactly what data they want to receive from the server.
  • The server becomes an executor for an arbitrary number of different queries that can be expressed with the schema.
  • Spring GraphQL library is used to annotate a class with @Controller and then use a set of new annotations to implement resolvers.

Querying Data with GraphQL

  • @QueryMapping is the entry point into your query that's executed if you send a query to fetch a single project.
  • @SchemaMapping or @BatchMapping are used to explain the server how to fetch the data that is part of the returned structure.
  • A playground can be used to play with API requests and get customized responses for client queries.

Subscriptions in GraphQL

  • Subscriptions give you a constant stream of data back from the server, which requires having a WebSocket connection open.
  • graphql-ws is a lightweight WebSocket wrapper that can transport any kind of request via WebSockets.
  • When subscribed for projects, you get back a list of all projects as a stream, and every item contains requested attributes.

GraphQL

This section covers the basics of GraphQL, its advantages, and some things to consider when using it.

Introduction to GraphQL

  • GraphQL is designed to address the problem of over or under-fetching data.
  • It reduces the number of round trips between clients and servers.
  • Schema introspection allows for easy exploration of APIs in a development environment.

Using GraphQL

  • GraphQL can be used in an existing application landscape by replicating data from other services and offering a public API.
  • Care must be taken with graph sizes as large data models can easily overload servers. Pagination mechanisms should be implemented to avoid returning too many nodes.

gRPC

This section covers the basics of gRPC, its advantages over REST APIs, and how it works.

Introduction to gRPC

  • gRPC was initially developed by Google as their internal successor to their RPC framework.
  • It is now open source and supports a variety of languages and frameworks.

Advantages of gRPC

  • Compared to JSON payloads used in REST APIs, gRPC is more efficient at transporting data due to its binary format.
  • The entire schema does not need to be transported with each request, reducing unnecessary payload size.

How gRPC Works

  • gRPC makes heavy use of the HTTP2 specification which allows for multiplexing requests over a single connection.

gRPC Streaming and Payloads

In this section, the speaker discusses how gRPC can be used for streaming scenarios and how it addresses the problem of payload size using protobuf.

Streaming Scenarios

  • gRPC supports various streaming scenarios such as request stream paradigm, server streaming, client-to-server streaming, and bi-directional streaming.
  • Bi-directional streaming allows communication in both ways with streams.

Payload Size

  • To address the issue of large payloads, gRPC uses protobuf to describe and serialize/deserialize data.
  • A protobuf schema is required to describe the service being exposed. The schema includes photobuff messages that describe all data sent as input or upper parameters.
  • Protobuf uses position numbers to identify attributes in a byte array representing the payload. This allows transport of the actual schema with your payload making it smaller.

Code Generation

  • Protobuf generates code from its schema using a maven or greater plugin. The generated code is language-specific (e.g., Java or Kotlin).
  • Generated base classes are inherited from and implemented on the server-side by adding a simple annotation at grpc service.

Client-Side Implementation

  • On the client-side, generated classes are used along with an injected step property defined by putting an annotation in front of it.
  • Configuration properties include endpoint information while blocking clients and non-blocking clients are available out-of-the-box.

Use Cases

  • gRPC is ideal for service-to-service communication between backend services.
  • It is also useful for fetching data from another service on demand or providing a streaming API to replicate data.
  • However, it is not ideal for browser-based applications as they do not offer a proper API to have low-level access to the HTTP 2 frames.

gRPC

In this section, the speaker discusses gRPC and its benefits.

Benefits of gRPC

  • gRPC is efficient thanks to protobuf, which reduces the size of messages being transported.
  • Communication patterns are varied and supported by Spring Cloud Gateway for routing.
  • Learning Protobuf may feel strange at first, but it's worth considering if you have a lot of traffic between back-end services.

RSocket

In this section, the speaker discusses RSocket and its benefits.

Benefits of RSocket

  • RSocket makes use of websockets or TCP for transportation.
  • It allows bidirectional streams with websockets API.
  • There aren't many default protocols available when using plain websockets.
  • The speaker suggests exploring what RSocket can do in terms of communication patterns.

Introduction to GraphQL, gRPC and RSocket

In this section, the speaker introduces three technologies - GraphQL, gRPC and RSocket. The speaker explains that these technologies are alternatives to REST APIs and can be used in situations where REST APIs are not ideal.

Technologies for Alternatives to REST APIs

  • GraphQL was founded by Facebook and is now a first-class citizen in the Spring world with Spring GraphQL.
  • gRPC is a library from the community.
  • RSocket was originally developed by Netflix and is also supported by the Spring framework natively as part of Spring messaging.

Limitations of REST APIs

In this section, the speaker discusses some limitations of REST APIs that make them unsuitable for certain use cases.

Limitations of REST APIs

  • Fetching data from different endpoints can result in a cascading sequence of requests which is slow and inefficient.
  • There is no standard way to approach this issue with REST APIs. Some public APIs offer custom expansion mechanisms but they are implemented differently across different platforms.

How GraphQL Works

In this section, the speaker explains how GraphQL works as an alternative to REST APIs.

Working with GraphQL

  • A schema describes the data that your API exposes.
  • With GraphQL, you have a single endpoint where all requests are received and handled. HTTP is only used for transportation of requests to the server.
  • Websockets can also be used for transportation of requests and provide a request stream communication style.
  • GraphQL is a language that describes the payloads being sent over to the single endpoint and how they are interpreted on the server side.

Experience with GraphQL

In this section, the speaker asks if anyone in the audience has experience working with GraphQL.

Audience Experience

  • A few people in the audience have worked with GraphQL.

Introduction to GraphQL

In this section, the speaker introduces GraphQL and its benefits.

What is GraphQL?

  • GraphQL is a query language for APIs that was developed by Facebook.
  • It addresses the problem of over or under fetching data, reducing the number of round trips when transporting data from clients to servers.
  • It allows you to define a schema that describes your data model and provides a strongly typed contract between client and server.

Benefits of GraphQL

  • Schema introspection allows tools to read and provide code completion for exploring the API.
  • Request-response pattern and request stream pattern are commonly used communication patterns.
  • HTTP can be used for transport, but caching may not make sense in this context.
  • Error reporting comes in the payload itself rather than using HTTP status codes.
  • Pagination mechanisms should be implemented to avoid overloading the server with large graph sizes.

Introduction to gRPC

In this section, the speaker introduces gRPC as an alternative to REST APIs.

What is gRPC?

  • gRPC is an open-source remote procedure call (RPC) framework initially developed by Google as their internal successor of their RPC framework.
  • It supports a variety of languages and frameworks, including Spring Boot Starter from the community when used with Spring.

Benefits of gRPC

  • It offers more efficient data transport than JSON payloads used in REST APIs because it uses binary serialization instead of text-based protocols like JSON.
  • It only transports necessary data without sending unnecessary attributes or schema information.
  • It simplifies API consumption by generating client-side code automatically based on service definitions.

HTTP/2 and gRPC

This section covers the benefits of using HTTP/2 and gRPC for efficient communication between backend services.

Benefits of gRPC

  • gRPC is efficient due to its use of Protobuf, which reduces the size of messages being transported.
  • gRPC offers a variety of different communication patterns.
  • Spring Cloud Gateway supports routing for gRPC services.
  • Learning Protobuf can be challenging, as it requires defining empty result types for command-based methods.

Benefits of HTTP/2

  • No bullet points with timestamps available in this section.

RSocket Protocol

This section covers the RSocket protocol and its advantages over plain web sockets.

Advantages of RSocket

  • RSocket uses websockets or TCP for transportation, making it ideal for browser-based applications that require bidirectional streams.
  • Plain web sockets lack default protocols, requiring custom protocols to connect messages being sent and received.
  • No additional bullet points with timestamps available in this section.

RSocket Communication Patterns

In this section, the speaker explains the communication patterns used in RSocket.

Requester and Responder

  • The terms requester and responder are used to refer to the two endpoints of communication in RSocket.
  • Once a connection is established, both ends can become requested and responder.
  • Requests can be sent from both ends to the other end.

Fire-and-Forget Semantics

  • RSocket has real fire-and-forget semantics since it's messaging.
  • You send a message and don't care what's coming back; you don't receive anything, not even an OK or something.

Streaming Patterns

  • RSocket supports basic request-response communication patterns as well as some streaming patterns.
  • Reactive streams inspired the name "RSocket."
  • The protocol implements reactive semantics over the wire to allow for back pressure working over the wire end-to-end from clients to servers.

Multiplexing

  • Similar to HTTP/2 or gRPC on HTTP/2, RSocket uses a single connection with multiplexing.
  • Any number of requests can go over that single connection at the same time by multiplexing.

Chat Room Implementation with RSocket

In this section, the speaker explains how they implemented a chat room using RSocket.

Message Mapping

  • A class is annotated with @Controller for implementing responders to receive messages.
  • A well-known annotation @MessageMapping is used to define a route similar to an HTTP route.

Chat Room Functionality

  • When sending a message to the route project with a project ID and then chat, you get a stream of chat messages from that chat room back.
  • Another endpoint receives those chat messages when you want to send a message to this chat room.
  • The RSocket client is used to be connected to this chat room and receive all the messages in both ways.

Online Updates

  • RSocket can also be used for online updates of clients upon changes.
  • Changes made on one user's UI are reflected on the other user's UI as well.

Using the Circuit Breaker Pattern in Your Applications

In

Video description

Spring I/O 2022 - Barcelona, 26-27 May Slides: https://speakerdeck.com/larsduelfer/beyond-rest-an-overview-about-modern-api-technologies GitHub repo: https://github.com/NovatecConsulting/YATT REST is the most popular web API technology used these days. It is well supported by programming languages, frameworks and all kind of monitoring tools as well as understood by many developers. It is therefore often the first choice when having to implement a web API for any kind of backend. But there are limitations and use cases where REST API’s do not fit that well. Luckily, there are a few interesting alternatives available these days. GraphQL is one such alternative so are gRPC and RSocket. In this talk I will introduce these technologies, explain their pros and cons compared to REST, talk about their maturity and how they are supported by Spring and Spring Boot. I will show code examples and a live demo.

Beyond REST API’s – An overview about modern API technologies by Lars Duelfer @ Spring I/O 2022 | YouTube Video Summary | Video Highlight