Documentation Index
Fetch the complete documentation index at: https://mintlify.com/deuxfleurs-org/garage/llms.txt
Use this file to discover all available pages before exploring further.
What is K2V?
K2V (Key/Key/Value) is an alternative storage API designed to efficiently store many small values in buckets, in contrast to S3 which is optimized for storing large blobs. K2V stores triplets of the form(partition key, sort key, value).
Key Characteristics
Data Model
K2V stores triplets consisting of:- Partition Key (
pk): A UTF-8 string that defines which partition the triplet is stored in. Triplets in different partitions cannot be listed or deleted together in batch operations. - Sort Key (
sk): A UTF-8 string that defines the index of the triplet within its partition. Triplets are uniquely identified by their partition key + sort key combination. - Value (
v): An opaque binary blob associated with the partition key + sort key. Values are transmitted as binary when possible, but are typically represented as base64-encoded strings in JSON responses. A value can also benullto indicate a deleted triplet (tombstone).
Buckets and Access
- Triplets are stored in buckets, with each bucket maintaining a separate set of triplets
- Bucket names and access keys are the same as for the S3 API
- K2V triplets exist separately from S3 objects - they are completely independent storage systems
Causality and Conflict Resolution
K2V implements sophisticated causality tracking based on DVVS (Dotted Version Vector Sets), which enables:- Concurrent write detection: Garage can detect when multiple writes occur concurrently
- Automatic conflict preservation: When concurrent writes are detected, all conflicting values are kept until a subsequent write supersedes them
- Causality tokens: Clients receive causality tokens with read responses, which must be provided on writes to indicate causal relationships
How Causality Works
- Reading values: When you read an item, you receive a causality token along with the value(s)
- Writing values: To supersede previous values, include the causality token in the
X-Garage-Causality-Tokenheader - Concurrent writes: If you write without a causality token (or with an outdated one), your write will not overwrite existing values - it creates a concurrent value instead
- Conflict resolution: When conflicts exist, all concurrent values are returned on read operations
Indexing
K2V maintains an asynchronous index that tracks:- Number of triplets per partition key
- Total number of values (accounting for conflicts)
- Total bytes of values
- Number of triplets in conflict state
Use Cases
K2V is ideal for scenarios requiring:- Storage of many small values (metadata, configuration, state)
- Efficient key-value lookups within partitions
- Range queries and prefix-based filtering
- Real-time updates with polling capabilities
- Applications that need to handle concurrent updates gracefully
Example Use Cases
- Email system metadata: Store mailbox structures, message indices, and flags
- Application state: Store user preferences, session data, and configuration
- Distributed coordination: Store lock information, membership lists, and cluster state
- Time-series data: Use sort keys for timestamps, partition keys for entity IDs
Authentication
The K2V API uses AWSv4 signatures for authentication, identical to the S3 API:- Use the same access keys and secret keys as your S3 API
- The AWS region for signature calculation matches your S3 API configuration
- All requests must include proper AWS Signature Version 4 headers
Enabling K2V
Download K2V-Enabled Build
Download a special build with thek2v feature flag enabled from the Garage downloads page (look for builds tagged with -k2v).
Configuration
Add the following to yourgarage.toml configuration file:
Client Libraries
Rust Client
An early-stage K2V client library is available for Rust:CLI Utility
A command-line utility for testing and development:k2v-cli --help for usage instructions.
Important Considerations
Concurrent values are deduplicated. If the same value is inserted multiple times concurrently (either by user action or internal retries), duplicate concurrent values are automatically deduplicated when items are read.
API Overview
The K2V API provides several categories of operations:Single Item Operations
ReadItem- Read a single item by partition key and sort keyInsertItem- Insert or update a single itemDeleteItem- Delete a single itemPollItem- Wait for changes to a single item
Batch Operations
ReadBatch- Read multiple items across sort keysInsertBatch- Insert/update multiple items in one requestDeleteBatch- Delete multiple items matching criteriaPollRange- Wait for changes to items in a range
Index Operations
ReadIndex- List partition keys and get aggregate statistics