Introduction to Redis for QA
Cache Testing and Data Persistence in Production
Shoeib Mahmud Shargo
What is Redis?
Definition: Redis is an open-source, in-memory (RAM) data storage that can be used as a
database, cache, and message broker.
Key Characteristics:
● In-memory data storage for extremely fast access
● Supports multiple data types (strings, lists, sets, hashes, etc.)
● Often used as a cache in production systems to reduce database load
● Includes persistence options to prevent data loss during restarts
How Redis Caching Works
In-Memory Data Storage: Redis stores data in RAM, enabling extremely fast data retrieval and
modification.
Key-Value Store: Redis operates as a key-value store, making it ideal for caching frequently accessed or
expensive-to-compute data.
Expiration and Eviction: Redis can expire keys automatically based on TTL settings, or evict keys based
on memory pressure.
Common Cache-Related Issues:
● Cache Miss: Data not found in the cache, requiring a fetch from the database.
● Cache Stale Data: Data in the cache is outdated and not properly invalidated.
● Cache Eviction: Redis evicts data when it runs out of memory, based on the configured eviction
policy.
Understanding Redis Persistence
● Default Behavior: Without persistence, Redis stores data in memory, so data is lost
when Redis restarts.
● RDB (Redis Database Backup): Redis takes periodic snapshots of the in-memory
dataset and saves them to disk. This can restore data after a restart, but some data
might be lost between snapshots.
● AOF (Append-Only File): Redis logs every write operation to an AOF file, ensuring
more durable persistence. When Redis restarts, the AOF file is replayed to restore the
data.
Redis Cluster - Distributed Redis
What is Redis Cluster?
● Redis Cluster is a distributed version of Redis that enables horizontal scaling and high
availability.
Key Features:
● Horizontal Scaling: Distributes data across multiple Redis nodes, allowing larger
datasets to be managed.
● Automatic Sharding: Data is automatically split across nodes using hash slots,
balancing the load.
● High Availability: Redis Cluster can tolerate node failures with automatic failover using
replicas.
Redis Pub/Sub - Real-Time Messaging
What is Redis Pub/Sub?
● A messaging system where publishers send messages to channels, and subscribers
listen to receive them in real-time.
Key Features:
● Decoupled Communication: Publishers and subscribers don’t need to know about
each other.
● Low-latency: In-memory, fast messaging for time-sensitive use cases.
● Order guarantee: No ordering guarantee is provided by Redis
● Message Retention: if no subscriber, message is lost. No retention.
Common Redis Challenges and QA Testing Focus
Cache Invalidation Issues: Ensure that TTL settings and invalidation mechanisms work as
expected. Stale data in the cache can lead to user-facing errors.
Data Consistency: Verify that the data in Redis matches the source database, especially
during complex read/write operations.
Failover and Recovery Testing: Test how well Redis handles failover scenarios, including
replication and persistence.
Eviction Policy Verification: Ensure that Redis’ configured eviction policy is functioning
properly under memory pressure.
Impact on Application Performance: Need to test how caching behavior affects application
load, response time, and overall performance.
Installing Redis on EC2 (Ubuntu)
● Connect to the EC2 Instance:
○ ssh -i /path/to/key.pem ubuntu@<EC2-Public-IP>
● Install Redis on Ubuntu:
○ sudo apt update
○ sudo apt install redis-server
● Configure Redis (Optional)
○ sudo vim /etc/redis/redis.conf
○ To enable persistence, configure appendonly yes
● Start Redis Server
○ sudo systemctl start redis
● Verify Installation
○ redis-cli ping
○ You should see the response PONG, confirming that Redis is running.
Common Redis Commands for Strings, Sets, and Hashes
1. Working with Strings
● Set a String Value: SET myKey "Hello, World!"
● Get a String Value: GET myKey
● Delete a Key: DEL myKey
2. Working with HashSets
● Add or Update a Field in a Hash: HSET myHash field1 "value1"
● Get All Fields and Values from a Hash: HGETALL myHash
● Delete a Field from a Hash: HDEL myHash field1
3. Working with Sets
● Set a Value in a Set: SADD fruits "apple" “banana” “cherry”
● Get All Members of a Set: SMEMBERS fruits
● Remove a member from the set: SREM mySet "apple"
Hands on Demonstration
Using:
- EC2 instance
- Docker
- Redis/redis-stack
- Demo Node Js application
Conclusion
● Redis is a powerful in-memory database often used as a cache to optimize application
performance.
● While QA engineers don't use Redis directly, understanding its behavior is crucial when
testing caching issues found in production.
● By reproducing and testing cache-related bugs, QA teams ensure that applications
function properly, even under complex caching scenarios.
Sources
● Redis official documentation: https://redis.io/docs/latest/operate/oss_and_stack/install/
● Redis Cluster: https://medium.com/@pubuduboteju95/deep-dive-into-redis-clustering-
1d71484578a9
● Redis Data Persistence: https://codedamn.com/news/backend/redis-data-persistence-aof-vs-
rdb
● Redis PubSub: https://medium.com/redis-with-raphael-de-lio/understanding-pub-sub-in-redis-
18278440c2a9