2019-02-14 16:25:17 +00:00
---
2020-04-07 18:55:19 +00:00
layout: docs
2022-09-23 21:31:00 +00:00
page_title: Key/Value (KV) Store Overview
description: >-
Consul includes a KV store for indexed objects, configuration parameters, and metadata that you can use to dynamically configure apps. Learn about accessing and using the KV store to extend Consul's functionality through watches, sessions, and Consul Template.
2019-02-14 16:25:17 +00:00
---
2022-09-23 21:31:00 +00:00
# Key/Value (KV) Store Overview
2019-02-14 16:25:17 +00:00
2019-05-15 15:49:41 +00:00
Consul KV is a core feature of Consul and is installed with the Consul agent.
2021-07-02 16:18:46 +00:00
Once installed with the agent, it will have reasonable defaults. Consul KV allows
2019-05-15 15:49:41 +00:00
users to store indexed objects, though its main uses are storing configuration
parameters and metadata. Please note that it is a simple KV store and is not
intended to be a full featured datastore (such as DynamoDB) but has some
2020-04-06 20:27:35 +00:00
similarities to one.
2019-05-15 15:49:41 +00:00
The Consul KV datastore is located on the servers, but can be accessed by any
agent (client or server). The natively integrated [RPC
2022-01-10 23:36:16 +00:00
functionality](/docs/architecture) allows clients to forward
2022-08-26 05:49:29 +00:00
requests to servers, including key/value reads and writes. Part of Consul's
2019-05-15 15:49:41 +00:00
core design allows data to be replicated automatically across all the servers.
Having a quorum of servers will decrease the risk of data loss if an outage
occurs.
2019-02-14 16:25:17 +00:00
2022-08-26 06:05:08 +00:00
If you have not used Consul KV, complete this [Getting Started
2022-09-06 15:35:01 +00:00
tutorial](https://learn.hashicorp.com/tutorials/consul/get-started-key-value-store?utm_source=docs) on HashiCorp.
2019-10-14 15:40:35 +00:00
2019-02-14 16:25:17 +00:00
## Accessing the KV store
2019-05-15 15:49:41 +00:00
The KV store can be accessed by the [consul kv CLI
2022-03-30 21:16:26 +00:00
subcommands](/commands/kv), [HTTP API](/api-docs/kv), and Consul UI.
2019-05-15 15:49:41 +00:00
To restrict access, enable and configure
2022-09-06 15:35:01 +00:00
[ACLs](https://learn.hashicorp.com/tutorials/consul/access-control-setup-production).
2019-05-15 15:49:41 +00:00
Once the ACL system has been bootstrapped, users and services, will need a
2022-01-10 23:36:16 +00:00
valid token with KV [privileges](/docs/security/acl/acl-rules#key-value-rules) to
2020-04-06 20:27:35 +00:00
access the the data store, this includes even reads. We recommend creating a
2019-05-15 15:49:41 +00:00
token with limited privileges, for example, you could create a token with write
privileges on one key for developers to update the value related to their
application.
The datastore itself is located on the Consul servers in the [data
2022-01-11 01:30:50 +00:00
directory](/docs/agent/config/cli-flags#_data_dir). To ensure data is not lost in
2020-10-14 15:23:05 +00:00
the event of a complete outage, use the [`consul snapshot`](/commands/snapshot/restore) feature to backup the data.
2019-10-14 15:40:35 +00:00
2019-02-14 16:25:17 +00:00
## Using Consul KV
2019-05-15 15:49:41 +00:00
Objects are opaque to Consul, meaning there are no restrictions on the type of
object stored in a key/value entry. The main restriction on an object is size -
the maximum is 512 KB. Due to the maximum object size and main use cases, you
should not need extra storage; the general [sizing
2022-01-11 01:30:50 +00:00
recommendations](/docs/agent/config/config-files#kv_max_value_size)
2020-07-16 21:13:06 +00:00
are usually sufficient.
2019-05-15 15:49:41 +00:00
Keys, like objects are not restricted by type and can include any character.
2020-09-09 03:58:40 +00:00
However, we recommend using URL-safe chars - `[a-zA-Z0-9-._~]` with the
2020-04-06 20:27:35 +00:00
exception of `/`, which can be used to help organize data. Note, `/` will be
2019-05-15 15:49:41 +00:00
treated like any other character and is not fixed to the file system. Meaning,
including `/` in a key does not fix it to a directory structure. This model is
2020-04-06 20:27:35 +00:00
similar to Amazon S3 buckets. However, `/` is still useful for organizing data
2019-05-15 15:49:41 +00:00
and when recursively searching within the data store. We also recommend that
2020-04-06 20:27:35 +00:00
you avoid the use of `*`, `?`, `'`, and `%` because they can cause issues when
using the API and in shell scripts.
2019-02-14 16:25:17 +00:00
## Extending Consul KV
### Consul Template
2019-05-15 15:49:41 +00:00
If you plan to use Consul KV as part of your configuration management process
review the [Consul
2022-09-06 15:35:01 +00:00
Template](https://learn.hashicorp.com/tutorials/consul/consul-template?utm_source=docs)
2020-08-17 16:17:51 +00:00
tutorial on how to update configuration based on value updates in the KV. Consul
2019-05-15 15:49:41 +00:00
Template is based on Go Templates and allows for a series of scripted actions
2020-04-06 20:27:35 +00:00
to be initiated on value changes to a Consul key.
2019-02-14 16:25:17 +00:00
### Watches
2019-05-15 15:49:41 +00:00
Consul KV can also be extended with the use of watches.
2022-01-10 23:36:16 +00:00
[Watches](/docs/dynamic-app-config/watches) are a way to monitor data for updates. When
2019-05-15 15:49:41 +00:00
an update is detected, an external handler is invoked. To use watches with the
2022-01-10 23:36:16 +00:00
KV store the [key](/docs/dynamic-app-config/watches#key) watch type should be used.
2019-02-14 16:25:17 +00:00
### Consul Sessions
2019-05-15 15:49:41 +00:00
Consul sessions can be used to build distributed locks with Consul KV. Sessions
act as a binding layer between nodes, health checks, and key/value data. The KV
API supports an `acquire` and `release` operation. The `acquire` operation acts
like a Check-And-Set operation. On success, there is a key update and an
increment to the `LockIndex` and the session value is updated to reflect the
session holding the lock. Review the session documentation for more information
2022-01-10 23:36:16 +00:00
on the [integration](/docs/dynamic-app-config/sessions#k-v-integration).
2019-10-14 15:40:35 +00:00
2022-09-06 15:35:01 +00:00
Review the following tutorials to learn how to use Consul sessions for [application leader election](https://learn.hashicorp.com/tutorials/consul/application-leader-elections) and
to [build distributed semaphores](https://learn.hashicorp.com/consul/developer-configuration/semaphore).
2019-02-14 16:25:17 +00:00
### Vault
2019-05-15 15:49:41 +00:00
If you plan to use Consul KV as a backend for Vault, please review [this
2022-08-26 05:49:29 +00:00
tutorial](/vault/tutorials/day-one-consul/ha-with-consul?utm_source=docs).