Taswar Bhatti
The synonyms of software simplicity
Redis

Redis Pub Sub, somehow may sound weird that it provides such a functionality since when you think of Redis the first thing that comes to mind is of a cache key value store. But indeed Redis does allow us to use the messaging paradigm by using channels to publish messages and for subscribers to listen for notification of the message. Redis Pub Sub allows multiple subscribers to listen to one or more channels, and to only receive messages that are of interest. It decouples the subscriber from the publisher since the subscriber has no knowledge of whom the publishers are and vice versa there could be multiple publisher not knowing whom the subscribers are.

Sample Diagram of Redis Pub Sub

RedisPubSub

Redis Pub Sub

There are definitely certain restrictions of using Redis Pub Sub as a Messaging System, it will not be like RabbitMQ, Kafka or Azure MessageBus etc. Those message bus are able to store the message for durability or even replay of an old message for consumption. Redis uses a listener model where there are no listeners (subscribers) it will not receive those messages. But if you wish to have a simple pub sub without the heavy tools then Redis does quite a good job at it.

Redis Pub Sub – Operations

  • PUBLISH channels message: Posts a message to the given channel O(N+M)
  • SUBSCRIBE [channel]: Subscribe to a given channel for message, O(N) where N is the number of channels to subscribe to
  • PSUBSCRIBE [channel]: Subscribes the client to the given patterns, O(N) where N is the number of patterns the client is already subscribed to.
  • PUBSUB CHANNELS pattern: Currently active channels, O(N)
  • PUBSUB NUMSUB channel: Number of subscribers to the channels provided, O(N)
  • PUBSUB NUMPAT: Number of subscriptions to all the patterns O(N)
  • PUNSUBSCRIBE: Unsubscribes the client from a pattern, O(N+M)
  • UNSUBSCRIBE: Unsubscribes the client from a channel, O(N) where N is the number of clients already subscribed to a channel.

C# code using Redis PubSub

So this covers the basic usage of Redis PubSub, in the next blog post I will cover how to use Pipelines in Redis.

For the code please visit
https://github.com/taswar/RedisForNetDevelopers/tree/master/9.RedisPubSub

For previous Redis topics

  1. Intro to Redis for .NET Developers
  2. Redis for .NET Developer – Connecting with C#
  3. Redis for .NET Developer – String Datatype
  4. Redis for .NET Developer – String Datatype part 2
  5. Redis for .NET Developer – Hash Datatype
  6. Redis for .NET Developer – List Datatype
  7. Redis for .NET Developer – Redis Sets Datatype
  8. Redis for .NET Developer – Redis Sorted Sets Datatype
  9. Redis for .NET Developer – Redis Hyperloglog
DevTeach Mntreal Speaker

Yeahhhh I am really exited that I have been selected to speak at DevTeach Montreal 2017 this year, July 3rd onward to July 7th in the lovely Montreal Canada.

I remember my first visit to DevTeach was in 2007 as an attendee, now 10 years later I am presenting in the conference. Looking back it has been one amazing ride of where things are for me. In 2007 when I was attending Roy Osherove presented on Agile Development and ended with singing a song at the end of the session, which was quite memorable moment. To JP Boodhoo session on refactoring code and Rules for Agile Design by Jeremy Miller, Oren Eini on MonoRails and Rhino Mocks. Those were quite the early days of ALT .NET and who would have known that .NET would now be open source and runs on Linux with dotnet core.

I wonder what the next 10 years would be like for .net community?

My two abstract which has been selected by DevTeach
– Store 2 million of audit a day into Elastic Search
– OAuth2 and OpenId Connect Demystified

You can read more on the abstract on the DevTeach site.

Come and join us at DevTeach Montreal, it would be an awesome event with the Jazz festival happening at the same time and one day you never know you may be speaking in DevTeach too….

Xamarin

I was invited to Sprott School of Business – Carleton University for an introduction to the class on Xamarin what Microsoft platform offers for mobile development.

Below you will see my sides that were present by me and Ahmed Al-Assad a fellow MVP.

A special thanks to Alex Ramirez for the invitation to speak to the students.

