Saturday, June 21, 2014

MemoryCache

If you ever need a in-memory cache to be maintained, then use MemoryCache.

It is an in memory object cache which uses a key to identify the item in the cache. From the first look it sounds like a dictionary.

Look at the sample code below :

Code
  1. using System;
  2. using System.Runtime.Caching;
  3.  
  4. namespace MemoryCacheExample
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             var myCache = new MemoryCache("SampleCache");
  11.  
  12.             var cacheItemPolicy = new CacheItemPolicy();
  13.             myCache.Add(new CacheItem(
  14.                             "FOO", new Contact
  15.                                 {
  16.                                     Address = new Address
  17.                                         {
  18.                                             AddressLine1 = "street 1",
  19.                                             AddressLine2 = "Area 1",
  20.                                             City = "Bangalore",
  21.                                             Country = "India",
  22.                                             State = "Kar",
  23.                                             ZipCode = 1243435
  24.                                         },
  25.                                     Id = 34,
  26.                                     Name = "Vikram"
  27.                                 }), cacheItemPolicy);
  28.  
  29.  
  30.             var retrievedItemFromCache = (Contact)myCache.Get("FOO");
  31.  
  32.             PrintContact(retrievedItemFromCache);
  33.         }
  34.  
  35.         private static void PrintContact(Contact contact)
  36.         {
  37.             Console.WriteLine("Name : {0}", contact.Name);
  38.             Console.WriteLine("Id : {0}", contact.Id);
  39.             Console.WriteLine("Address1 : {0}", contact.Address.AddressLine1);
  40.             Console.WriteLine("Address2 : {0}", contact.Address.AddressLine2);
  41.             Console.WriteLine("City : {0}", contact.Address.City);
  42.             Console.WriteLine("State : {0}", contact.Address.State);
  43.             Console.WriteLine("Country : {0}", contact.Address.Country);
  44.         }
  45.     }
  46.  
  47.     public class Contact
  48.     {
  49.  
  50.         public Contact()
  51.         {
  52.             Address = new Address();
  53.         }
  54.         public int Id { get; set; }
  55.         public string Name { get; set; }
  56.         public Address Address { get; set; }
  57.     }
  58.  
  59.     public class Address
  60.     {
  61.         public string AddressLine1 { get; set; }
  62.         public string AddressLine2 { get; set; }
  63.         public string City { get; set; }
  64.         public string State { get; set; }
  65.         public string Country { get; set; }
  66.         public long ZipCode { get; set; }
  67.     }
  68. }


However memory cache provides some advantages.
The MemoryCache has an eviction strategy, so that it can discard objects that are no longer needed or for that you do not have enough memory anymore.

MemoryCache is thread-safe and has methods such as AddOrGetExisting. Doing things yourself in a dictionary or concurrent dictionary is tedious.


Sunday, June 15, 2014

What are Proxy Servers ?

Generally from the client PC it appears, as though, it is directly communicating to the server.












































But, it is not ever so. There is always an intermediary called proxy server.

We all have used encapsulation in the software programming world. In the case of distributed systems, the encapsulation is obtained by placing an encapsulating server in between client and server. This proxy server acts as an intermediary between the client and the other servers.




Uses of proxy server

That is just a bookish definition of proxy server. But a proxy is needed because there are many uses of proxy servers though.

  • A proxy server provides more control for the administrator on the data passing over the network. Using authentication on the proxy server, administrator can control who accesses the data and also restrict access to certain contents. This is usually done in offices, colleges and schools.
  • Logging of data flowing over the network
  • Encryption of data can be done using a SSL, whose certificate/keys can only be placed in proxy server instead of every client on the network. For more on SSL, read my previous blog post.
  • Proxy servers can also improve performance by acting as a cache and only going to server in case of a cache miss.
  • For localization of content, a proxy server can be used.
  • Of-course, a proxy encapsulates, which means that it can guard the client from the outside world. This means that the computers behind the proxy are isolated from the world on the other side of proxy.
In today's world, people use proxy on world wide web to have anonymity on the web.


Based on what the proxy server does, there are types of proxy servers:

Forward Proxy












































This is the one, which "forwards" the traffic from the client to the internet. and retrieve the data based on the that request to the client. These proxies generally reside more closer to client. This is a client side concept.
The forward proxy needs to keep track of which client made which request and correspondingly route the responses from various servers.



Reverse proxy











































A reverse proxy is a server side concept. This usually resides near the server serving content. Server owners have to service request of clients, and most of the times the client are not from your internal network. A reverse proxy acts as an intermediary to these nearby servers and provides the data to client as though the server returned them.


The whole end to end looks like this:





Wednesday, June 11, 2014

Security on the web – How HTTPS works using SSL




Use Case – Credit card Application

Let us consider the use case of the online shopping where credit card information has to be sent over the internet to the server. To begin with let us use plain HTTP.


As shown above, the sensitive data, the credit card information of a person, sent over the internet data is insecure since security is not applied(i.e. HTTPS is not used).
Because of the nature of internet, the data doesn’t directly go to the server, but passes through routers, servers and other computers. This is shown below :



Any person with notorious intent who has access to any one of these intermediate computers can peek into the data being sent and thus can potentially steal the sensitive data. This is shown below :

Another problem that can arise is that if someone spoofs the server website and tricks the user into revealing the data. The user doesn’t know whether he is talking to correct server or not, i.e. who is at other end. Thus the identity of the server is also not confirmed. This is shown below:


How do we secure the data being sent over the internet?

To secure the data sent over the internet, HTTP protocol is not sufficient. We need a new secure protocol which uses cryptography. This protocol was called Secure Sockets Layer a.k.a SSL. The combination of HTTP and SSL is called HTTPS.
This will address the two problems of the insecure HTTP:

Encrypting Insecure Data

Encryption is a mathematical transformation that is applied on a data, to yield a different data which is indecipherable. To achieve this, another string data is applied in the mathematical transformation and this string data is called encryption key.
Decryption is a mathematical transformation that is applied on indecipherable encrypted data, to yield the original data. To achieve this, a string data is also applied in the mathematical transformation and this string data is called decryption key.
This is shown below:



The encryption and decryption may be the same, in which case this is called symmetric cryptography. In SSL, we use something called as asymmetric cryptography which is a.k.a. public key encryption. We use public key encryption because, the encryption key can be shared with all the users, whereas the decryption is private and only server knows it. In this way, we can avoid sharing the key to everyone, thereby only server knows how to decrypt the data.

Trusting Server Identity

The client knows whether the server is whom they expect via the SSL handshake process. When the client makes a TCP connection to the server, the SSL handshake process starts.
During the SSL handshake process, the server sends the client a signed SSL certificate indicating that it is the authentic server. This signed SSL certificate certifies that the server is trustworthy. The signed SSL certificate has to be installed in the server. The company owning the server needs to contact an organization called certificate authority to obtain this.
This process is shown below :

Before creating the certificate, the CA verifies the identity of the company. Only after that it provides a signed certificate. This certificate has to be installed on the server by the company. Once the certificate is installed in the server, the handshake can happen with client. The handshake process is depicted below:


Hello World