or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

examples

edge-cases.mdreal-world-scenarios.md
index.md
tile.json

quick-start.mddocs/guides/

Quick Start Guide

Get started with FoundationDB in your preferred language.

Installation

Python

pip install foundationdb

Go

go get github.com/apple/foundationdb/bindings/go

Java

Add to pom.xml:

<dependency>
    <groupId>com.apple</groupId>
    <artifactId>foundationdb</artifactId>
    <version>7.4.5</version>
</dependency>

Ruby

gem install fdb

C

Download client libraries from FoundationDB releases

Basic Usage

Python

import fdb

# Select API version (required first)
fdb.api_version(740)

# Open database connection
db = fdb.open()

# Use transactional decorator for automatic retry
@fdb.transactional
def set_value(tr, key, value):
    tr[key] = value
    result = tr[key]  # Read within same transaction
    return result

# Execute transaction
result = set_value(db, b'hello', b'world')
print(result)  # b'world'

# Manual transaction with range read
tr = db.create_transaction()
try:
    # Write operations
    tr[b'key1'] = b'value1'
    tr[b'key2'] = b'value2'

    # Range read
    for key, value in tr[b'key1':b'key3']:
        print(f"{key}: {value}")

    # Commit
    tr.commit().wait()
except fdb.FDBError as e:
    print(f"Error: {e.description}")

Go

package main

import (
    "fmt"
    "github.com/apple/foundationdb/bindings/go/src/fdb"
)

func main() {
    // Select API version (required first)
    fdb.MustAPIVersion(740)

    // Open database
    db := fdb.MustOpenDefault()

    // Use Transact for automatic retry
    result, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) {
        tr.Set(fdb.Key("hello"), []byte("world"))
        return tr.Get(fdb.Key("hello")).MustGet(), nil
    })

    if err != nil {
        panic(err)
    }

    fmt.Println(string(result.([]byte)))  // "world"
}

Java

import com.apple.foundationdb.*;

public class Example {
    public static void main(String[] args) {
        // Select API version (required first)
        FDB fdb = FDB.selectAPIVersion(740);

        // Open database
        try (Database db = fdb.open()) {
            // Use run() for automatic retry
            String result = db.run(tr -> {
                tr.set("hello".getBytes(), "world".getBytes());
                return new String(tr.get("hello".getBytes()).join());
            });

            System.out.println(result);  // "world"
        }
    }
}

Key Concepts

1. API Version Selection

Required first step - Must select API version before any other operation:

  • Python: fdb.api_version(740)
  • Go: fdb.MustAPIVersion(740)
  • Java: FDB.selectAPIVersion(740)

2. Transactions

All operations must occur within transactions:

  • Use transactional wrappers for automatic retry
  • Keep transactions short (< 5 seconds)
  • Avoid side effects (functions may retry)

3. Future-Based Operations

Read operations return Future objects:

  • Python: Use .wait() to block
  • Go: Use .MustGet() or .Get()
  • Java: Use .join() or .get()

4. Error Handling

FoundationDB uses error codes:

  • Retryable errors: Handled automatically by wrappers
  • Non-retryable errors: Must be handled explicitly
  • Common codes: 1007 (timeout), 1020 (conflict), 1021 (unknown result)

Next Steps

  • See Real-World Scenarios for comprehensive examples
  • Review Edge Cases for advanced scenarios
  • Explore API Reference for detailed documentation