Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing Applications of trees in data structures Binary Tree Implementation Using Arrays Convert a Binary Tree into a Binary Search Tree Create a binary search tree Horizontal and Vertical Scaling Invert binary tree LCA of binary tree Linked List Representation of Binary Tree Optimal binary search tree in DSA Serialize and Deserialize a Binary Tree Tree terminology in Data structures Vertical Order Traversal of Binary Tree What is a Height-Balanced Tree in Data Structure Convert binary tree to a doubly linked list Fundamental of Algorithms Introduction and Implementation of Bloom Filter Optimal binary search tree using dynamic programming Right side view of binary tree Symmetric binary tree Trim a binary search tree What is a Sparse Matrix in Data Structure What is a Tree in Terms of a Graph What is the Use of Segment Trees in Data Structure What Should We Learn First Trees or Graphs in Data Structures All About Minimum Cost Spanning Trees in Data Structure Convert Binary Tree into a Threaded Binary Tree Difference between Structured and Object-Oriented Analysis FLEX (Fast Lexical Analyzer Generator) Object-Oriented Analysis and Design Sum of Nodes in a Binary Tree What are the types of Trees in Data Structure What is a 2-3 Tree in Data Structure What is a Spanning Tree in Data Structure What is an AVL Tree in Data Structure Given a Binary Tree, Check if it's balanced B Tree in Data Structure Convert Sorted List to Binary Search Tree Flattening a Linked List Given a Perfect Binary Tree, Reverse Alternate Levels Left View of Binary Tree What are Forest Trees in Data Structure Compare Balanced Binary Tree and Complete Binary Tree Diameter of a Binary Tree Given a Binary Tree Check the Zig Zag Traversal Given a Binary Tree Print the Shortest Path Given a Binary Tree Return All Root To Leaf Paths Given a Binary Tree Swap Nodes at K Height Given a Binary Tree Find Its Minimum Depth Given a Binary Tree Print the Pre Order Traversal in Recursive Given a Generate all Structurally Unique Binary Search Trees Perfect Binary Tree Threaded Binary Trees Function to Create a Copy of Binary Search Tree Function to Delete a Leaf Node from a Binary Tree Function to Insert a Node in a Binary Search Tree Given Two Binary Trees, Check if it is Symmetric A Full Binary Tree with n Nodes Applications of Different Linked Lists in Data Structure B+ Tree in Data Structure Construction of B tree in Data Structure Difference between B-tree and Binary Tree Finding Rank in a Binary Search Tree Finding the Maximum Element in a Binary Tree Finding the Minimum and Maximum Value of a Binary Tree Finding the Sum of All Paths in a Binary Tree Time Complexity of Selection Sort in Data Structure How to get Better in Data Structures and Algorithms Binary Tree Leaf Nodes Classification of Data Structure Difference between Static and Dynamic Data Structure Find the Union and Intersection of the Binary Search Tree Find the Vertical Next in a Binary Tree Finding a Deadlock in a Binary Search Tree Finding all Node of k Distance in a Binary Tree Finding Diagonal Sum in a Binary Tree Finding Diagonal Traversal of The Binary Tree Finding In-Order Successor Binary Tree Finding the gcd of Each Sibling of the Binary Tree Greedy Algorithm in Data Structure How to Calculate Space Complexity in Data Structure How to find missing numbers in an Array Kth Ancestor Node of Binary Tree Minimum Depth Binary Tree Mirror Binary Tree in Data Structure Red-Black Tree Insertion Binary Tree to Mirror Image in Data Structure Calculating the Height of a Binary Search Tree in Data Structure Characteristics of Binary Tree in Data Structure Create a Complete Binary Tree from its Linked List Field in Tree Data Structure Find a Specified Element in a binary Search Tree Find Descendant in Tree Data Structure Find Siblings in a Binary Tree Given as an Array Find the Height of a Node in a Binary Tree Find the Second-Largest Element in a Binary Tree Find the Successor Predecessor of a Binary Search Tree Forest of a Tree in Data Structure In Order Traversal of Threaded Binary Tree Introduction to Huffman Coding Limitations of a Binary Search Tree Link State Routing Algorithm in Data Structure Map Reduce Algorithm for Binary Search Tree in Data Structure Non-Binary Tree in Data Structure Quadratic Probing Example in Hashing Scope and Lifetime of Variables in Data Structure Separate Chaining in Data Structure What is Dynamic Data Structure Separate Chaining vs Open Addressing Time and Space Complexity of Linear Data Structures Abstract Data Types in Data Structures Binary Tree to Single Linked List Count the Number of Nodes in the Binary Tree Count Total No. of Ancestors in a Binary Search Tree Elements of Dynamic Programming in Data Structures Find cost of tree with prims algorithm in data structures Find Preorder Successor in a Threaded Binary Tree Find Prime Nodes Sum Count in Non-Binary Tree Find the Right Sibling of a Binary Tree with Parent Pointers Find the Width of the Binary Search Tree Forest trees in Data Structures Free Tree in Data Structures Frequently asked questions in Tree Data Structures Infix, Postfix and Prefix Conversion Time Complexity of Fibonacci Series What is Weighted Graph in Data Structure Disjoint Set ADT in Data Structure Binary Tree to Segment Tree Binary Tree to List in Order State Space Tree for 4 Queen Problem Hash Function Booth Algorithm Flowchart What is an Algorithm Master Theorem Top-down and Bottom-up Approach in Data Structure Base Address of Array Insertion into a Max Heap in Data Structure Delete a Node From Linked List Find Loop in Linked List How to implement Forward DNS Lookup Cache? How to Implement Reverse DNS Look Up Cache? Primitive vs non primitive data structure Concurrency Algorithms GCD Using Recursion Priority Queue using Linked List Difference between Array and Structure Conversion of Binary to Gray Code

