Scala FS2 and HTTP4S

Part of our "Scala" courses

1 day (onsite)
2 days (online)

Scala FS2 and HTTP4S
Outline Last updated:

Course Overview

A solid understanding of fs2 and http4s is essential for a more comprehensive grasp of Scala. If you are looking for an efficient and speedy polymorphic stream processing library, then FS2, or Functional Streams for Scala, is for you. It is compatible with Cats-effect typeclasses and can be used with other effect libraries. Built on FS2, HTTP4S is the ideal companion topic for this course and is the minimal Scala interface for your HTTP services.

Who should attend

Scala developers building HTTP services on the Typelevel stack, or about to, who already have Cats and Cats Effect and now need the streaming and HTTP layers on top. Also for teams inheriting an fs2 or http4s codebase who can follow the code but could not yet write it from scratch.

What you'll learn

  • Understand what fs2 offers, and how it compares with other streaming libraries
  • Build pure and effectful streams, and compile them down to an effect type
  • Work fluently with the core fs2 abstractions (Stream, Pipe, Chunk and Pull)
  • Handle errors, build retry logic and manage resource lifecycles in a stream
  • Understand how http4s is built on Cats Effect and fs2, and why Kleisli underpins its routing
  • Build an HTTP server with the http4s DSL, covering routing, path and query parameters
  • Encode and decode entities, including JSON with Circe
  • Write and apply your own middleware, and test your routes
  • Consume HTTP services with the http4s client and its middleware

Course Prerequisites

Students should be familiar with the Scala programming language (preferably version 3), cats and cats-effect (preferably version 3). We advise students to follow our SCALA-CATS course

Outline

fs2

  • Introduction to fs2
  • Compare fs2 to other streaming libraries
  • Defining pure streams
  • using core operations (map, flatMap, take, …)
  • Combining Streams
  • Define effectful streams
  • Understand stream compilation to F[_]
  • List key fs2 API (Stream, Pipe, Chunk, Pull, …)
  • Transforming values using pipes
  • Handling errors
  • Building Retry logic
  • Building declarative flows
  • Handling and releasing Resources
  • Introduction to Concurrency

http4s

  • Introduce http4s as a functional HTTP client/server
  • Introduce its Architecture
  • Understand the relevance of Kleisli, MonoidK and other typeclasses
  • Understand the role of fs2
  • Introduce the Ember backend, and why http4s is independent of any one backend

The Server and APIs/DSL

  • Configure a server using EmberServerBuilder
  • Introduce the server DSL
  • Build polymorphic http4s code
  • Understand how to build a Response (body, headers)
  • How to map HTTP Requests
  • Extract Path Parameters
  • Use Query Parameters
  • Introduce Entity Handling and codecs
  • Understand how EntityDecoder/EntityEncoder works
  • Using built-in codecs (strings, fs2 Stream, …)
  • Introduce/Recap Circe for Json handling
  • Integrate http4s and circe
  • Accessing the request body (for POST)
  • Adding request Validation using cats
  • Introduce and use Middleware
  • Define your own Middleware
  • Apply Middleware to your routes (logging, Authentication, CORS, etc.)
  • Testing http4s routes

The Client API/DSL

  • Set up a client using EmberClientBuilder
  • Introduce the client API
  • Making requests use the Client DSL
  • Handling the responses
  • Working with Headers
  • Discuss client middleware (retry, follow-redirects, logging, metrics)

Frequently asked questions

Does the course use Blaze or Ember?

Ember, for both the server and the client. It is the backend the http4s project itself points people at, and blaze is now the older option. http4s is deliberately independent of its backend, and we cover why that matters rather than treating the choice as fixed.

What are the prerequisites?

Scala, preferably Scala 3, plus working knowledge of Cats and Cats Effect 3. This course builds directly on those abstractions rather than reintroducing them, so if effects and type classes are still new, take SCALA-CATS first.

Can I attend for fs2 alone, without the http4s half?

The two are taught together by design, because http4s request and response bodies are fs2 streams and the second half keeps reinforcing the first. The fs2 material stands on its own if streaming is your only interest, but you would be paying for a day you only half use.

How hands-on is the course?

Very. Eight lab sessions carry the material, from fs2 pipes and resilience through building an http4s application from scratch, routing, path handling, JSON and testing, alongside shorter exercises in the slides.

Do I need to install anything on my laptop?

No. Every student gets their own pre-configured cloud machine with the JDK and Scala tooling already in place, so all you need locally is an SSH client (already present on Linux, macOS and Windows). Work in the editors installed on the machine (vim, LazyVim, Emacs) or connect your own with IntelliJ's SSH remote development or VS Code Remote Development. Login details are emailed a week before the course.

How long is the Scala FS2 and HTTP4S course?

1 day on-site or 2 days online. Sessions can run on consecutive days or be spread out to fit your team's schedule.

How large are the groups?

Deliberately small so the trainer can adapt to every participant: at most 10 on-site and 7 online.

In which languages can the course be delivered?

English, French or Dutch.

This Scala FS2 and HTTP4S course looks very interesting, I do however have a question

Related courses