Skip to content
kv-store-grpc logo

gRPC Key-Value Store Implementation

kv-store-grpc

Guidance for building gRPC-based key-value store services in Python. This skill should be used when tasks involve creating gRPC servers, defining protocol buffer schemas, or implementing key-value storage APIs with gRPC. Covers proto file creation, code generation, server implementation, and veri...

SKILL.md

Full skill instructions

gRPC Key-Value Store Implementation

Overview

This skill provides procedural knowledge for implementing gRPC-based key-value store services in Python. It covers the complete workflow from proto file definition through server implementation and verification.

Workflow

Step 1: Environment Setup

Install the required gRPC packages with specific versions if provided:

pip install grpcio==<version> grpcio-tools==<version>

If no versions are specified, install the latest:

pip install grpcio grpcio-tools

Verification: Run pip show grpcio grpcio-tools to confirm installation.

Step 2: Define the Protocol Buffer Schema

Create a .proto file defining the service and messages.

Key elements to include:

  • syntax = "proto3"; declaration
  • package declaration (best practice for larger projects)
  • Service definition with RPC methods
  • Message definitions for requests and responses

Common proto patterns for key-value stores:

syntax = "proto3";

package kvstore;

service KVStore {
    rpc SetVal(SetRequest) returns (SetResponse);
    rpc GetVal(GetRequest) returns (GetResponse);
}

message SetRequest {
    string key = 1;
    int32 value = 2;
}

message SetResponse {
    bool success = 1;
}

message GetRequest {
    string key = 1;
}

message GetResponse {
    int32 value = 1;
}

Common pitfall: Forgetting the package declaration - while not strictly required, it prevents naming conflicts in larger projects.

Step 3: Generate Python gRPC Code

Run the protobuf compiler to generate Python code:

python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. <service_name>.proto

This generates two files:

  • <service_name>_pb2.py - Message classes
  • <service_name>_pb2_grpc.py - Service stubs and base classes

Verification: Confirm both files exist and contain the expected classes by reading them.

Important: Examine the generated *_pb2_grpc.py file to understand:

  • The servicer base class to inherit from
  • Method signatures to implement
  • How to add the servicer to the server

Step 4: Implement the Server

Create a server file that:

  1. Imports the generated modules
  2. Implements the servicer class
  3. Sets up and starts the gRPC server

Key implementation details:

import grpc
from concurrent import futures
import <service_name>_pb2
import <service_name>_pb2_grpc

class KVStoreServicer(<service_name>_pb2_grpc.<ServiceName>Servicer):
    def __init__(self):
        self.store = {}

    def SetVal(self, request, context):
        self.store[request.key] = request.value
        return <service_name>_pb2.SetResponse(success=True)

    def GetVal(self, request, context):
        value = self.store.get(request.key, 0)  # Default to 0 for missing keys
        return <service_name>_pb2.GetResponse(value=value)

def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    <service_name>_pb2_grpc.add_<ServiceName>Servicer_to_server(
        KVStoreServicer(), server
    )
    server.add_insecure_port('[::]:50051')
    server.start()
    print("Server started on port 50051", flush=True)  # flush=True is important!
    server.wait_for_termination()

if __name__ == '__main__':
    serve()

Critical pitfall - Output buffering: When running the server in the background, Python buffers stdout by default. To see output:

  • Use print(..., flush=True) for all print statements
  • Or run Python with -u flag: python -u server.py
  • Or use the logging module instead of print

Step 5: Start the Server

Run the server in the background:

python server.py &

Or with unbuffered output:

python -u server.py &

Step 6: Verify the Server

Preferred approach: Write and run a test client. This is more reliable than system tools.

Common mistake: Attempting to use system tools like netstat, ss, lsof, or ps to verify the server. These tools may not be available in all environments. Instead, test the server directly with a client.

Test client template:

import grpc
import <service_name>_pb2
import <service_name>_pb2_grpc

def test_kv_store():
    channel = grpc.insecure_channel('localhost:50051')
    stub = <service_name>_pb2_grpc.<ServiceName>Stub(channel)

    # Test SetVal
    set_response = stub.SetVal(<service_name>_pb2.SetRequest(key="test_key", value=42))
    print(f"SetVal response: success={set_response.success}")

    # Test GetVal
    get_response = stub.GetVal(<service_name>_pb2.GetRequest(key="test_key"))
    print(f"GetVal response: value={get_response.value}")

    # Test non-existent key (should return default value)
    get_missing = stub.GetVal(<service_name>_pb2.GetRequest(key="missing_key"))
    print(f"GetVal missing key: value={get_missing.value}")

if __name__ == '__main__':
    test_kv_store()

Comprehensive testing should verify:

  • Setting and retrieving values
  • Retrieving non-existent keys (verify default behavior)
  • Overwriting existing keys
  • Multiple different keys

Common Pitfalls

PitfallSolution
Print statements not appearing when server runs in backgroundUse flush=True or run with python -u
Trying to use netstat/​ss/​lsof to verify serverWrite a test client instead - more reliable
Missing package declaration in protoAdd package <name>; after syntax declaration
Not examining generated code before implementingRead *_pb2_grpc.py to understand the servicer interface
Incomplete verificationTest all operations including edge cases
No default value for missing keysUse dict.get(key, default) pattern

Edge Cases to Consider

  • Missing keys: Return a sensible default (often 0 or empty string)
  • Empty keys: Consider whether to validate or allow
  • Type boundaries: Protobuf enforces int32 bounds automatically
  • Connection failures: Client should handle gRPC exceptions
  • Server shutdown: Consider implementing graceful shutdown for production

Efficiency Tips

  1. Verification strategy: Go directly to application-level testing with a client rather than system-level inspection tools
  2. Examine generated code: Read the generated *_pb2_grpc.py file before implementing to understand method signatures
  3. Test incrementally: Verify each step works before moving to the next
  4. Keep test clients: Do not delete test clients - they serve as useful debugging tools

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View