How to Connect PHP to Redis Using PHPRedis and Predis

How to Connect PHP to Redis Using PHPRedis and Predis

Learn how to connect PHP to Redis using PHPRedis and Predis. Understand installation, configuration, and best practices for interacting with Redis in PHP applications.

Introduction

Redis is a high-performance in-memory data store used for caching, session management, and real-time data processing. Connecting PHP to Redis allows applications to store and retrieve data quickly, reducing database load and improving speed.

PHP supports Redis through two primary clients:

  • PHPRedis – A fast, C-based extension for Redis.
  • Predis – A pure PHP Redis client that does not require extensions.

This guide covers:

  • Installing and configuring Redis for PHP
  • Setting up PHPRedis and Predis
  • Performing basic Redis operations
  • Choosing between PHPRedis and Predis

1. Installing Redis on Your System

Before connecting PHP to Redis, install Redis on your local system or server.

For Ubuntu/Debian

sudo apt update
sudo apt install redis-server
sudo systemctl enable redis
sudo systemctl start redis

For macOS (Homebrew)

brew install redis
brew services start redis

For Windows

Use Redis for Windows from Microsoft's archive.

Verify Redis Installation

Run:

redis-cli ping

If Redis is running, it returns:

PONG

2. Connecting PHP to Redis Using PHPRedis

PHPRedis is a native C extension that provides high-speed interaction with Redis.

Step 1: Installing PHPRedis

To install PHPRedis, use:

sudo apt install php-redis  # For Ubuntu/Debian
brew install php-redis      # For macOS

Or install via PECL:

pecl install redis

Enable the Redis extension in php.ini:

extension=redis

Restart Apache or PHP-FPM:

sudo systemctl restart apache2   # For Apache
sudo systemctl restart php-fpm   # For PHP-FPM

Step 2: Connecting to Redis with PHPRedis

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

echo "Connection successful: " . $redis->ping();
?>

Output

Connection successful: +PONG

Step 3: Storing and Retrieving Data

$redis->set("user:1001", "John Doe");
echo $redis->get("user:1001"); // Output: John Doe
  • set("key", "value") stores data in Redis.
  • get("key") retrieves the stored value.

Step 4: Closing the Redis Connection

$redis->close();

3. Connecting PHP to Redis Using Predis

Predis is a pure PHP Redis client that does not require extensions.

Step 1: Installing Predis via Composer

composer require predis/predis

Step 2: Connecting to Redis with Predis

require 'vendor/autoload.php';

$redis = new Predis\Client([
    'scheme' => 'tcp',
    'host'   => '127.0.0.1',
    'port'   => 6379,
]);

echo "Connection successful: " . $redis->ping();

Output

Connection successful: PONG

Step 3: Storing and Retrieving Data

$redis->set("user:1002", "Alice Smith");
echo $redis->get("user:1002"); // Output: Alice Smith

Step 4: Closing the Predis Connection

$redis->disconnect();

4. Choosing Between PHPRedis and Predis

Feature PHPRedis Predis
Installation Requires PECL extension Requires Composer package
Performance Faster (C extension) Slightly slower (pure PHP)
Flexibility Limited to installed servers Works on all PHP environments
Best Use Case High-performance applications Portable and shared hosting environments

When to Use PHPRedis

  • If you need maximum performance.
  • If your server allows installing PHP extensions.

When to Use Predis

  • If you cannot install PHP extensions (e.g., shared hosting).
  • If you prefer easier setup with Composer.

5. Handling Redis Authentication

If Redis requires a password, use:

PHPRedis Authentication

$redis->auth("your_redis_password");

Predis Authentication

$redis = new Predis\Client([
    'scheme'   => 'tcp',
    'host'     => '127.0.0.1',
    'port'     => 6379,
    'password' => 'your_redis_password'
]);

6. Using Redis with Persistent Connections

To avoid reconnecting on every request:

PHPRedis Persistent Connection

$redis->pconnect('127.0.0.1', 6379);

Predis Persistent Connection

$redis = new Predis\Client([
    'persistent' => true
]);

7. Best Practices for PHP and Redis Connection Handling

Use PHPRedis for performance-sensitive applications.
Use Predis for shared hosting or environments without PECL access.
Enable persistent connections to reduce overhead.
Always close Redis connections when done ($redis->close() or $redis->disconnect()).
Use authentication (auth()) if Redis requires a password.
Catch connection errors to prevent crashes.

Handling Connection Failures

try {
    $redis = new Redis();
    $redis->connect('127.0.0.1', 6379);
} catch (Exception $e) {
    echo "Redis connection failed: " . $e->getMessage();
}

Conclusion

Connecting PHP to Redis provides fast, in-memory data storage that enhances caching, session management, and performance optimization.

This guide covered:

  • Installing Redis and setting up PHPRedis and Predis
  • Connecting PHP to Redis and performing basic operations
  • Comparing PHPRedis vs. Predis for different environments
  • Using persistent connections and authentication

By following best practices, you can optimize Redis integration for high-speed data handling in PHP applications.

Leave a Reply