Remote config in MicroServices

We are in a world of microservices, an application typically uses one or more infrastructure and 3rd party services. Examples of infrastructure services include: multiple environments, cloud configurations, a message broker and a database server.Examples of 3rd party services include: payment processing, email and messaging, etc.Although every microservice instance is an independent unit, it is recommended an approach to externalized their configurations management from one central service/location.

The concept of using configuration server inside microservices architecture is visualized on my design below.It is based on basic Spring boot implementation, further, different layers and orchestration can be added to enhance it.

Feel free to contact me if you want to discuss this in detail.

RemoteConfig

 

How to achieve parallel code execution without user Threads

What comes to your mind when you have to run a piece of code in parallel? you will create some threads to do it, correct! But this is not only a way to achieve it. Recently I came across one interesting requirement where I had to run a piece of code of an API for three parallel calls (without Threads).My approach is not something new, many of you might be doing it already and others might have not noticed this approach while maintaining the legacy code 🙂 .

Well, so this approach is nothing but a combination of Bean loading in Spring container and corresponding mapped Java class execution, and hooking a API to support parallelism. when spring container loads a annotated/configured Bean it executes Java method associated with it. using this concept we can define and load the same bean with three different ids to execute the method ( the same code runnable implementation ) code rather than doing it through Three different threads.


<beans profile="cluster">
<bean id="mongoQueryServList" class="com.skilledminds.thirdPartyService.configure.db.connectionProvider.cacheQueryProcessor"/>
</beans>
<beans profile="cluster">
<bean id="cassandraQueryServList" class="com.skilledminds.thirdPartyService.configure.db.connectionProvider.cacheQueryProcessor"/>
</beans>
<beans profile="no_cluster">
<bean id="graphQueryServList" class="com.skilledminds.thirdPartyService.configure.db.connectionProvider.cacheQueryProcessor"/>
</beans>

Above piece of code is just a replacement of Three threads for  "cacheQueryProcessor" task, now to achieve parallelism/conditional task execution this method should be designed properly to meet the requirement. For more details on parallelism/conditional task execution please refer Spring-Batch Scaling and Parallel Processing or Java executor framework ( which is a base of every Parallelism implementation).

Still you want to take a deep dive of similar implementation then take a look at ongoing Spring Jira https://jira.spring.io/browse/SPR-8767

PS: For an experienced java professional this may look very simple to do but considering this in a bigger picture gives the insight of an option to design an utility tool/Jar without heavy Spring batch or similar APIs.

Effective use of IdentityHashMap and Flyweight Pattern

Imagine a online finance dashboard where a Finance consultant and his Client discuss, do some calculation , presentation, template and query filling like activities, and finally ends session of discussion. Such dashboard can have various tools and repetitive actions and forms which can be used through discussion. Lets take a case of 1000 customers at any time on dashboard, 100 tasks and forms , again 40 conditions for each task and form. So at least
1000*100*40= 4000000 Objects ( or near to it if consider some static design ) and corresponding GC() cycle we have to support to meet our requirement.

By using proper design and data structure we can reduce this problem and can speed up Dashboard loading and tool performance etc… use of Flyweight pattern and IdentityHashMap is perfect combination for use-case like above.

The Flyweight pattern is suitable for a context free and frequently used large object creations.
IdentityHashMap – This class implements the Map interface with a hash table, in an IdentityHashMap, two keys k1 and k2 are considered equal if and only if (k1==k2). (In normal Map implementations (like HashMap) two keys k1 and k2 are considered equal if and only if (k1==null ? k2==null : k1.equals(k2)).)

Here is one sample code that you can refactor for your own similar use-case.


import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map.Entry;

public class HashedIdentityDashBoard {

