Current Location: Home> Latest Articles> How to Achieve Persistent Data Storage with PHP and Redis

How to Achieve Persistent Data Storage with PHP and Redis

M66 2025-06-24

PHP and Redis: How to Achieve Persistent Data Storage

Introduction:
In web development, persistent data storage is crucial. Redis, as a high-performance key-value store, is well-suited for caching and storing data. This article explains how to use PHP together with Redis to achieve persistent data storage.

1. Installing and Configuring Redis

  1. Download and Install Redis:
    Obtain the Redis package from the official website and install it. After installation, proceed to configuration.
  2. Configure Redis:
    Edit the Redis configuration file redis.conf for basic settings.
  3. a) Open the configuration file:

    <span class="fun">vim /etc/redis/redis.conf</span>

    b) Set a Redis password:

    <span class="fun">requirepass your_password</span>

    c) Save and close the file.

  4. Start the Redis server:
  5. <span class="fun">redis-server</span>

2. Connecting to Redis Server with PHP

  1. Install Redis Extension:
    To use Redis in PHP, install the php-redis extension, usually via pecl.
  2. Connect to Redis:
  3. $redis = new Redis();
    $redis->connect('127.0.0.1', 6379);
    $redis->auth('your_password'); // Authenticate if password set
    
  4. Store Data:
  5. $redis->set('my_key', 'my_value');
    
  6. Retrieve Data:
  7. $value = $redis->get('my_key');
    echo $value; // Outputs my_value
    
  8. Store Complex Data:
  9. Serialize arrays or objects before storing:

    $data = ['name' => 'John', 'age' => 25];
    $redis->set('my_data', serialize($data));
    

    Deserialize when retrieving:

    $data = unserialize($redis->get('my_data'));
    print_r($data); // Outputs array content
    

3. Implementing Data Persistence

By default, Redis stores data in memory and will lose data on restart. To ensure data safety, Redis offers two persistence methods: RDB and AOF.

RDB Persistence (Snapshotting)

Redis creates snapshot files saving memory data to disk at intervals.

  1. Configure RDB persistence:
  2. save 900 1
    save 300 10
    save 60 10000
    

    These configurations define the intervals and data change thresholds for snapshots.

  3. Enable RDB persistence:
    Make sure these lines are uncommented.
  4. Save the file and restart Redis server.

After restart, Redis restores data from the latest snapshot but may lose data changed after the snapshot.

AOF Persistence (Append-Only File)

AOF persistence records every write command; on restart, Redis replays commands to restore data.

  1. Configure AOF persistence:
  2. appendonly yes
    appendfilename "appendonly.aof"
    
  3. Save and restart Redis.

Conclusion

This article has explained how to connect PHP with Redis and use Redis’s RDB and AOF mechanisms for data persistence. Proper configuration ensures data survives Redis server restarts, improving data reliability and application stability.

References

  • Redis Official Website: https://redis.io/
  • Redis Source Code: https://github.com/redis/redis