Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 42 ยท Working with Redis

Using Redis from Node.js, Python, PHP and Java

Connect to Redis from Node.js, Python, PHP and Java: popular drivers, connection pooling, timeouts, retries, error handling and value serialization.

So far you have typed commands. Real applications send them from code, through a client library (also called a driver). Every language has several, and they all do the same four jobs: open a connection, encode your command as RESP, read the reply, and turn it into a value of your language. The differences are in names, defaults and how they handle the awkward parts: connection reuse, timeouts, failures and complex values.

This lesson shows the popular choices for Node.js, Python, PHP and Java as read-only snippets (they need a running server and the library installed), then teaches the ideas that matter in every language: pooling, timeouts, retries, error handling and serialization. The runnable examples are JavaScript models: a small client wrapper over an emulated server, and a pool with retries over a flaky fake network.

Driver APIs change between major versions, so treat the snippets as the shape of a typical call, and check your library's documentation for exact option names.

The same program in four languages

Each snippet connects, stores a value that expires in 60 seconds, reads it back and closes. The command names mirror Redis (SET, GET), so what you learned in earlier lessons carries over.

Node.js has two widely used clients. node-redis is the official one, and ioredis is a long-standing alternative. Both are promise based.

JavaScript
// node-redis
import { createClient } from "redis";

const client = createClient({ url: "redis://localhost:6379" });
client.on("error", (err) => console.error("Redis client error:", err));
await client.connect();

await client.set("greeting", "hello", { EX: 60 });
console.log(await client.get("greeting"));
await client.quit();
JavaScript
// ioredis
import Redis from "ioredis";

const redis = new Redis({ host: "localhost", port: 6379 });
await redis.set("greeting", "hello", "EX", 60);
console.log(await redis.get("greeting"));
await redis.quit();

Python uses redis-py. Pass decode_responses=True to get str back instead of bytes.

Python
import redis

r = redis.Redis(host="localhost", port=6379, decode_responses=True)
r.set("greeting", "hello", ex=60)
print(r.get("greeting"))

PHP has the C extension phpredis and the pure-PHP library Predis.

PHP
<?php
// phpredis (extension)
$redis = new Redis();
$redis->connect('127.0.0.1', 6379, 2.0);
$redis->set('greeting', 'hello', ['EX' => 60]);
echo $redis->get('greeting');

// Predis (Composer package)
$client = new Predis\Client('tcp://127.0.0.1:6379');
$client->set('greeting', 'hello', 'EX', 60);
echo $client->get('greeting');

Java has Jedis, a straightforward blocking client, and Lettuce, which supports synchronous, asynchronous and reactive styles.

Java
// Jedis
try (JedisPool pool = new JedisPool("localhost", 6379);
     Jedis jedis = pool.getResource()) {
    jedis.setex("greeting", 60, "hello");
    System.out.println(jedis.get("greeting"));
}

// Lettuce
RedisClient client = RedisClient.create("redis://localhost:6379");
try (StatefulRedisConnection<String, String> connection = client.connect()) {
    RedisCommands<String, String> commands = connection.sync();
    commands.setex("greeting", 60, "hello");
    System.out.println(commands.get("greeting"));
}
client.shutdown();

What every client does

Connect Open and reuse a TCP connection.
Encode Turn commands into RESP.
Decode Turn replies into native values.
Fail well Timeouts, retries, clear errors.

Pick any library; the habits below apply to all of them.

1 / 2

Connections: create once, reuse everywhere

Opening a TCP connection (and a TLS handshake, and authentication) costs far more than a Redis command. The golden rule is: create the client once when the process starts and share it. Do not connect per request.

How sharing works depends on the library:

  • Multiplexing clients (node-redis, ioredis, Lettuce) use one connection that many in-flight commands share. Commands are pipelined for you. One client object for the whole application is normal.
  • Pooling clients (Jedis, redis-py with its pool, Predis in some modes) hand out a connection for the duration of a call or block and take it back after. You pick the pool size.

Blocking commands (BLPOP, BRPOP), SUBSCRIBE, and WATCH/MULTI sequences occupy a connection. Give those a dedicated connection, outside the shared pool, or you will stall unrelated requests or have a stranger's command land in the middle of your transaction (see Transactions).

In PHP, where each request usually starts from nothing, persistent connections (pconnect in phpredis) or a process manager that keeps workers alive avoid reconnecting every time.

Timeouts

A client that waits forever turns a Redis problem into an application outage. Set at least two timeouts: a connect timeout (how long to wait to open the socket, typically 1 to 5 seconds) and a command or socket timeout (how long to wait for a reply, often 0.5 to 3 seconds for caching traffic). Keep them short for a cache, because a slow cache is worse than a miss: the application should fall back to the database. Blocking commands need a timeout longer than their own block time.

