Skip to content

Migrating From PHPRedis

This guide helps you migrate from PHPRedis to Valkey GLIDE PHP, highlighting key differences and providing code examples for common use cases.

Valkey GLIDE PHP is a high-performance client for Valkey and Redis that provides:

  • Better performance through Rust-based core with C extension bindings
  • Connection pooling with automatic connection management
  • Type safety with comprehensive PHP type hints
  • Cluster support with automatic failover and resharding
  • Cross-platform compatibility (Linux, macOS)
  • PHPRedis compatibility through helper methods to create aliases for classes
Terminal window
# Using pie
pie install valkey-io/valkey-glide-php
# Or via Composer
composer require valkey-io/valkey-glide-php
# Or from PECL
pecl install https://github.com/valkey-io/valkey-glide-php/releases/download/v<version>/valkey_glide-<version>.tgz

After installation, enable the extension in your php.ini:

extension=valkey_glide

For seamless migration, Valkey GLIDE PHP provides a helper method that creates alias class names to match PHPRedis:

// Register PHPRedis compatibility aliases
ValkeyGlide::registerPHPRedisAliases();
// Now you can use Redis instead of ValkeyGlide
$client = new Redis();
$client->connect('127.0.0.1', 6379);
// Exceptions are also aliased
try {
$client->set('key', 'value');
} catch (RedisException $e) {
echo "Error: " . $e->getMessage() . "\n";
}

Aliases provided:

  • RedisValkeyGlide
  • RedisClusterValkeyGlideCluster
  • RedisExceptionValkeyGlideException

Requirements: PHP 8.3 or higher (required for class_alias() support with internal classes)

Valkey GLIDE PHP supports flexible connection patterns for compatibility with PHPRedis.

Two-Step Connection (construct then connect):

// With named parameters
$client = new ValkeyGlide();
$client->connect(
addresses: [['host' => '127.0.0.1', 'port' => 6379]],
credentials: ['password' => 'password'],
database_id: 1
);

One-Step Connection (constructor):

// With named parameters
$cluster = new ValkeyGlideCluster(
addresses: [
['host' => '127.0.0.1', 'port' => 7000],
['host' => '127.0.0.1', 'port' => 7001]
],
read_from: ValkeyGlide::READ_FROM_PREFER_REPLICA,
database_id: 1 // Requires Valkey 9.0+ with cluster-databases > 1
);
// Register PHPRedis compatibility aliases
ValkeyGlide::registerPHPRedisAliases();
// Standalone - two-step
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth('password');
$redis->select(1);
// Cluster - one-step
$cluster = new RedisCluster(
name: null,
seeds: [
['host' => '127.0.0.1', 'port' => 7000],
['host' => '127.0.0.1', 'port' => 7001]
]
);
$redis->set('key', 'value');
$value = $redis->get('key');
$redis->setex('key', 3600, 'value'); // TTL in seconds
$redis->mset(['key1' => 'val1', 'key2' => 'val2']);
$values = $redis->mget(['key1', 'key2']);
$redis->hset('hash', 'field', 'value');
$value = $redis->hget('hash', 'field');
$redis->hmset('hash', ['field1' => 'val1', 'field2' => 'val2']);
$values = $redis->hmget('hash', ['field1', 'field2']);
$all = $redis->hgetall('hash');
$redis->lpush('list', 'value');
$redis->rpush('list', 'value');
$value = $redis->lpop('list');
$value = $redis->rpop('list');
$length = $redis->llen('list');
$range = $redis->lrange('list', 0, -1);
$redis->sadd('set', 'member');
$redis->srem('set', 'member');
$members = $redis->smembers('set');
$exists = $redis->sismember('set', 'member');
$count = $redis->scard('set');

Standalone: Supports both one-step (constructor) and two-step (constructor + connect) patterns
Cluster: Supports one-step connection via constructor

