Recent Post

Saturday, 22 September 2018


Following are some factors to consider while designing a cloud application:

Clients previously connected to applications through the enterprise’s local network. So, it was generally pretty small, may be 100,000 employees and some partners accessing the system but now we are talking about millions or 100s of million people accessing the application over internet. The public internet will introduce increased latency, reduced bandwidth and is less reliable. So the scale is wildly different now than it was when we were building services in the past.
Demand and Load on the application was relatively small, stable and fairly predictable. But now the the load depends on adaptability and it’s hard to predict demand and load in the beginning. So that also requires that we design things for scale kind of mindset from the beginning. So that it can scale well as the load increases. And of course it can scale down when the demand decreases to save the cost.
Data Centers generally served a single tenant or enterprise, and are now serving multiple tenants. The multi tenant environment comes with its own set of challenges, such as the “noisy neighbour problem.” The application may be sharing resources, like network or physical disks. Although they are isolated, load from one may potentially impact the other.
Operations managing or the care and feeding of the services can be rather expensive. In the past, we would use people for this and then you had to deal with the things like upgrading the operating system, hardware, networking infrastructure and so on. In this day and age, we’re trying to get more into an automation point of view, where these things are just happening automatically, which makes it much cheaper to run the service.
Scale was handled by increasing the resources in very expensive and specialized hardware (scale up). In the cloud, this is achieved by distributing the load across a larger number of commodity machines with cheap hardware(scale out) and we just assume that failure is going to happen.
Failure When we are building services in the past, we never really consider that the hardware would fail. We might take backups once in a week by assuming that database engine is going to be fine but, In this new world, because we’re on these cheaper, more commodity PCs, it’s more likely that they are going to fail. And so we have to embrace failure when we are designing or architecting these distributed cloud applications, we assume that failure is always possible. Is that in the past If your machine that ran your database server failed, that was usually catastrophic. It meant that none of your clients could go and access your service, they couldn’t look up anything.
Customers wouldn’t be able to make purchases at your service. And so now, it was costing real money to your business. In this new world, the cloud world, we’re thinking that machine loss is an expected thing. It’s actually normal, and even comment for that to happen, especially if you’re running its scale, it’s much more likely that some of the hardware is going to fail on you.
And so if we design and take this into account as we are architecting, then the failure is really no big deal. And we will design our services to be resilient against this and keep running even in the case of failure.
Now there may be some loss of scale, there may be some loss of data, but for the most part everything keeps running. And the more resilient you wanna be against this, the harder it is for you to architect these kinds of applications and develop them and the more costly it tends to be to run. So it’s largely going to be a business decision, first and foremost as to how much to invest. And it may be, that for your company, the service going down for an hour is not that big a deal. And it could be that for company, the service going down for an hour is loss of millions of dollars.

A different approach is required when architecting cost effective and performant applications for the cloud that are resilient to failures.

Exception Handling: In the past, a lot of times people would write their code to catch all exceptions, because if you don’t catch an exception, it becomes unhandled, your service would go down and crash. And that means it was no longer responding to client request that were coming into it. But this is really not the best thing to do in terms of managing state. If an application gets an unhandled exception it usually means that something unexpected happened and the state of the data in the application is potentially in an unpredictable state right now. And continuing to have the service run under these circumstances might mean more data corruption and unpredictable results. So really the best thing to do for an unhandled exception is to have your application terminate, have it destroy any potentially corrupted state in memory. And then restart the application so then it is in a well known
good state and then pick up from there. But in the past, a lot of programmers haven’t wanted to do that, because when the application crashed it stops taking requests from clients for a period of time. However in this new world in the present, now we can really do what is the right thing, which is allow the application to crash and to restart. And the reason why this is okay is because you’re now gonna be running multiple instances of the same service on different machines. We’re embracing this concept of failure, and so if one of the applications on one of the machines crashes, then the client request can still be handled by the other instances on the other machines. And so the clients are still being able to interact with your service successfully.
Communication: When everything is working perfectly, when a client goes and sends a network request, communicates with a server via a network request. Then those messages are sent in order and you can pretty much expect that the messages are gonna be sent exactly one time to the server.
But in this new world where again, we are embracing failure, it’s possible that a client sends a message to a server, the server starts to process the request but then the server crashes. Especially because of the conversation we just
had about exception handling. So that means in order to be resilient to this failure, the client must retry the operation against the server. So, we have to design our applications, our client applications, to expect failure from the server, and to automatically retry communication request. It’s also possible that the server might get the first request from a client, start to process it successfully and then maybe the client receives a timeout for some reason.
And the client may send the request again cuz it’s going to do a retry. This means that the server could get the request multiple times. But, we don’t want the server to process the request multiple times. So, the server has to be designed to handle these requests in an item potent fashion. Meaning, that there’s no ill effect for performing the same operation multiple times. And this also means that the messaging may occur out of order. So we have to be resilient about that when we are designing our applications.
0

