Redis Introduction
NoSQL Concept
Before understanding Redis, let's examine the NoSQL concept. Consider the following scenarios:
- High User Volume: During peak periods like Chinese New Year, systems face massive user traffic.
- High Concurrency: Applications like 12306 (ticket booking) and Taobao (e-commerce) experience overwhelming concurrent requests.
These scenarios reveal two common characteristics: massive user bases and high concurrency, leading to system failures. The core issue isn't the application servers but the relational databases.
Causes of System Failures
- Performance Bottleneck: Disk I/O performance limitations
- Scalability Bottleneck: Complex data relationships hinder horizontal scaling
Solution Approach
To address these issues, we need to:
- Reduce disk I/O operations
- Simplify data relationships
This leads to the concept of NoSQL - Not Only SQL databases that complement relational databases for handling massive user data and high concurrency scenarios.
NoSQL Characteristics
- Scalable and flexible
- High performance with large datasets
- Flexible data models
- High availability
Common NoSQL Databases
- Redis
- Memcache
- HBase
- MongoDB
Redis Concept
What is Redis?
Redis (REmote DIctionary Server) is an open-source, high-performance key-value database developed in C.
Redis Features
- No necessary relationships between data
- Single-threaded architecture
- High performance (110,000 reads/s, 81,000 writes/s under 50 concurrent threads)
- Multiple data type support
- Persistence support for disaster recovery
Redis Data Types
- String
- Hash
- List
- Set
- Sorted Set (ZSet)
Redis Application Scenarios
- Accelerating queries for hot data (primary use case)
- Real-time information queries (leaderboards, access statistics)
- Time-sensitive information control (verification codes, voting)
- Distributed data sharing (session separation, message queues)
Redis Installation and Setup
Installation on CentOS 7
Download Redis
wget http://download.redis.io/releases/redis-5.0.0.tar.gz
Extract Installation Package
tar –xvf redis-5.0.0.tar.gz
Compile
make
Install
make install
Redis Server Components
redis-server: Server startup commandredis-cli: Client commandredis.conf: Core configuration fileredis-check-dump: RDB file verification toolredis-check-aof: AOF file repair tool
Starting Redis Server
Parameter-based Startup
redis-server [--port port]
Example:
redis-server --port 6379
Configuration File-based Startup
redis-server config_file_name
Example:
redis-server redis.conf
Starting Redis Client
Client Startup
redis-cli [-h host] [-p port]
Example:
redis-cli –h 61.129.65.248 –p 6384
Note: Server port specification uses --port while client uses -p.
Common Options
-h 127.0.0.1: Specify Redis node IP address (default: 127.0.0.1)-p 6379: Specify Redis node port (default: 6379)-a 123321: Specify Redis access password
Common Commands
ping: Heartbeat test with server (returnspongif server is normal)
Redis Basic Operations
Data Operations
Set Key-Value Data
set key value
Example:
set name techcompany
Retrieve Data by Key
get key
Example:
get name
Help Information
Retrieve Command Help Documentation
help [command]
Example:
help set
Retrieve All Commands in a Group
help [@group-name]
Example:
help @string
Exit Client
quit
exit
Keyboard shortcut:
Ctrl+C
Redis Data Types
Redis is a typical key-value database where keys are generally strings while values can be various data types.
Basic Data Types
- String
- Hash
- List
- Set
- Sorted Set (ZSet)
Special Data Structures
- Geospatial
- HyperLogLog
- Bitmap
String Type
Characteristics
- Stores single data values
- Simplest and most commonly used data type
- Can store strings or integers (for numeric operations)
Basic Operations
# Set/modify data
set key value
# Get data
get key
# Delete data
del key
# Set data only if key doesn't exist
setnx key value
# Set/modify multiple data
mset key1 value1 key2 value2
# Get multiple data
mget key1 key2
# Get string length
strlen key
# Append value to existing string
append key value
Numeric Operations
# Increment value
incr key
incrby key increment
incrbyfloat key increment
# Decrement value
decr key
decrby key increment
Expiration Operations
# Set expiration time in seconds
setex key seconds value
# Set expiration time in milliseconds
psetex key milliseconds value
String Type Considerations
- Maximum storage size: 512MB
- All operations are atomic
- Numeric operations require data to be convertible to numbers
- Maximum numeric value: 9223372036854775807 (Long.MAX_VALUE in Java)
Hash Type
Characteristics
- Stores multiple key-value pairs within a single key
- Uses hash table structure internally
- Ideal for storing objects
Basic Operations
# Set/modify field value
hset key field value
# Get field value
hget key field
hgetall key
# Delete field
hdel key field1 [field2]
# Set field only if it doesn't exist
hsetnx key field value
# Set/modify multiple fields
hmset key field1 value1 field2 value2
# Get multiple fields
hmget key field1 field2
# Get field count
hlen key
# Check if field exists
hexists key field
Extended Operations
# Get all field names
hkeys key
# Get all field values
hvals key
# Increment numeric field
hincrby key field increment
hincrbyfloat key field increment
Hash Type Considerations
- Values can only be strings
- Maximum 2^32 - 1 key-value pairs
- Not designed for storing large numbers of objects
- Avoid using
hgetallwith many fields; usehscaninstead
List Type
Characteristics
- Stores multiple ordered values
- Uses doubly linked list internally
- Supports both stack (LIFO) and queue (FIFO) operations
Basic Operations
# Add values to left (head)
lpush key value1 [value2] ...
# Add values to right (tail)
rpush key value1 [value2] ...
# Get range of values
lrange key start stop
# Get value by index
lindex key index
# Get list length
llen key
# Get and remove value from left
lpop key
# Get and remove value from right
rpop key
Extended Operations
# Remove specified count of values
lrem key count value
# Get and remove values within timeout
blpop key1 [key2] timeout
brpop key1 [key2] timeout
brpoplpush source destination timeout
List Type Considerations
- Maximum 2^32 - 1 elements
- Supports indexing but typically used as queue or stack
- Use
-1as end index to get all elements
Set Type
Characteristics
- Stores unordered unique values
- Similar to hash structure but only stores keys
- Efficient for membership testing
Basic Operations
# Add members
sadd key member1 [member2]
# Get all members
smembers key
# Remove members
srem key member1 [member2]
# Get member count
scard key
# Check if member exists
sismember key member
# Get random members
srandmember key [count]
# Get and remove random members
spop key [count]
Set Operations
# Set intersection
sinter key1 [key2 ...]
# Set union
sunion key1 [key2 ...]
# Set difference
sdiff key1 [key2 ...]
# Store results of set operations
sinterstore destination key1 [key2 ...]
sunionstore destination key1 [key2 ...]
sdiffstore destination key1 [key2 ...]
# Move member between sets
smove source destination member
Set Type Considerations
- Does not allow duplicate values
- Use
sscaninstead ofsmembersfor large sets
Redis Persistence
Introduction to Persistence
Persistence is the mechanism of saving data from volatile memory to permanent storage to prevent data loss in case of unexpected shutdowns.
RDB (Redis Database)
Concept
RDB creates point-in-time snapshots of the dataset at specified intervals.
Configuration
# Set database filename
dbfilename dump.rdb
# Set storage directory
dir /path/to/data
# Enable/disable compression
rdbcompression yes|no
# Enable/disable checksum verification
rdbchecksum yes|no
Commands
# Save synchronously (blocks server)
save
# Save asynchronously (background process)
bgsave
# Set automatic save conditions
save seconds changes
# Example: save 900 1 (save if at least 1 key changed in 900 seconds)
RDB Advantages
- Compact binary file format
- Faster recovery than AOF
- Good for backups
RDB Disadvantages
- Possible data loss between snapshots >Fork operation can be resource-intensive
AOF (Append Only File)
Concept
AOF records write operations as they happen, allowing to reconstruct the dataset by replaying these operations.
Configuration
# Enable AOF persistence
appendonly yes|no
# Set AOF filename
appendfilename appendonly.aof
# Set write strategy
appendfsync always|everysec|no
# Set auto-rewrite conditions
auto-aof-rewrite-min-size size
auto-aof-rewrite-percentage percent
Commands
# Rewrite AOF file in background
bgrewriteaof
AOF Write Strategies
always: Every write operation is synced (safe but slow)everysec: Syncs every second (recommended balance)
>
no: Let OS decide when to sync (fast but risky)
AOF Advantages
- Better data durability >Less risk of data loss
AOF Disadvantages
-
>
- Larger file sizes than RDB >Slower recovery than RDB
RDB vs AOF
| Feature | RDB | AOF | |||
|---|---|---|---|---|---|
| File Size | Smaller (compressed) | Larger (command-based) | |||
| Write Speed | Slower | Faster | |||
| Recovery Speed | Faster | Slower | Data Safety | Possible data loss | Based on sync strategy |
| Resource Usage | Higher (fork operation) | Lower |
Jedis: Java Redis Client
Introduction to Jedis
Jedis is a popular Java client for Redis that provides a simple interface to interact with Redis servers.
Setup
Maven Dependency
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
Basic Usage
// Create connection
Jedis jedis = new Jedis("localhost", 6379);
// Set value
jedis.set("name", "techcompany");
// Get value
String name = jedis.get("name");
System.out.println(name);
// Close connection
jedis.close();
Connection Pool
Configuration File (jedis.properties)
redis.host=localhost
redis.port=6379
redis.maxTotal=50
redis.maxIdle=10
JedisPool Utility Class
public class JedisPoolUtil {
private static JedisPool jedisPool;
static {
ResourceBundle bundle = ResourceBundle.getBundle("jedis");
String host = bundle.getString("redis.host");
int port = Integer.parseInt(bundle.getString("redis.port"));
int maxTotal = Integer.parseInt(bundle.getString("redis.maxTotal"));
int maxIdle = Integer.parseInt(bundle.getString("redis.maxIdle"));
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
jedisPool = new JedisPool(config, host, port);
}
public static Jedis getResource() {
return jedisPool.getResource();
}
}
Usage with Connection Pool
// Get connection from pool
Jedis jedis = JedisPoolUtil.getResource();
// Perform operations
jedis.set("key", "value");
String value = jedis.get("key");
System.out.println(value);
// Return connection to pool
jedis.close();