Building a Simple Redis‑like Service with RocksDB and RestExpress (kedis)
This article demonstrates how to create a lightweight key/value store named kedis by integrating Facebook's RocksDB storage engine with the RestExpress HTTP container, providing step‑by‑step instructions, code examples for initializing the database, defining routes, and running the service via HTTP GET requests.
The author introduces two open‑source projects—RocksDB, a high‑performance C++ key/value storage engine, and RestExpress, a small yet powerful Java HTTP container—to build a simplified Redis‑style service called kedis . The goal is to learn how to use these frameworks by implementing a basic key/value system accessible through HTTP GET requests.
RocksDB
RocksDB (https://github.com/facebook/rocksdb) is a C++ library that provides a key/value interface with support for various storage hardware, including fast flash and memory. It offers backward compatibility with LevelDB APIs and includes a Java wrapper called RocksJava.
Key features include handling multi‑TB data, optimization for small‑to‑medium keys on flash or memory, and suitability for multi‑core processors. RocksDB is also used as the storage engine in MyRocks, TiKV, and other databases.
RestExpress
RestExpress (https://github.com/RestExpress/RestExpress) is a lightweight, high‑performance HTTP container built on Netty. It uses non‑blocking I/O and can delegate blocking business logic to executors, making it ideal for creating scalable RESTful services.
Implementing kedis
The main entry point creates a RestExpress server, configures it with a port, and binds a KedisCore instance that wraps RocksDB operations.
/**
* @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();
}
}The KedisCore class loads the RocksDB native library, opens the database, and provides put and get methods that translate HTTP request parameters into RocksDB operations.
/**
* @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;
}
}
}The Routes class registers two GET endpoints, /put and /get, mapping them to the corresponding methods in KedisCore.
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 the project with Maven, the resulting kedis-1.0.jar can be started: java -jar kedis-1.0.jar --port 8081 Data can be inserted and retrieved using simple curl commands:
curl http://localhost:8081/put?key=name&value=ckl
curl http://localhost:8081/get?key=nameConclusion
RocksDB provides a compact yet powerful embedded storage engine (≈10 MB Java wrapper) suitable for use as a backend store, while RestExpress offers a lightweight framework to expose HTTP services. Together they enable rapid prototyping of a Redis‑like key/value service.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect's Tech Stack
Java backend, microservices, distributed systems, containerized programming, and more.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
