Skip to content

Widhian Bramantya

coding is an art form

Menu
  • About Me
Menu
nats

Key-Value Store in NATS: Simple State Management

Posted on September 6, 2025September 6, 2025 by admin

So far, we’ve looked at different messaging patterns in NATS like Publish–Subscribe, Request–Reply, and Queue Groups. Those patterns are great for communication, but sometimes you need a way to store small pieces of state that services can read, update, or watch for changes.

NATS provides this through its Key-Value Store (KV), built on top of JetStream.

What is KV in NATS?

A Key-Value Store is a simple data structure:

  • Key → the name of the value (like a variable or identifier).
  • Value → the data associated with that key.

In NATS, the KV store is distributed and persistent. Multiple clients can update values, and others can subscribe to changes in real time. It feels like combining a database with a messaging system.

Why Use KV in NATS?

  1. Configuration Sharing
    • Store application configs (e.g., feature flags) that multiple services can read and react to.
  2. Service Coordination
    • Workers can update their status in the KV store, and other services can watch for changes.
  3. Lightweight State Tracking
    • Keep track of counters, tokens, or session info without needing a heavy database.

NATS KV vs Redis

It’s natural to compare NATS KV with Redis, since both can store key-value pairs. But they are designed for different use cases:

AspectNATS KVRedis
Built onJetStream (message persistence layer)In-memory database
PersistenceDurable, message-log based, supports historyOptional persistence (RDB, AOF), but primarily in-memory
Real-time WatchNative, subscribers get notified instantly when keys changeRequires Pub/Sub feature, separate from main KV
HistoryKeeps a versioned history of updatesOnly stores latest value (unless you implement manually)
ScaleLightweight, integrated with NATS messagingVery fast at caching and counters, but heavier to cluster
Use caseConfig sharing, service coordination, leader electionCaching, high-speed counters, session store

What is “Version” in KV?

In NATS KV, every update to a key creates a new version (a sequence number).

  • When you Put a value, JetStream stores it as an entry in the log with an incremented version.
  • If you Get or Watch, you can see the version of the value.
  • This allows optimistic concurrency control: you can update a key only if it’s still at a certain version.
See also  Queue Groups in NATS: Load Balancing for Subscribers

Example in Go

// Put a new value
rev, _ := kv.Put("featureX.enabled", []byte("true"))
fmt.Println("Current version:", rev)

// Try update only if version matches
err := kv.Update("featureX.enabled", []byte("false"), rev)
if err != nil {
    fmt.Println("Update failed, value changed in the meantime")
}

Why Version Matters?

  • Consistency: Prevents overwriting changes from another client.
  • Auditing: You can trace back changes by version.
  • Safe collaboration: Multiple services can update the same key without stepping on each other’s changes.
Pages: 1 2
Category: NATS

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Linkedin

Widhian Bramantya

Recent Posts

  • Log Management at Scale: Integrating Elasticsearch with Beats, Logstash, and Kibana
  • Index Lifecycle Management (ILM) in Elasticsearch: Automatic Data Control Made Simple
  • Blue-Green Deployment in Elasticsearch: Safe Reindexing and Zero-Downtime Upgrades
  • Maintaining Super Large Datasets in Elasticsearch
  • Elasticsearch Best Practices for Beginners
  • Implementing the Outbox Pattern with Debezium
  • Production-Grade Debezium Connector with Kafka (Postgres Outbox Example – E-Commerce Orders)
  • Connecting Debezium with Kafka for Real-Time Streaming
  • Debezium Architecture – How It Works and Core Components
  • What is Debezium? – An Introduction to Change Data Capture
  • Offset Management and Consumer Groups in Kafka
  • Partitions, Replication, and Fault Tolerance in Kafka
  • Delivery Semantics in Kafka: At Most Once, At Least Once, Exactly Once
  • Producers and Consumers: How Data Flows in Kafka
  • Kafka Architecture Explained: Brokers, Topics, Partitions, and Offsets
  • Getting Started with Apache Kafka: Core Concepts and Use Cases
  • Security Best Practices for RabbitMQ in Production
  • Understanding RabbitMQ Virtual Hosts (vhosts) and Their Uses
  • RabbitMQ Performance Tuning: Optimizing Throughput and Latency
  • High Availability in RabbitMQ: Clustering and Mirrored Queues Explained

Recent Comments

  1. Playing with VPC AWS (Part 2) – Widhian's Blog on Playing with VPC AWS (Part 1): VPC, Subnet, Internet Gateway, Route Table, NAT, and Security Group
  2. Basic Concept of ElasticSearch (Part 3): Translog, Flush, and Refresh – Widhian's Blog on Basic Concept of ElasticSearch (Part 1): Introduction
  3. Basic Concept of ElasticSearch (Part 2): Architectural Perspective – Widhian's Blog on Basic Concept of ElasticSearch (Part 3): Translog, Flush, and Refresh
  4. Basic Concept of ElasticSearch (Part 3): Translog, Flush, and Refresh – Widhian's Blog on Basic Concept of ElasticSearch (Part 2): Architectural Perspective
  5. Basic Concept of ElasticSearch (Part 1): Introduction – Widhian's Blog on Basic Concept of ElasticSearch (Part 2): Architectural Perspective

Archives

  • October 2025
  • September 2025
  • August 2025
  • November 2021
  • October 2021
  • August 2021
  • July 2021
  • June 2021
  • March 2021
  • January 2021

Categories

  • Debezium
  • Devops
  • ElasticSearch
  • Golang
  • Kafka
  • Lua
  • NATS
  • Programming
  • RabbitMQ
  • Redis
  • VPC
© 2025 Widhian Bramantya | Powered by Minimalist Blog WordPress Theme