elastic-logo-light

Let's build our new startup!

Let's build our new startup!

Let's build the next billion dollar startup, Moo!

Basically Twitter, but with more characters and cows.

A user should be able to:

Let's build our new startup!

The API will expose three different services, login, post and user.

/signup
/post/:id
/post?search=${text}
/post/create
/user/:username/posts

benchmarks

A little bit of configuration

Being consistent across microservices is a difficult task,
to help you Fastify provides a powerful CLI.

npx fastify generate moo-project
cd moo-project
npm install

Project structure

Scripts

“Let's the hack begin”

Let's take a look at our monolith

// demo

monolith

Monolith vs Microservices

monoliths-vs-microservices

Fastify Plugins

A brief overview

fastify.register(
  require('my-plugin'),
  { options }
)
function myPlugin (fastify, opts, next) {
  // register other plugins
  fastify.register(...)

  // add hooks
  fastify.addHook(...)

  // add decorator
  fastify.decorate(...)

  // add routes
  fastify.route(...)

  next()
}

module.exports = myPlugin

async await
is supported as well!

Plugins: Architecture

dag

Plugins: Encapsulation

dag-decorate

Exposing functionality to parents

const fp = require('fastify-plugin')

async function myPlugin (fastify, options) {
  fastify.decorate('util', yourAwesomeUtility)
  // now you can use it with `fastify.util`
}

module.exports = fp(myPlugin)

Plugins: Encapsulation

dag-fp-encapsulate

Plugins: Real world

plugin-real-world

Encapsulation enables many great things

such as custom log-level per plugin


const fastify = require('fastify')()

fastify.register(require('./api/v1'), {
  prefix: '/v1',
  logLevel: 'error'
})

fastify.register(require('./api/v2'), {
  prefix: '/v2',
  logLevel: 'debug'
})

Everything is a plugin

From monolith to microservices

Let's begin!

// demo

monolith

Awesome!

Now update all your clients so they know which address to call based on the service they need to use.

clients

WRONG!

The infrastructure
should be transparent
to the client.

How can we fix this?

Gateway.

npm install fastify-http-proxy
fastify.register(require('fastify-http-proxy'), {
  upstream: 'http://localhost:3030',
  prefix: '/post'
})

// demo

gateway