Content · Web Development · PHP

PHP Redis::exec() Method: Usage and Examples

Executes commands previously queued with multi or pipeline and returns their results. MULTI does not automatically roll back earlier queued commands when another command

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Executes commands previously queued with multi or pipeline and returns their results.

The objective is to understand the behavior and operational limits, not only the method name.

What It Does


Executes commands previously queued with multi or pipeline and returns their results.

Syntax and Return Value


Redis::exec(...)

The return value and exceptional cases depend on the call described here: MULTI does not automatically roll back earlier queued commands when another command fails.

Requirements: install and enable the corresponding PHP driver or extension and configure the service before running the examples.

Example 1: Basic Use


Requires the phpredis extension and a running Redis server. Keys use a guide-specific prefix; clean up example keys after experimenting.

<?php
$redis = new Redis();
$redis->connect(getenv('REDIS_HOST') ?: '127.0.0.1', (int) (getenv('REDIS_PORT') ?: 6379), 2.0);
$redis->multi();
$redis->set('guide:exec:1','draft');
$redis->incr('guide:exec:1:secondary');
$results = $redis->exec();
echo json_encode($results);

Example 2: A Second Scenario


Requires the phpredis extension and a running Redis server. Keys use a guide-specific prefix; clean up example keys after experimenting.

<?php
$redis = new Redis();
$redis->connect(getenv('REDIS_HOST') ?: '127.0.0.1', (int) (getenv('REDIS_PORT') ?: 6379), 2.0);
$redis->multi();
$redis->set('guide:exec:2','review');
$redis->incr('guide:exec:2:secondary');
$results = $redis->exec();
echo json_encode($results);

Example 3: Practical Checks


Requires the phpredis extension and a running Redis server. Keys use a guide-specific prefix; clean up example keys after experimenting.

<?php
$redis = new Redis();
$redis->connect(getenv('REDIS_HOST') ?: '127.0.0.1', (int) (getenv('REDIS_PORT') ?: 6379), 2.0);
$redis->multi();
$redis->set('guide:exec:3','published');
$redis->incr('guide:exec:3:secondary');
$results = $redis->exec();
echo json_encode($results);

Important Behavior and Edge Cases


MULTI does not automatically roll back earlier queued commands when another command fails.

For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.

Comparison with Related Approaches


ApproachImportant distinction
Redis::execExecutes commands previously queued with multi or pipeline and returns their results.
Related API / patternmulti queues commands atomically on execution; pipeline only batches network traffic.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesMULTI does not automatically roll back earlier queued commands when another command fails.
Using a related API interchangeablymulti queues commands atomically on execution; pipeline only batches network traffic.

Key Takeaways


  • Executes commands previously queued with multi or pipeline and returns their results.
  • MULTI does not automatically roll back earlier queued commands when another command fails.
  • multi queues commands atomically on execution; pipeline only batches network traffic.

Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.

Share this resourceSend this page to someone who may find it useful.