Errors and retries

Distinguish three situations:

  1. Redis replied with an error (WRONGTYPE, OOM, NOSCRIPT). The server is fine and the command was understood. Retrying the same command will fail again, so fix the code or handle it explicitly.
  2. The connection failed before the command was sent. Retrying is safe.
  3. The connection broke after you sent the command. You do not know whether it ran. Retrying an idempotent command (GET, SET, DEL) is safe. Retrying a non-idempotent one (INCR, LPUSH) may apply it twice.

Use exponential backoff (wait 50 ms, 100 ms, 200 ms, ...) with a small random jitter and a low maximum number of attempts, so a recovering server is not flattened by a retry storm. For a cache, the best retry policy is often none: treat a failure as a miss and carry on. Wrap cache calls in try/catch and log, so a cache outage degrades to slower pages rather than errors.

Serialization

Redis stores bytes. Your language has numbers, objects, dates and nulls, and the client has to flatten them. Most drivers send strings and return strings, and anything more is your job:

  • Numbers: stored as text. INCR and friends interpret them as 64-bit integers, so keep counters as plain integers.
  • Objects: a common choice is JSON in a string key (simple, readable), or a hash when you update fields separately (see Choosing the Right Data Type). Binary formats such as MessagePack are smaller and faster but harder to inspect.
  • Dates: store UTC timestamps as integers or ISO 8601 strings, never locale-dependent formats.
  • Missing values: a missing key returns nil. Do not store the string "null" and confuse the two.
  • Versioning: if the format may change, include a version in the key (v2:user:42) so old and new code never read each other's data.

Pick one serialization in one wrapper, so the rest of the code never repeats it.

A client wrapper over an emulated server

The model below is the wrapper you would write in a real project: a small class that adds a key prefix, JSON serialization and a default expiry on top of a basic client. The "server" is an in-memory emulator with a fake clock, so the output is repeatable. In production the inner client would be ioredis, redis-py or similar, and the wrapper would look much the same.

JS A typed client wrapper: prefix, JSON and TTL

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The wrapper hides three decisions in one place: the key naming scheme, the serialization format, and the default lifetime. A caller just says setJson("42", user). The emulator reproduces what SET ... EX, GET and TTL return, including -2 for a missing key and -1 for a key without expiry, but it is not a real client.

Pools, timeouts and retries in one model

The second model puts the earlier ideas together: a connection pool of two connections, a scripted flaky network that fails on chosen attempts, and a retry helper with exponential backoff that retries only commands that are safe to repeat. Time is a fake clock, so no real waiting happens.

JS Pool, backoff and safe retries

Press Run (or Ctrl + Enter) to see the console output here.

No output.

This shows the rules: the idempotent SET survives two failures thanks to backoff (the clock advances 50 and then 100 ms), the endlessly failing GET gives up after four attempts instead of looping, INCR is surfaced to the caller rather than blindly repeated (a lost reply might hide a successful increment), and an exhausted pool fails fast instead of waiting forever. Real libraries implement the same ideas with more options and real sockets.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How should an application normally manage its Redis client?

    Connecting (and TLS and auth) costs far more than a command. Reuse a shared client or pool for the life of the process.
  2. 2. Which command should never run on a shared pooled connection that other code also uses?

    Blocking commands, subscriptions and WATCH/MULTI hold the connection's state, so use a dedicated connection for them.
  3. 3. A connection drops right after you sent INCR counter. What is the safest approach?

    INCR is not idempotent. A blind retry could count twice, so only retry commands that are safe to repeat, or design an idempotent operation.
  4. 4. What does a missing key look like to a client?

    Redis replies with nil for a missing key, and the driver maps it to the language's null value.
  5. 5. Why include a version such as v2 in a cache key prefix?

    Changing the prefix makes old entries unreachable (they expire naturally), avoiding migration code.

Summary

  • Every client does the same job: connect, encode commands as RESP, decode replies; pick a mature library for your language (node-redis or ioredis, redis-py, phpredis or Predis, Jedis or Lettuce).
  • Create one shared client or pool per process; give blocking commands, subscriptions and WATCH sequences their own connection.
  • Always set connect and command timeouts, and make a cache failure degrade to a miss.
  • Retry with exponential backoff and a low attempt limit, and only for commands that are safe to repeat.
  • Redis stores bytes: choose one serialization (usually JSON or a hash), use UTC timestamps, and version your keys.

Next, put Redis to work as a cache in Caching Patterns.