Observability Done Right: Best Practices and Anti-Patterns for Effective System Monitoring

Image
  WHAT Observability is a concept that refers to the ability to gain insights into the behavior and performance of complex systems. In the context of software engineering, observability involves the collection, analysis, and visualization of data from software applications, infrastructure, and other components of a system. In the animal kingdom, observability plays a critical role in survival, allowing animals to monitor their surroundings, detect threats, and find food. Dolphins use echolocation to observe their surroundings. They emit high-frequency sounds that bounce off objects, allowing them to create a 3D map of their environment. Thanks for reading Knowledge Cafe! Subscribe for free to receive new posts and support my work. Subscribed WHY In today's era, architectures are becoming increasingly large, complex, and fast-paced due to the faster development and deployment of software by distributed teams with the help of DevOps, continuous delivery, and agile development methodo...

WeakHashMap in Java

A WeakHashMap is a special Map implementation where the keys of the map are stored in a java.lang.ref.WeakReference. By storing the keys in a weak reference, key-value pairs can dynamically be dropped from the map when the only reference to the key is from the weak reference. This makes the WeakHashMap an excellent implementation for a weakly referenced list, where entries that aren't used elsewhere may be dropped with no side effects. Also, just because a key may be dropped, doesn't mean it will immediately be dropped. If the system has sufficient resources, the weak key reference that isn't externally referenced could stay around for a long time.

This class provides the easiest way to harness the power of weak references. It is useful for implementing "registry-like" data structures, where the utility of an entry vanishes when its key is no longer reachable by any thread.

The WeakHashMap functions identically to the HashMap with one very important exception: if the Java memory manager no longer has a strong reference to the object specified as a key, then the entry in the map will be removed.

Weak Reference: If the only references to an object are weak references, the garbage collector can reclaim the object's memory at any time.it doesn't have to wait until the system runs out of memory. Usually, it will be freed the next time the garbage collector runs.

WeakHashMap can also be used fairly straightforwardly as a cache that can be cleared automatically when JVM memory is low. The only difference is in needing the keys to be objects which can be equal to other keys of the same class. This is most easily explained with some examples:

  • Unique keys. If the keys were of the Object class, each key would be unique and could not evaluate equal to another key. Consequently, once you no longer retained a reference to the Object key, there would be no way of re-accessing values, short of enumerating all keys or values (this is true for any Map). Since a cache normally works by allowing values to be re-accessed, this would be a severe problem.

  • Equal Keys. If the keys are objects like Strings or Integers, then any particular key value could be reaccessed by creating a new key which evaluates as equal (according to the equal() method for that class) to the original key. This allows the original key to have no further strong references to it, thus enabling it to be sucessfully used as a WeakHashMap key.


Lets understand the behavior using program.
package in.softcare;

import java.util.*;

public class WeakHashMapDemo {

private static Map map;

public static void main(String args[]) {
map = new WeakHashMap();
map.put(new String("Amit"), "Himani");

Runnable runner = new Runnable() {
public void run() {
while (map.containsKey("Amit")) {
try {
System.out.println("Refernece Found-Sleeping");
Thread.sleep(500);
} catch (InterruptedException ignored) {
}
System.out.println("Thread Ending-Live Reference gone ");
System.gc();
}
}
};
Thread t = new Thread(runner);
t.start();
System.out.println("Main waiting");
try {
t.join();
} catch (InterruptedException ignored) {
}
}
}

Output:
Main waiting
Refernece Found-Sleeping
Thread Ending-Live Reference gone

If you do not include the call to System.gc(), the system may never run the garbage collector as not much memory is used by the program. For a more active program, the call would be unnecessary.

Popular posts from this blog

Chain of responsibility using Spring @Autowired List

Iterate Through a HashMap

Under the Hood: Understanding the Gossip Protocol in Apache Cassandra