Welcome to CodeYourCraft's No SQL Tutorial! Today, we're diving into Key-Value Stores, a versatile data storage system perfect for real-world projects.
Key-Value Stores, also known as Key-Value Databases (KVDB), are simple data stores that use an associative array-like structure to store data. Each data item is associated with a unique key, allowing for fast access and retrieval.
📝 Note: Unlike Relational Databases (RDBMS), KVDB doesn't use a table-based structure or SQL for queries. This makes KVDB more lightweight, flexible, and faster.
In-Memory KVDB: Stores data in RAM, providing the fastest read-write performance. However, it loses data when the server is restarted.
On-Disk KVDB: Stores data on the disk, offering persistence. It's slower than In-Memory KVDB but more suitable for long-term data storage.
We'll be using the popular In-Memory Key-Value Store, Redis, in our examples.
Download Redis from redis.io and follow the installation instructions for your operating system.
Start the Redis server by running the following command in your terminal:
redis-server
Install the Redis client for your programming language. For example, to install the Python Redis client, run:
pip install redis
Connect to the Redis server using the client. Here's an example using Python:
import redis
r = redis.Redis(host='localhost', port=6379, db=0)Setting a Key-Value Pair: Use the set() method to set a key-value pair.
r.set('mykey', 'myvalue')Getting a Value by Key: Use the get() method to retrieve the value of a key.
value = r.get('mykey')
print(value) # Output: b'myvalue'Deleting a Key: Use the delete() method to delete a key.
r.delete('mykey')What is the key-value pair stored in the following Redis command?
That's it for today! In the next lesson, we'll dive deeper into Key-Value Stores, learning about more advanced concepts and operations. Happy coding! 🚀