How to implement Forward DNS Lookup Cache?

Introduction

The DNS (Domain Name System) is a crucial component of the Internet infrastructure that translates human-readable domain names into machine-readable IP addresses. When you type a website URL into your browser, the DNS resolver on your device performs a DNS lookup to obtain the corresponding IP address for that domain name. However, repeatedly performing DNS lookups for the same domain name can result in unnecessary overhead and slow response issues. To address this problem, implementing a forward DNS lookup cache can significantly increase the collectability of DNS resolution and improve overall Internet browsing performance.

What is Forward DNS Lookup Cache

The forward DNS lookup cache is a local storage process that stores the results of DNS queries performed by DNS resolvers. When a device requests an IP address for a domain name, the DNS resolver checks the cache to see if it already has the necessary information. If the IP address of the domain name is found in the cache, the resolver can return results directly without contacting the DNS server. This caching process reduces pre-preparation for repetitive DNS elements and provides a better overall response for Internet browsing.

Steps to implement Forward DNS Lookup Cache

There are a few crucial steps involved in implementing a forward DNS lookup cache

Design of the cache data structure

Determine the appropriate data structure for storing DNS lookup results in the cache. A common choice is a key-value store, where the domain name is the key, and the associated IP address is the value. Each cache's time to live (TTL) may also be stored for the cache entry so that the caching data is valid for a specified time.

Setting the Cache Timeout

Set the appropriate TTL for cache entries. The TTL determines the length of time that DNS records should be kept in the cache until they expire. A balance must be struck between the benefits of caching and ensuring DNS records reflect changes quickly.

Cache miss

 When a DNS query does not yield a result (i.e., a cache miss occurs), the DNS resolver should proceed with a standard DNS lookup, contacting primary DNS servers. Upon receiving the IP addresses, the resolver should store the result in a cache for future use.

Clearning and Flushing DNS Cache

Sometimes, it may be necessary to clear the DNS cache to carry the most recent information. The method to clear the cache depends on the operating system or DNS server.

a. For Windows

For example, on Windows, you can automatically clean up the DNS cache using the "ipconfig" command

ipconfig /flushdns

This command will require administrator (root) rights, prompting you to enter your user password. Once you enter the password, the DNS cache will be cleared, and any cached DNS records will be removed.

b. For Linux-based Systems

Open Terminal and run the following command with administrator privileges

sudo systemd-resolve --flush-caches