Docker for .NET Developers

Docker for .NET Developers

Last week I was presenting at the Ottawa IT User Group for .NET Developers on Docker for .NET Developers. We covered what docker is, how it fits into microservices, overview of using docker as a .NET Developer etc

Topics that we covered were

  • What is Docker
  • Images and Containers
  • Docker vs VM
  • Tooling around Docker (Visual Studio Tools for Docker)
  • Docker linking with other containers
  • How to use docker compose to link containers

Sample code for the demo on using Postgres, Dotnet core and docker-compose file are located at
https://github.com/taswar/postgre-dotnetcore

If you would like to see me write more on docker, feel free to get in touch or comment.

Here are the presentation slides. Enjoy and feel free to share or clip them 🙂

Redis

Redis HyperLogLog is used for calculating the cardinality of a set or non mathematically speaking it is a probabilistic data structure used to count unique values. Basically it is an algorithm that use randomization such that it can provide an approximation of the number of unique elements in a set by just using a constant complexity, and a fixed/small amount of memory footprint. Each key stored in hyperloglog is only about 12 kbytes per key and with a standard error of 0.81%. There is no limit to the number of items you can count in the HyperLogLog Datatype, unless you approach 2^64 items. If you are planning to use some form of unique count, e.g ipadress

Redis HyperLogLog Datatype – Operations

  • PFADD: Adds or updates one or more members to HyperLogLog, Returns 1 if at least 1 HyperLogLog internal register was altered, 0 otherwise O(1)
  • PFCOUNT: Returns the approximated cardinality computed by the HyperLogLog, 0 if the variable does not exist. O(1).
  • PFMERGE: Merge multiple HyperLogLog values into an unique value O(N)

C# code using Redis HyperLogLog Datatype

So this covers the basic usage of Redish HyperLogLog Datatype, in the next blog post I will cover how to use Pub/Sub in Redis.

For the code please visit
https://github.com/taswar/RedisForNetDevelopers/tree/master/8.RedisHyperLogLog

For previous Redis topics

  1. Intro to Redis for .NET Developers
  2. Redis for .NET Developer – Connecting with C#
  3. Redis for .NET Developer – String Datatype
  4. Redis for .NET Developer – String Datatype part 2
  5. Redis for .NET Developer – Hash Datatype
  6. Redis for .NET Developer – List Datatype
  7. Redis for .NET Developer – Redis Sets Datatype
  8. Redis for .NET Developer – Redis Sorted Sets Datatype
Redis

Redis Sorted Sets are similar to Sets, with unique feature of values stored in Set called scores. The score is used in order to take the sorted set ordered, from the smallest to the greatest score. Just like in Sets, members are unique but scores can be repeated. Sorted Sets are ideal for storing index data in Redis, or using some of the Sorted Set commands to find out how many users have authenticated to a system, or top users using the system etc.

Redis Sorted Sets Datatype – Operations

  • ZADD: Adds or updates one or more members to a Sorted Set O(log (N))
  • ZRANGE: Gets the specified range by rank of elements in the Sorted Set O(log (N) +M).
  • ZRANGEBYSCORE: Gets elements from the Sorted Sets within the range by score given values are in ascending order O(log (N) +M)
  • ZREVRANGEBYSCORE: Gets elements from the Sorted Sets within the score given O(log (N) +M)
  • ZREVRANK: The rank of the member in the Sorted Set O (log (N))
  • ZREVRANGE: Returns the specified range of elements in the Sorted Set O(log (N) + M)
  • ZREM: Removes the specified members in the Sorted Set O(M*log (N))
  • ZREMRANGEBYRANK: Removes the members in a Sorted Set within the given indexes O(log (N) * M)
  • ZREMRANGEBYSCORE: Removes the members in a Sorted Set within the given scores O(log (N) * M)
  • ZCARD: Gets the number of members in a Sorted Set O(1)
  • ZCOUNT: Gets the number of members in a Sorted Set within the score boundaries O(log (N) * M)
  • ZINCRBY: Increases the score of an element in the Sorted Set O(log (N))
  • ZINTERSTORE: Calculates the common elements in the Sorted Sets given by the specified keys, and stores the result in destination Sorted Set O(N*K) + O (M*log (M))
  • ZRANK: Gets the index of the element in the Sorted Set O(log (N))
  • ZSCORE: Returns the score of the member O(1)
  • ZUNIONSTORE: Computes the union of keys in the given Sorted Set and stores the result in the resultant Sorted Set O(N) + O(M log (M))

