gRPC API Reference
Primatomic exposes its distributed coordination primitives through multiple specialized gRPC services. Each service provides focused functionality for key-value storage, locks, memory regions, and cluster management with strong consistency guarantees.
Service Overview
Section titled “Service Overview”KeyValueService - Atomic Key-Value Operations
Section titled “KeyValueService - Atomic Key-Value Operations”service KeyValueService { rpc CreateNamespace(CreateNamespaceRequest) returns (CreateNamespaceResponse); rpc GetKey(GetKeyRequest) returns (GetKeyResponse); rpc PutKey(PutKeyRequest) returns (PutKeyResponse); rpc DeleteKey(DeleteKeyRequest) returns (DeleteKeyResponse); rpc ListKeys(ListKeysRequest) returns (ListKeysResponse); rpc CompareAndSwap(CompareAndSwapRequest) returns (CompareAndSwapResponse);}
LockService - Distributed Synchronization
Section titled “LockService - Distributed Synchronization”service LockService { // Exclusive Locks rpc CreateLock(CreateLockRequest) returns (CreateLockResponse); rpc DescribeLock(DescribeLockRequest) returns (DescribeLockResponse); rpc AcquireLock(AcquireLockRequest) returns (AcquireLockResponse); rpc ReleaseLock(ReleaseLockRequest) returns (ReleaseLockResponse); rpc ForceReleaseLock(ForceReleaseLockRequest) returns (ForceReleaseLockResponse);
// Read-Write Locks rpc CreateRWLock(CreateRWLockRequest) returns (CreateRWLockResponse); rpc DescribeRWLock(DescribeRWLockRequest) returns (DescribeRWLockResponse); rpc AcquireRWLock(AcquireRWLockRequest) returns (AcquireRWLockResponse); rpc ReleaseRWLock(ReleaseRWLockRequest) returns (ReleaseRWLockResponse);}
MemoryService - High-Performance Memory Regions
Section titled “MemoryService - High-Performance Memory Regions”service MemoryService { rpc AllocateMemory(AllocateMemoryRequest) returns (AllocateMemoryResponse); rpc DescribeMemory(DescribeMemoryRequest) returns (DescribeMemoryResponse); rpc ResizeMemory(ResizeMemoryRequest) returns (ResizeMemoryResponse); rpc FreeMemory(FreeMemoryRequest) returns (FreeMemoryResponse); rpc WriteMemory(WriteMemoryRequest) returns (WriteMemoryResponse); rpc ReadMemory(ReadMemoryRequest) returns (ReadMemoryResponse); rpc ListMemoryRegions(ListMemoryRegionsRequest) returns (ListMemoryRegionsResponse);}
Protocol Characteristics
Section titled “Protocol Characteristics”Request Routing
Section titled “Request Routing”- Leader Forwarding: All write operations are automatically forwarded to the cluster leader
- Linearizable Consistency: Operations observe a consistent global ordering through Raft consensus
- Automatic Failover: Clients automatically discover and connect to new leaders during failover
Authentication & Authorization
Section titled “Authentication & Authorization”- Credential Authentication: All operations require valid credential ID authentication via
authorization
metadata header - Namespace Isolation: Credential IDs can only access resources within authorized namespaces
- API Versioning: Clients must specify
api-version
header for compatibility
Error Handling
Section titled “Error Handling”- Standard gRPC Codes: Operations return standard gRPC status codes (OK, NOT_FOUND, ALREADY_EXISTS, etc.)
- Detailed Messages: Error responses include descriptive messages for debugging
- Retry Semantics: Idempotent operations can be safely retried with the same request ID
Authentication
Section titled “Authentication”JWT Token Format
Section titled “JWT Token Format”{ "sub": "user-id", "iat": 1640995200, "exp": 1641081600, "namespaces": ["myapp", "shared"], "access": ["read", "write", "admin"]}
Header Requirements
Section titled “Header Requirements”authorization: Bearer <jwt-token>api-version: v1request-id: <unique-uuid>
Python SDK Usage
Section titled “Python SDK Usage”Basic Client Setup
Section titled “Basic Client Setup”import primatomic
# Connect with automatic leader discoveryclient = primatomic.Client("localhost:50051", token="jwt-token-here")
# Alternative: explicit leader endpointleader_client = primatomic.Client.from_leader("localhost:50051", token="jwt-token-here")
Key-Value Operations
Section titled “Key-Value Operations”# Basic CRUD operationsawait client.put_key("config", b'{"debug": true}', namespace="myapp")value = await client.get_key("config", namespace="myapp")await client.delete_key("config", namespace="myapp")
# Atomic compare-and-swapsuccess = await client.compare_and_swap( key="counter", expected_value=b"5", new_value=b"6", namespace="myapp")
Distributed Locking
Section titled “Distributed Locking”# Exclusive locks with automatic cleanupasync with client.lock("resource", namespace="myapp", ttl=30): # Critical section - only one client can execute this await perform_exclusive_operation()
# Read/write locks for concurrent readersasync with client.read_lock("data", namespace="myapp", ttl=60): # Multiple readers can access simultaneously data = await read_shared_data()
async with client.write_lock("data", namespace="myapp", ttl=60): # Exclusive write access await update_shared_data()
Memory Regions
Section titled “Memory Regions”# Allocate high-performance shared memoryregion_id = await client.allocate_memory(1024*1024, namespace="myapp")
# Write data efficientlyawait client.write_memory(region_id, offset=0, data=b"shared data")
# Read data with zero-copy optimizationdata = await client.read_memory(region_id, offset=0, length=100)
# Clean up resourcesawait client.free_memory(region_id, namespace="myapp")
Watch Operations
Section titled “Watch Operations”# Watch for key changes in real-timeasync for event in client.watch_keys(prefix="config/", namespace="myapp"): print(f"Key {event.key} changed: {event.value}")
# Watch lock acquisitions and releasesasync for event in client.watch_locks(namespace="myapp"): print(f"Lock {event.key} {event.action} by {event.owner}")
Error Handling
Section titled “Error Handling”Common Status Codes
Section titled “Common Status Codes”- UNAUTHENTICATED: Invalid or expired credential ID
- PERMISSION_DENIED: Insufficient access for namespace or operation
- NOT_FOUND: Requested key, lock, or memory region does not exist
- ALREADY_EXISTS: Resource already exists (e.g., duplicate namespace creation)
- DEADLINE_EXCEEDED: Operation timeout (configurable per request)
- UNAVAILABLE: Cluster leader unavailable or undergoing election
Retry Strategy
Section titled “Retry Strategy”import backoffimport grpc
@backoff.on_exception( backoff.expo, grpc.RpcError, max_tries=3, giveup=lambda e: e.code() in [grpc.StatusCode.PERMISSION_DENIED])async def reliable_operation(): return await client.get_key("important-data", namespace="myapp")
This gRPC API provides the foundation for building distributed applications requiring coordination, with comprehensive language support and production-ready operational features.