	public static void main(String[] args) {
		HashMap<String, DashBoard> hm = new HashMap<String, DashBoard>();
		IdentityHashMap<DashBoard, String> idenHM = new IdentityHashMap<DashBoard, String>();

		hm.put("PendingTasks", new PendingTasks());
		hm.put("DailyTasks", new DailyTasks());
		hm.put("EscalatedTasks", new EscalatedTasks());
		// and 100 more repetitive tasks

		// identity map for repetitive tasks and their Templates and Query Forms
		idenHM.put(hm.get("PendingTasks"), "40 Templates and Query form of PendingTasks");
		idenHM.put(hm.get("DailyTasks"), "40 Templates and Query form of DailyTasks");
		idenHM.put(hm.get("EscalatedTasks"), "40 Templates and Query form of EscalatedTasks");

		// only for 1000 users we need 1000*100*40 = 4000000 Objects

		for (Entry<DashBoard, String> entry : idenHM.entrySet()) {
			DashBoard dashBoard = entry.getKey();
			System.out.println("Key : " + dashBoard + " : Value : " + idenHM.get(dashBoard));
		}

	}

}

interface DashBoard {

	public void setTitle(String widgetTitle);

}

abstract class AbstractUserDashBoard implements DashBoard {
	public String dashBoardName;

	abstract public void setTitle(String widgetTitle);

	@Override
	public String toString() {
		return "Title of DashBoard is :" + this.dashBoardName + " and hashCode is " + this.hashCode();
	}
}

class DailyTasks extends AbstractUserDashBoard {

	public DailyTasks() {
		setTitle("DailyTasks");
		System.out.println(this);
	}

	@Override
	public void setTitle(String widgetTitle) {
		this.dashBoardName = widgetTitle;
	}
}

class PendingTasks extends AbstractUserDashBoard {
	public PendingTasks() {
		setTitle("PendingTasks");
		System.out.println(this);
	}

	@Override
	public void setTitle(String widgetTitle) {
		this.dashBoardName = widgetTitle;
	}

}

class EscalatedTasks extends AbstractUserDashBoard {
	public EscalatedTasks() {
		setTitle("EscalatedTasks");
		System.out.println(this);
	}

	@Override
	public void setTitle(String widgetTitle) {
		this.dashBoardName = widgetTitle;
	}

}

Shared queue processing with Pub and Sub Threads

import java.util.Random;

public class PubSubDemo {

	public static void main(String[] args) {
		SharedQ sharedQ = new SharedQ();

		new Thread(new Consumer(sharedQ), "Consumer").start();
		// Delay publishing
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		new Thread(new Producer(sharedQ), "Producer").start();
	}

}

class Producer implements Runnable {
	SharedQ sharedQPro = null;

	public Producer(SharedQ sharedQ) {
		this.sharedQPro = sharedQ;
	}

	@Override
	public void run() {
		for (int i = 0; i < 3; i++) {
			produce();
		}
	}

