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….

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 264 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

Redis

Redis List Datatype are similar in C# world as LinkeList e.g LinkedList. Just like in LinkedList in C#, Redis can store items at Head or Tail of a List. The nice thing about them is that the insertion speed is just O(1), but do note that if you have a large list, the reading speed goes down since it sequentially traverses through the items (i.e walking down the chain). One could also say its an advantage since Redis was made to write faster than read, and LinkedList are great data type for such things. List are ideal candidate for logs, since the write speed is more important than read when dealing with logs.

Redis List Datatype – Operations

  • LPUSH: Prepends the values to the left of the list O(1).
  • RPUSH: Prepends the values to the right of the list O(1).
  • LPUSHX: Prepends the values to the left of the list if key exist O(1).
  • RPUSHX: Prepends the values to the right of the list if key exist O(1).
  • LINSERT: Inserts a value in the list after position calculated from left O(N).
  • LSET: Sets the value of an element in a list based on index O(N).
  • LRANGE: Gets the sub list of elements based on start and end position O(S+E).
  • LTRIM: Deletes the elements outside the range specified O(N), where N is the length.
  • RPOP: Removes the last element O(1).
  • LREM: Removes the element at the index point O(N) , where N is the length.
  • LPOP: Removes the first element of the list O(1).
  • LINDEX: Gets the element from the list based on the index O(N) where N is the length of traverse.
  • LLEN: This command gets the length of the list O(1).
  • RPOPLPUSH: Operates on two lists source and destination list, takes the last element on the source list and push it to the first element of the destination list O(1).

C# code using Redis List Datatype

So this covers the basic usage of Redish List Datatype, in the next blog post I will cover using Sets 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
Redis

Redis Hash Datatype are similar in C# world as Dictionary e.g Dictionary. Just like in C# redis stores map of attributes using key value pair. One thing to note is in Redis a Hash both the field name and the value are strings. Therefore, a Hash Datatype is a mapping of a string to a string.

Memory Optimized

Redis Hashes are memory-optimized. Their optimization is based on the hash-max-ziplist-entries and hash-max-ziplist-value configurations. Internally in Redis, a Hash can be a ziplist or a hash table. So what does it mean for a ziplist? Basically it is designed to be memory efficient dually linked list and integers are stored as real integers rather than a sequence of characters.

Speed

Something to note is a hashtable in Redis has a constant time lookup, the down side is it is not memory optimized, while the ziplist is memory optimized but the lookup time are not constant. To read more on Redis Memory Optimization please view Redis documentation.

So which one is used when?

  • The ziplist is by default when the number of fields do not exceed the configuration ones in hash-max-ziplist-entries
  • The hashtable is used when a the size or any of its values exceeds

Redis Hash Datatype – Operations

  • HSET: Sets the value of a field for a key O(1).
  • HGET: Gets the value of a field for a key O(1).
  • HLEN: Gets the number of fields for the key O(1).
  • HMGET: Gets the values for the fields for a key O(N), where N is the number of fields and O (1) if N is small.
  • HMSET: Sets multiple values for respective fields for a key O(N), where N is the number of fields and O (1) if N is small.
  • HGETALL: Gets all the values and fields for a key O(1).
  • HKEYS: Gets all the fields in the Hash for the key O(1).
  • HEXISTS: Checks for the existence of a field for a key O(1).
  • HVALS: Gets all the values in the Hash for the key O(N), where N is the number of fields and O(1) if N is small.
  • HSETNX: Sets the value against the field for the key provided the field does not exist O(1).
  • HDEL: Deletes the fields for a key O (N), where N is the number of fields and O(1) if N is small.
  • HINCRBY: Increments the value (provided the value is an integer) of a field for a key O(1).
  • HINCRBYFLOAT: Increments the value if value is a float of a field for a key O(1).

C# code using Redis Hash Datatype

For the code please visit
https://github.com/taswar/RedisForNetDevelopers/tree/master/4.RedisHashes

So this covers the basic usage of Redish Hash Datatype, in the next blog post I will cover using List Datatype.

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
Redis

Continuing on with our Redis for .NET Developer, the Redis String Datatype is also used to store integers, float and other utility commands to manipulate strings.

Redis String Datatype using Integers

In Redis float and integers are represented by string. Redis parses the string value as an integer and the neat thing about them is when you do operations on strings they are atomic. When there are multiple clients issuing INCR against the same key, it will never enter into a race condition.

  • INCR key : Increment the value by one O(1)
  • INCRBY key value: Increments the value by the given value (integer) O(1)
  • DECR key: Decrements the value by one O(1)
  • DECRBY key value: Decrements the value by the given value (integer) O(1)
  • APPEND key value: Concatenate the existing integer with the new integer O(1)
  • INCRBYFLOAT key value : Increment the float value by given value (float) O(1)

C# Code using integer and float

For source code please visit
https://github.com/taswar/RedisForNetDevelopers/tree/master/3.RedisStringsAsInt

For our next blog post, I will cover more functionality and other datatype in Redis.

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
UA-4524639-2