c. For macOS

Open Terminal and run the following command with administrator privileges

sudo dscacheutil -flushcache

Cache size management

Implement a mechanism to manage the size of the cache so that it does not grow infinitely and consume excess memory. Cache entries can be removed based on LRU (least most minor use) or LFU (least excess use) policies, ensuring that the most appropriate and frequently used entries remain in the cache.

Thread safety

If the DNS resolver is used by multiple processes or threads concurrently, ensure that the cache application is thread-safe to avoid race conditions and data corruption.

Refreshing Cache Entries

Regularly check and refresh cache entries that are nearing their TTL expiration time. DNS information is cross-verified in this process with the authoritative DNS servers, and the cache is updated with the latest information.

Simple example in Python to how you can use a hash table for the Forward DNS Lookup Cache

import time

class DNSCache:

    def __init__(self):

        self.cache = {}  # The hash table to store DNS records

        self.ttl = 300   # Default Time-to-Live (5 minutes)

    def resolve_dns(self, domain):

        if domain in self.cache:

            dns_record, timestamp, ttl = self.cache[domain]

            if time.time() - timestamp <= ttl:

                return dns_record

        # If the domain is not found in the cache or the record is expired,

        # perform a DNS resolution here and obtain the dns_record.

        # For demonstration purposes, let's assume we obtain the dns_record somehow.

        dns_record = "192.168.1.1"

        # Store the dns_record in the cache along with the current timestamp and TTL.

        self.cache[domain] = (dns_record, time.time(), self.ttl)

        return dns_record

# Example usage:

cache = DNSCache()

print(cache.resolve_dns("example.com"))  # This will perform DNS resolution and store the result in the cache.

print(cache.resolve_dns("example.com"))  # This will use the cached DNS record without performing a new resolution.

Java implementation for Forward DNS Look Up Cache

import java.util.HashMap;

import java.util.Map;

class TrieNode {

    boolean isLeaf;

    String ipAddress;

    Map<Character, TrieNode> children;

    public TrieNode() {

        isLeaf = false;

        ipAddress = null;

        children = new HashMap<>();

    }

}

public class DNSCache {

    // Helper Functions:

    // get_index: This function takes a character c and returns the index corresponding to that character.

    static int getIndex(char c) {

        return c == '.' ? 26 : c - 'a';

    }

    // get_char_from_index: This function takes an index i and returns the character corresponding to that index.

    static char getCharFromIndex(int i) {

        return i == 26 ? '.' : (char) ('a' + i);

    }

    // insert Function: Used to insert a URL and its corresponding IP address into the Trie data structure.

    static void insert(TrieNode root, String url, String ipAddress) {

        TrieNode node = root;

        for (char c : url.toCharArray()) {

            int index = getIndex(c);

            node.children.putIfAbsent(c, new TrieNode());

            node = node.children.get(c);

        }

        node.isLeaf = true;

        node.ipAddress = ipAddress;

    }

    // search_dns_cache Function: Performs a forward DNS lookup on the Trie data structure.

    static String search_dns_cache(TrieNode root, String url) {

        TrieNode node = root;

        for (char c : url.toCharArray()) {

            int index = getIndex(c);

            if (!node.children.containsKey(c)) {

                return null;

            }

            node = node.children.get(c);

        }

        return node.isLeaf ? node.ipAddress : null;

    }

