NATS is a cloud-native messaging system — handling pub/sub, request-reply, and streaming queues at up to 30 million messages per second on commodity hardware. It’s the backbone of many microservice architectures, replacing heavier alternatives like RabbitMQ or Kafka for simple pub/sub and request-reply patterns. Running NATS on a Hong Kong VPS gives Asia-Pacific microservices an ultra-low-latency message bus with CN2 GIA intra-data-centre connectivity.
NATS vs Kafka vs RabbitMQ
| Factor | NATS | Kafka | RabbitMQ |
|---|---|---|---|
| Throughput | 30M msg/sec | ~1M msg/sec | ~50K–200K msg/sec |
| Latency | <1ms | 2–10ms | 1–5ms |
| Memory (idle) | ~10 MB | ~500 MB (JVM) | ~80 MB |
| Persistence | JetStream (optional) | Always persistent | Optional |
| Complexity | Very low | High (ZooKeeper/KRaft) | Medium |
| Best for | Pub/sub, request-reply, IoT telemetry | Event streaming, log aggregation | Work queues, complex routing |
Step 1: Install NATS Server
<code">NATS_VERSION=$(curl -s https://api.github.com/repos/nats-io/nats-server/releases/latest | grep tag_name | cut -d'"' -f4)
wget "https://github.com/nats-io/nats-server/releases/download/${NATS_VERSION}/nats-server-${NATS_VERSION}-linux-amd64.zip"
unzip nats-server-*.zip
mv nats-server-*/nats-server /usr/local/bin/
nats-server --version
# Also install NATS CLI
curl -sf https://binaries.nats.dev/nats-io/natscli/nats@latest | sh
mv nats /usr/local/bin/
Step 2: Configure NATS with JetStream
<code"># /etc/nats/nats.conf
server_name: hk-nats-01
host: 127.0.0.1
port: 4222
# TLS for external connections (via Nginx tunnel)
# Internal: plain TCP, Nginx proxies TLS
# Authentication
authorization {
users = [
{ user: "admin", password: "AdminNATSPass!" }
{ user: "publisher", password: "PubPass!", permissions: {
publish: { allow: ["events.>", "orders.>"] }
subscribe: { deny: ">" }
}}
{ user: "consumer", password: "ConPass!", permissions: {
subscribe: { allow: ["events.>", "orders.>"] }
publish: { deny: ">" }
}}
]
}
# JetStream — persistent message storage
jetstream {
store_dir: /var/lib/nats/jetstream
max_memory_store: 1GB
max_file_store: 10GB
}
# Monitoring endpoint
http_port: 8222
# Logging
log_file: /var/log/nats/nats.log
logtime: true
max_traced_msg_len: 1024
Step 3: Systemd Service
<code">mkdir -p /var/lib/nats/jetstream /var/log/nats /etc/nats useradd -m -s /sbin/nologin nats chown -R nats:nats /var/lib/nats /var/log/nats cat > /etc/systemd/system/nats.service << 'EOF' [Unit] Description=NATS Message Server After=network.target [Service] User=nats ExecStart=/usr/local/bin/nats-server -c /etc/nats/nats.conf Restart=always RestartSec=5 LimitNOFILE=65536 [Install] WantedBy=multi-user.target EOF systemctl enable --now nats nats server check --server nats://admin:AdminNATSPass!@127.0.0.1:4222
Step 4: Core Messaging Patterns
<code"># npm install nats
// publisher.ts — publish events
import { connect, StringCodec } from 'nats'
const nc = await connect({
servers: 'nats://publisher:PubPass!@127.0.0.1:4222',
})
const sc = StringCodec()
// Publish order event
await nc.publish('orders.placed', sc.encode(JSON.stringify({
orderId: 'ord_123',
userId: 'usr_456',
amount: 299.00,
currency: 'HKD',
})))
// Request-reply (synchronous RPC pattern)
const reply = await nc.request(
'pricing.calculate',
sc.encode(JSON.stringify({ productId: 1, quantity: 3 })),
{ timeout: 5000 }
)
const price = JSON.parse(sc.decode(reply.data))
console.log('Price:', price)
await nc.close()
// subscriber.ts — consume events
const nc = await connect({ servers: 'nats://consumer:ConPass!@127.0.0.1:4222' })
const sc = StringCodec()
// Queue group — load-balanced across multiple consumers
nc.subscribe('orders.placed', {
queue: 'order-processors', // Only ONE consumer in the group receives each message
callback: (err, msg) => {
const order = JSON.parse(sc.decode(msg.data))
console.log('Processing order:', order.orderId)
// Process order...
msg.respond(sc.encode('{"status":"processed"}'))
}
})
Step 5: JetStream Persistent Streams
<code"># Create a persistent stream (survives server restart)
nats stream add orders \
--server nats://admin:AdminNATSPass!@127.0.0.1:4222 \
--subjects "orders.*" \
--retention limits \
--max-age 7d \
--max-msgs 1000000 \
--storage file \
--replicas 1
# Create a consumer (durable — remembers position across restarts)
nats consumer add orders order-fulfillment \
--server nats://admin:AdminNATSPass!@127.0.0.1:4222 \
--deliver all \
--ack explicit \
--filter "orders.placed" \
--max-deliver 3 \
--backoff linear
# In code — JetStream consumer with delivery guarantees
import { connect, AckPolicy, DeliverPolicy } from 'nats'
const nc = await connect({ servers: '...' })
const js = nc.jetstream()
const jsm = await nc.jetstreamManager()
const consumer = await js.consumers.get('orders', 'order-fulfillment')
for await (const msg of await consumer.consume()) {
const order = JSON.parse(msg.string())
await fulfillOrder(order)
msg.ack() // Acknowledge — prevents redelivery
}
Step 6: NATS Cluster (Two HK VPS Nodes)
<code"># NATS clustering for HA (add to config on each node)
cluster {
name: hk-cluster
host: VPS1_IP
port: 6222
routes: [
nats-route://VPS2_IP:6222
]
}
# Second node:
cluster {
name: hk-cluster
host: VPS2_IP
port: 6222
routes: [
nats-route://VPS1_IP:6222
]
}
# Clients connect to either node — routing is automatic
Conclusion
NATS on a Hong Kong VPS provides a sub-1ms message bus for Asia-Pacific microservices — pub/sub event fanout, request-reply RPC patterns, and JetStream persistent queues with exactly-once processing semantics. Its 10 MB memory footprint means it coexists on the same VPS as your application without meaningful resource contention. For e-commerce order processing, IoT telemetry from HK-based devices, and real-time notifications distributed across Southeast Asia microservices, NATS delivers Kafka-like capabilities with a fraction of the operational complexity.
Connect your microservices: Browse Server.HK Hong Kong VPS plans — NATS runs on any plan; it’s so lightweight it fits alongside your main application on even a 2 GB VPS.