Home

Awesome

<p align="center"> <a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="120" alt="Nest Logo" /></a> </p> <p align="center"> A module for improving the reliability and fault-tolerance of your <a href="https://nestjs.com/" target="_blank">NestJS</a> applications </p> <p align="center"> <a href='https://img.shields.io/npm/v/nestjs-resilience'><img src="https://img.shields.io/npm/v/nestjs-resilience" alt="NPM Version" /></a> <a href='https://img.shields.io/npm/l/nestjs-resilience'><img src="https://img.shields.io/npm/l/nestjs-resilience" alt="NPM License" /></a> <a href='https://img.shields.io/npm/dm/nestjs-resilience'><img src="https://img.shields.io/npm/dm/nestjs-resilience" alt="NPM Downloads" /></a> <a href='https://img.shields.io/github/last-commit/SocketSomeone/nestjs-resilience'><img src="https://img.shields.io/github/last-commit/SocketSomeone/nestjs-resilience" alt="Last commit" /></a> </p>

About

NestJS Resilience is an open-source library that provides a set of reliable patterns for building resilient applications on top of NestJS. The library includes several key features, including retry, circuit breaker, and timeout patterns, which help to ensure that your application can handle failures and recover quickly from them.

With NestJS Resilience, you can easily configure these patterns for your application, allowing you to improve the reliability and fault-tolerance of your services. Whether you're building a high-traffic web application or a distributed system, NestJS Resilience provides the tools you need to build robust, failure-resistant services that can withstand even the most challenging environments.

Features

Installation

$ npm install nestjs-resilience
$ yarn add nestjs-resilience
$ pnpm add nestjs-resilience

Usage

Import the module

import { Module } from '@nestjs/common';
import { ResilienceModule } from 'nestjs-resilience';

@Module({
    imports: [ResilienceModule.forRoot()]
})
export class AppModule {}

Use store field to configure cache (e.x. store: new RedisStore({ host: 'localhost', port: 6379 })). We use memory from cache-manager store by default.

Ways to use

You can use it in three ways:

1. Use ResilienceCommand

You can create a command by extending the ResilienceCommand class. You can also use the ResilienceFactory to create a command with a set of policies.

import { Injectable } from '@nestjs/common';
import { ResilienceCommand, ResilienceFactory } from 'nestjs-resilience';
import { UsersService } from './user.service';
import { User, NullUserObject } from './user.entity';

@Injectable()
export class GetUserByIdCommand extends ResilienceCommand {
    constructor(
        private readonly factory: ResilienceFactory,
        private readonly userService: UsersService
    ) {
        super([
            // You can use the injected factory to create a strategy
            factory.createTimeout(1000),
            // Or you can create a strategy directly
            ResilienceFactory.createFallback((id) => new NullUserObject(id))
            // You can also use mannually created strategies
            // new TimeoutStrategy(1000),
        ]);
    }

    async run(id: number): User {
        return this.usersService.getUser(id);
    }
}

This way supports DI, just what you need add @Injectable() decorator to your command and to providers of your module. Inject your command in the constructor or use resilienceService.getCommand(GetUserByIdCommand).

FAQ:

2. Use the @UseResilience() decorator

You can use @UseResilience() decorator to wrap your service methods.

import { Injectable } from '@nestjs/common';
import { TimeoutStrategy } from "./timeout.strategy";
import { NullUserObject, User } from './user.entity';

@Injectable()
export class UsersService {
    @UseResilience(new TimeoutStrategy(1000), ResilienceFactory.createFallback((id) => new NullUserObject(id)))
    async getUser(id: number): User {
        return this.httpService.get(`https://example.com/users/${id}`).toPromise();
    }
}

Not the best way to use in controller methods. @UseResilience rewrite your method.

3. Interceptors

You also can wrap your controller methods with ResilienceInterceptor and use all the features of the library.

import { Controller, Get, UseInterceptors } from '@nestjs/common';
import { ResilienceInterceptor } from 'nestjs-resilience';
import { UsersService } from './users.service';

@Controller('users')
export class UsersController {
    constructor(private readonly usersService: UsersService) {
    }

    @Get()
    @UseInterceptors(ResilienceInterceptor(new TimeoutStrategy(1000), ResilienceFactory.createFallback(() => [])))
    async getUsers(): User[] {
        return this.usersService.getUsers();
    }
}

Observables

We also support Observable as a return type. You can use it with @UseResilienceObservable() decorator or ResilienceCommandObservable.

import { Injectable } from '@nestjs/common';
import { TimeoutStrategy } from "./timeout.strategy";
import { NullUserObject, User } from './user.entity';
import { Observable, of } from 'rxjs';

@Injectable()
export class UsersService {
    @UseResilienceObservable(new TimeoutStrategy(1000), ResilienceFactory.createFallback((id) => new NullUserObject(id)))
    getUser(id: number): Observable<User> {
        return of(new User(id, 'John Doe'));
    }
}

Order of execution

Strategies processing in order which you pass them to the ResilienceCommand or ResilienceInterceptor.

What it means? Let's take a look at the example:

Timeout before Retry:

Retry after Timeout:

Strategies

StrategyDescription
TimeoutStrategyAutomatically fail fast when a service is taking too long to respond
RetryStrategyAutomatically retry failed requests
CircuitBreakerStrategyAutomatically fail fast when a service is unavailable
BulkheadStrategyLimit the number of concurrent requests to a service
FallbackStrategyProvide a fallback response when a service is unavailable
ThrottleStrategyLimit the number of requests to a service
HealthCheckStrategyCheck the health of a service
CacheStrategyCache the result of a service call

Stay in touch

License

MIT © Alexey Filippov