C# code using Redis Sorted Set Datatype

So this covers the basic usage of Redish Sorted Sets Datatype, in the next blog post I will cover using Hyperloglog Datatype.

For the code please visit
https://github.com/taswar/RedisForNetDevelopers/tree/master/5.RedisList/RedisList

For previous Redis topics

  1. Intro to Redis for .NET Developers
  2. Redis for .NET Developer – Connecting with C#
  3. Redis for .NET Developer – String Datatype
  4. Redis for .NET Developer – String Datatype part 2
  5. Redis for .NET Developer – Hash Datatype
  6. Redis for .NET Developer – List Datatype
  7. Redis for .NET Developer – Redis Sets Datatype
Redis

Redis Sets Datatype are similar in C# world as HashSet, they are an unordered collection used for storing strings. One of the great benefit of Redis Sets is that the operation of add, remove, and testing for existence of items is of constant time of O(1) regardless of the number of items in the Set. An important thing about Sets is when adding the same element multiple times, it will only result in a set having a single copy of the item. In a way, one does not have to check if an item exist or not before adding. The amount of members you can store in a Set is more that 4 billion, thus one can store quite a few things in them. Sets are ideal candidate for set theory related items like unique number of people visiting a site, tagging of items, etc.

Redis Sets Datatype – Operations

  • SADD: Adds one or more elements to the Set O(N).
  • SPOP: Removes and returns a random element from the set O(1).
  • SREM: Removes and returns the specified elements from the set O(N).
  • SCARD: Gets the number of elements in a Set O(1).
  • SDIFF: Gets the list of elements from the first set after subtracting its elements from the other mentioned sets O(N).
  • SDIFFSTORE: Create or override a set, getting the list of elements from the first set after subtracting its elements from the other mentioned sets O(N).
  • SINTER: Gets the common elements in all the sets mentioned O(N * M).
  • SINTERSTORE: Store the elements in a set after intersection of all specified sets similar to SINTER but results are stored in the mentioned set O(N * M).
  • SISMEMBER: Finds if the value is a member of set O(1).
  • SMOVE: Moves members from one set to another set O(1).
  • SRANDMEMBER: Gets one or multiple random members from the set O(N).
  • SUNION: Adds multiple sets O(N).
  • SUNIONSTORE: Adds multiple sets and stores the result in a set O(N).

C# code using Redis Set Datatype

So this covers the basic usage of Redish Set Datatype, in the next blog post I will cover using Sorted Sets Datatype.

For the code please visit
https://github.com/taswar/RedisForNetDevelopers/tree/master/6.RedisSet

For previous Redis topics

  1. Intro to Redis for .NET Developers
  2. Redis for .NET Developer – Connecting with C#
  3. Redis for .NET Developer – String Datatype
  4. Redis for .NET Developer – String Datatype part 2
  5. Redis for .NET Developer – Hash Datatype
  6. Redis for .NET Developer – List Datatype
golang

Below is a VSCode with Golang video on mine on channel9 that I recorded at Channel 9 Studio, Redmond at Microsoft Campus (Seattle).
Its a short introduction of using VSCode with Go Lang and how to debug in go lang using VSCode.

Channel9 Video

Taswar @ Channel9 Studio

Hope you enjoy.
Taswar

jwt-token

Json Web Token JWT have performance overhead?

I wanted to find out if using jwt token during authentication of a web application creates any performance issue.

But what are Jwt Tokens you may ask?

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA.
jwt.io

Additionally Jwt tokens are:

  1. Compact: Small size means transmission is fast, JWT can be sent through a URL, POST parameter, or inside an HTTP header. Note usually JWT have a size limit of 7k to 8k in size.
  2. Self-contained: The payload contains all the required information about the user, and avoids the need to query the database more than once

