Build a Simple Redis‑Like Service with RocksDB and RestExpress

This article walks through creating a lightweight Redis‑style key/value service called kedis by integrating Facebook's RocksDB storage engine and the RestExpress HTTP container, showing how to set up the server, define API routes, and perform basic put/get operations via HTTP requests.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Build a Simple Redis‑Like Service with RocksDB and RestExpress

Introduction

Today we introduce two open‑source projects and build an application that behaves like a simplified Redis service. By sending HTTP GET requests you can insert and retrieve data. The project, temporarily named kedis , will have its source uploaded to a Git repository.

RocksDB

Project address: https://github.com/facebook/rocksdb

RocksDB is a key/value storage engine (keys and values are arbitrary byte streams) implemented as a C++ library. It was developed by Facebook based on Google’s LevelDB and maintains backward compatibility with the LevelDB API.

RocksDB supports various storage hardware, initially focusing on fast flash storage. It uses a log‑structured merge‑tree, is fully written in C++, and provides a Java wrapper called RocksJava.

It can adapt to many production environments, including pure memory, flash, disk, or remote storage, and offers highly flexible configuration settings for fine‑tuning. It supports multiple compression algorithms and provides robust tools for production support and debugging.

Features

Designed for applications that need to store up to several terabytes on local or remote storage.

Optimized for storing small‑to‑medium sized keys and values on fast storage such as flash or memory.

Suitable for multi‑core processors.

RocksDB is used as the underlying engine for MyRocks (a MySQL storage engine) and TiKV in the TiDB NewSQL database, offering significant storage savings compared to InnoDB.

RestExpress

Project address: https://github.com/RestExpress/RestExpress

RestExpress is a highly efficient lightweight HTTP container for building high‑performance, scalable RESTful services in Java. It is built on the Netty framework, uses non‑blocking I/O, and leverages an Executor to handle backend logic.

Implementing kedis

Service Startup

/**
 * @author: kl @kailing.pub
 * @date: 2019/4/12
 */
public class Main {
    public static void main(String[] args) {
        Configs configs = new Configs();
        configs.fromArgs(args);
        RestExpress server = new RestExpress()
                .setName("kedis-server")
                .setBaseUrl("http://localhost:" + configs.getPort());
        KedisCore core = new KedisCore(configs.getDbPath());
        Routes.define(server, core);
        server.bind(configs.getPort());
        server.awaitShutdown();
    }
}

RocksDB Engine API

/**
 * @author: kl @kailing.pub
 * @date: 2019/4/12
 */
public class KedisCore {
    private RocksDB db;

    public KedisCore(String path) {
        RocksDB.loadLibrary();
        try {
            Options options = new Options().setCreateIfMissing(true);
            this.db = RocksDB.open(options, path);
        } catch (RocksDBException ex) {
            ex.printStackTrace();
        }
    }

    public String put(Request request, Response response) throws Exception {
        Map<String, String> map = request.getQueryStringMap();
        String key = map.get("key");
        String value = map.get("value");
        db.put(key.getBytes(), value.getBytes());
        return "ok";
    }

    public String get(Request request, Response response) throws Exception {
        Map<String, String> map = request.getQueryStringMap();
        String key = map.get("key");
        byte[] values = db.get(key.getBytes());
        if (values != null) {
            return new String(values, "utf-8");
        } else {
            return null;
        }
    }
}

Routing Configuration

/**
 * @author: kl @kailing.pub
 * @date: 2019/4/12
 */
public abstract class Routes {
    public static void define(RestExpress server, KedisCore core) {
        server.uri("/put", core).action("put", HttpMethod.GET).noSerialization();
        server.uri("/get", core).action("get", HttpMethod.GET).noSerialization();
    }
}

Running the Service

After building with mvn install, run the generated JAR: java -jar kedis-1.0.jar --port 8081 Insert data:

curl http://localhost:8081/put?key=name&value=ckl

Retrieve data:

curl http://localhost:8081/get?key=name

Conclusion

Both RocksDB and RestExpress have distinct strengths. RocksDB is a compact (≈10 MB) embedded storage engine with powerful features suitable as a MySQL storage layer, while RestExpress is a lightweight yet fully‑featured HTTP container ideal for exposing small tools as services.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentRocksDBkey-value storeRestExpress
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.