owned this note
owned this note
Published
Linked with GitHub
# redis & cashing
## 🔧 1. What Is **Redis**?
**Redis** stands for **REmote DIctionary Server**. It is an **in-memory data store** often used as:
* A **cache**
* A **database**
* A **message broker**
---
## ✅ Key Characteristics of Redis:
* **In-memory**: Stores data in RAM, making it extremely fast.
* **Key-Value store**: Data is stored as key-value pairs (e.g., `"user:123" -> "John"`).
* **Supports data structures**: Strings, hashes, lists, sets, sorted sets, bitmaps, streams, etc.
* **Persistence options**: Can write to disk periodically (RDB) or in real-time (AOF).
* **Pub/Sub & Streams**: Used for messaging and real-time event processing.
---
## 💡 2. What Is **Caching**?
**Caching** is the practice of **storing frequently accessed data** in a **temporary storage (cache)** so that future requests for that data can be served **faster**.
### 🔁 Typical Use Case:
Instead of querying a **slow database** or API repeatedly, you **store the result** of the first query in memory:
```plaintext
1st request: Query DB -> Store in cache
2nd request: Check cache -> Return cached data (much faster)
```
---
## 🔗 3. How Redis Is Used for **Caching**
Redis is ideal for caching because of its:
* **Speed**: Memory access is much faster than disk or database.
* **TTL support**: You can set keys to automatically expire (`EXPIRE key 60`).
* **Atomic operations**: Supports atomic reads/writes, preventing race conditions.
* **Eviction policies**: Redis can auto-remove old keys when memory is full.
### 🧠 Common Redis Caching Strategies:
| Strategy | Description |
| ----------------------- | ------------------------------------------------------------------- |
| **Read-through cache** | App checks cache first → if miss → loads from DB → stores in cache. |
| **Write-through cache** | App writes to both DB and cache at the same time. |
| **Cache-aside** | App manually manages what gets stored in cache. |
| **TTL-based caching** | Cached data auto-expires after a set time. |
---
### ⚙️ 4. Real-World Example: Redis as Cache
#### Scenario: Caching User Profile Data
```python
# Pseudocode
def get_user_profile(user_id):
cache_key = f"user:{user_id}"
# Check cache first
if redis.exists(cache_key):
return redis.get(cache_key)
# If not in cache, get from DB
profile = db.query("SELECT * FROM users WHERE id = ?", user_id)
# Store in cache for next time
redis.setex(cache_key, 3600, profile) # 1 hour TTL
return profile
```
This saves your database from repeated queries and **improves response time**.
---
### ⚠️ 5. When Not to Use Redis for Caching
Redis is powerful, but not always the right choice:
* ❌ **Large datasets** that don’t fit in memory
* ❌ **Critical persistence requirements** (though Redis has persistence modes, it’s RAM-first)
* ❌ **Complex relational queries** (Redis is NoSQL, not SQL)
---
## 🧾 redis Summary
| Concept | Redis | Caching |
| ----------- | -------------------------------------------- | -------------------------------------- |
| What is it? | In-memory key-value datastore | Strategy to store frequently used data |
| Used for | DB, cache, queue, pub/sub | Speeding up data access |
| Key trait | Fast, simple, versatile | Reduces latency and DB load |
| Relation | Redis is commonly used **as** a cache engine | |
---
# 🔍 Redis Testing Checklist:
## ✅ Container Level Tests:
```bash!
# 1. Container running
docker-compose ps | grep redis
# Should show: redis ... Up ... 6379/tcp
# 2. Redis service responding
docker-compose exec redis redis-cli ping
# Should return: PONG
# 3. Redis server info
docker-compose exec redis redis-cli info server
# Should show Redis version and server info
# 4. Redis process running
docker-compose exec redis ps aux | grep redis
# Should show redis-server process
# 5. Redis configuration
docker-compose exec redis redis-cli config get "*"
# Should show Redis configuration parameters
```
## ✅ Network Level Tests:
```bash!
# 6. Test Redis from other containers
docker-compose exec wordpress redis-cli -h redis ping
# Should return: PONG
# 7. Test Redis from nginx
docker-compose exec nginx redis-cli -h redis ping
# Should return: PONG (if redis-cli installed)
# 8. Test Redis connectivity
docker-compose exec redis netstat -tlnp | grep 6379
# Should show Redis listening on port 6379
# 9. Test Redis internal network
docker-compose exec redis redis-cli -h localhost ping
# Should return: PONG
```
## ✅ Functional Tests:
```bash!
# 10. Test Redis SET/GET operations
docker-compose exec redis redis-cli set test_key "test_value"
# Should return: OK
docker-compose exec redis redis-cli get test_key
# Should return: "test_value"
# 11. Test Redis data types
docker-compose exec redis redis-cli lpush test_list "item1" "item2"
# Should return: (integer) 2
docker-compose exec redis redis-cli lrange test_list 0 -1
# Should return: 1) "item2" 2) "item1"
# 12. Test Redis hash operations
docker-compose exec redis redis-cli hset test_hash field1 "value1"
# Should return: (integer) 1
docker-compose exec redis redis-cli hget test_hash field1
# Should return: "value1"
```
## 🎯 Performance Tests:
```bash!
# 13. Redis benchmark
docker-compose exec redis redis-benchmark -q -n 1000 -c 10
# Should show performance metrics
# 14. Test Redis memory usage
docker-compose exec redis redis-cli info memory
# Should show memory usage statistics
# 15. Test Redis statistics
docker-compose exec redis redis-cli info stats
# Should show connection and command statistics
# 16. Test Redis keyspace
docker-compose exec redis redis-cli info keyspace
# Should show database and key statistics
```
## 🔐 Security Tests:
```bash!
# 17. Test Redis authentication (if configured)
docker-compose exec redis redis-cli auth your_password
# Should return: OK (if password is set)
# 18. Test Redis ACL (if configured)
docker-compose exec redis redis-cli acl list
# Should show ACL rules
# 19. Check Redis bind configuration
docker-compose exec redis redis-cli config get bind
# Should show bind configuration
# 20. Test Redis protected mode
docker-compose exec redis redis-cli config get protected-mode
# Should show protected mode status
```
## 🚀 WordPress Integration Tests:
```bash!
# 21. Test WordPress Redis cache (if configured)
docker-compose exec wordpress wp cache flush --allow-root
# Should flush WordPress cache
# 22. Test Redis with WordPress
docker-compose exec redis redis-cli keys "*wordpress*"
# Should show WordPress-related keys (if caching is enabled)
# 23. Test session storage
docker-compose exec redis redis-cli keys "*session*"
# Should show session keys (if Redis is used for sessions)
# 24. Monitor Redis commands
docker-compose exec redis redis-cli monitor
# Should show real-time Redis commands (Ctrl+C to stop)
```
## 📊 Advanced Tests:
```bash!
# 25. Test Redis pub/sub
# Terminal 1:
docker-compose exec redis redis-cli subscribe test_channel
# Terminal 2:
docker-compose exec redis redis-cli publish test_channel "Hello Redis"
# Should show message in Terminal 1
# 26. Test Redis persistence
docker-compose exec redis redis-cli bgsave
# Should return: Background saving started
# 27. Test Redis replication (if configured)
docker-compose exec redis redis-cli info replication
# Should show replication status
# 28. Test Redis cluster (if configured)
docker-compose exec redis redis-cli cluster nodes
# Should show cluster information
```
## 🔧 Debugging Tests:
```bash!
# 29. Redis logs
docker-compose logs redis
# Should show Redis startup and operation logs
# 30. Redis configuration dump
docker-compose exec redis redis-cli config get "*" > redis_config.txt
# Should create configuration file
# 31. Test Redis slowlog
docker-compose exec redis redis-cli slowlog get 10
# Should show slow queries
# 32. Test Redis client list
docker-compose exec redis redis-cli client list
# Should show connected clients
```
---
---
# adminer
## **Architecture:**
```bash!
Browser → nginx (port 443) → Adminer (port 80)
→ WordPress + MariaDB
```
## Apache & php
### 🌐 Apache - The Web Server
#### What is Apache?
- Full name: Apache HTTP Server
- Purpose: Serves web pages over HTTP/HTTPS
- Role: Listens for web requests and sends back responses
#### What Apache Does:
1. Listens on port 80 (HTTP) for incoming requests
2. Receives requests like GET /index.php
3. Finds the file in /var/www/html/
4. Processes the file (if it's PHP, hands it to PHP)
5. Sends response back to the browser
### 🐘 PHP - The Programming Language
#### What is PHP?
- Full name: PHP: Hypertext Preprocessor
- Purpose: Server-side scripting language
- Role: Executes code to generate dynamic web content
#### What PHP Does:
1. Executes PHP code in .php files
2. Connects to databases (like your MariaDB)
3. Generates HTML dynamically
4. Handles forms and user input
### 🤝 How Apache + PHP Work Together
#### The Request Flow:
```bash!
Browser → Apache → PHP → Database → PHP → Apache → Browser
```
#### Step-by-Step Example:
1. User visits: http://yilin.42.fr
2. Apache receives: Request for /index.php
3. Apache sees: This is a PHP file, not static HTML
4. Apache calls: PHP interpreter to execute the code
5. PHP runs: Adminer code (connects to database, generates interface)
6. PHP returns: Generated HTML to Apache
7. Apache sends: Final HTML to user's browser
### 🔧 In Your Adminer Container:
#### Apache's Job:
- Web server: Serves the Adminer interface
- Port 80: Listens for web requests
- File serving: Serves /var/www/html/index.php
#### PHP's Job:
- Runs Adminer: Executes the PHP code in index.php
- Database connection: Connects to your MariaDB container
- Interface generation: Creates the database management interface
#### 🎯 Why need both for adminer Dockerfile?
```bash!
# Install web server + scripting language
apache2 # Web server (serves pages)
php # PHP interpreter (runs code)
php-mysql # Connects PHP to MySQL/MariaDB
php-mysqli # Alternative MySQL connection method
php-pdo # Database abstraction layer
php-pdo-mysql # PDO driver for MySQL/MariaDB
````
#### Apache Alone:
❌ Can only serve static files (HTML, CSS, images)
❌ Cannot execute dynamic code
❌ Cannot connect to databases
#### PHP Alone:
❌ Cannot listen for web requests
❌ Cannot serve over HTTP
❌ Needs a web server to handle network requests
#### Apache + PHP Together:
✅ Apache handles web requests and HTTP protocol
✅ PHP executes dynamic code and database connections
✅ Perfect combination for web applications like Adminer
### 🔄 Real Example - Adminer:
#### When you access Adminer:
1. Apache receives your browser request
2. Apache sees you want index.php (the Adminer file)
3. Apache passes the PHP file to PHP interpreter
4. PHP executes Adminer code:
- Shows login form
- Connects to MariaDB when you login
- Displays database tables and data
5. PHP generates HTML and sends it back to Apache
6. Apache sends the final webpage to your browser
## launch adminer
### Open any browser in terminal
```bash
chromium https://yilin.42.fr/adminer/
```
### Or use curl to see HTML:
```
# Get Adminer HTML
curl -k https://yilin.42.fr/adminer/
# Should return Adminer login form HTML
```
### 🎯 Expected Browser Result:
When you visit ```https://yilin.42.fr/adminer/```, you should see:
```
┌─────────────────────────────────┐
│ Adminer │
│ Database management tool │
│ │
│ System: [MySQL ▼] │
│ Server: [mariadb ] │
│ Username: [yilin ] │
│ Password: [*********** ] │
│ Database: [wordpress ] │
│ │
│ [Login] │
└─────────────────────────────────┘
```
#### 🔐 Login Credentials:
```
System: MySQL / mariaDB
Server: mariadb
Username: yilin
Password: XXXXX
Database: wordpress
```
---
## 🌐 Complete Adminer Testing Checklist:
```bash!
# 1. Container running
docker-compose ps | grep adminer
# Should show: adminer ... Up ... 80/tcp
# 2. Apache responding
docker-compose exec adminer curl -I localhost:80
# Should return: HTTP/1.1 200 OK
# 3. PHP working
docker-compose exec adminer php -v
# Should show: PHP 7.4.33
# 4. Apache processes
docker-compose exec adminer ps aux | grep apache
# Should show apache2 processes
# 5. PHP extensions loaded
docker-compose exec adminer php -m | grep -i mysql
# Should show: mysql, mysqli, pdo_mysql
```
---
## ✅ Network Level Tests:
```bash!
# 6. nginx proxy working
curl -k https://localhost/adminer/
# Should return HTML with login form
# 7. Database connectivity (if ping installed)
docker-compose exec adminer ping -c 3 mariadb
# Should show successful pings
# 8. Test internal web server
docker-compose exec adminer curl -I http://localhost:80
# Should return: HTTP/1.1 200 OK
# 9. Test from nginx to adminer
docker-compose exec nginx curl -I http://adminer:80
# Should return: HTTP/1.1 200 OK
```
---
## ✅ Application Level Tests:
```bash!
# 10. Login form loads
curl -k https://localhost/adminer/ | grep -i login
# Should return: <title>Login - Adminer</title>
# 11. Test MySQL connectivity via PHP
docker-compose exec adminer php -r "
try {
\$pdo = new PDO('mysql:host=mariadb;dbname=wordpress', 'yilin', 'happybirthday');
echo 'SUCCESS: Database connection working!\n';
} catch (Exception \$e) {
echo 'ERROR: ' . \$e->getMessage() . '\n';
}"
# 12. Check Adminer version
curl -k https://localhost/adminer/ | grep -i version
# Should show Adminer version info
```
---
## 🌐 Browser Testing Checklist:
✅ Access and Interface:
```
1. Visit: https://localhost/adminer/
2. Accept SSL warning (self-signed certificate)
3. Should see clean, modern Adminer interface
4. Login form should be visible and functional
```
✅ Login Form Testing:
```
System: [MySQL / MariaDB ▼] (should be pre-selected)
Server: mariadb
Username: yilin
Password: happybirthday
Database: wordpress
[✓] Permanent login (optional)
```
✅ Database Connection Testing:
```
1. Click "Login" button
2. Should successfully connect to WordPress database
3. Should see database structure in left sidebar
4. Should see WordPress tables listed
```
✅ WordPress Database Exploration:
```
Tables to check:
- wp_posts (blog posts and pages)
- wp_users (user accounts)
- wp_options (WordPress settings)
- wp_comments (comments)
- wp_postmeta (post metadata)
- wp_usermeta (user metadata)
```
✅ Functionality Testing:
```
1. Browse table data (click on table names)
2. Execute SQL queries (SQL command tab)
3. View table structure (Structure tab)
4. Check table indexes (Indexes tab)
5. Test data editing (Edit button on rows)
```
---
## 🔍 Key Security Checks:
✅ Access Control:
```bash!
# 13. Should only be accessible via nginx proxy
curl -k https://localhost/adminer/ # ✅ Should work
curl http://localhost:8080/adminer/ # ❌ Should fail (no direct access)
# 14. Check SSL/TLS encryption
curl -k -I https://localhost/adminer/ | grep -i "strict-transport"
# Should show security headers
```
✅ Database Security:
```bash!
# 15. No credentials in Dockerfile
grep -i password /path/to/Dockerfile
# Should return nothing
# 16. Test wrong credentials
# Try logging in with wrong password in browser
# Should show: "Invalid credentials" or similar
```
✅ File Permissions:
```bash!
# 17. Check web files ownership
docker-compose exec adminer ls -la /var/www/html/
# Should show: www-data:www-data index.php
# 18. Check file permissions
docker-compose exec adminer stat /var/www/html/index.php
# Should show: Access: (0755/-rwxr-xr-x)
```
---
## 🎯 Advanced Testing:
✅ Performance Testing:
```bash!
# 19. Test response time
time curl -k https://localhost/adminer/
# Should be fast (under 1 second)
# 20. Test concurrent connections
for i in {1..5}; do curl -k https://localhost/adminer/ & done
# Should handle multiple requests
```
✅ SQL Query Testing:
```
-- Test queries in Adminer interface:
-- 1. Check WordPress installation
SELECT option_value FROM wp_options WHERE option_name = 'home';
-- 2. List all users
SELECT user_login, user_email FROM wp_users;
-- 3. Count posts
SELECT COUNT(*) FROM wp_posts WHERE post_type = 'post';
-- 4. Check database size
SELECT table_name,
round(((data_length + index_length) / 1024 / 1024), 2) as 'Size (MB)'
FROM information_schema.tables
WHERE table_schema = 'wordpress';
```