    public static void main(String[] args) {

        // Sample URLs and their corresponding IP addresses

        String[] urls = {"www.google.com", "www.facebook.com", "www.youtube.com", "www.yahoo.com"};

        String[] ipAddresses = {"172.217.166.68", "157.240.218.35", "78.155.223.109", "150.99.125.46"};

        // Create the root of the Trie

        TrieNode root = new TrieNode();

        // Insert each URL and its corresponding IP address into the Trie

        for (int i = 0; i < urls.length; i++) {

            insert(root, urls[i], ipAddresses[i]);

        }

        // Perform forward DNS lookups for two sample URLs

        String url1 = "www.facebook.com";

        String resIp1 = search_dns_cache(root, url1);

        System.out.println("Performing Forward lookup in the DNS cache...");

        if (resIp1 != null) {

            System.out.println("Forward DNS cache lookup Successful!\nHostname: " + url1 + " --> IP address: " + resIp1);

        } else {

            System.out.println("Forward DNS cache lookup Unsuccessful! Hostname not present in DNS cache.");

        }

        System.out.println();

        String url2 = "www.gmail.com";

        String resIp2 = search_dns_cache(root, url2);

        System.out.println("Performing Forward lookup in the DNS cache...");

        if (resIp2 != null) {

            System.out.println("Forward DNS cache lookup Successful!\nHostname: " + url2 + " --> IP address: " + resIp2);

        } else {

            System.out.println("Forward DNS cache lookup Unsuccessful! Hostname not present in DNS cache.");

        }

    }

}
How to implement Forward DNS Lookup Cache

C++ implementation for Forward DNS Look Up Cache

#include <iostream>

#include <unordered_map>

using namespace std;

// TrieNode Class: Represents a node in the Trie data structure

class TrieNode {

public:

    bool isLeaf;

    string ipAddress;

    unordered_map<char, TrieNode*> children;

    TrieNode() {

        isLeaf = false;

        ipAddress = "";

    }

};

// Helper Functions:

// get_index: This function takes a character c and returns the index corresponding to that character.

int get_index(char c) {

    return c == '.' ? 26 : c - 'a';

}

// get_char_from_index: This function takes an index i and returns the character corresponding to that index.

char get_char_from_index(int i) {

    return i == 26 ? '.' : 'a' + i;

}

// insert Function: Used to insert a URL and its corresponding IP address into the Trie data structure.

void insert(TrieNode* root, string url, string ip_address) {

    TrieNode* node = root;

    for (char c : url) {

        int index = get_index(c);

        if (node->children.find(c) == node->children.end()) {

            node->children[c] = new TrieNode();

        }

        node = node->children[c];

    }

    node->isLeaf = true;

    node->ipAddress = ip_address;

}

// search_dns_cache Function: Performs a forward DNS lookup on the Trie data structure.

string search_dns_cache(TrieNode* root, string url) {

    TrieNode* node = root;

    for (char c : url) {

        int index = get_index(c);

        if (node->children.find(c) == node->children.end()) {

            return "";

        }

        node = node->children[c];

    }

    return node->isLeaf ? node->ipAddress : "";

}

int main() {

    // Sample URLs and their corresponding IP addresses

    string urls[] = {"www.google.com", "www.facebook.com", "www.youtube.com", "www.yahoo.com"};

    string ip_addresses[] = {"172.217.166.68", "157.240.218.35", "78.155.223.109", "150.99.125.46"};

    // Create the root of the Trie

    TrieNode* root = new TrieNode();

    // Insert each URL and its corresponding IP address into the Trie

    int n = sizeof(urls) / sizeof(urls[0]);

    for (int i = 0; i < n; i++) {

        insert(root, urls[i], ip_addresses[i]);

    }

    // Perform forward DNS lookups for two sample URLs

    string url1 = "www.facebook.com";

    string res_ip1 = search_dns_cache(root, url1);

    cout << "Performing Forward lookup in the DNS cache..." << endl;

    if (!res_ip1.empty()) {

        cout << "Forward DNS cache lookup Successful!\nHostname: " << url1 << " --> IP address: " << res_ip1 << endl;

    } else {

        cout << "Forward DNS cache lookup Unsuccessful! Hostname not present in DNS cache." << endl;

    }

    cout << endl;

    string url2 = "www.gmail.com";

    string res_ip2 = search_dns_cache(root, url2);

    cout << "Performing Forward lookup in the DNS cache..." << endl;

    if (!res_ip2.empty()) {

        cout << "Forward DNS cache lookup Successful!\nHostname: " << url2 << " --> IP address: " << res_ip2 << endl;

    } else {

        cout << "Forward DNS cache lookup Unsuccessful! Hostname not present in DNS cache." << endl;

    }

    // Free the allocated memory for Trie nodes

    delete root;

    return 0;

}
How to implement Forward DNS Lookup Cache

