Caching Strategies and Redis Implementation

Understanding Caching Concepts

Buffer vs. Cache

buffer: Primarily used for write operations, acting as a write buffer.
cache: Primarily used for read operations, serving as a read cache.
Both address speed inconsistencies and involve I/O operations.

Key Cache Considerations

1. Storage location (multi-level cache):
    Client-side (browser cache)
    Memory (local or remote server)
    Disk (local or remote server)
2. Time-to-live (TTL)
3. Forced invalidation
4. Cache hit rate (critical): A cache with zero hit rate is ineffective.

Browser Caching Mechanisms

Cache Negotiation

Dialogue between browser and web server for caching decisions.

Browser Cache Methods

Last-Modified: File modification timestamp
Etag: MD5 hash
Expires: Expiration time

    Nginx configuration:
        location ~ .*\.(js|css){
            expires 1h; # 1-hour expiration
        }
        (If JS errors occur before expiration, rename or add timestamp: 123.js → 123.js?202310)
Cache-Control: Solves client clock synchronization issues

Browser Refresh Behavior

Address bar Enter: Uses all valid cache mechanisms
F5 or refresh button: Expires remains, other cache mechanisms invalidated
Ctrl+F5: Force refresh, all cache mechanisms disabled

CDN Fundamentals

CDN Problem Solutions

Inter-carrier connectivity issues (BGP expensive)
Long network distances between users and servers
Limited server bandwidth
Improved response speed and success rate

CDN Core Technologies

1. Content routing - Intelligent DNS (based on localDNS IP)
2. Monitoring system - Node health and link status
3. Content distribution - Pre-caching
4. Expiration refresh - Cache invalidation
5. Data storage - File storage
6. Configuration management - Automation tools
7. Billing system
8. Data visualization - Log analysis
9. Attack prevention - WAF protection

Nginx Reverse Proxy Caching

Proxy Cache Configuration

# /etc/nginx/conf/proxy.conf
proxy_temp_path  /data/cache/temp_dir;
proxy_cache_path /data/cache/cache_dir levels=1:2 keys_zone=my_cache:50m inactive=1d max_size=1g;
proxy_connect_timeout 5;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404;

# Virtual host configuration
location ~ .*\.(gif|jpg|png|bmp)$ {
    proxy_pass http://backend;
    proxy_cache my_cache;
    proxy_cache_key "$host$request_uri";
    add_header X-Cache-Status $upstream_cache_status;
    proxy_cache_valid 200 304 301 302 8h;
    proxy_cache_valid 404 1m;
    proxy_cache_valid any 2d;
}

Cache File Management

Cache files generated using MD5 hashing of cache keys
File paths determined by hash suffix patterns (levels=1:2)
Manual deletion possible through hash calculation

Cache Purge Script

#!/bin/bash
while read -r url
do
  hash=$(echo -n $url | md5sum | awk '{print $1}')
  file_path="/data/cache/cache_dir/$(echo $hash | awk '{print substr($0,length($0),1)"/"substr($0,length($0)-2,2)"/"$0}')"
  rm -f "$file_path"
done < purge_list.txt

PHP Caching Implementation

OPcache Configuration

zend_extension=opcache.so
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.enable_cli=1

Alternative PHP Caches

PHP-YAC (Yet Another Cache)

Storage System Caching

RAID Cache Policies

WriteBack: Write caching enabled
WriteThrough: Direct disk writes
ReadAheadNone: No read-ahead
ReadAdaptive: Adaptive read-ahead
NoCachedBadBBU: Disable write cache on battery failure

Redis Implementation

Redis vs Memcached

Memcached: No persistence, simple key-value, no authentication
Redis: Persistence support, multiple data types, clustering, authentication

Redis Installation

wget http://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

# Configuration
daemonize yes
bind 192.168.1.100
requirepass securepassword
dir /var/lib/redis

Redis Data Types

Strings: set, get, incr, decr
Hashes: hset, hget, hgetall
Lists: lpush, rpush, lrange
Sets: sadd, smembers, sinter
Sorted Sets: zadd, zrange

Redis Persistence

RDB: Periodic snapshots
AOF: Append-only log of write operations

# RDB configuration
save 900 1
save 300 10
save 60 10000

# AOF configuration
appendonly yes
appendfsync everysec
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb

Redis Replication

slaveof 192.168.1.101 6379
Asynchronous replication with configurable synchronization intervals

Redis Cluster Solutions

Cluster Architectures

Client-side sharding
Proxy-based sharding
Redis Cluster (native)
Codis (proxy-based with coordination)

Codis Cluster Setup

# Zookeeper configuration
tickTime=2000
dataDir=/data/zookeeper
clientPort=2181
server.1=node1:2888:3888
server.2=node2:2888:3888

# Codis components
Dashboard: Cluster management
Proxy: Request routing
FE: Web management interface
Server: Redis instances

Cluster Management

Slot allocation across server groups
Proxy registration and health monitoring
Automatic failover and data migration
Web-based management through FE interface

Tags: Caching Redis nginx CDN opcache

Posted on Thu, 14 May 2026 03:41:58 +0000 by Axeia