- Checkout the full blog: Blog
This project is a simplified Redis-like server built using TypeScript. It implements core features of Redis, such as key-value storage, transactions, streams, replication, and more. The server is designed to handle commands using the RESP (REdis Serialization Protocol) and provides a robust foundation for understanding the internals of a Redis-like in-memory database.
- SET Command: Store a key-value pair in memory.
- GET Command: Retrieve the value associated with a key.
- Key Expiration: Optionally set a TTL (Time-To-Live) for keys.
- MULTI/EXEC: Group multiple commands into an atomic transaction.
- DISCARD: Cancel a transaction.
- XADD: Add entries to a stream.
- XRANGE: Retrieve entries from a stream within a specified range.
- LPUSH: Add an element to the head of a list.
- RPUSH: Add an element to the tail of a list.
- LPOP: Remove and return the first element of a list.
- RPOP: Remove and return the last element of a list.
- LLEN: Get the length of a list.
- LRANGE: Get a range of elements from a list.
- LTRIM: Trim a list to a specified range.
- LMOVE: Atomically move elements from one list to another.
- LINDEX: Get an element at a specific index in a list.
- LSET: Set the value of an element in a list at a specific index.
- LREM: Remove elements from a list.
- LINSERT: Insert an element before or after another element in a list.
- LGETALL: Retrieve all elements in a list.
- SADD: Add one or more members to a set.
- SREM: Remove one or more members from a set.
- SISMEMBER: Check if a member exists in a set.
- SINTER: Return the intersection of multiple sets.
- SCARD: Return the cardinality (size) of a set.
- ZADD: Add one or more members to a sorted set, or update the score for members that already exist.
- ZREM: Remove one or more members from a sorted set.
- ZSCORE: Get the score associated with the given member in a sorted set.
- ZRANK: Determine the index of a member in a sorted set, with scores ordered from low to high.
- ZRANGE: Return a range of members in a sorted set, by index.
- SUBSCRIBE: Subscribe to a channel to receive messages.
- UNSUBSCRIBE: Unsubscribe from a channel.
- PUBLISH: Publish a message to a channel.
- REPLCONF/PSYNC: Support for master-slave replication.
- RDB Loading: Load key-value pairs from an RDB file on server startup.
- PING: Test server responsiveness.
- ECHO: Echo back the provided message.
- INFO: Retrieve server information.
- WAIT: Block until write commands are acknowledged by replicas.
- Node.js: Ensure you have Node.js installed (v14.x or higher recommended).
- TypeScript: The project is built using TypeScript, so make sure to have TypeScript installed.
-
Clone the Repository:
git clone https://github.com/Vivek-C-Shah/Redis-CLI-TypeScript.git cd Redis-CLI-TypeScript
-
Install Dependencies:
npm install
-
Run the Server:
npm start
You can specify a different port if needed.
-
Connect using Redis CLI:
redis-cli -p 6380
-
SET Command:
SET mykey "Hello, World!"
-
GET Command:
GET mykey
-
Start a Transaction:
MULTI
-
Queue Commands:
SET key1 "Value1" SET key2 "Value2"
-
Execute the Transaction:
EXEC
-
Discard the Transaction:
DISCARD
-
Add to Stream:
XADD mystream * field1 "value1"
-
Read from Stream:
XRANGE mystream 0-0 +
-
Add Elements to a List:
LPUSH mylist "first" RPUSH mylist "second"
-
Remove Elements from a List:
LPOP mylist RPOP mylist
-
Get the Length of a List:
LLEN mylist
-
Get a Range of Elements from a List:
LRANGE mylist 0 -1
-
Trim a List to a Specified Range:
LTRIM mylist 0 1
-
Move Elements Between Lists:
LMOVE list1 list2 LEFT RIGHT
-
Get an Element by Index:
LINDEX mylist 0
-
Set the Value of an Element at a Specific Index:
LSET mylist 0 "new_value"
-
Remove Elements from a List:
LREM mylist 1 "value_to_remove"
-
Insert an Element Before or After Another Element:
LINSERT mylist BEFORE "pivot_value" "new_value"
-
Retrieve All Elements in a List:
LGETALL mylist
-
Add Elements to a Set:
SADD myset "member1" "member2"
-
Remove Elements from a Set:
SREM myset "member1"
-
Check for Membership in a Set:
SISMEMBER myset "member2"
-
Get the Intersection of Multiple Sets:
SINTER myset1 myset2
-
Get the Cardinality of a Set:
SCARD myset
-
Add Elements to a Sorted Set:
ZADD myzset 1 "member1" 2 "member2"
-
Remove Elements from a Sorted Set:
ZREM myzset "member1"
-
Get the Score of an Element in a Sorted Set:
ZSCORE myzset "member2"
-
Get the Rank of an Element in a Sorted Set:
ZRANK myzset "member2"
-
Get a Range of Elements from a Sorted Set:
ZRANGE myzset 0 -1
-
Subscribe to a Channel:
SUBSCRIBE mychannel
-
Unsubscribe from a Channel:
UNSUBSCRIBE mychannel
-
Publish a Message to a Channel:
PUBLISH mychannel "Hello, Subscribers!"
Start the server on a different port as a replica:
npm start -- --port 6380 --replicaof 127.0.0.1 6379
Place an RDB file in the specified directory, and it will be loaded upon server start:
npm start -- --dir ./data --dbfilename dump.rdb
-
src/: Contains all the TypeScript source code.
-
dist/: Contains the compiled JavaScript files.
-
test/: Tests for the various functionalities.
Contributions are welcome! Please open an issue or submit a pull request with your changes. Ensure that your code is well-tested and documented.
This project is licensed under the MIT License.
Vivek Shah
- LinkedIn: Vivek Shah
- GitHub: Vivek-C-Shah
- Twitter: @ShVivek25