Deploy Applications with the Milvus SDK
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
Section titled “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
Section titled “Serverless Platforms”Vercel
Section titled “Vercel”Deploy to Vercel serverless functions with minimal configuration.
Quick Start:
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
Cloudflare Workers
Section titled “Cloudflare Workers”Deploy to Cloudflare Workers for edge computing.
Quick Start:
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
AWS Lambda
Section titled “AWS Lambda”Deploy to AWS Lambda for scalable serverless functions.
Quick Start:
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
Traditional Server Deployment
Section titled “Traditional Server Deployment”Node.js/Express
Section titled “Node.js/Express”For traditional Node.js servers, you can use either client:
// 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 Clientimport { HttpClient } from '@zilliz/milvus2-sdk-node';
const client = new HttpClient({ endpoint: 'http://localhost:19530', token: 'your-token',});Docker
Section titled “Docker”Deploy in Docker containers:
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]Environment Variables
Section titled “Environment Variables”Always use environment variables for sensitive configuration:
MILVUS_ENDPOINT=https://your-instance.zillizcloud.comMILVUS_TOKEN=your-api-tokenconst client = new HttpClient({ endpoint: process.env.MILVUS_ENDPOINT, token: process.env.MILVUS_TOKEN,});Platform Comparison
Section titled “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
Section titled “Common Patterns”Client Reuse
Section titled “Client Reuse”Reuse client instances to improve performance:
let client;
function getClient() { if (!client) { client = new HttpClient({ endpoint: process.env.MILVUS_ENDPOINT, token: process.env.MILVUS_TOKEN, }); } return client;}Error Handling
Section titled “Error Handling”Reuse the HTTP client from the previous example. Inspect the Milvus response code and catch errors from the HTTP request:
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
Section titled “Timeout Configuration”Set timeouts based on platform limits:
const client = new HttpClient({ endpoint: process.env.MILVUS_ENDPOINT, token: process.env.MILVUS_TOKEN, timeout: 10000, // Adjust based on platform});Next Steps
Section titled “Next Steps”- Vercel: Detailed Vercel Deployment Guide
- Cloudflare: Cloudflare Workers Guide
- AWS Lambda: AWS Lambda Guide
- HTTP Client: HTTP Client Documentation
- Best Practices: Best Practices Guide