Thursday, 26 April 2018

Examples: https://gist.github.com/aditya01933/c6a867e981110885369f06c5a4103644

Topics:

1. 3 pillars of ruby.
2. Classes and objects.
3. Inheritance - diving deep
4. Meta programming and reflection - diving deep.
5. Power of method missing.
6. Mixins and ducktyping
7. Super - diving deep
8. Yield
9. Closure
10. Block, proc and lambda
11. More meta programming(examples).
12. Ruby open classes.



0

3 PILLARS OF RUBY

Following are the 3 pillars of Ruby

  1. Everything is an object.
  2. Every operation is a method call on some object.
  3. Everything is metaprogramming.

1. Everything is an object (Almost)

In ruby everything you see is an object.
Lets take an example:
irb(main):001:0> 2.to_s
=> "2"
irb(main):002:0> 'hello'.to_sym
=> :hello
Here 2 and hello is also an object.
So what is the class of 2 and ‘hello’?
Let’s find out
irb(main):005:0> 2.class
=> Fixnum
irb(main):006:0> ‘hello’.class
=> String
As you can see, even data types are the objects of some pre-defined classes.
What about nil?
Let’s find out
irb(main):009:0> nil.class
=> NilClass
Even nilis an object of class NilClass😅
Why (Almost)?
There are some things that are not purely object in Ruby like keywords like ifalias, and beginendrescue are not object in ruby.
Also, it’s worth noting that everything or every statement evaluates to an object.

2. Every operation is a method call on some object

Every operation you see is a method call on some object. Ruby provides you syntactic sugar to make the statement more intuitive. Let’s take some examples:
The simple statement
irb(main):001:0> 1 + 2
=> 3
can be re-written as
irb(main):001:0> 1.+(2)
=> 3
In this example we are calling + method on Fixnum object and passing 2 as an argument to it.
Some more examples:
1 + 2                    => 1.+(2) or 1.send(:+, 2)
my_array[4]              => my_array.[](4)
                            or my_array.send(:[], 4)
my_array[3] = "foo"      => my_array.[]=(3, ‘foo’)
if (x == 3) ....         => if (x.==(3))
my_func(z)               => self.my_func(z)
                            or self.send(:my_func, z)

3. Everything is metaprogramming.

Metaprogramming is a technique by which you can write code that writescode by itself dynamically at runtime. This means you can define methods and classes during runtime or you can manipulate methods and objects at run time.
The statement that everything is metaprogramming because even the class declaration or object initializations is the type of metaprogramming where you define new objects on existing objects.
Lets take an example:
If you go to the ruby console and run
irb(main):004:0> self.class
=> Object
you will see that self is the object of class Object. Now whatever you will define on console will become the part of Object class.
irb(main):021:0> def print_hello
irb(main):022:1> puts 'hello'
irb(main):023:1> end
=> :print_hello
irb(main):024:0> self.print_hello
hello
=> nil
As you can see, we have defined a method(called as singleton method)print_hello on pre-existing object self of class Object.
The same thing happens when you declare a class, for example:
irb(main):035:0> class Meta
irb(main):036:1> end
=> nil
irb(main):037:0> Meta.class
=> Class
Here, you are declaring a new object Meta of class Class.
That’s why what ever you write in ruby is meta-programming.
Wow!! that means you have been using Metaprogramming from the beginning without even knowing it. 😛

0

Thursday, 13 October 2016




Developers often neglect the core concepts of ruby when they start working on Rails projects.

" A good rails developer may not be a good ruby developer but a good ruby developer is always a good rails developer."

In my journey to be a good ruby developer, I am demonstrating a simple but powerful core ruby concept that can be used in rails or other ruby based projects.

You might have often seen this kind of code pattern in ruby on rails projects:

  class User
      def format_name
          do_something_with name
          do_another_thing_with name
      end

      def name
          @name ||= begin
              User.first.name
          end
      end
  end

In above code name method is called multiple times and that's why we are using memoization.

Following is another way of memoization that is using some core concepts of ruby:

  class User
      def format_name
          do_something_with name
          do_another_thing_with name
      end

      def name
          def self.name
              @name
          end
          @name = User.first.name
       end
  end

Following are the concepts behind the above code:
1. Whenever you are going to call  name method  for first time, It is going to create  Singleton method - name  for declared object.
2. Whenever you will call  Singleton method  for 2nd time, it is going to execute  Singleton - name method instead of main class  name method because  Singleton method  have high preference over main class methods.
3.  Singleton method  have access to all instance variables of an object.

Above methodology can be used when code inside  begin block is complex or large or may be in different situations to simply code.

This way also memoizes `false` and `nil` values as well: 

