redka

repository·main·Indexed 26 days ago

https://github.com/nalgeon/redka

A Redis-compatible data store that uses SQLite or PostgreSQL as its backend. Redka reimplements core Redis functionality to provide ACID transactions, SQL views for analysis, and the ability for data to exceed RAM capacity. It can be deployed as a standalone server implementing the RESP wire protocol or as an in-process Go module. It supports five core Redis data types: Strings, Lists, Sets, Hashes, and Sorted sets (zsets).

Tokens
23.7K
Snippets
113
Records
187
Agent score
87%

What's inside redka

  1. Overview of Redka

    main

    Redka is a Redis-compatible data store that reimplements core Redis functionality using a SQL backend (SQLite or PostgreSQL). It allows data to exceed RAM capacity, supports ACID transactions, and provides SQL views for analysis.

    Redka can be used in two ways:

    1. Standalone Server: A Redis-compatible server implementing the RESP wire protocol.
    2. Go Module: An in-process library for embedding Redka directly into Go applications.
  2. Select and install a database driver for Redka

    main

    Redka supports both SQLite and PostgreSQL. You must install a driver for your chosen database using go get.

    SQLite Drivers

    • github.com/mattn/go-sqlite3: CGO-based, fastest performance.
    • github.com/ncruces/go-sqlite3: Pure Go, supports WASM.
    • modernc.org/sqlite: Pure Go, libc port.

    PostgreSQL Drivers

    • github.com/lib/pq
    • github.com/jackc/pgx/v5
    go get github.com/ncruces/go-sqlite3
  3. Use an in-memory database with Redka

    main

    To create an in-memory database that does not persist to disk, use the special path file:/redka.db?vfs=memdb with redka.Open(). Note that all data will be lost once the database is closed.

    // All data is lost when the database is closed.
    redka.Open("file:/redka.db?vfs=memdb", nil)
  4. Run Redka as a standalone server via CLI

    main

    Redka is distributed as a single-file binary. You can run it using the following command structure:

    redka [-h host] [-p port] [-s unix-socket] [db-path]

    Arguments and Flags:

    • -h host: The network interface to listen on (defaults to localhost).
    • -p port: The port to listen on (defaults to 6379).
    • -s unix-socket: The path to a Unix socket. If provided, this overrides the host and port arguments.
    • db-path: The path to a SQLite database file. If omitted, Redka runs with an in-memory database (data is not persisted).
    # Use in-memory sqlite database.
    ./redka
    
    # Use file sqlite database.
    ./redka redka.db
    
    # Listen on all network interfaces.
    ./redka -h 0.0.0.0 -p 6379 redka.db
    
    # Listen on unix socket.
    ./redka -s /tmp/redka.sock redka.db
    
    # Use postgres database.
    ./redka -p 6379 "postgres://redka:redka@localhost:5432/redka?sslmode=disable"
  5. Query Redka data using SQL views

    main

    When accessing Redka data via SQL, do not query the raw tables directly. Instead, use the provided views which join the metadata from rkey with the specific data type tables.

    There is a dedicated view for every data type:

    • vkey: General key metadata
    • vstring: String data
    • vlist: List data
    • vset: Set data
    • vhash: Hash data
    • vzset: Sorted set data

    Note that etime (expiration) and mtime (modification) timestamps are stored in UTC.

  6. Install and use Redka as a standalone server

    main
    To run Redka as a standalone, Redis-compatible server, follow the standalone installation and usage guides. This is useful for replacing Redis in environments where you want to use a relational database (SQLite or PostgreSQL) as the backend while maintaining RESP compatibility.
  7. Run Redka using Docker

    main

    You can run Redka using Docker containers. The default host is 0.0.0.0, the default port is 6379, and the default database is an in-memory SQLite database.

    Use the following commands for different storage configurations:

    # In-memory sqlite database.
    docker run --rm -p 6379:6379 nalgeon/redka
    
    # Persistent sqlite database using a host directory volume.
    docker run --rm -p 6379:6379 -v /path/to/data:/data nalgeon/redka redka.db
    
    # Postgres database on host machine.
    docker run --rm -p 6379:6379 nalgeon/redka "postgres://redka:redka@host.docker.internal:5432/redka?sslmode=disable"
  8. Install and use Redka as a Go module

    main
    To embed Redka directly into your Go application as an in-process key-value store, use the Redka Go module. This is ideal for applications that already use SQLite or need a built-in store without the overhead of a separate server process.
  9. Manage transactions in Redka

    main

    Redka provides ACID-compliant transactions that support automatic rollback in case of failure. You can manage transactions using the following commands via the DB.View or DB.Update Go APIs:

    • MULTI: Starts a new transaction.
    • EXEC: Executes all commands queued within the current transaction.
    • DISCARD: Discards the current transaction and its queued commands.

    Note: Unlike Redis, Redka does not support WATCH or UNWATCH commands.