Client ID

Difficulty: Beginner

Question

How do you find the unique ID of the current Redis connection, and what is that ID actually used for?

Answer

Run CLIENT ID. It returns a 64-bit integer that uniquely identifies the current connection on that server; IDs increase monotonically and are never reused while the server runs. You use it to target that connection from elsewhere, for example CLIENT KILL ID <id>, CLIENT UNBLOCK <id> or CLIENT TRACKING ON REDIRECT <id>.

Properties of the client ID

  • Per connection, not per user. Each TCP (or Unix socket) connection gets a new ID. Reconnect and you get a different one. A connection pool of 10 has 10 IDs.
  • Monotonic and never repeated. A higher ID means a newer connection, which makes it easy to spot clients that keep reconnecting.
  • Per server. The same number on two different Redis nodes refers to two unrelated connections.
  • Cheap. CLIENT ID is O(1). It has existed since Redis 5.0. HELLO (Redis 6+) also returns the ID in its reply.

Related CLIENT subcommands

CommandPurpose
CLIENT INFODetails of the current connection (6.2+)
CLIENT LISTOne line per connected client with id=, addr=, name=, idle=, cmd=
CLIENT LIST ID 7 9Filter the list by IDs
CLIENT SETNAME / CLIENT GETNAMEAttach a readable name like order-service
CLIENT KILL ID <id>Close one specific connection
CLIENT UNBLOCK <id>Wake a client stuck in BLPOP, XREAD BLOCK, etc.
CLIENT TRACKING ON REDIRECT <id>Send client-side cache invalidations to another connection
CLIENT SETINFO LIB-NAME ...Libraries report their name and version (7.2+)

Where it helps in practice

  1. Debugging leaks. Production Redis shows 5,000 connections. CLIENT LIST with name= and addr= tells you which service is not closing connections, and the IDs tell you how fast new ones appear.
  2. Killing a bad consumer. A runaway worker issuing KEYS * can be stopped with CLIENT KILL ID 18231 without touching anything else on that host. Killing by ADDR works too, but ports can be reused; IDs cannot.
  3. Unblocking workers on shutdown. A worker blocked on BRPOP for 30 seconds can be released immediately with CLIENT UNBLOCK id so deploys finish faster.
  4. Client-side caching with RESP2. The invalidation messages go to a second connection subscribed to __redis__:invalidate, and you pass that connection's ID in REDIRECT.

Gotchas

  • Most libraries use connection pools, so calling CLIENT ID through a pooled client may hit a different connection than the next command. Use a dedicated connection when the ID matters.
  • Always CLIENT SETNAME your services. IDs are opaque numbers; names make CLIENT LIST readable during an incident.
  • CLIENT KILL and CLIENT LIST are in the @dangerous / @admin ACL categories, so application users usually should not have them.

Code examples

Finding and acting on a connection

# In connection A
CLIENT SETNAME payment-worker
CLIENT ID
# -> (integer) 412
BLPOP jobs:payments 0   # blocks forever

# In connection B (an admin shell)
CLIENT LIST ID 412
CLIENT UNBLOCK 412       # A returns nil and continues
CLIENT KILL ID 412       # or close the connection entirely

The ID lets an admin connection target exactly one client, even if many share the same host.

Key points

  • CLIENT ID returns a unique, monotonically increasing 64-bit integer per connection
  • A reconnect always produces a new ID; IDs are not shared across servers
  • Used with CLIENT KILL ID, CLIENT UNBLOCK and CLIENT TRACKING REDIRECT
  • Name your connections with CLIENT SETNAME so CLIENT LIST is readable
  • Pooled clients may run CLIENT ID on a different connection than you expect

Common follow-up questions

  • How would you find which service is leaking Redis connections?
  • What does CLIENT UNBLOCK return and when would you use the ERROR option?
  • How does REDIRECT work for client-side caching in RESP2?
  • Which ACL categories would you deny to an application user?

Interview tips

  • Go beyond the one-word answer: explain that the ID is per connection and what you do with it
  • A short story about debugging connection counts with CLIENT LIST lands well in experience-style rounds
  • Mention CLIENT SETNAME as a habit; interviewers like operational hygiene

Concepts covered

CLIENT ID, CLIENT LIST, CLIENT KILL, CLIENT SETNAME, Connection tracking

More Redis & Caching questions