Fast, in-memory key-value cache server written in Go. RESP-compatible protocol with built-in replication and high availability.
- In-memory storage with sharded hash maps for low latency
- TTL-based expiration (default 20 days, max 20 days)
- LRU eviction with configurable memory limits
- AOF persistence for warm restarts
- Master-replica replication for high availability
- Sentinel failover for automatic master promotion
- Binary-safe values
- 29 commands including strings, hashes, lists, counters
- Pub/Sub messaging with pattern matching for real-time events
npm install -g moledbThis installs pre-compiled binaries for your platform (macOS, Linux, Windows).
git clone https://github.com/shivang-16/mole_db.git
cd mole_db
go build -o mole ./cmd/mole
go build -o mole ./cmd/mole# Install via npm
npm install -g moledb
# Start interactive mode (server + client in one)
mole
# OR
mole server
127.0.0.1:7379> SET mykey "Hello World"
OK
127.0.0.1:7379> GET mykey
"Hello World"# Start daemon server (background)
mole server -d
# OR
mole -d
# Connect to running server
mole connect
127.0.0.1:7379> SET mykey "Hello World"
OK# Try to connect to existing server, fallback to interactive mode
mole connect
# If server exists: connects as client
# If no server: starts interactive modegit clone https://github.com/shivang-16/mole_db.git
cd mole_db
go build -o mole ./cmd/mole
./molePING- Test connectionSET key value [EX seconds] [PX milliseconds]- Set keyGET key- Get keyDEL key- Delete keyEXISTS key- Check if key exists
EXPIRE key seconds- Set expirationPEXPIRE key milliseconds- Set expiration (ms)TTL key- Get remaining TTL (seconds)PTTL key- Get remaining TTL (milliseconds)
INCR key- Increment by 1DECR key- Decrement by 1INCRBY key delta- Increment by deltaDECRBY key delta- Decrement by delta
MSET key1 val1 key2 val2 ...- Set multiple keysMGET key1 key2 ...- Get multiple keys
SETNX key value- Set if not exists (returns 1 if set)SETEX key seconds value- Set with expiration
APPEND key value- Append to keySTRLEN key- Get string length
SCAN cursor [MATCH pattern] [COUNT count]- Iterate keys
HSET key field value- Set hash fieldHGET key field- Get hash fieldHGETALL key- Get all hash fieldsHDEL key field [field ...]- Delete hash fieldsHEXISTS key field- Check if field existsHLEN key- Get hash field count
LPUSH key value [value ...]- Push to list headRPUSH key value [value ...]- Push to list tailLPOP key- Pop from list headRPOP key- Pop from list tailLLEN key- Get list lengthLRANGE key start stop- Get list range
MOLE.PUB channel message- Publish message to channelMOLE.SUB channel [channel ...]- Subscribe to channelsMOLE.PSUB pattern [pattern ...]- Subscribe to patterns (supports*and?)MOLE.UNSUB [channel ...]- Unsubscribe from channelsMOLE.PUNSUB [pattern ...]- Unsubscribe from patternsMOLE.PUBSUB CHANNELS [pattern]- List active channelsMOLE.PUBSUB NUMSUB [channel ...]- Get subscriber count per channelMOLE.PUBSUB NUMPAT- Get total pattern subscriptions
┌─────────────────────────────────────┐
│ TCP Server (Port 7379) │
├─────────────────────────────────────┤
│ RESP Protocol Parser/Writer │
├─────────────────────────────────────┤
│ Command Handler │
├─────────────────────────────────────┤
│ Sharded Memory Store (256 shards) │
│ ├─ Strings │
│ ├─ Hashes │
│ └─ Lists │
├─────────────────────────────────────┤
│ Pub/Sub Manager (Zero-Copy) │
│ ├─ Channel Subscriptions │
│ └─ Pattern Matching (Trie-based) │
├─────────────────────────────────────┤
│ AOF Persistence (optional) │
├─────────────────────────────────────┤
│ Replication (Master/Replica) │
└─────────────────────────────────────┘
mole server [options]
-addr string # TCP address (default "127.0.0.1:7379")
-d # Run in daemon mode (background)
-aof # Enable AOF persistence
-aof-path string # AOF file path (default "mole.aof")
-maxmemory int # Max memory in bytes (0 = no limit)
-maxmemory-policy string # Eviction policy: noeviction|allkeys-lru
-role string # Server role: master|replica
-master-addr string # Master address (for replicas)mole connect [options]
-h string # Server host (default "127.0.0.1")
-p int # Server port (default 7379)# Start master
mole server -role master -addr 127.0.0.1:7379
# Start replica
mole server -role replica -master-addr 127.0.0.1:7379 -addr 127.0.0.1:7380# Start sentinel
./sentinel -id s1 -master 127.0.0.1:7379 \
-replicas 127.0.0.1:7380,127.0.0.1:7381 \
-peers 127.0.0.1:9001,127.0.0.1:9002- Session storage - Fast user session cache
- Rate limiting - Counter-based API throttling
- Leaderboards - Sorted rankings
- Real-time analytics - Event counting
- Cache layer - Database query caching
- Message queues - List-based queues
- Pub/Sub messaging - Real-time notifications and chat systems
mole connect
127.0.0.1:7379> SUB news sports
Subscribed to news (total: 1)
Subscribed to sports (total: 2)
Entering subscription mode. Press Ctrl+C to exit.mole connect
127.0.0.1:7379> PUB news "Breaking: Mole DB released!"
(integer) 1
127.0.0.1:7379> PUB sports "Game starts at 3pm"
(integer) 1[14:30:15] news: Breaking: Mole DB released!
[14:30:20] sports: Game starts at 3pm
# Subscribe to all user events
127.0.0.1:7379> MOLE.PSUB user:*
Subscribed to user:* (total: 1)
# Publish to specific user
127.0.0.1:7379> MOLE.PUB user:123 "logged in"
(integer) 1 # Delivered to pattern subscriber- Sharded storage - 256 shards for concurrent access
- O(1) operations - Most commands are constant time
- Low latency - In-memory operations
- Batch operations - MGET/MSET reduce round-trips by 10x
- Zero-copy pub/sub - Message delivery via pointer sharing
- Non-blocking publishes - Slow subscribers don't affect publishers
- Pattern matching - O(1) trie-based channel matching
See CONTRIBUTING.md for setup and development guide.
MIT License - See LICENSE for details.