C# Async Pipeline using Observer Design Pattern

Saurabh Singh
5 min readJan 2, 2019

--

Welcome to the second part of parallel processing. In this article, I will try to explain the Observer pattern. What, How and When to use to use this pattern.

A few months back, I was integrating several online casino games on the website for my client. There is a page where a user can select and play a game from a list of available games.

Problem

We need to maintain a list of 5 most recent games played by a user. We will use this information to study user’s behavior and recommend him similar games accordingly.

Users can also play such games directly from the recent games section, rather than searching or filtering it from the long list of games.

Other Considerations

Recent games list, it is not very critical data. We use this information to implement some good to have features but it is not something very critical for us and of course not for the end user.

Save Operation/Function:

# Even if our piece of code fails to save this information for some reason, we don’t have to notify end user. We just log the error and development team can look into it.

# Most importantly, this can be executed asynchronously in the background. The user doesn’t have to wait for it. What I meant is that user can start playing the game and we can update his recent games list in the database even after 5-10 seconds, it doesn’t matter much.

It is not like an online payment or account creation or update operations, that we must complete before letting the user do other things in our application.

The problem discussed above is the perfect scenario where we can implement the observer pattern.

What is Observer Design Pattern

Subject — is the generator of an event

Observer — is the handler, that subscribed to listen to events generated by Subject. If there are more than 1 observer listening to an event, then the Subject maintains the record of all Observers.

In this pattern, both Subject and Observer are aware of each other’s existence. And it is the fundamental difference between Observer pattern and Publisher-Subscriber pattern.

Please look at the following FavouriteGameProcessor class. To create this class you need ‘System.Threading.Tasks.Dataflow’ and ‘System.Reactive’ NuGet packages.

# We initialized a pipeline/queue using BufferBlock (_queue) and an observable (_games) that subscribes to FavouriteEvent, in the constructor.

# We exposed a public method Publish(), for the consumer of this class to add new events to this pipeline. In this case, the event is to add a game to the recent games list.

# FavouriteEvent() method is the handler that does the actual task of adding the game to the recent games list.

And here we are done, easy and simple implementation.

Now, when the user opens any game in our application, we just add the game detail in FavouriteGame queue and returns the control back to the main thread. There won’t be any blocking or delay in the response.

The Subscriber(FavouriteEvent) will process the event asynchronously in a separate thread.

Since it is an online games website, there are thousands of simultaneous open game events, we just add them to the pipeline and subscriber process them.

Conclusion

There can be other implementations of this pattern as well.

Let say you want to capture all user activities in your application. For that purpose, you create a middleware in your request handler pipeline.

In that middleware, we can identify the activity and add to asynchronous queue/pipeline, without impacting or adding delaying response time. The subscriber(s) can process each user activity in a separate thread asynchronously.

Note: There can multiple subscribers as well listening to an event.

Other Readings

There are other ways to implement a similar design pattern, each having its own design considerations and applicability.

You can find a few in below link:

Instead of BufferBlock, you can also use

There is so much more to explore in C# parallel programming concepts. Keep a check for upcoming articles and if you can’t wait, please go and check microsoft website.

Cheers

More Readings

--

--