> For the complete documentation index, see [llms.txt](https://docs.kayaknet.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.kayaknet.io/architecture/persistence.md).

# Data Persistence

KayakNet persists all data locally to survive restarts and provide history.

## Data Directory

Default location: `./data/`

```
data/
├── identity.key          # Node identity (Ed25519 keypair)
├── peers.json            # Known peer addresses
├── config.json           # Runtime configuration
├── dht/
│   └── records.json      # DHT records
├── chat/
│   ├── rooms/
│   │   └── #general.json # Room messages
│   └── dms/
│       └── <node_id>.json # Direct messages
├── market/
│   ├── listings.json     # All listings
│   └── my_listings.json  # Your listings
├── escrow/
│   ├── orders.json       # Order records
│   └── wallets.json      # Crypto wallet data
├── names/
│   └── domains.json      # .kyk registrations
└── cache/
    └── ...               # Temporary cache
```

## Identity Persistence

### identity.key

```json
{
  "private_key": "<base64 encoded Ed25519 private key>",
  "public_key": "<base64 encoded public key>",
  "node_id": "<hex encoded node ID>",
  "created_at": "2026-01-15T10:00:00Z"
}
```

**Important:**

* Back this up!
* Losing it = new identity
* Never share with anyone

## Chat Persistence

### Room Messages

`data/chat/rooms/#general.json`:

```json
{
  "room": "#general",
  "messages": [
    {
      "id": "msg_abc123",
      "sender_id": "node_123...",
      "sender_name": "alice",
      "content": "Hello!",
      "timestamp": 1234567890,
      "signature": "..."
    }
  ],
  "last_sync": "2026-01-15T10:00:00Z"
}
```

### Direct Messages

`data/chat/dms/<recipient_id>.json`:

```json
{
  "peer_id": "node_456...",
  "peer_name": "bob",
  "messages": [
    {
      "id": "dm_xyz789",
      "from_me": true,
      "content": "Private message",
      "timestamp": 1234567890,
      "read": true
    }
  ]
}
```

## Marketplace Persistence

### Listings

`data/market/listings.json`:

```json
{
  "listings": [
    {
      "id": "listing_abc",
      "title": "Product",
      "price": 99.99,
      "seller_id": "node_123...",
      "created_at": "2026-01-15T10:00:00Z",
      "synced_at": "2026-01-15T10:05:00Z"
    }
  ]
}
```

## Escrow Persistence

### Orders

`data/escrow/orders.json`:

```json
{
  "orders": [
    {
      "id": "esc_123",
      "order_id": "ord_456",
      "listing_id": "listing_abc",
      "buyer_id": "node_buyer...",
      "seller_id": "node_seller...",
      "amount": 0.5,
      "currency": "XMR",
      "state": "funded",
      "payment_address": "...",
      "created_at": "2026-01-15T10:00:00Z"
    }
  ]
}
```

## Domain Persistence

### Registered Domains

`data/names/domains.json`:

```json
{
  "domains": [
    {
      "name": "mysite.kyk",
      "target": "node_id:abc123...",
      "owner": "<public_key>",
      "registered_at": "2026-01-15T10:00:00Z",
      "signature": "..."
    }
  ]
}
```

## Sync Mechanism

### On Startup

1. Load local data from files
2. Connect to network
3. Request updates from peers
4. Merge with local data
5. Save merged state

### During Runtime

* Changes saved immediately
* Background sync every 5 minutes
* Conflict resolution by timestamp

### Conflict Resolution

* Newer timestamp wins
* For listings: Seller's version wins
* For domains: First registration wins

## Backup & Recovery

### Creating Backup

```bash
# Backup entire data directory
tar -czf kayaknet-backup.tar.gz ./data/

# Or just identity
cp data/identity.key identity-backup.key
```

### Restoring Backup

```bash
# Full restore
tar -xzf kayaknet-backup.tar.gz

# Identity only
cp identity-backup.key data/identity.key
```

### What to Backup

| File                     | Priority | Notes               |
| ------------------------ | -------- | ------------------- |
| identity.key             | Critical | Cannot recover      |
| names/domains.json       | High     | Your domains        |
| escrow/orders.json       | High     | Transaction records |
| chat/                    | Medium   | Message history     |
| market/my\_listings.json | Medium   | Your listings       |
| peers.json               | Low      | Rebuilds quickly    |

## Storage Limits

### Default Limits

| Data Type              | Limit  |
| ---------------------- | ------ |
| Chat messages per room | 10,000 |
| DM conversations       | 1,000  |
| Listings cache         | 5,000  |
| DHT records            | 10,000 |

### Cleanup

Old data is automatically cleaned:

* Expired DHT records: Deleted on expiry
* Old messages: Keep last N
* Failed escrows: Archive after 30 days

## Custom Data Directory

```bash
./kayakd --data-dir /custom/path/data
```

Or environment variable:

```bash
export KAYAKNET_DATA_DIR=/custom/path/data
```

## Database Format

Currently using JSON files for simplicity.

Future plans:

* SQLite for large datasets
* LevelDB for DHT
* Encrypted storage option


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.kayaknet.io/architecture/persistence.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
