Performance is a critical aspect of any application. Hive Gateway Runtime provides a set of features to help you optimize the performance of your gateway.
Hive Gateway provides a set of features to help you optimize the performance of your GraphQL
gateway. Hive Gateway provides a shared caching storage that can be used across plugins, transforms,
subgraph execution and remote schema caching.
Providing Cache Storage
In order to enable features that need a storage to keep the data, you need to define a cache storage
implementation, and pass it to the gateway.
You can choose the best-fit cache storage for your use case.
LocalForage
LocalForage is a library that improves the existing storage mechanism in the browser by using
IndexedDB, WebSQL and localStorage, see more.
Even if it is known as a browser storage, Hive Gateway provides you as a platform-agnostic cache
storage to leverage the well-known storage APIs that are available in most JavaScript environments.
gateway.config.ts
import { defineConfig } from '@graphql-hive/gateway'export const gatewayConfig = defineConfig({ cache: { type: 'localforage', // All of the following options are listed with default values, you don't need to provide them driver: ['WEBSQL', 'INDEXEDDB', 'LOCALSTORAGE'] // The order of the drivers to use name: 'HiveGateway', // The name of the database version: 1.0, // The version of the database size: 4980736, // The size of the database storeName: 'keyvaluepairs', // The name of the store description: 'Cache storage for Hive Gateway', // The description of the database } responseCaching: { session: () => null, }})
Redis
Redis is an in-memory data structure store, used as a database, cache, and message broker. You can
use Redis as a cache storage for your Hive Gateway.
gateway.config.ts
import { defineConfig } from "@graphql-hive/gateway";export const gatewayConfig = defineConfig({ cache: { type: "redis", host: "localhost", // The host of the Redis server port: 6379, // The port of the Redis server password: undefined, // The password of the Redis server lazyConnect: true, // If true, the connection will be established when the first operation is executed // or url: "redis://localhost:6379", // The URL of the Redis server }, responseCaching: { session: () => null, },});
AWS IAM Authentication
Hive Gateway supports IAM authentication for Amazon ElastiCache and MemoryDB. It generates a
short-lived SigV4 presigned URL and uses it as the Redis password. The token is valid for up to 15
minutes and uses AWS credentials from the standard credential chain, including environment
variables, ~/.aws/credentials, EC2 instance roles and ECS task roles.
bun add @aws-crypto/sha256-js @aws-sdk/credential-providers @aws-sdk/util-format-url @smithy/protocol-http @smithy/signature-v4
These packages are loaded only when iamAuth is configured.
gateway.config.ts
import { defineConfig } from "@graphql-hive/gateway";export const gatewayConfig = defineConfig({ cache: { type: "redis", host: "my-cluster.abc123.0001.use1.cache.amazonaws.com", port: 6379, username: "iam-user-01", tls: true, iamAuth: { // aws region where the cluster is deployed region: "us-east-1", // cluster name used as the host in the sigv4 presigned url clusterName: "my-cluster", // iam-enabled redis username, matching the elasticache or memorydb user id userId: "iam-user-01", // aws service to sign for, defaults to "elasticache" serviceName: "elasticache", // token expiry in seconds, defaults to the maximum of 900 tokenExpirySeconds: 900, }, }, responseCaching: { session: () => null, },});
If you are using Redis Cluster, You can use the
startupNodes option to connect to the Redis Cluster.
gateway.config.ts
import { defineConfig } from "@graphql-hive/gateway";export const gatewayConfig = defineConfig({ cache: { type: "redis", startupNodes: [ { host: "localhost", port: 7000 }, { host: "localhost", port: 7001 }, { host: "localhost", port: 7002 }, ], // If you are using AWS ElastiCache with TLS, you need to use the following options tls: true, dnsLookupAsIs: true, }, responseCaching: { session: () => null, },});
Cloudflare Workers KV
Cloudflare Workers KV is a distributed, eventually consistent key-value store available in the
Cloudflare Workers runtime. You can use Cloudflare Workers KV as a cache storage for your Hive
Gateway. Learn more about KV here.
You can also implement your own cache storage by extending the CacheStorage class. It needs to
match KeyValueCache interface from @graphql-hive/gateway.
my-cache-storage.ts
import { LRUCache } from "lru-cache";import { KeyValueCache } from "@graphql-hive/gateway";export class MyKeyValueCache<V = any> implements KeyValueCache<V> { // Your cache implementation here private cache = new LRUCache<string, V>(); // Get the value of the key async get(key: string) { return this.cache.get(key); } // Set the key with the value and optional options async set(key: string, value: V, options?: { ttl?: number }) { this.cache.set(key, value, options?.ttl); } // Delete the key from the cache async delete(key: string) { this.cache.del(key); } // Get all keys that match the given prefix async getKeysByPrefix(prefix: string) { return Array.from(this.cache.keys()).filter((key) => key.startsWith(prefix), ); } // This should be implemented if you want to clear the cache on shutdown [Symbol.asyncDispose]() { this.cache.reset(); }}
This site uses cookies for analytics and improving your experience.