Redis is often introduced into a .NET application as a fast cache, but serialization can become an important part of the performance equation once request volume increases.
Every time an application stores a complex object in Redis, it has to convert that object into bytes. When the value is read back, those bytes must be converted into an object again.
The basic flow looks simple:
.NET Object
|
v
Serialization
|
v
Bytes
|
v
Redis
|
v
Bytes
|
v
Deserialization
|
v
.NET Object
At low traffic, the difference between serialization formats may be difficult to notice.
At high throughput, however, serialization can affect:
CPU consumption
network payload size
Redis memory usage
request latency
garbage collection
allocation rate
throughput
This makes serialization an important benchmark target for high-throughput .NET APIs.
The goal of this article is not to declare one format as universally better. The right format depends on the data structure, compatibility requirements, payload size, latency target, and workload.
Why Redis Serialization Matters
Suppose an API stores a customer profile:
public sealed class CustomerProfile
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Country { get; set; } = string.Empty;
}
The application cannot send the .NET object directly to Redis.
It needs a representation such as:
JSON
Binary
MessagePack-style binary representation
Custom byte format
The representation affects both the size of the stored value and the work required to produce and consume it.
For a high-throughput API, this happens repeatedly:
10,000 requests/sec
|
+--> Serialize
|
+--> Redis operation
|
+--> Deserialize
Even a small amount of additional CPU work per request can become significant at scale.
What Should Be Benchmarked?
A meaningful serialization benchmark should measure more than raw serialization speed.
The most useful metrics include:
| Metric | Why It Matters |
|---|---|
| Serialization time | CPU cost before Redis write |
| Deserialization time | CPU cost after Redis read |
| Serialized size | Network and Redis memory impact |
| End-to-end latency | Real API performance |
| Allocations | GC pressure |
| Throughput | Maximum sustainable workload |
| Redis memory | Storage efficiency |
| CPU utilization | Infrastructure cost |
This allows you to distinguish between a format that is fast but produces large payloads and one that is slightly slower but dramatically smaller.
Choose Formats for a Controlled Comparison
A benchmark could compare formats such as:
JSON
Binary serialization
Compact binary serialization
The exact implementations should be selected according to your application's compatibility requirements.
The important rule is to keep the test fair.
Do not compare:
Format A + compression
against:
Format B without compression
unless compression itself is one of the variables being studied.
Likewise, avoid comparing completely different object models.
Use the Same Object Graph
The benchmark should serialize the same logical object.
For example:
public sealed class Order
{
public Guid Id { get; init; }
public int CustomerId { get; init; }
public DateTime CreatedAt { get; init; }
public decimal Total { get; init; }
public string Currency { get; init; } = string.Empty;
public List<OrderItem> Items { get; init; } = [];
}
public sealed class OrderItem
{
public int ProductId { get; init; }
public int Quantity { get; init; }
public decimal Price { get; init; }
}
The same Order instance should be passed through every serializer.
Otherwise, the benchmark may measure differences in the object itself rather than the serialization format.
Test Multiple Payload Sizes
A single payload size is rarely representative.
Use at least three categories:
Small
Medium
Large
For example:
| Payload | Approximate Size |
|---|---|
| Small | 0.5–2 KB |
| Medium | 5–20 KB |
| Large | 50–250 KB |
These values are benchmark categories, not requirements.
The correct ranges should reflect your application's actual Redis values.
A format that performs well for 1 KB objects may behave differently for 100 KB objects.
Build a Reproducible Benchmark
A benchmark project should isolate serialization from unrelated application behavior.
A useful structure is:
RedisSerializationBenchmark/
Models/
Serializers/
Benchmarks/
Results/
The benchmark should avoid including:
HTTP middleware
Database queries
Business logic
Logging
Authentication
unless the purpose is an end-to-end API benchmark.
Benchmark Serialization Separately
Start with pure serialization.
Conceptually:
[Benchmark]
public byte[] Serialize()
{
return Serializer.Serialize(order);
}
Then benchmark deserialization independently:
[Benchmark]
public Order Deserialize()
{
return Serializer.Deserialize<Order>(payload);
}
This gives you a clean measurement of serializer overhead.
Why Allocation Matters
Two serializers can have similar execution time but very different allocation behavior.
Consider:
Serializer A
2.0 ms
100 KB allocations
Serializer B
2.1 ms
20 KB allocations
At a small workload, the difference may not matter.
At high throughput, it can increase garbage collection activity.
For example:
20,000 operations/sec
×
80 KB additional allocation
creates a large amount of allocation pressure.
The result can be:
More allocations
|
v
More GC work
|
v
Higher CPU usage
|
v
Higher tail latency
This is why allocation metrics belong in the benchmark.
Measure Serialized Payload Size
Payload size is one of the easiest metrics to overlook.
Suppose three formats produce:
| Format | Payload |
|---|---|
| JSON | 18 KB |
| Binary A | 11 KB |
| Binary B | 8 KB |
The smaller representation can reduce:
Redis memory
Network transfer
Network bandwidth
But smaller does not automatically mean faster.
A format may require more CPU to encode or decode.
That is why size and latency should be evaluated together.
A Simple Size Benchmark
You can measure serialized size directly:
[Benchmark]
public int SerializedSize()
{
var bytes = Serializer.Serialize(order);
return bytes.Length;
}
For a complete benchmark, avoid serializing multiple times just to calculate the size if the benchmark is intended to measure one serialization operation.
Capture the payload once during setup when appropriate.
Benchmark Redis Separately From Serialization
After the serializer benchmark, add Redis.
The complete path becomes:
.NET object
|
v
Serialize
|
v
Redis SET
|
v
Redis GET
|
v
Deserialize
This gives you end-to-end behavior.
However, do not replace the pure serialization benchmark with the Redis benchmark.
Both answer different questions.
Local Redis Can Produce Misleading Results
A Redis server running on the same machine can make network latency almost irrelevant.
That is useful for measuring serialization overhead, but it does not represent a production deployment where Redis may be:
Separate host
Container
VM
Different availability zone
Different network segment
For a realistic production benchmark, test both:
Local Redis
Remote Redis
The first helps isolate application overhead.
The second helps understand actual network behavior.
Control Redis Configuration
Keep the Redis environment consistent between tests.
Document:
Redis version
Memory configuration
Persistence configuration
Network location
Connection pool settings
TLS configuration
If one benchmark uses TLS and another does not, the comparison becomes less useful.
Reuse Redis Connections
Do not create a new Redis connection for every benchmark operation.
A realistic API generally uses a shared connection manager.
Conceptually:
public sealed class CacheService
{
private readonly IConnectionMultiplexer _connection;
public CacheService(IConnectionMultiplexer connection)
{
_connection = connection;
}
}
Connection establishment should be benchmarked separately from steady-state Redis operations.
Otherwise, connection setup can dominate the result.
Measure GET and SET Separately
Redis reads and writes are different operations.
Benchmark:
SET + serialization
and:
GET + deserialization
independently.
For example:
Write path:
Object
|
v
Serialize
|
v
Redis SET
Read path:
Redis GET
|
v
Deserialize
|
v
Object
A serializer may perform differently on encode and decode.
Pipeline Workloads Matter
High-throughput APIs frequently issue multiple cache operations.
A realistic workload may look like:
Request
|
+--> GET customer
|
+--> GET preferences
|
+--> GET permissions
|
+--> SET response cache
Benchmarking only one Redis command at a time may hide application-level behavior.
After isolated benchmarks, introduce representative multi-operation workloads.
Test Concurrent Requests
Single-threaded benchmarks are useful for understanding serializer behavior but do not represent a busy API.
Join the conversation! Your thoughts help the community grow.