Thursday 31 January 2019

Redis: Advanced data structures

Whenever we think of Cache, we think of Redis Cache unless you are exclusively working with an In-Memory cache. However, many a times, we continue to look at Redis as a store of Key-Value pair. That is a mistake!!

Imagine you have a situation where you have to keep information about members of a group and your app can support dynamic generation of group - how would you store it?

Option 1:

Keep it like a Key-Value pair. E.g.

Key: {GroupName}
Value: {MemberName1}{Delimiter}{MemberName2}{Delimiter}{MemberName3}....

Here delimiter can be of your choice like ",".

It will work fine for you. Redis documentation says that maximum size of a value can be 512MB.

However, now imagine you need to add and remove members from this value. You would end up writing code that will fetch the value, manipulate it and save it again. 2 trips to Redis and some CPU cycles.

Option 2:

Keep it in Set.

SetName: {GroupName}

You can simply add and remove items from it. There is even a single command to list all members of a set.

Now this is handy and faster than Option 1.

Similarly there are multiple advanced data structures built into Redis which can be leveraged in specific situations. Better to use it :).

Tuesday 8 January 2019

Accessing website running on Ubuntu VM from Windows Host

So this might appear simple, but it does require a bit of attention and work. 

First of all, let us create the Ubuntu VM on Hyper-V using the "Quick Create" option. You can follow the details from here

Once you are through, you will have an Ubuntu VM running on Hyper-V with Enhanced Session mode which is actually quite nicer than doing all of the hacks on your own to get a better Remote Desktop experience. 

The network switch configured for the VM will be "Default Switch". Read more about it here This means that you should be able to ping the VM machine from Host (and vice-versa) using a domain name "mshome.net".


Once you are done, you can host/run a web application on the Ubuntu VM. I used VSCode to create a .NET Core MVC Web application and ran it. I configured the application to use "HostName" of the VM during startup so that .NETCore understands and allows the HostName for accessing the site - by default .NET core's web server does not allow other host names unless specified.


You should be able to access the web application using both the URLs on the VM. Right now, if you attempt to access the application from host VM, it will fail.

Configure Firewall

We need to configure firewall on the Ubuntu VM. I installed UFW application on the VM and allowed 5002 port. Here is the tutorial to achieve the same.

Once it is done, the application should be accessible using "VMName.mshome.net" host name.


Hope it helps.