Go has no dedicated interprocess communication (IPC) or cross-process locking API. You build both from standard library pieces: the net package for TCP, UDP and Unix domain sockets, net/rpc or gRPC for calls between services, pipes and os/exec for parent and child processes, and file locks or an external store such as etcd or Redis for synchronisation between processes. Channels and sync.Mutex only work inside one process.
This distinction matters. Go's concurrency primitives coordinate goroutines in a single address space. Once work spans processes or machines, you need an explicit transport and an agreed locking or consensus mechanism.
Channels, sync.Mutex, sync.WaitGroup and sync/atomic synchronise goroutines that share memory in one process. Two separate processes do not share that memory, so a sync.Mutex in one is invisible to the other. The old claim that mutexes can be used between processes is wrong.
| Need | Same process | Different processes |
|---|---|---|
| Pass data | Channel | Socket, pipe, gRPC, message queue |
| Mutual exclusion | sync.Mutex | File lock, database lock, etcd or Redis lock |
| Wait for completion | sync.WaitGroup | Process exit status, message, health check |
| Cancel work | context.Context | Signal, RPC deadline, shutdown message |
For processes on the same host, Unix domain sockets avoid the network stack and can be restricted with file permissions. For processes on different hosts, use TCP. The code is identical apart from the network name passed to net.Listen and net.Dial.
This echo server handles each connection in a goroutine and uses bufio so that it reads lines instead of arbitrary byte chunks:
The matching client dials, sends one line and prints the reply:
TCP is a byte stream with no message boundaries, so you must frame messages yourself, for example with newlines, length prefixes or a protocol such as HTTP. In production, also set deadlines with conn.SetDeadline so a stalled peer cannot hold a goroutine forever.
Raw sockets are rarely the best choice for service-to-service calls. Common options:
net/http is the simplest and works with every language.net/rpc package works but is frozen and only speaks Go-specific encodings, so new projects usually prefer gRPC.When one Go program starts another, use os/exec and connect to its standard streams. This is the safest form of local IPC because the pipes are private to the two processes.
For unrelated processes on Unix, create a named pipe outside Go with mkfifo /tmp/myfifo (or syscall.Mkfifo) and open it with os.OpenFile using os.O_RDONLY or os.O_WRONLY. Opening a FIFO blocks until the other end opens it. Windows has a different named pipe API, so portable code usually prefers sockets.
On a single machine, an advisory file lock lets several processes take turns. The standard library does not expose flock portably, so on Linux and macOS use syscall.Flock, or a maintained module such as github.com/gofrs/flock for cross-platform code.
Across machines you need a shared system. Options include etcd leases and the go.etcd.io/etcd/client/v3/concurrency package, ZooKeeper, Consul sessions, database advisory locks such as PostgreSQL pg_advisory_lock, or Redis.
A Redis lock must have an expiry and an owner token, otherwise a crashed holder blocks everyone forever and one client can release another's lock:
A single-node Redis lock is a best-effort mutual exclusion. If correctness is critical, use a lock with fencing tokens from a consensus system such as etcd, and make the protected operation idempotent.
context.Context for deadlines and cancellation, and propagate it into every RPC and database call.SIGTERM using signal.NotifyContext so in-flight work finishes and locks are released.No. It lives in one process's memory. Use a file lock, a database lock or a distributed lock service instead.
Unix domain sockets and pipes are fast and simple. Shared memory via mmap is faster still but needs careful manual synchronisation, and Go offers no safe high-level API for it.
Use gRPC or plain HTTP for new work. net/rpc is frozen and not available to non-Go clients.
Give it a time-to-live or lease and renew it while work continues. If the holder crashes, the lease expires and others can proceed.
Go's channels and mutexes coordinate goroutines inside one process only. For separate processes, communicate with sockets, pipes, HTTP or gRPC, and synchronise with file locks or a distributed lock service backed by expiry and unique tokens. Add timeouts, context cancellation and idempotent operations so the system tolerates failures.