// Standalone - Two-step (ValkeyGlide-style)
$client = new ValkeyGlide();
$client->connect(
addresses: [['host' => '127.0.0.1', 'port' => 6379]]
);
// Standalone - Two-step (PHPRedis-style)
$client = new ValkeyGlide();
$client->connect(host: '127.0.0.1', port: 6379);
// Cluster - One-step (ValkeyGlide-style)
$cluster = new ValkeyGlideCluster(
addresses: [
['host' => '127.0.0.1', 'port' => 7000],
['host' => '127.0.0.1', 'port' => 7001]
]
);
// Cluster - One-step (PHPRedis-style)
$cluster = new ValkeyGlideCluster(
name: null,
seeds: [
['host' => '127.0.0.1', 'port' => 7000],
['host' => '127.0.0.1', 'port' => 7001]
]
);
  • PHPRedis: Variadic arguments for multi-value operations
  • Valkey GLIDE: Array parameters for consistency
// PHPRedis - variadic
$redis->sadd('set', 'val1', 'val2', 'val3');
// Valkey GLIDE - array
$client->sadd('set', ['val1', 'val2', 'val3']);
// Both support variadic for some commands
$client->del('key1', 'key2', 'key3');
  • PHPRedis: Mixed return types, sometimes inconsistent
  • Valkey GLIDE: Consistent, typed return values
// PHPRedis - returns int|false
$result = $redis->set('key', 'value');
// Valkey GLIDE - returns string "OK" or throws exception
$result = $client->set('key', 'value');
  • PHPRedis: Returns false on error, throws exceptions in constructors
  • Valkey GLIDE: Same pattern - returns false on method errors, throws exceptions in constructors
// Both throw exceptions on connection errors
try {
$client = new ValkeyGlide();
$client->connect('invalid-host', 6379);
} catch (ValkeyGlideException $e) {
// Handle connection error
}
// Both return false for missing keys
if ($client->get('nonexistent') === false) {
// Key doesn't exist
}

Configuration is done through the connect() method:

$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_JSON);
$redis->setOption(Redis::OPT_PREFIX, 'myapp:');
// Subscribe to channels
$client->subscribe(['channel1', 'channel2'], function($message) {
echo "Received: {$message['message']} on {$message['channel']}\n";
});
// Publish messages
$client->publish('channel1', 'Hello World');
// Pattern subscription
$client->psubscribe(['news.*'], function($message) {
echo "Pattern match: {$message['message']}\n";
});
// Execute commands in a transaction
$client->multi();
$client->set('key1', 'value1');
$client->set('key2', 'value2');
$client->incr('counter');
$results = $client->exec();
// With WATCH for optimistic locking
$client->watch('balance');
$balance = $client->get('balance');
if ($balance >= 100) {
$client->multi();
$client->decrby('balance', 100);
$client->exec();
}
// With TLS
$client = new ValkeyGlide();
$client->connect(
addresses: [['host' => 'localhost', 'port' => 6379]],
use_tls: true,
advanced_config: ['tls_config' => ['root_certs' => $root_certs_data]]
);
// With stream context
$client = new ValkeyGlide();
$client->connect(
addresses: [['host' => 'localhost', 'port' => 6379]],
context: stream_context_create(['ssl' => ['cafile' => 'ca-cert.pem']])
);
$client = new ValkeyGlide();
$client->connect(
addresses: [['host' => 'my-cluster.xxxxx.use1.cache.amazonaws.com', 'port' => 6379]],
use_tls: true, // REQUIRED for IAM
credentials: [
'username' => 'my-iam-user',
'iamConfig' => [
ValkeyGlide::IAM_CONFIG_CLUSTER_NAME => 'my-cluster',
ValkeyGlide::IAM_CONFIG_REGION => 'us-east-1',
ValkeyGlide::IAM_CONFIG_SERVICE => ValkeyGlide::IAM_SERVICE_ELASTICACHE,
ValkeyGlide::IAM_CONFIG_REFRESH_INTERVAL => 300
]
]
);

Valkey GLIDE automatically handles multi-key operations across cluster slots:

// Works seamlessly across cluster shards
$client->mget(['key1', 'key2', 'key3']);
$client->mset(['key1' => 'val1', 'key2' => 'val2']);
$client->del(['key1', 'key2', 'key3']);

Unified key iteration across cluster shards:

