-
TCP/IP and Sockets:
The server uses TCP sockets to communicate with clients over the network. -
Concurrency:
Each client connection is handled in its own thread, allowing multiple clients to interact with the server at the same time. -
Mutex and Synchronization:
A mutex ensures that only one thread can modify the database at a time, preventing data corruption. -
Data Structures:
The server uses hash tables (unordered_map) for fast key lookups and vectors for lists. -
RESP Protocol:
The server understands the Redis Serialization Protocol (RESP), which is how Redis clients and servers talk to each other. -
File Persistence:
The database can be saved to and loaded from a file, so your data isn’t lost when the server restarts. -
Signal Handling:
The server can handle signals (like Ctrl+C) to shut down gracefully and save data. -
Command Processing:
Commands from clients are parsed, processed, and formatted before sending back a response. -
Singleton Pattern:
Only one instance of the database exists in the program. -
Bitwise Operators:
Used for combining results, for example when deleting keys from multiple data structures. -
Standard Libraries:
The project uses C++ standard libraries for data structures, threading, and file I/O.
- RedisServer: Handles network connections and client threads.
- RedisDatabase: Stores all the data and provides methods for commands.
- RedisCommandHandler: Parses and processes commands from clients.
-
PING
Check if the server is alive.
Example: Before doing anything, sendPINGto make sure the server is running. -
ECHO
Test the connection or debug.
Example: SendECHO Helloand getHelloback. -
FLUSHALL
Clear all data.
Example: Use this to reset the database during development or testing.
-
SET
Store a value for a key.
Example:SET user:1 "Alice" -
GET
Retrieve the value for a key.
Example:GET user:1returns"Alice" -
KEYS
List all keys.
Example:KEYS *shows every key in the database. -
TYPE
Find out what kind of value a key holds.
Example:TYPE user:1might returnstring,list, orhash. -
DEL / UNLINK
Delete a key.
Example:DEL user:1removes the key and its value. -
EXPIRE
Set a time-to-live for a key.
Example:EXPIRE session:1 60deletes the key after 60 seconds. -
RENAME
Change a key’s name.
Example:RENAME oldkey newkey
-
LGET
Get all items in a list.
Example:LGET mylistreturns all elements. -
LLEN
Get the number of items in a list.
Example:LLEN mylistreturns3if there are three items. -
LPUSH / RPUSH
Add items to the start or end of a list.
Example:LPUSH mylist aaddsato the front;RPUSH mylist baddsbto the end. -
LPOP / RPOP
Remove and return the first or last item in a list.
Example:LPOP mylistremoves and returns the first item. -
LREM
Remove items from a list by value.
Example:LREM mylist 2 xremoves up to 2 occurrences ofx. -
LINDEX
Get an item by its position in the list.
Example:LINDEX mylist 1returns the second item. -
LSET
Change the value at a specific position in the list.
Example:LSET mylist 1 new_valuesets the second item tonew_value.
-
HSET
Set fields in a hash (like a dictionary).
Example:HSET user:1 name Alice age 30 -
HGET
Get a field’s value from a hash.
Example:HGET user:1 namereturnsAlice. -
HEXISTS
Check if a field exists in a hash.
Example:HEXISTS user:1 agereturns1if it exists. -
HDEL
Delete a field from a hash.
Example:HDEL user:1 age -
HGETALL
Get all fields and values from a hash.
Example:HGETALL user:1returns all data for that user. -
HKEYS / HVALS
Get all field names or all values from a hash.
Example:HKEYS user:1returns all field names. -
HLEN
Get the number of fields in a hash.
Example:HLEN user:1returns how many fields there are. -
HMSET
Set multiple fields at once in a hash.
Example:HMSET user:2 name Bob age 25 city Paris
./my_redis_server 6379redis-cli -p 6379| Command | Example | What You Get |
|---|---|---|
| PING | PING |
PONG |
| ECHO | ECHO Hello World |
Hello World |
| FLUSHALL | FLUSHALL |
OK |
| Command | Example | What You Get |
|---|---|---|
| SET / GET | SET mykey myvalueGET mykey |
OKmyvalue |
| KEYS | KEYS * |
List of keys |
| TYPE | TYPE mykey |
string |
| DEL | DEL mykey |
1 (if deleted) |
| EXPIRE | SET session:1 dataEXPIRE session:1 5 |
OK |
| RENAME | SET a xRENAME a bGET b |
OKx |
| Command | Example | What You Get |
|---|---|---|
| LGET | RPUSH L a b cLGET L |
a, b, c |
| LLEN | LLEN L |
3 |
| LPUSH/RPUSH | LPUSH L startRPUSH L end |
45 |
| LPOP/RPOP | LPOP LRPOP L |
startend |
| LREM | RPUSH L x y x z xLREM L 2 xLREM L 0 x |
21 |
| LINDEX | LINDEX L 1LINDEX L -1 |
yz |
| LSET | LSET L 1 new_valLINDEX L 1 |
OKnew_val |
| Command | Example | What You Get |
|---|---|---|
| HSET | HSET user:1 name Alice age 30 |
1 (per field) |
| HGET | HGET user:1 name |
Alice |
| HEXISTS | HEXISTS user:1 age |
1 or 0 |
| HDEL | HDEL user:1 age |
1 |
| HGETALL | HGETALL user:1 |
All fields/values |
| HKEYS/HVALS | HKEYS user:1HVALS user:1 |
Field names Values |
| HLEN | HLEN user:1 |
Number of fields |
| HMSET | HMSET user:2 name Bob age 25 city ParisHGETALL user:2 |
OKAll fields/values |
Tip:
After setting an expiry with EXPIRE, wait for the time to pass and then try GET to see that the key is gone.