Python implementation for Forward DNS Look Up Cache

The below-mentioned code is a Python based program on how to implement forward DNS look up cache.

# TrieNode Class: Represents a node in the Trie data structure

class TrieNode:

    def __init__(self):

        # is_leaf: A boolean flag indicating if the node represents the end of a URL (leaf node).

        self.is_leaf = False

        # ip_address: A string representing the IP address associated with the URL (only valid for leaf nodes).

        self.ip_address = None

        # children: A dictionary to store child nodes of the current node.

        # The keys of the dictionary are indices corresponding to characters ('a' to 'z' and '.').

        self.children = {}

# Helper Functions:

# get_index: This function takes a character c and returns the index corresponding to that character.

def get_index(c):

    return 26 if c == '.' else ord(c) - ord('a')

# get_char_from_index: This function takes an index i and returns the character corresponding to that index.

def get_char_from_index(i):

    return '.' if i == 26 else chr(ord('a') + i)

# insert Function: Used to insert a URL and its corresponding IP address into the Trie data structure.

def insert(root, url, ip_address):

    node = root

    for char in url:

        index = get_index(char)

        if index not in node.children:

            node.children[index] = TrieNode()

        node = node.children[index]

    node.is_leaf = True

    node.ip_address = ip_address

# search_dns_cache Function: Performs a forward DNS lookup on the Trie data structure.

def search_dns_cache(root, url):

    node = root

    for char in url:

        index = get_index(char)

        if index not in node.children:

            return None

        node = node.children[index]

    return node.ip_address if node.is_leaf else None

if __name__ == "__main__":

    # Sample URLs and their corresponding IP addresses

    urls = ["www.google.com", "www.facebook.com", "www.youtube.com", "www.yahoo.com"]

    ip_addresses = ["172.217.166.68", "157.240.218.35", "78.155.223.109", "150.99.125.46"]

    # Create the root of the Trie

    root = TrieNode()

    # Insert each URL and its corresponding IP address into the Trie

    for url, ip_address in zip(urls, ip_addresses):

        insert(root, url, ip_address)

    # Perform forward DNS lookups for two sample URLs

    url1 = "www.facebook.com"

    res_ip1 = search_dns_cache(root, url1)

    print("Performing Forward lookup in the DNS cache...")

    if res_ip1:

        print(f"Forward DNS cache lookup Successful!\nHostname: {url1} --> IP address: {res_ip1}")

    else:

        print("Forward DNS cache lookup Unsuccessful! Hostname not present in DNS cache.")

    print("\n")

    url2 = "www.gmail.com"

    res_ip2 = search_dns_cache(root, url2)

    print("Performing Forward lookup in the DNS cache...")

    if res_ip2:

        print(f"Forward DNS cache lookup Successful!\nHostname: {url2} --> IP address: {res_ip2}")

    else:

        print("Forward DNS cache lookup Unsuccessful! Hostname not present in DNS cache.")
How to implement Forward DNS Lookup Cache

Advanatge of Forward DNS Lookup Cache

Low latency: By caching DNS lookup results, IP addresses can be retrieved locally quickly for subsequent queries for the same domain name, reducing latency.

Improved performance: Faster DNS resolution improves overall Internet browsing performance and responsiveness.

Network load reduction: Reduces traffic and load on the DNS infrastructure by sending fewer queries to external DNS servers.

Reliability: If the DNS servers have a temporary setup or downtime, the cached DNS data can be used to access websites reliably.

Improvement of User Experience: Faster DNS resolution enables faster access times for website loading, thereby improving user experience.

Note that this example code is a simplified application to illustrate and does not perform an actual DNS lookup because of security restrictions in the browser environment prohibiting DNS lookup calls in the browser environment.

Conclusion

In conclusion, Implementing a forward DNS lookup cache is an effective way to optimize the portability of DNS resolution and improve Internet browsing performance. By storing DNS lookup results locally, devices can obtain IP addresses faster for subsequent queries, thereby reducing latency and network load. Ensuring proper management of cache size, expiration time, and cache refresh is a valuable technology that helps improve the overall responsiveness and user experience of Internet applications and services.