Skip to content

Commit 1a1c974

Browse files
JamesNKRick-Andersonguardrex
authored
gRPC large binary performance best practices (#24250)
Co-authored-by: Rick Anderson <[email protected]> Co-authored-by: Luke Latham <[email protected]>
1 parent 660836d commit 1a1c974

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

aspnetcore/grpc/performance.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,20 @@ The preceding code:
264264
* Attempts to get an array from `ByteString.Memory` with <xref:System.Runtime.InteropServices.MemoryMarshal.TryGetArray%2A?displayProperty=nameWithType>.
265265
* Uses the `ArraySegment<byte>` if it was successfully retrieved. The segment has a reference to the array, offset and count.
266266
* Otherwise, falls back to allocating a new array with `ByteString.ToByteArray()`.
267+
268+
### gRPC services and large binary payloads
269+
270+
gRPC and Protobuf can send and receive large binary payloads. Although binary Protobuf is more efficient than text-based JSON at serializing binary payloads, there are still important performance characteristics to keep in mind when working with large binary payloads.
271+
272+
gRPC is a message-based RPC framework, which means:
273+
274+
* The entire message is loaded into memory before gRPC can send it.
275+
* When the message is received, the entire message is deserialized into memory.
276+
277+
Messages with large binary payloads can allocate byte arrays on the [large object heap](/dotnet/standard/garbage-collection/large-object-heap). Large allocations impact server performance and scalability.
278+
279+
Advice for creating high-performance applications with large binary payloads:
280+
281+
* **Avoid** large binary payloads in gRPC messages. A byte array larger than 85,000 bytes is considered a large object. Keeping below that size avoids allocating on the large object heap.
282+
* **Consider** splitting large binary payloads [using streaming](xref:grpc/client#client-streaming-call). Binary data is chunked and streamed over multiple messages.
283+
* **Consider** not using gRPC for large binary data. HTTP endpoints can be used alongside gRPC services. An HTTP endpoint that supports sending or receiving large files using the stream body is efficient.

0 commit comments

Comments
 (0)