Amazon DAX Client for JavaScript

1.2.10 · active · verified Tue Apr 21

The `amazon-dax-client` package provides a client library for Node.js applications to interact with Amazon DynamoDB Accelerator (DAX), a fully managed caching service. DAX offers high-performance in-memory caching for DynamoDB, significantly boosting read performance by up to 10 times, even at millions of requests per second. This client is API compatible with DynamoDB's AWS SDK v2, allowing for minimal code changes to integrate DAX caching into existing applications. The current stable version, 1.2.10, focuses on optimizing connection clean-up and addressing various connection stability and data parsing issues, indicating an active maintenance and bug-fix release cadence for the v1.x branch. It differentiates itself by providing a seamless caching layer for AWS SDK v2 DynamoDB operations, transparently handling caching logic. For applications utilizing AWS SDK for JavaScript v3, a separate client, `@aws-sdk/client-dax`, is available and features a modular architecture, which represents a significant shift in usage patterns.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate the `AmazonDaxClient` and integrate it with the `AWS.DynamoDB.DocumentClient` to perform basic `put` and `get` operations, leveraging DAX for caching. It assumes `aws-sdk` is already installed and configures region and endpoint from environment variables or defaults.

const AmazonDaxClient = require('amazon-dax-client');
const AWS = require('aws-sdk'); // Make sure aws-sdk is installed

// Replace with your actual DAX cluster discovery endpoint
const endpoint = process.env.DAX_CLUSTER_ENDPOINT ?? 'dax://my-cluster.abc123.dax-clusters.us-east-1.amazonaws.com';
const region = process.env.AWS_REGION ?? 'us-east-1';

console.log(`Connecting to DAX endpoint: ${endpoint} in region: ${region}`);

try {
  // Instantiate the DAX client
  const dax = new AmazonDaxClient({ endpoints: [endpoint], region: region });

  // Integrate DAX with DynamoDB DocumentClient
  const docClient = new AWS.DynamoDB.DocumentClient({ service: dax });

  // Example: Put an item using DocumentClient with DAX
  const tableName = 'MyTestTable'; // Replace with your table name
  const item = { pk: '123', sk: 'data', attribute: 'cached value' };

  console.log('Putting item with DAX-enabled DocumentClient...');
  docClient.put({ TableName: tableName, Item: item }, (err, data) => {
    if (err) {
      console.error('Error putting item:', JSON.stringify(err, null, 2));
    } else {
      console.log('Successfully put item:', data);

      // Example: Get the item using DocumentClient with DAX
      console.log('Getting item with DAX-enabled DocumentClient...');
      docClient.get({ TableName: tableName, Key: { pk: '123', sk: 'data' } }, (getErr, getData) => {
        if (getErr) {
          console.error('Error getting item:', JSON.stringify(getErr, null, 2));
        } else {
          console.log('Successfully got item:', getData);
        }
      });
    }
  });
} catch (e) {
  console.error('Failed to initialize DAX client:', e);
}

view raw JSON →