$cursor = $client->scan(0);
while ($cursor->hasNext()) {
$keys = $cursor->getKeys();
foreach ($keys as $key) {
echo "Key: $key\n";
}
$cursor = $client->scan($cursor->getCursor());
}

Valkey GLIDE PHP supports all major Redis/Valkey features including:

  • Data Structures: Strings, Hashes, Lists, Sets, Sorted Sets, Streams, Bitmaps, HyperLogLog, Geospatial
  • Pub/Sub: Channel subscriptions, pattern subscriptions, publish
  • Transactions: MULTI/EXEC, WATCH for optimistic locking
  • Scripting: Lua scripts via EVAL, EVALSHA, SCRIPT commands
  • Cluster Operations: Cluster-aware MGET/MSET/DEL, cluster scan
  • Connection: TLS/SSL, IAM authentication, connection pooling
  • Server Commands: INFO, CONFIG, CLIENT commands
  • Update installation method to use PIE, Composer, or PECL
  • Optional: Call ValkeyGlide::registerPHPRedisAliases() for drop-in compatibility (PHP 8.3+)
  • Update connection code to use two-step pattern (construct then connect)
  • Update array parameters for push/set operations (lpush, rpush, sadd, srem)
  • Add exception handling around connection operations
  • Update return type expectations for consistency
  • Test cluster operations if using cluster mode
  • Update configuration to use connect() method parameters
  • Verify TLS/SSL configuration if using encrypted connections
  • Test IAM authentication if using AWS ElastiCache
  • Performance test to validate improvements

Valkey GLIDE PHP typically provides:

  • Faster command execution through Rust-based core
  • Lower memory usage due to efficient C extension implementation
  • Reduced latency through optimized protocol handling
  • Cluster-aware operations without manual key grouping
  • PHP Version: Requires PHP 8.2+ (PHP 8.3+ for ValkeyGlide::registerPHPRedisAliases() method)
  • Redis/Valkey: Compatible with Redis 6.2, 7.0, 7.2 and Valkey 7.2, 8.0, 8.1
  • Extensions: No conflicts with other Redis extensions
  • Platforms: Linux (Ubuntu 20+), macOS 14.7+ (Alpine/MUSL not supported)
<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth('mypassword');
$redis->select(2);
try {
$redis->set('user:1', json_encode(['name' => 'John', 'age' => 30]));
$userData = json_decode($redis->get('user:1'), true);
$redis->hset('user:1:profile', 'email', 'john@example.com');
$email = $redis->hget('user:1:profile', 'email');
echo "User: {$userData['name']}, Email: $email\n";
} catch (RedisException $e) {
echo "Error: " . $e->getMessage() . "\n";
}
<?php
// Load PHPRedis compatibility aliases
ValkeyGlide::registerPHPRedisAliases();
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->auth('mypassword');
$redis->select(2);
try {
$redis->set('user:1', json_encode(['name' => 'John', 'age' => 30]));
$userData = json_decode($redis->get('user:1'), true);
$redis->hset('user:1:profile', 'email', 'john@example.com');
$email = $redis->hget('user:1:profile', 'email');
echo "User: {$userData['name']}, Email: $email\n";
} catch (RedisException $e) {
echo "Error: " . $e->getMessage() . "\n";
}

Migration effort: Add one line (ValkeyGlide::registerPHPRedisAliases()) - that’s it!

<?php
$client = new ValkeyGlide();
$client->connect(
addresses: [['host' => '127.0.0.1', 'port' => 6379]],
credentials: ['password' => 'mypassword'],
database_id: 2
);
try {
$client->set('user:1', json_encode(['name' => 'John', 'age' => 30]));
$userData = json_decode($client->get('user:1'), true);
$client->hset('user:1:profile', 'email', 'john@example.com');
$email = $client->hget('user:1:profile', 'email');
echo "User: {$userData['name']}, Email: $email\n";
} catch (ValkeyGlideException $e) {
echo "Error: " . $e->getMessage() . "\n";
}

The migration provides the benefits of Valkey GLIDE while maintaining familiar Redis command patterns. Use the ValkeyGlide::registerPHPRedisAliases() method for minimal code changes.