Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How does Go support interprocess communication and synchronisation?

Go
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedMar 13, 2023
UpdatedNov 17, 2026
8 min read

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.

Table of Contents

  • What Go provides inside a process and what it does not
  • Communicating over TCP and Unix sockets
  • Structured calls with HTTP, gRPC and net/rpc
  • Pipes, child processes and named pipes
  • Synchronising processes with file locks and distributed locks
  • Design guidance for distributed and parallel Go systems
  • Frequently Asked Questions
    • Can sync.Mutex lock between two Go processes?
    • What is the fastest IPC for processes on the same machine?
    • Should I use net/rpc or gRPC?
    • How do I stop a distributed lock from being held forever?
  • Summary

What Go provides inside a process and what it does not

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.

NeedSame processDifferent processes
Pass dataChannelSocket, pipe, gRPC, message queue
Mutual exclusionsync.MutexFile lock, database lock, etcd or Redis lock
Wait for completionsync.WaitGroupProcess exit status, message, health check
Cancel workcontext.ContextSignal, RPC deadline, shutdown message

Communicating over TCP and Unix sockets

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:

go

The matching client dials, sends one line and prints the reply:

go
text

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.

Structured calls with HTTP, gRPC and net/rpc

Raw sockets are rarely the best choice for service-to-service calls. Common options:

  • HTTP with JSON using net/http is the simplest and works with every language.
  • gRPC with Protocol Buffers gives typed contracts, streaming and deadlines, and is the standard choice for internal Go services.
  • The standard library net/rpc package works but is frozen and only speaks Go-specific encodings, so new projects usually prefer gRPC.
  • Message brokers such as NATS, Kafka or RabbitMQ decouple producers and consumers and survive restarts.

Pipes, child processes and named pipes

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.

go
text

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.

Synchronising processes with file locks and distributed locks

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.

go

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:

go

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.

Design guidance for distributed and parallel Go systems

  1. Prefer messages over shared state. Design processes so that each owns its data and others ask it through an API.
  2. Use context.Context for deadlines and cancellation, and propagate it into every RPC and database call.
  3. Make operations idempotent and retry with backoff, since networks fail between any two steps.
  4. Always set timeouts on dials, reads and writes.
  5. Shut down gracefully on SIGTERM using signal.NotifyContext so in-flight work finishes and locks are released.
  6. Keep locks short and give them leases or expiry times.

Frequently Asked Questions

Can sync.Mutex lock between two Go processes?

No. It lives in one process's memory. Use a file lock, a database lock or a distributed lock service instead.

What is the fastest IPC for processes on the same machine?

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.

Should I use net/rpc or gRPC?

Use gRPC or plain HTTP for new work. net/rpc is frozen and not available to non-Go clients.

How do I stop a distributed lock from being held forever?

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.

Summary

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.

Share