For example:
  class Foo < Object
      def thing1
          do_something with: expensive_computation1
          do_something_else with: expensive_computation1
      end

      def thing2
          do_something with: expensive_computation2
          do_something_else with: expensive_computation2
      end
  
  private

      def expensive_computation1
          @expensive_computation1||= Model.where(id: 1742).first
      end

      def expensive_computation2
          def self.expensive_computation2
              @expensive_computation2
          end
          @expensive_computation2= Model.where(id: 4217).first
      end
  end

 thing1  and  thing2  both are memoized but  thing2  also memoizes when the record is not found.  thing1   will go hit the db again.

Benchmark comparison of different memoization methods: 


  require "benchmark"

  class A
      def name
        @name ||= begin
                    rand
                  end
      end
   end

  class B
      def name
          return(@name) if defined?(@name)
          @name = rand
       end
  end

  class C
      def name
          def self.name
              @name
          end
          @name = rand
      end
   end

  class D
      def name
          class << self
             def name
                 @name
             end
          end
          @name = rand
      end
  end

  n = 20_000
  n1 = 2_000
  Benchmark.bm(2) do |x|
      x.report("A:") { n.times { k = A.new; n1.times { k.name } } }
      x.report("B:") { n.times { k = B.new; n1.times { k.name } } }
      x.report("C:") { n.times { k = C.new; n1.times { k.name } } }
      x.report("D:") { n.times { k = D.new; n1.times { k.name } } }
  end


Output: 


           user     system      total        real
  A:   3.810000   0.000000   3.810000 (  3.817210)
  B:   4.000000   0.010000   4.010000 (  4.007852)
  C:   2.850000   0.010000   2.860000 (  2.848843)
  D:   2.850000   0.000000   2.850000 (  2.854403)



Although there is not much difference but comparatively 'C' is fastest.
Also it's worth noting that initialisation of singleton method is slower than Boolean initialisation but calling singleton method is much faster and that make it usable.

I hope you would like this new way of memoization. Suggestions to improve the code and article are most welcome. :-)

Special Credit: My friend Brian Giaraffa , Kaushik and Philip Hallstrom :-)
5

Saturday, 1 August 2015

Crazy Material Design Examples


Material Design

Created and designed by Google, Material Design is a design language that combines the classic principles of successful design along with innovation and technology. Google's goal is to develop a system of design that allows for a unified user experience across all their products on any platform.
Material Design makes more liberal use of grid-based layouts, responsive animations and transitions, padding, and depth effects such as lighting and shadows. Designer Matías Duarte explained that, "unlike real paper, our digital material can expand and reform intelligently. Material has physical surfaces and edges. Seams and shadows provide meaning about what you can touch." Google states that their new design language is based on paper and ink.
Material Design will gradually be extended throughout Google's array of web and mobile products, providing a consistent experience across all platforms and applications. Google has also released application programming interfaces (APIs) for third-party developers to incorporate the design language into their applications.



You can read more about material design here

Following are some crazy material design examples you can use in your website.

1. Material Design Responsive Table:

Table design with material design looks awesome. Check out code by clicking below.
 

2. Material Design Buttons

Crazy look for buttons can be achived with material design.



3. Material Design Form

This is my favorite one, Form with material design.
 

4. Material Design Preloader

You can see preloader commonly in many Google apps.


 

5. Bootstrap With Material Design

You can also use bootstrap with material design check out following demo.
0

Sunday, 19 July 2015

Using Google Translate as Proxy Server

During the boring lectures, I always like to watch youtube videos or my social media accounts but my collage have blocked them.  I have seen tons of tutorials on using a cracker and using a proxy etc. But my collage blocks that too. Also, using proxy server is pain in neck as most of them are very slow. Its also not safe to login via proxy server into your social media account. So I found a way to run proxy server by using most trusted and fastest network - Google.

Ok, let's start from the beginning. We all know that Google is more than a search engine; we do use it as provider for email, mapping, news and many other services. Google is now also a free proxy service. Proxy is a device that stands between a PC and the internet, providing all the connections to the world wide web. What a proxy does is to receive all data from a requested site, so when you access web pages all data come from proxy.

Our main purpose is simple. To access blocked websites.

Here is how to use it.

Step 1: 
Open up your browser

Step 2: 


Step 3:
Enter the website URL and Translate from Spanish to English or any other language to english. That's it. Enjoy surfing....


10

Saturday, 18 July 2015

Transfer File between computer and Android using Wifi via Browser

Many people struggle to transfer files between computer and android. Today I will tell you most easy way to Transfer any kind of file and te good news is...No need to install any kind of software in computer. 

IT is only 3 Step Procedure.

Step 1: 
Download and install this Android app from playstore, IT's free....:


Its only 981kb file :)

Step 2:
Open app and Click on Start Service button.
start service wifidroid
Step 3:
Open browser in your computer and go to the url/address shown in android app.
wifidroid browser

That's it ... enjoy file transfer..
3

Followers