# Deploy Applications with the Milvus SDK

Choose gRPC or HTTP for Milvus Node.js deployments on servers, containers, serverless functions, and edge platforms.

SDK: `@zilliz/milvus2-sdk-node@3.0.10` · Tested with Milvus 3.0.6

Canonical documentation: https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/

## Section links

- [Choosing the Right Client](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#choosing-the-right-client)
- [Serverless Platforms](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#serverless-platforms)
- [Vercel](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#vercel)
- [Cloudflare Workers](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#cloudflare-workers)
- [AWS Lambda](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#aws-lambda)
- [Traditional Server Deployment](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#traditional-server-deployment)
- [Node.js/Express](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#nodejsexpress)
- [Docker](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#docker)
- [Environment Variables](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#environment-variables)
- [Platform Comparison](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#platform-comparison)
- [Common Patterns](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#common-patterns)
- [Client Reuse](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#client-reuse)
- [Error Handling](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#error-handling)
- [Timeout Configuration](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#timeout-configuration)
- [Next Steps](https://milvus-io.github.io/milvus-sdk-node/getting-started/deployment/#next-steps)

Use `MilvusClient` on Node.js servers with gRPC support. Choose `HttpClient` for HTTP-only environments such as Cloudflare Workers and Vercel Edge, or when a proxy requires REST.

## Choosing the Right Client

| Decision | MilvusClient (gRPC) | HttpClient (REST) |
| --- | --- | --- |
| Protocol | gRPC over HTTP/2 | Milvus REST v2 over HTTP with JSON |
| Request field casing | snake_case, such as `collection_name` | camelCase, such as `collectionName` |
| Serverless and edge support | Use in Node.js runtimes that support gRPC connections | Use in Fetch-capable runtimes, including HTTP-only serverless and edge environments |
| Streaming and gRPC-only admin RPCs | Supports replication and WAL streams, plus proxy and telemetry administration | Supports REST management operations and returns JSON responses |
| Response and error handling | Inspect `error_code` on a status or `status.error_code` on a result. Transport failures can reject. | Inspect `code` where the REST response provides it. Non-2xx responses throw `HttpError`. |

Set the REST client’s `endpoint` to your cluster origin. The SDK appends `/v2`. For a custom `baseURL`, include the full versioned URL ending in `/v2`.

## Serverless Platforms

### Vercel

Deploy to Vercel serverless functions with minimal configuration.

**Quick Start:**

```javascript
import { HttpClient } from '@zilliz/milvus2-sdk-node';

let client;

function getClient() {
  if (!client) {
    client = new HttpClient({
      endpoint: process.env.MILVUS_ENDPOINT,
      token: process.env.MILVUS_TOKEN,
      timeout: 10000,
    });
  }
  return client;
}

export default async function handler(req, res) {
  const milvusClient = getClient();
  const results = await milvusClient.search({
    collectionName: 'my_collection',
    data: req.body.vectors,
    limit: 10,
    outputFields: [],
  });
  res.status(200).json({ results });
}
```

**Learn more:** [Vercel Deployment Guide](https://milvus-io.github.io/milvus-sdk-node/advanced/vercel/)

### Cloudflare Workers

Deploy to Cloudflare Workers for edge computing.

**Quick Start:**

```javascript
import { HttpClient } from '@zilliz/milvus2-sdk-node/dist/milvus/HttpClient.js';

export default {
  async fetch(request, env) {
    const { vector } = await request.json();
    const client = new HttpClient({
      endpoint: env.MILVUS_ENDPOINT,
      token: env.MILVUS_TOKEN,
      timeout: 25000,
    });

    const results = await client.search({
      collectionName: 'my_collection',
      data: [vector],
      limit: 10,
      outputFields: [],
    });

    return new Response(JSON.stringify({ results }), {
      headers: { 'Content-Type': 'application/json' },
    });
  },
};
```

**Learn more:** [Cloudflare Workers Deployment Guide](https://milvus-io.github.io/milvus-sdk-node/advanced/cloudflare/)

### AWS Lambda

Deploy to AWS Lambda for scalable serverless functions.

**Quick Start:**

```javascript
import { HttpClient } from '@zilliz/milvus2-sdk-node';

let client;

function getClient() {
  if (!client) {
    client = new HttpClient({
      endpoint: process.env.MILVUS_ENDPOINT,
      token: process.env.MILVUS_TOKEN,
      timeout: 25000,
    });
  }
  return client;
}

export const handler = async (event) => {
  const milvusClient = getClient();
  const results = await milvusClient.search({
    collectionName: 'my_collection',
    data: JSON.parse(event.body).vectors,
    limit: 10,
    outputFields: [],
  });

  return {
    statusCode: 200,
    body: JSON.stringify({ results }),
  };
};
```

**Learn more:** [AWS Lambda Deployment Guide](https://milvus-io.github.io/milvus-sdk-node/advanced/aws-lambda/)

## Traditional Server Deployment

### Node.js/Express

For traditional Node.js servers, you can use either client:

```javascript
// Using gRPC Client (Recommended)
import { MilvusClient } from '@zilliz/milvus2-sdk-node';

const client = new MilvusClient({
  address: 'localhost:19530',
  token: 'your-token',
});

await client.connectPromise;

// Using HTTP Client
import { HttpClient } from '@zilliz/milvus2-sdk-node';

const client = new HttpClient({
  endpoint: 'http://localhost:19530',
  token: 'your-token',
});
```

### Docker

Deploy in Docker containers:

```dockerfile
FROM node:20-alpine

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 3000

CMD ["node", "index.js"]
```

## Environment Variables

Always use environment variables for sensitive configuration:

```bash
MILVUS_ENDPOINT=https://your-instance.zillizcloud.com
MILVUS_TOKEN=your-api-token
```

```javascript
const client = new HttpClient({
  endpoint: process.env.MILVUS_ENDPOINT,
  token: process.env.MILVUS_TOKEN,
});
```

## Platform Comparison

| Platform | Client Type | Timeout Limit | Best For |
| --- | --- | --- | --- |
| Vercel | HTTP | 10s (Hobby), 60s (Pro) | Next.js apps, static sites |
| Cloudflare Workers | HTTP | 30s (free), 15min (paid) | Edge computing, global distribution |
| AWS Lambda | HTTP | Up to 15 minutes | Enterprise serverless |
| Traditional Server | gRPC or HTTP | No limit | Long-lived applications |

## Common Patterns

### Client Reuse

Reuse client instances to improve performance:

```javascript
let client;

function getClient() {
  if (!client) {
    client = new HttpClient({
      endpoint: process.env.MILVUS_ENDPOINT,
      token: process.env.MILVUS_TOKEN,
    });
  }
  return client;
}
```

### Error Handling

Reuse the HTTP client from the previous example. Inspect the Milvus response code and catch errors from the HTTP request:

```javascript
async function searchCollection(queryVector) {
  try {
    const results = await getClient().search({
      collectionName: 'my_collection',
      data: [queryVector],
      limit: 10,
      outputFields: [],
    });
    if (results.code !== 0) {
      return { success: false, error: results.message, status: 502 };
    }
    return { success: true, results };
  } catch (error) {
    console.error('Milvus error:', error);
    return {
      success: false,
      error: error.message,
      status: error.status || 500,
    };
  }
}
```

### Timeout Configuration

Set timeouts based on platform limits:

```javascript
const client = new HttpClient({
  endpoint: process.env.MILVUS_ENDPOINT,
  token: process.env.MILVUS_TOKEN,
  timeout: 10000, // Adjust based on platform
});
```

## Next Steps

- **Vercel**: [Detailed Vercel Deployment Guide](https://milvus-io.github.io/milvus-sdk-node/advanced/vercel/)
- **Cloudflare**: [Cloudflare Workers Guide](https://milvus-io.github.io/milvus-sdk-node/advanced/cloudflare/)
- **AWS Lambda**: [AWS Lambda Guide](https://milvus-io.github.io/milvus-sdk-node/advanced/aws-lambda/)
- **HTTP Client**: [HTTP Client Documentation](https://milvus-io.github.io/milvus-sdk-node/advanced/http-client/)
- **Best Practices**: [Best Practices Guide](https://milvus-io.github.io/milvus-sdk-node/advanced/best-practices/)
