How to build streaming API

Sumeet More
3 min readApr 24, 2020

--

Imagine you are a software engineer, you are told to build application for some XYZ company, you got all the requirement and you are set to develop this application.

You start with API development and you start with simplest get all employees API.

Let’s understand what and how this get all employees API works. You call DB, get data, stored in List and make json format of that list and give it to frontend. You must be like Sumit? what’s the big deal here? Imagine company XYZ has 1 million employees all over world. Now when someone makes call to this get all employees api , 1million objects will be allocated in memory and imagine you get 1000 such requests per second. Yup! you are correct this is an expensive API.

Let’s see how we can solve this problem or atleast try to solve it.

Streaming might help us here. how? Instead of DB to give us all records of employees and store it memory and give to frontend, why can’t we give one record to frontend when DB fetches that one record instead of storing it in list/array.

HOLD ON!!! what?

Yes. you heard it right. That’s streaming right? when you watch video on YouTube do you think entire video comes to your app?Nope. small small parts of video keep coming. On similar fashion why send all employees list in one call? as DB fetches one record, send it to client instead of storing in a list. I hope now I have convinced you!

To complete this solution in .net(any language/framework), two concepts will help us.

  • Websocket : Normally it is request response http calls for apis, but we will require websocket here since we need to maintain that socket between frontend and API till all records are send.
  • IAsyncEnumerable<T>: This is very specific to .NET world but basically it is asynchronous collection type which gets data in asynchronous manner. If you are a careful reader, you will understand that asynchronous nature is so crucial for streaming nature.( if you want more detail explanation on this concept, I have added a link)

Let me show you now how API will work

Code :

On right side, you can see I have endpoint of api and records coming as they are available.(right screen is like api tester so that you can give this endpoint to frontend to consume) and left side you can see code, I am using web socket and Iasyncenumerable.(Code is not production ready but idea is concrete and implemented).

Video demo(I highly encourage you to watch video since it will show streaming of records from DB) :

I hope you enjoyed reading this article. I think streaming api is very powerful tool for Software Engineer these days 😊🚀🚀

Educational Link: https://youtu.be/2J9AylG6eHg . For good clear understanding of how IAsyncEnumerable works , I highly recommend this YouTube video by Shiv.

--

--

Sumeet More
Sumeet More

Written by Sumeet More

Software Engineer 2 at Microsoft | Backend Engineer and Architect| Blockchain & ML enthusiast | C#,.NET Core, Rust, Javascript and Go

No responses yet