Design Pattern
why they are still important in the microservice era
Once upon a time,
there were only monoliths
your application was written in a one repo
and the deployment was simple
We know how to structure the code
We know how to test the code
We know how to deploy our application
We know how to monitor our application
We know how to improve performance
We know how to add new feature
More than 20 years of knowledge
Erich Gamma
Richard Helm
Ralph Johnson
John Vlissides
Design Patterns
Factory
Builder
Singleton
Adapter
Facade
Proxy
Iterator
Observer
Strategy
Visitor
They helped us
TILL NOW
Know which domains are involved
What we need to build a microservice ecosystem
Choose which µs should scale up
Find the common needs to refactorize them
Consistent deployment system
Another way
We can use our knowledge
to reach the business
as soon as possible
without complicating the code too much
Build a monolith using the design patterns
to split it later
It is easier with the right tool
Fastify can be a solution
Decorators
Encapsulation
// Injection
fastify.decorate('myObject', new MyObject({ /* ... */ ))
fastify.decorate('myCounter', 0)
fastify.decorateRequest('getUserId', function () {
return this.headers['mia-user']
})
// Usage
fastify.get('/', myHandler)
function myHandler (request, reply) {
const userId = request.getUserId()
const result = this.myObject.getResult()
reply.send(result)
}
fastify.register(plugin1, { prefix: '/plugin1/' })
fastify.register(plugin2, { prefix: '/plugin2/' })
// Plugin1
function plugin1 (fastify, o, n) {
fastify.decorate('myObject', new MyObject({ /* ... */ })
fastify.get('/', myHandler)
}
// Plugin2
function plugin2 (fastify, o, n) {
fastify.decorate('myObject', new MyObject({ /* ... */ })
fastify.get('/', myHandler)
}
An example
User
UserService
UserClient
HTTP routes
Tweet
TweetService
TweetClient
HTTP routes
Follow
FollowService
FollowClient
HTTP routes
Timeline
TimelineService
HTTP routes
Fastify
So
start simple
complicate later
Thanks
Design pattern in microservice era
By Tommaso Allevi
Design pattern in microservice era
- 625