# MIGRATE connections

> How MIGRATE reaches another server, and its current limits.

---

LLMS index: [llms.txt](/llms.txt)

---

`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 {#decision}

Redis 7.2.4 classifies `MIGRATE` socket failures according to the result of the initial non-blocking `connect()` syscall:

- Once `connect()` succeeds immediately or returns `EINPROGRESS`, Redis stops traversing the remaining resolved addresses and waits on that socket. Any later connection refusal or timeout is reported as `IOERR error or timeout writing to target instance`.
- Standard Go `net.Dialer` collapses address iteration and returns only the final `errno` or timeout, losing the boundary between `connecting to the client` and `writing 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 {#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 (`glibc` on Linux returns `EAI_NONAME`, whereas macOS resolves to loopback). Host names containing `NUL` bytes or exceeding `1,023` bytes are rejected before calling libc.
- Up to `8` concurrent resolver calls are permitted per gateway process; resolution and connection establishment share one overall deadline.

## Acknowledgement and commit ordering {#ownership}

1. The `pg_redis` extension checks permissions, locks the source keys and serializes `DUMP` payloads in a source PostgreSQL transaction.
2. After the connector hands off an established socket, the gateway sends `AUTH` (when requested) and `SELECT` to the target and waits for each acknowledgement before sending `RESTORE`.
3. Only keys whose target `RESTORE` returns an exact `+OK` are deleted from the source database (unless `COPY` is set). The gateway replies to the caller only after the source PostgreSQL transaction confirms commit.
4. 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 `MIGRATE` or deletes target keys to compensate for a source rollback.

## Current limits {#limits}

- **Targets sharing the same PostgreSQL database:** Because a `MIGRATE` transaction 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 database `0`–`15`) blocks when the target attempts `AUTH`, `SELECT` or `RESTORE`, and times out. Migrating between gateways backed by separate PostgreSQL databases works as expected (verified across 96 cases in 8 auth/protocol profiles on commit `715827aa0`).
- **SQL transactions and `MULTI`/`EXEC`:** Calling `MIGRATE` inside `MULTI`/`EXEC` or via `redis.command` / `redis.command_bin` returns `NOKEY` when 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.

---

Backlinks:

- [Design](/docs/redis/design/)
