NoSQL Overview
NoSQL Use Cases
Traditional relational databases face limitations with modern web-scale applications:
- Single-machine MySQL systems struggle with large datasets exceeding storage capacity
- Index sizes can surpass available memory resources
- Read/write workloads overwhelm single-server capabilities
Caching solutions like Memcached emerged to reduce database load by storing frequently accessed data in memory. Modern applications require NoSQL databases to handle diverse data types including user locations, social graphs, and unstructured logs.
NoSQL Characteristics
- Horizontally scalable architecture
- High performance for read/write operations
- Schema-less data models
- Eventual consistency guarantees
Redis (Remote Dictionary Server) is an in-memory key-value store supporting persistence and multpile data structures.
Installation
# Linux installation
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
tar xzf redis-6.2.6.tar.gz
cd redis-6.2.6
make
make install
Database Operations
SELECT 3 # Switch to database 3
DBSIZE # Check key count
FLUSHDB # Clear current database
SET user:101 "data" EX 3600 # Set key with expiration
Core Data Structures
Strings
APPEND buffer "data" # Append to value
INCR counter # Atomic increment
GETRANGE text 0 4 # Substring extraction
Lists
LPUSH tasks "itemA" # Prepend to list
RPOP tasks # Remove last element
LTRIM logs 0 99 # Keep last 100 items
Sets
SADD admins "user1" # Add member
SINTER group1 group2 # Set intersection
SPOP notifications 5 # Get random members
Hashes
HSET profile name "Alex" # Set field
HINCRBY profile age 1 # Increment field
HGETALL profile # Get all fields
Sorted Sets
ZADD rankings 100 "playerA" # Add with score
ZREVRANGE rankings 0 2 # Top 3 players
ZCOUNT scores 80 100 # Count in range
Specialized Data Types
Geospatial
GEOADD cities 13.4050 52.5200 "Berlin"
GEODIST cities "Berlin" "Paris" km
GEORADIUS cities 15 50 500 km WITHDIST
HyperLogLog
PFADD visitors "192.168.1.1"
PFCOUNT visitors # Unique count
PFMERGE total v1 v2 # Combine counters
Bitmaps
SETBIT attendance 101 1 # Mark present
GETBIT attendance 101 # Check status
BITCOUNT active_users # Count active
Persistence Mechenisms
RDB (Snapshotting)
Periodic full dataset snapshots. Configure in redis.conf:
save 900 1 # After 900sec if ≥1 change
save 300 10 # After 300sec if ≥10 changes
AOF (Append-Only File)
Log-based persistence recording write operations:
appendonly yes
appendfsync everysec # Balance safety/performance
High Availability
Replication
# Slave configuration
replicaof 192.168.1.10 6379
replica-read-only yes
Sentinel Configuration
sentinel monitor primary-cluster 192.168.1.10 6379 2
sentinel down-after-milliseconds primary-cluster 5000
sentinel failover-timeout primary-cluster 180000
Cache Management Strategies
Cache Penetration Mitigation
- Bloom filters for existence checks
- Cache null results with short TTL
Cache Avalanche Prevention
- Staggered expiration times
- Hotspot data never expires
- Pre-warming cache before events
Cache Breakdown Protection
- Distributed mutex locks
- Request coalescing
- Failover mechanisms