# rpc error: code = ResourceExhausted desc = grpc: received message larger than max (5242881 vs. 4194304)

- **ID:** `go/grpc-resource-exhausted-message-larger-than-max`
- **Domain:** go
- **Category:** resource_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

The received message exceeds the default 4 MiB max receive size (grpc.MaxRecvMsgSize). gRPC rejects messages above the configured limit to protect memory. The default is 4 MiB for both send and receive.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.x | active | — | — |

## Workarounds

1. **** (90% success)
   ```
   Raise the limit deliberately on both ends to a value you can justify:

srv := grpc.NewServer(grpc.MaxRecvMsgSize(16<<20), grpc.MaxSendMsgSize(16<<20))
conn, _ := grpc.Dial(addr, grpc.WithDefaultCallOptions(
    grpc.MaxCallRecvMsgSize(16<<20),
    grpc.MaxCallSendMsgSize(16<<20)))
   ```
2. **** (88% success)
   ```
   For genuinely large payloads, switch to client/server streaming instead of a unary message:

stream, _ := client.Upload(ctx)
for _, chunk := range chunks { stream.Send(&pb.Chunk{Data: chunk}) }
_, err := stream.CloseAndRecv()
   ```

## Dead Ends

- **** — The max-size check is applied to the uncompressed message on the receiving side, so compression does not raise the limit and the error persists. (80% fail)
- **** — Allowing unbounded messages exposes the service to OOM from a single malicious or buggy client; the protection exists for a reason. (60% fail)
