REDIS - Remote Dictionary Server
Open Source, advanced key-value store.
It supports various data structures such as strings, hashes, lists, sets, and sorted sets.
Redis is known for its high performance, flexibility, and ease of use, making it a popular choice for developers.
Why
- The real question is, why do we need Redis? For example, in apps like LinkedIn, Facebook, or Instagram, loading large amounts of data (such as profile pictures, names, followers, and posts) from the database can be slow, especially if the user refreshes the page frequently. Redis helps by caching the data after the first load, so subsequent requests fetch the stored data quickly, improving the user experience.
Redis
In-memory data structure store used as a database, cache, and message broker. Good for building high performance, scalable web applications.
When we say “everything is in-memory” with Redis, it means that all the data is stored in the computer’s RAM (Random Access Memory) instead of on a hard drive. This makes data access extremely fast because RAM is much quicker to read from and write to compared to a hard drive.
It is often referred to as a data structure server, since the keys can contain strings, hashes, lists, sets and sorted sets.
Redis is written in C.
Data Types
Strings: The simplest type, storing text or binary data. Think of it like a single piece of information, such as a name or a number.
Lists: Ordered collections of strings. Imagine a to-do list where you can add, remove, and access items by their position.
Sets: Unordered collections of unique strings. Useful for storing items where duplicates aren’t allowed, like a list of unique tags.
Sorted Sets: Similar to sets but with a score assigned to each item, allowing them to be sorted. Great for leaderboards where you need to rank items.
Hashes: Maps of fields and values, like a dictionary. Perfect for storing objects with multiple attributes, such as user profiles.
Bitmaps: Strings that can be treated as arrays of bits, useful for tracking binary states, like user activity.
HyperLogLogs: Probabilistic data structures for counting unique items with minimal memory usage. Handy for large-scale analytics.
Streams: Log-like data structures for storing sequences of messages, useful for real-time data processing.
Redis Replication - Redis can create copies of its data on multiple other Redis servers, called “slaves.” This process is known as replication. The main server, called the “master,” sends updates to all its slaves. This way, if the master server goes down, one of the slaves can take over, ensuring that your data is always available and safe.
Pub/Sub - It is like a messaging system, where the senders(publishers) sends the messaged while the receivers(subscribers) receive them through channel. A client can subscribe any number of channels.
- Redis can be secured so that any client making a connection needs to authenticate before executing commands. To secure Redis, you need to set a password in the configuration file. After setting the password, if a client tries to run a command without authentication, they will receive an (error) NOAUTH Authentication required error. Therefore, the client must use the AUTH command to authenticate.
Redis Persistence
Persistence refers to the ability to save data to disk so that it can be recovered after a restart or crash. This ensures that your data is not lost and can be restored to its previous state.
2 main methods for persistence: RDB and AOF.
1. RDB (Redis Database Backup) - Creates a snapshot of the entire dataset at configured intervals. Saves the snapshot to disk as a binary file. Efficient and quick to load. Minimal impact on performance during snapshot creation.
Example - An e-commerce website uses RDB to take snapshots of user sessions and shopping carts every 15 minutes for quick recovery.
Drawbacks - Risk of data loss between snapshots. Not suitable for applications requiring high durability.
2. AOF (Append Only File) - Logs every write operation received by the server. Periodically rewrites the AOF file to keep it compact. Higher durability with less risk of data loss.
Example - A financial transaction system uses AOF to log every transaction immediately, ensuring high durability and minimal data loss.
Drawbacks - Larger file size compared to RDB. Slower performance due to logging every operation.
- Redis allows you to use both RDB and AOF together to combine the benefits of both methods, providing a balance between performance and data safety.
Redis Sharding
Redis Sharding is a way to split your data across multiple Redis servers to handle more data and traffic efficiently.
Imagine you have a library with thousands of books. If all the books are stored in one room, it can get crowded and slow to find a book. To solve this, you can split the books into different rooms based on their genres. Now, each room has fewer books, making it faster to find what you need.
In Redis, sharding works similarly. Instead of storing all data on one server, you split it across multiple servers (shards). Each shard handles a portion of the data, making the system faster and more scalable.
Disadvantages
- Memory Usage: Redis stores all data in RAM, which can be costly for large datasets.
- Single-Threaded: Redis runs on a single thread, which can limit performance for CPU-intensive tasks.
- Basic Security: Redis has limited built-in security features, which might not be enough for sensitive data.
- No Complex Queries: Redis doesn’t support complex queries like SQL databases do.
- Risk of Data Loss: Since Redis is primarily in-memory, there’s a risk of data loss if the server crashes before data is saved to disk.
Top comments (3)
Redis supported for load balancing also, reference: dev.to/hoainhoblogdev/6-ways-handl...
thank you for sharing.
thank you amazing overview definitely informative!