MIGRATE connections
How MIGRATE reaches another server, and its current limits.
MIGRATE moves or copies keys from a local pgredis database to a target Redis or pgredis server over TCP while holding a PostgreSQL transaction on the source.
Connection-stage versus write-stage errors
Redis 7.2.4 classifies MIGRATE socket failures according to the result of the initial non-blocking connect() syscall:
- Once
connect()succeeds immediately or returnsEINPROGRESS, Redis stops traversing the remaining resolved addresses and waits on that socket. Any later connection refusal or timeout is reported asIOERR error or timeout writing to target instance. - Standard Go
net.Dialercollapses address iteration and returns only the finalerrnoor timeout, losing the boundary betweenconnecting to the clientandwriting to target instance.
To match Redis 7.2.4 error classification, the pgredis MIGRATE connector walks resolved addresses in order and inspects the initial connect() result directly:
| Observation | Connector action |
|---|---|
Host resolution fails, setup fails, or context is canceled before connect() | Return a connecting to the client error |
Socket creation fails, or initial connect() fails immediately | Try the next resolved address |
Initial connect() succeeds or returns EINPROGRESS | Stop traversing addresses and wait on this socket |
| Accepted/in-progress socket later fails, times out or is canceled | Return a writing to target instance error without trying another address |
SO_ERROR is 0 and getpeername() succeeds | Hand the connected socket to the MIGRATE protocol handler |
Platform libc address resolution
In standard cgo builds on Linux and macOS, MIGRATE resolves target host names through the platform’s libc getaddrinfo() using a zeroed addrinfo hints structure with AF_UNSPEC and SOCK_STREAM:
- Address order, socket family (
AF_INET/AF_INET6), protocol, port and numeric IPv6 scope IDs are preserved as returned by libc. - Empty host strings follow the host libc (
glibcon Linux returnsEAI_NONAME, whereas macOS resolves to loopback). Host names containingNULbytes or exceeding1,023bytes are rejected before calling libc. - Up to
8concurrent resolver calls are permitted per gateway process; resolution and connection establishment share one overall deadline.
Acknowledgement and commit ordering
- The
pg_redisextension checks permissions, locks the source keys and serializesDUMPpayloads in a source PostgreSQL transaction. - After the connector hands off an established socket, the gateway sends
AUTH(when requested) andSELECTto the target and waits for each acknowledgement before sendingRESTORE. - Only keys whose target
RESTOREreturns an exact+OKare deleted from the source database (unlessCOPYis set). The gateway replies to the caller only after the source PostgreSQL transaction confirms commit. - If an acknowledgement is lost or the source commit outcome is unknown, the target may already hold a copy while the source key remains. The gateway never replays
MIGRATEor deletes target keys to compensate for a source rollback.
Current limits
- Targets sharing the same PostgreSQL database: Because a
MIGRATEtransaction holds the per-database exclusive lock on the source database until commit, migrating to a gateway bound to the same PostgreSQL database (even with a different Redis logical database0–15) blocks when the target attemptsAUTH,SELECTorRESTORE, and times out. Migrating between gateways backed by separate PostgreSQL databases works as expected (verified across 96 cases in 8 auth/protocol profiles on commit715827aa0). - SQL transactions and
MULTI/EXEC: CallingMIGRATEinsideMULTI/EXECor viaredis.command/redis.command_binreturnsNOKEYwhen all named source keys are missing or expired, and returns an explicit scope error when any source key exists because remote network writes cannot be rolled back if the caller later rolls back its SQL transaction.
