Database / REDIS
Redis is an open source in-memory data structure store which can be used as a database and/or a cache and message broker.
NoSQL Key/Value Store.
Supports Multiple data structures.
Built in Replication.
Redis supports,
- Strings,
- Lists,
- Sets,
- Sorted Sets,
- Hashes,
- Bitmaps,
- Hyperlogs,
- and Geospatial indexes.
- Very Flexible.
- No Schema and column names.
- Very fast, can perform around 110K Set per second and 81K GETS per second.
- Rich Datatype support,
- command level Atomic Operation,
- Caching & Disk Persistence.
REDIS supports most of the programming languages including Java, C#, Python, Scala, C++, R, PHP and many more.
Redis supports simple master to slave replication. When a relationship is established, data from the master is replicated to the slave.
Redis is designed to be accessed by trusted clients.
REDIS can be restricted to certain interfaces.
Data encryption not supported and hence do not allow external access/internet exposure.
Redis stands for REmote DIctionary Server.
Redis is developed using ANSI C and mostly used for cache solution and session management. It creates unique keys for store values.
SET command creates one key-value pair while using MSET command, multiple key-value pairs can be created.
LPUSH inserts all the specified values at the head of the list stored at key. If the key does not exist, it is created as an empty list before performing the push operations. When key holds a value that is not a list, an error is returned.
Usage: LPUSH key value [value ...]
redis> LPUSH mylist "World" (integer) 1 redis> LPUSH mylist "Hello" (integer) 2 redis> LRANGE mylist 0 -1 1) "Hello" 2) "World" redis>
REDIS is single threaded.
It has got limited client support for consistent hashing.
It has significant overhead for persistence.
It cannot be deployed widely.
No. Redis compromises with durability to enhance the speed. In Redis, in the case of system failure or crash, it writes to disk but may fall behind and lose the data which is not stored.
| Memcached. | REDIS. |
| Memcached is multi-threaded. | Redis is single threaded. |
| Memcached only does caching information. | Redis does caching information and also supports persistence and replication. |
| Memcached supports the functionality of LRU (Least Recently Used) eviction of values. | Redis does not support the functionality of LRU eviction of values. |
| In Memcached when they overflow memory, the one you have not used recently (LRU- Least Recently Used) will get deleted. | In Redis you can set a time out on everything, when memory is full, it will look at three random keys and deletes the one which is closest to expiry. |
| Memcached supports CAS(Check And Set). It is useful for maintaining cache consistency. | Redis does not support CAS ( Check And Set). |
Add the specified members to the set stored at key. Specified members that are already a member of this set are ignored. If key does not exist, a new set is created before adding the specified members.
An error is returned when the value stored at key is not a set.
Usage: SADD key member [member ...]
Example: redis> SADD myset "Hello" (integer) 1 redis> SADD myset "World" (integer) 1 redis> SADD myset "World" (integer) 0 redis> SMEMBERS myset 1) "World" 2) "Hello"
LPUSH adds an element to the beginning of a list.
RPUSH add an element to the end of a list.
LPOP removes the first element from a list and returns it.
RPOP removes the last element from a list and returns it.
LLEN gets the length of a list.
LRANGE gets a range of elements from a list.
Spring Boot primarily supports two main Redis drivers (clients): Lettuce (which is the default) and Jedis. It abstracts these clients using the Spring Data Redis framework, allowing developers to switch between them easily.
Supported Redis Drivers
Lettuce
This is the default Redis client used in Spring Boot applications when you include the spring-boot-starter-data-redis dependency.
- It is built on Netty and is an advanced, thread-safe Redis client.
- Its thread-safe nature means a single connection instance can be shared across multiple threads, which can lead to more efficient resource usage and fewer physical connections to the Redis server.
- Lettuce supports both synchronous and reactive (non-blocking) APIs, making it a good fit for modern, reactive Spring applications.
Jedis
This client is also fully supported by Spring Data Redis, although it requires explicitly excluding the default Lettuce dependency and including the Jedis one instead in your pom.xml or build.gradle file.
- It provides a straightforward, synchronous API that closely mirrors the original Redis commands.
- Unlike Lettuce, Jedis is generally not thread-safe for shared instances, so it relies on connection pooling in multi-threaded environments to manage connections effectively.
Redisson is a popular, open-source Java client for Redis (and Valkey) that offers a simplified, thread-safe way for Java developers to interact with the Redis data store. It abstracts the complexities of the Redis API by providing familiar Java objects and data structures, such as Map, List, Queue, and Lock, as distributed objects.
Key Features and Benefits of Redisson
- Familiar Java Interfaces
- Redisson provides implementations of common Java collections and data structures, allowing developers to use Redis with a minimal learning curve.
- Distributed Objects and Services
- It extends Redis's capabilities with distributed implementations of services like locks, semaphores, and Remote Procedure Calls (RPC).
- Integration with Java Ecosystem
- Redisson offers seamless integration with various popular Java frameworks, including
Spring Cache,Spring Session,Hibernate Cache, andJCache API (JSR-107). - Asynchronous and Reactive APIs
- It supports synchronous, asynchronous, reactive streams, and
RxJavaAPIs, catering to different application needs and performance requirements. - High Performance Caching
- Redisson enhances caching performance with features like "near cache," which stores frequently accessed data on the Java heap for faster retrieval.
- Support for Various Redis Deployments
- It supports different Redis configurations, including standalone, master-slave, and cluster modes.
Cache penetration is a performance issue where requests for non-existent data repeatedly bypass the cache and hit the backend database, causing overload, often due to malicious attacks or data deletion. Solutions involve caching null/empty results with short TTLs, using a Bloom filter to pre-check for non-existent keys, or implementing logical checks to filter invalid IDs before database queries.
Redis uses I/O multiplexing to enable its single-threaded core to efficiently handle thousands of concurrent client connections. This technique allows the Redis server to monitor multiple sockets simultaneously and process data on those that are ready, without blocking on any single connection.
- Single-threaded core
- Redis processes all commands sequentially in a single main thread, which eliminates the overhead of thread creation, context switching, locks, and race conditions.
- Non-blocking I/O
- All client sockets are set to non-blocking mode. This means that an I/O operation (read/write) returns immediately if no data is available, rather than pausing the entire thread.
- Event Loop
- The main thread runs an event loop that uses a specific I/O multiplexing system call provided by the operating system (e.g.,
epollon Linux,kqueueon macOS/BSD,evporton Solaris). - Kernel Notification
- Instead of constantly polling all connections (which wastes CPU), the event loop blocks in a single, efficient system call (like
epoll_wait). The operating system kernel is responsible for monitoring all the file descriptors (sockets) and notifying Redis when one or more of them are ready for an I/O operation. - Event Handling
- When the system call returns, Redis's event loop processes only the "ready" sockets one by one. It reads the command, executes it from memory, and writes the response back to the client.
