Implememt a KVDB Key-Value Database effectively using Interfaces

Feb 15, 2021 4 minute read Upd. Feb 15, 2021
@
Implememt a KVDB Key-Value Database effectively using Interfaces

Simple (KVDB) Key-Value Database Implementation

For this post I will be showing you how to implement a Database interface. This will be a simple Key-Value Database.

  • The database should have at least a minimum interface for a KVDB
    • Get, Set and Delete operations
    • Close Database connection

First lets create a database.go file. We can call it a database package.

Now we need to implement a Database instance. The code below shows the minimum required calls (operations) needed for a Key-Value Database.


// DBStore is a database instance
type DBStore interface {
  
  // Get returns a value for a given key, or a error response.
  Get(key string) ([]byte, error)

  // Set will store the value in the assosiated key block, or create a key
  // block and store the value. If there is an invalid error of data it should 
  // return a bad request error.
  Set(key string, value []byte) error

  // Delete will delete the value for a given key.
  Delete(key string) error

  // Close will close the database, expect no error if the database has been
  // disconnected, otherwise an error response.
  Close() error

}

// NotFoundError indicates that no value was found for the given key
type NotFoundError struct {
	missingKey string
}

// NewNotFoundError returns a new error for the missing key
func NewNotFoundError(missingKey string) error {
	return &NotFoundError{missingKey}
}

// Error returns missing key error string
func (n *NotFoundError) Error() string {
	return fmt.Sprintf("Could not find value for key: %s", n.missingKey)
}

Now we can select our Key-Value Database. From this interface implementation we could also link an in-memory store database, however for this post we will just focus on using redis.

Assuming that you have created a go file, given the file a package name like redis and imported the go-redis repository.

We can now do the following:

import (
  ...
  // path to your database.go file
  "github.com/punitnaran/example/database"
)

// RedisDatabase wraps the redis client so we can now expose it to
// our application
type RedisDatabase struct {
  Client *redis.Client

  // If you have created a connection or are using a different version of redis 
  // you may want to use the Do command which sends a command to the 
  // server and returns the received reply. Have a look at this as an example:
  // https://pkg.go.dev/github.com/garyburd/redigo/redis#hdr-Executing_Commands
}

var (
  // ErrNil Redis no match response
  ErrNil = errors.New("no matching record found in redis database")

  // Ctx an empty context to use with the client
  Ctx    = context.TODO()
)

// NewRedisDatabase connects the client to the redis server
// returning an redis client instance
func NewRedisDatabase(address string) (*Database, error) {
  // Client connects to redis server with provided options
  client := redis.NewClient(&redis.Options{
      Addr: address,
      Password: "",
      DB: 0,
  })

  // Ping to the connection to check if it is alive
  if err := client.Ping(Ctx).Err(); err != nil {
      return nil, err
  }

  // Return the redis client instance
  return &RedisDatabase{
    Client: client,
  }, nil
}

// Get returns the value from the specifed key otherwise a NotFoundError if the
// key has not been found, or any other error encountered
func (db *RedisDatabase) Get(key string) ([]byte, error) {
  value, err := client.Get(key).Result()
  if err == redis.Nil {
    // Key not found
    return nil, database.NewNotFoundError(key)
  }
  if err != nil {
    // Encountered another error
    return nil, err
  }
  return value, nil
}

// Sut adds value to the database and returns any error encountered
func (db *RedisDatabase) Set(key string, value []byte) error {
  err = client.Set(key, value, 0).Err()
  if err != nil {
    return err
  }
  return nil
}

// Deletes the key and value pair
func (db *RedisDatabase) Delete(key string) error{
   return client.Del(key).Err()
}

// Close closes the client and any open resources
func (db *RedisDatabase) Close() error{
  return db.Close()
}

Referances

Niklas – Implementing a key-value database in Golang

Michael Okoko – How to use Redis as a database with go-redis

On This Page

About Me

I am a Senior/Lead Site Reliability Engineer (SRE) at TikTok, where I collaborate with multiple teams across many countries to maintain systems at massive global scale.

I define myself as a driven Polyglot Engineer. I don’t just use tools; I build them. Deeply involved in AI Engineering—creating and fine-tuning models rather than just consuming APIs.

My passion lies in the deep end of technology: Embedded Systems, Reverse Engineering (Hacking), and Quantum Computing. I obsess over Design Patterns and advanced system architecture, specializing in microservice patterns like Live Context, RPC, and mTLS.

I know a lot because I never stop learning. Whether it’s optimizing a high-frequency trading bot or securing an IoT network, I bring a depth of knowledge and a hacker’s mindset to every challenge.

Experience

01/2024 - Present
Platform Engineer @ Deloitte
Contracted to improve client services, innovate solutions, and manage on-call duties. Focused on cloud platform services and automation to streamline processes across various languages.
09/2022 - 01/2024
Senior Software Engineer @ Crypto Quantique
Architected Post-Quantum Cryptography solutions. Implemented mTLS on embedded devices (RPi/Arduino) using Kyber/Dilithium. Optimized Go microservices for high-throughput IoT security.
05/2021 - 09/2022
Software Engineer @ Roke
Defense and National Security projects. Utilized Java, C++, and Python for secure communications and signal processing duties. Worked in hardened environments.
08/2019 - 05/2021
Software Developer @ ASM Assembly Systems
Developed industrial automation software for SMT placement machines. Focus on C#, WPF, and real-time control systems.
06/2018 - 08/2019
Development Support @ Epos Now
Triaged and resolved production issues for cloud-based POS systems. Scripted automation tools in Python to reduce manual support load.

Professional Skills

Interactive 3D Skill Matrix

Projects

🔐
Cryptography Post-Quantum mTLS on IoT
Implemented quantum-resistant mTLS key exchange using Kyber and Dilithium algorithms. Optimized for constrained resources on RPi Zero and Arduino.
🧠
AI Engineering LLM Fine-Tuning
Fine-tuned domain-specific large language models using Llama-Factory. Optimized inference pipelines for performance and relevance.
🏎️
Go Lang Advanced Go Profiling
Deep diagnostics of production systems using pprof, heap analysis, and goroutine tracing to identify and resolve complex memory leaks and race conditions.
🛡️
Security Offensive Pen Testing
Ethical Hacking and penetration testing workflows. Automated vulnerability scanning and capability auditing for hardened infrastructure.
🤖
DevOps Polyglot Automation
Comprehensive automation framework leveraging Linux internals, Python scripting, and Go binaries to orchestrate complex system operational tasks.
☸️
Cloud Native Custom K8s Operator
Built a custom Kubernetes Controller in Go to manage stateful application lifecycles and automate Day-2 operations.
🌡️
Observability Chaos Engineering
Resilience testing framework injecting network latency and pod failures to validate system recovery SLAs in production environments.