Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Flexible compression layer for Apollo Server Caching modules. Used to reduce memory and in-transit data usage.

License

Notifications You must be signed in to change notification settings

kdybicz/apollo-server-compression-cache-wrapper

Repository files navigation

⛔ DEPRECATED ⛔

Since Apollo Server v4 it's recommend to use Keyv as a database adapter for cache and as Keyv improved support for cache compression, this project is no longer needed and will not be maintained anymore.

Instead I recommend using:

import { KeyvAdapter } from '@apollo/utils.keyvadapter';
import KeyvBrotli from '@keyv/compress-brotli';

import Keyv from 'keyv';
import zlib from 'zlib';

const keyv = new Keyv(process.env.REDIS_HOST, {
  ...
  compression: new KeyvBrotli({
    compressOptions: {
      params: {
        [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
        [zlib.constants.BROTLI_PARAM_QUALITY]: 3,
      },
    },
  }),
  ...
});

const server = new ApolloServer<ApolloContext>({
  ...
  cache: new KeyvAdapter(keyv, { disableBatchReads: true }),
  ...
});

CompressionCacheWrapper

No Maintenance Intended Tests CodeQL npm version npm downloads

This package exports an implementation of KeyValueCache that allows wrapping any other Apollo KeyValueCache implementation with a configurable (default: zlib deflate) compression layer. Its main goal is to limit the amount of memory used by the caching environment and at the same time the amount of data being in-transit from and to the caching environment.

Usage

const { RedisCache } = require('apollo-server-cache-redis');
const { CompressionCacheWrapper } = require('apollo-server-compression-cache-wrapper');
const 'zlib' = require('zlib');

const redisCache = new RedisCache({
  host: 'redis-server',
});

const server = new ApolloServer({
  typeDefs,
  resolvers,
  cache: new CompressionCacheWrapper(redisCache, {
    compress: (data: Buffer) => zlib.deflateSync(data, { level: 1 }),
    decompress: (data: Buffer) => zlib.inflateSync(data),
    minimumCompressionSize: 262144,
  }),
  dataSources: () => ({
    moviesAPI: new MoviesAPI(),
  }),
});

Options

  • compress (default: zlib deflate) - defines a custom compression method taking and returning a Buffer.
  • decompress (default: zlib deflate) - defines a custom decompression method taking and returning a Buffer.
  • minimumCompressionSize (default: 262144) - defines minimal length of the data string, after exceeding which data proxied to wrapped cache are compressed before being passed forward.

Debug

For better performance monitor of CompressionCacheWrapper module in your app, run your app with DEBUG env.

To get all debug messages from all modules:

DEBUG=* npm run start

To get debug messages only from compression-wrapper module:

DEBUG=compression-wrapper npm run start