	public void produce() {
		synchronized (sharedQPro.sharedQueue) {

			for (int i = 0; i < 3; i++) {
				sharedQPro.sharedQueue[i] = (new Random().nextInt(20));
				System.out.println(" ### Produced ### " + sharedQPro.sharedQueue[i]);
			}
			sharedQPro.sharedQueueIsEmpty = false;
			sharedQPro.sharedQueue.notify();
			try {
				sharedQPro.sharedQueue.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

class Consumer implements Runnable {
	SharedQ sharedQCon = null;

	public Consumer(SharedQ sharedQ) {
		this.sharedQCon = sharedQ;
	}

	@Override
	public void run() {
		for (int i = 0; i < 3; i++) {
			consume();
		}
	}

	public void consume() {
		synchronized (sharedQCon.sharedQueue) {
			if (sharedQCon.sharedQueueIsEmpty) {
				try {
					sharedQCon.sharedQueue.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

			System.out.println();

			for (int i = 0; i < 3; i++) {
				System.out.println(" *** Consumed *** " + sharedQCon.sharedQueue[i]);
				sharedQCon.sharedQueue[i] = 0;
			}
			sharedQCon.sharedQueue.notify();
			System.out.println(" --------------------------");
			try {
				sharedQCon.sharedQueue.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}

}

class SharedQ {

	int[] sharedQueue = new int[3];
	boolean sharedQueueIsEmpty = true;

}

/*
Output of pub sub :

 ### Produced ### 1
 ### Produced ### 0
 ### Produced ### 3

 *** Consumed *** 1
 *** Consumed *** 0
 *** Consumed *** 3
 --------------------------
 ### Produced ### 12
 ### Produced ### 0
 ### Produced ### 14

 *** Consumed *** 12
 *** Consumed *** 0
 *** Consumed *** 14
 --------------------------
 ### Produced ### 13
 ### Produced ### 8
 ### Produced ### 17

 *** Consumed *** 13
 *** Consumed *** 8
 *** Consumed *** 17
 --------------------------
*/

What Is Middle Tier Clustering?

Middle tier clustering is just a cluster that is used for service the middle tier in a application. This is popular since many clients may be using middle tier and a lot of heavy load may also be served by middle tier that requires it be to highly available.

Failure of middle tier can cause multiple clients and systems to fail, therefore its one of the approaches to do clustering at the middle tier of a application.

In java world, it is really common to have EJB server clusters that are used by many clients. In general any application that has a business logic that can be shared across multiple client can use a middle tier cluster for high availability.

custom linkedList implementation

Computing systems are concerned with the storage and retrieval of information.For systems to be economical the data must be organized (into data structures) in such a way as to support efficient manipulation (by algorithms).Choosing the wrong algorithms and data structures makes a program slow at best and unmaintainable and insecure at worst.

Lets take an example of purchase order of an e-commerce store. If purchase order implemented as custom linkedlist then we can take benefit of

a) constant-time insertions/deletions from the list
b) maintaining many versions of purchase order without any additional complexity.
c) to insert items in the middle of the list (such as a priority queue)
d) many priority queue based operation
e) In-memory operations

below is one such implementation

 
package com;

import java.util.Iterator;

public class LinkedListDemo {

	public static void main(String[] args) {

		LinkedList lnkls = new LinkedList();
		lnkls.add(11);
		lnkls.add(12);
		lnkls.add(13);
		lnkls.add(14);
		lnkls.add(15);
		lnkls.add(16);
		lnkls.add(17);
		lnkls.add(18);
		System.out.println("Total added items :" + lnkls.size);
		Iterator<LinkedList.Node> it = lnkls.iterator();

		System.out.println("****** Items of LinkedList ******");

		while (it.hasNext()) {
			System.out.println(it.next().objValue);
		}

	}
}

class LinkedList implements Iterable<LinkedList.Node>, Iterator<LinkedList.Node> {

	Node head;
	Node lastNode;
	Node visitorNode;

	int size = 0;
	int visited = 0;

	public LinkedList() {
	}

	public void add(Object o) {
		Node newNode = new Node(o);

		if (this.head == null) {
			this.lastNode = this.head = newNode;
		} else {
			this.lastNode.next = newNode;
			this.lastNode = newNode;
		}
		size++;
		System.out.println("Added " + newNode);
	}

	@Override
	public Iterator<LinkedList.Node> iterator() {
		visitorNode = lastNode = head;
		return this;
	}

	@Override
	public boolean hasNext() {
		if (visited++ < size)
			return true;
		else
			return false;
	}

	@Override
	public Node next() {
		Node visitedNode = lastNode;
		lastNode = lastNode.next;
		return visitedNode;
	}

	class Node {

		Object objValue;
		Node next;

		public Node(Object o) {
			this.objValue = o;
			this.next = null;
		}

		@Override
		public String toString() {
			return objValue.toString();
		}

	}
 // additional operational and supportive APIs for custom linkedlist
}

use Weak reference for high availability of cache and In-Memory DB operations

We should think about using WeakReference whenever we need a reference to an object, but you don’t want that reference to protect the object from the garbage collector. A classic example is a cache that we want to be garbage collected when memory usage gets too high.

Basically a WeakReference will be GC-d by the JVM eagerly, once the referenced object has no hard references to it. A SoftReferenced object on the other hand, will tend to be left about by the garbage collector until it really needs to reclaim the memory.

Suppose there is a Query level cache and it is persisted and attached to a session. If such queries are very high, certainly they will occupy a huge memory and then they need to be claimed ( GC-d ) as soon as not required. For this, Class Query should be implemented or enqueued through WeakReference.