Skip to content

Latest commit

 

History

History
233 lines (165 loc) · 7.55 KB

File metadata and controls

233 lines (165 loc) · 7.55 KB

Concepts & Use Cases


Key Concepts

  1. TCP/IP and Sockets:
    The server uses TCP sockets to communicate with clients over the network.

  2. Concurrency:
    Each client connection is handled in its own thread, allowing multiple clients to interact with the server at the same time.

  3. Mutex and Synchronization:
    A mutex ensures that only one thread can modify the database at a time, preventing data corruption.

  4. Data Structures:
    The server uses hash tables (unordered_map) for fast key lookups and vectors for lists.

  5. RESP Protocol:
    The server understands the Redis Serialization Protocol (RESP), which is how Redis clients and servers talk to each other.

  6. File Persistence:
    The database can be saved to and loaded from a file, so your data isn’t lost when the server restarts.

  7. Signal Handling:
    The server can handle signals (like Ctrl+C) to shut down gracefully and save data.

  8. Command Processing:
    Commands from clients are parsed, processed, and formatted before sending back a response.

  9. Singleton Pattern:
    Only one instance of the database exists in the program.

  10. Bitwise Operators:
    Used for combining results, for example when deleting keys from multiple data structures.

  11. Standard Libraries:
    The project uses C++ standard libraries for data structures, threading, and file I/O.


Main Classes

  • RedisServer: Handles network connections and client threads.
  • RedisDatabase: Stores all the data and provides methods for commands.
  • RedisCommandHandler: Parses and processes commands from clients.

Commands & Use Cases

Common Commands

  • PING
    Check if the server is alive.
    Example: Before doing anything, send PING to make sure the server is running.

  • ECHO
    Test the connection or debug.
    Example: Send ECHO Hello and get Hello back.

  • FLUSHALL
    Clear all data.
    Example: Use this to reset the database during development or testing.


Key/Value Commands

  • SET
    Store a value for a key.
    Example: SET user:1 "Alice"

  • GET
    Retrieve the value for a key.
    Example: GET user:1 returns "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:1 might return string, list, or hash.

  • DEL / UNLINK
    Delete a key.
    Example: DEL user:1 removes the key and its value.

  • EXPIRE
    Set a time-to-live for a key.
    Example: EXPIRE session:1 60 deletes the key after 60 seconds.

  • RENAME
    Change a key’s name.
    Example: RENAME oldkey newkey


List Commands

  • LGET
    Get all items in a list.
    Example: LGET mylist returns all elements.

  • LLEN
    Get the number of items in a list.
    Example: LLEN mylist returns 3 if there are three items.

  • LPUSH / RPUSH
    Add items to the start or end of a list.
    Example: LPUSH mylist a adds a to the front; RPUSH mylist b adds b to the end.

  • LPOP / RPOP
    Remove and return the first or last item in a list.
    Example: LPOP mylist removes and returns the first item.

  • LREM
    Remove items from a list by value.
    Example: LREM mylist 2 x removes up to 2 occurrences of x.

  • LINDEX
    Get an item by its position in the list.
    Example: LINDEX mylist 1 returns the second item.

  • LSET
    Change the value at a specific position in the list.
    Example: LSET mylist 1 new_value sets the second item to new_value.


Hash Commands

  • 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 name returns Alice.

  • HEXISTS
    Check if a field exists in a hash.
    Example: HEXISTS user:1 age returns 1 if 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:1 returns all data for that user.

  • HKEYS / HVALS
    Get all field names or all values from a hash.
    Example: HKEYS user:1 returns all field names.

  • HLEN
    Get the number of fields in a hash.
    Example: HLEN user:1 returns how many fields there are.

  • HMSET
    Set multiple fields at once in a hash.
    Example: HMSET user:2 name Bob age 25 city Paris


How to Test

1. Start Your Server

./my_redis_server 6379

2. Connect with redis-cli

redis-cli -p 6379

3. Try These Commands

Common

Command Example What You Get
PING PING PONG
ECHO ECHO Hello World Hello World
FLUSHALL FLUSHALL OK

Key/Value

Command Example What You Get
SET / GET SET mykey myvalue
GET mykey
OK
myvalue
KEYS KEYS * List of keys
TYPE TYPE mykey string
DEL DEL mykey 1 (if deleted)
EXPIRE SET session:1 data
EXPIRE session:1 5
OK
RENAME SET a x
RENAME a b
GET b
OK
x

Lists

Command Example What You Get
LGET RPUSH L a b c
LGET L
a, b, c
LLEN LLEN L 3
LPUSH/RPUSH LPUSH L start
RPUSH L end
4
5
LPOP/RPOP LPOP L
RPOP L
start
end
LREM RPUSH L x y x z x
LREM L 2 x
LREM L 0 x
2
1
LINDEX LINDEX L 1
LINDEX L -1
y
z
LSET LSET L 1 new_val
LINDEX L 1
OK
new_val

Hashes

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:1
HVALS user:1
Field names
Values
HLEN HLEN user:1 Number of fields
HMSET HMSET user:2 name Bob age 25 city Paris
HGETALL user:2
OK
All 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.