Task with this I wrote a sample C# console application to find out if different size of keys made any significant performance issue when validating a jwt token.
Below is the sample code I wrote to test this out.

Here are some results of the first test run

jwt-test-run1

jwt-test-run1

Test Run 2

jwt-test-run2

jwt-test-run2

Test Run 3

jwt-test-run3

jwt-test-run3

Conclusion

As expected the larger key generated does create a slightly larger time to verify the JWT Token but using C# and .NET 4.6, I found the time to verify was insignificant for a web application, when using a small key size of 2048 key size and 256 Sha algorithm. Having Jwt verification to secure your web application outpays the time it takes to verify it, I have not put in any network latency in this application, but probably those would be more significant than JWT verification.

VsCode-with-Python-Django

Visual Studio Code with Python Django

I wanted to continue on my python exploration and show how to get started with Visual Studio Code with Python Django.
What is Django you may ask ? Not the movie for sure.

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
https://www.djangoproject.com/

Basically you can think of Django somewhat like WebApi plus Entity Framework together, since Django provides an ORM with the framework.

First thing first, in python we should set up our virtualenv such that we have a dev environment and don’t pollute our global space with our packages. You should already have pip install with your python if you are using the Python 3.x series.

Open up your powershell or command prompt and type

Once you have virtualenv we will need virtualenvwrapper-win since we are running under windows.

Below is a sample gif of the interaction.
Install virtualenvwrapper-win

Now we need to create the virtualenv that we will be using.

This will create your virtualenv inside your home directory for mine it is under

In order to activate the virtualenv we need to use the activate.ps1 script but you must make sure we have the correct execution-policy in your powershell but before we do that lets launch our Vscode

Below is an example of using powershell to do the above action

djangoProjectCode

django project in powershell

You may want to set your terminal correctly in vscode like below.

djangoProjectCodeTerminal

djangoProjectCodeTerminal

Lets now set up our directory to use the virtualenv, remember the Set-ExecutionPolicy for powershell and use the terminal inside your vscode with Ctrl+Shift+` (right tick)

Lets now install Django

Lets now create a our first Django project.

Note: That we in the root folder manage.py file inside the HelloWorld folder there is another HelloWorld project which contains.

  1. settings.py: Setting is the configuration for this Django project. The file contains all apps, database settings information.
  2. urls.py: The url declarations for this Django project, kind of like the url routes you have in WebApi in global.ascx.cs file, which can list all your urls.
  3. wsgi.py: This is the entry-point for WSGI-compatible web servers to serve the project.
  4. __init__.py: Is an empty file, to signal to Python that this is a Python package.

The image below shows the process of above commands in VSCode.

djangoProjectCodDjango

Setting up Django in VSCode

Running Django

In order to run our django application we can type

This will start the django server.

VsCode-with-Python-Django-1

VsCode-with-Python-Django-1

Creating our HelloWorld App

Now that we have django up and running we need to create our first app. Hmm have we not create the app yet? Well in Django what we have done so far is we have created a project for Django but we have not created an App yet.
What is the difference between an App and Project then?

What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.
https://docs.djangoproject.com/en/1.11/intro/tutorial01/

Lets get started to create an HelloWorldApp

Lets now edit our settings.py under HelloWorld such that we can add the HelloWorldApp into it. Open settings.py in VSCode, and look for INSTALLED_APPS, append our HelloWorldApp to the list (Note the command at the end)

Now let’s modify the urls.py under the HelloWorld project directory.

Now lets modify views.py under HelloWorldApp app directory

We can now run our app

When everything is up and running you should be able to go to Chrome or your favorite browser and view port 8000 and see Hello world there.

VsCode-with-Python-Django-2

VsCode-with-Python-Django-2

Hope this helps you in setting up vscode with development of Python Django.

To learn about running VSCode with python read my previous post on
Getting Started with Visual Studio Code with Python.

Also my post on Flask
Getting Started with Visual Studio Code with Python

UA-4524639-2