AlterNats — High Performance .NET PubSub Client and How implement of optimized socket programming in .NET 6

  • Lack of monitoring for PubSub
  • Clustering support for PubSub
  • Unbalanced pricing of managed service(PubSub no needs memory)
  • Performance

Intro to AlterNats

AlterNats’s API fully adopts async/await and keeping C# native style.

High-Performance Socket Programming

Use Better Socket API

The class that can handle network processing at the lowest level in C# is Socket. And if you want asynchronous, high-performance processing, you need to use callbacks while reusing SocketAsyncEventArgs well.

Binary code determination for Text Protocols

The NATS protocol is a text protocol similar to Redis and others. It can be easily cut out by string processing. It is easy to implement the protocol by using StreamReader, since all you have to do is ReadLine. However, since it is (UTF8) binary data that flows over the network, and stringing is an overhead, if performance is desired, it must be processed as binary data.

Automatically pipelining

All writes and reads in the NATS protocol are pipelined (batch). This is easily explained by Pipelining in Redis. For example, if you send three messages, one at a time, and wait for a response each time, the many round-trips in sending and receiving will become a bottleneck.

Many functionality into a single object

To implement such a PublishAsync method, we need to put the data into a write message object for queueing channel and hold it in the heap. We also need a Promise for an asynchronous method that waits until the write is complete.

Zero-copy Architecture

The data to be Publish/Subscribe is usually serialized C# types to JSON, MessagePack, and so on. In this case, we inevitably exchange bytes[], for example, the contents of RedisValue in StackExchange.Redis are actually bytes[], and whether sending or receiving, we have to generate and hold bytes[].



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yoshifumi Kawai

Yoshifumi Kawai

a.k.a. neuecc. Creator of UniRx, UniTask, MessagePack for C#, MagicOnion etc. Microsoft MVP for C#. CEO/CTO of Cysharp Inc. Live and work in Tokyo, Japan.