Skip to content
Effect Days 2026 Get your ticket
Docs menu / KeyValueStore

KeyValueStore

The @effect/platform/KeyValueStore module provides a robust and effectful interface for managing key-value pairs. It supports asynchronous operations, ensuring data integrity and consistency, and includes built-in implementations for in-memory, file system-based, and schema-validated stores.

Basic Usage

The module exposes a single service, KeyValueStore, which acts as the gateway for interacting with the store.

Example (Accessing the KeyValueStore Service)

import { KeyValueStore } from "@effect/platform"
import { Effect } from "effect"
const program = Effect.gen(function* () {
const kv = yield* KeyValueStore.KeyValueStore
// Use `kv` to perform operations on the store
})

The KeyValueStore interface includes the following operations:

OperationDescription
getReturns the value as string of the specified key if it exists.
getUint8ArrayReturns the value as Uint8Array of the specified key if it exists.
setSets the value of the specified key.
removeRemoves the specified key.
clearRemoves all entries.
sizeReturns the number of entries.
modifyUpdates the value of the specified key if it exists.
modifyUint8ArrayUpdates the value of the specified key if it exists.
hasCheck if a key exists.
isEmptyCheck if the store is empty.
forSchemaCreate a SchemaStore for the specified schema.

Example (Basic Operations with a Key-Value Store)

import { KeyValueStore, layerMemory } from "@effect/platform/KeyValueStore"
import { Effect } from "effect"
const program = Effect.gen(function* () {
const kv = yield* KeyValueStore
// Store is initially empty
console.log(yield* kv.size)
// Set a key-value pair
yield* kv.set("key", "value")
console.log(yield* kv.size)
// Retrieve the value
const value = yield* kv.get("key")
console.log(value)
// Remove the key
yield* kv.remove("key")
console.log(yield* kv.size)
})
// Run the program using the in-memory store implementation
Effect.runPromise(program.pipe(Effect.provide(layerMemory)))
/*
Output:
0
1
{ _id: 'Option', _tag: 'Some', value: 'value' }
0
*/

Built-in Implementations

The module includes two built-in implementations of the KeyValueStore interface. Both are provided as layers that you can inject into your effectful programs.

ImplementationDescription
In-Memory StorelayerMemory provides a simple, in-memory key-value store, ideal for lightweight or testing scenarios.
File System StorelayerFileSystem offers a file-based store for persistent storage needs.

Working with Non-String Values

By default, KeyValueStore works with string and Uint8Array values. To store other types such as objects, numbers, or booleans, use the forSchema method to create a SchemaStore.

A SchemaStore uses a schema to validate and convert values. Internally, it serializes data using JSON.stringify and deserializes it with JSON.parse.

Example (Storing a Typed Object Using a Schema)

import { KeyValueStore, layerMemory } from "@effect/platform/KeyValueStore"
import { Effect, Schema } from "effect"
// Define a JSON-compatible schema
const Person = Schema.Struct({
name: Schema.String,
age: Schema.Number,
})
const program = Effect.gen(function* () {
// Create a typed store based on the schema
const kv = (yield* KeyValueStore).forSchema(Person)
// Store a typed value
const value = { name: "Alice", age: 30 }
yield* kv.set("user1", value)
console.log(yield* kv.size)
// Retrieve the value
console.log(yield* kv.get("user1"))
})
// Use the in-memory store for this example
Effect.runPromise(program.pipe(Effect.provide(layerMemory)))
/*
Output:
1
{ _id: 'Option', _tag: 'Some', value: { name: 'Alice', age: 30 } }
*/