Monday, 14 April 2025

"consistency" in CAP vs ACID

 

🧱 1. ACID Consistency (Database Transactions)

Context: ACID is a set of properties that ensure reliable processing of database transactions (especially in traditional relational databases).

  • ACID stands for:

    • Atomicity – All or nothing.

    • Consistency – Maintains database rules.

    • Isolation – Transactions don’t interfere.

    • Durability – Once committed, it stays.

✅ What "Consistency" Means in ACID:

The database must start and end in a valid state, following all integrity rules (e.g., constraints, triggers, foreign keys, etc.).

For example:

  • If you transfer $100 from Account A to B, the total balance should remain the same.

  • If a transaction leaves the database in an invalid state (like negative balance when it’s not allowed), it violates consistency.

🔁 If any step fails, the transaction is rolled back, preserving data integrity.


🌐 2. CAP Consistency (Distributed Systems)

Context: CAP theorem (also called Brewer’s theorem) applies to distributed systems and says you can only guarantee two out of the three:

  • Consistency

  • Availability

  • Partition Tolerance

✅ What "Consistency" Means in CAP:

All nodes see the same data at the same time – like a single, up-to-date view of the data, regardless of which node you query.

Imagine a replicated database:

  • If you write a value x = 5 to one node,

  • A read from any node immediately after should return x = 5.

This is often called "linearizability" or strong consistency.


🔍 Key Differences Between ACID and CAP Consistency:

FeatureACID ConsistencyCAP Consistency
📚 ContextDatabases / TransactionsDistributed systems / networks
🎯 GoalEnsure valid data by enforcing rules (e.g., constraints, referential integrity)Ensure all replicas have the same data at the same time
🧠 Focuses onCorrectness of state before and after transactionSame view of data across nodes
💥 ViolationHappens when data violates rules after a transaction (e.g., null in a NOT NULL column)Happens when nodes return different results after a write
🔄 Rollback?Yes, rollback restores consistencyNo rollback; focus is on how to sync nodes or handle divergence

🧠 Example to Compare:

Imagine a banking app:

ACID Consistency:

  • You must not allow transferring money from an account with insufficient balance.

  • If this happens, the system rolls back.

CAP Consistency:

  • You made a transaction to transfer money.

  • You check balance on one server and it shows updated balance.

  • Your phone (connected to another replica) still shows the old balance.

  • This is a CAP consistency problem, because both replicas are not in sync.


🚨 Common Confusion:

  • ACID consistency is about valid states, often on a single node or database.

  • CAP consistency is about same view of data across multiple nodes in a network.

Wednesday, 2 April 2025

Request Hedging / Cache debouncing

 package com.example.personal;


import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class RequestHedgingCache {
private static final ConcurrentHashMap<String, String> cache = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String, Semaphore> locks = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<String, Integer> dbTracker = new ConcurrentHashMap<>(); // Track which user fetched from DB

public static String fetchBookData(int userID, String bookID) throws InterruptedException {
Semaphore lock = locks.computeIfAbsent(bookID, k -> new Semaphore(1));
System.out.println(lock);
// Try to acquire lock (First user gets access to fetch from DB)
if (lock.tryAcquire()) {
try {
// Double-check cache **after acquiring the lock** to avoid duplicate cache misses
String cachedData = cache.get(bookID);
if (cachedData != null) {
return "User " + userID + " - Cached Response (After Lock Acquisition): " + cachedData;
}

// 🚀 **Only First User Logs This Cache Miss**
System.out.println("🚨 Cache Miss! User " + userID + " is fetching from DB for book ID: " + bookID);

// Simulated DB fetch
String dbData = fetchFromDB(bookID);
cache.put(bookID, dbData); // Update cache
dbTracker.put(bookID, userID); // Track who fetched from DB

return "User " + userID + " - Fetched from DB: " + dbData;
} finally {
System.out.println(" User " + userID + " finished DB fetch. Releasing lock for waiting users.");
lock.release(); // Allow waiting threads to proceed
}
} else {
// Other users immediately wait for cache update **without checking cache first**
System.out.println(" User " + userID + " is waiting for cache update..."+lock);
lock.acquire(); // Block until first user finishes fetching
System.out.println("🔓 User " + userID + " is released. Fetching from cache...");
lock.release(); // Ensure proper semaphore behavior
return "User " + userID + " - Hedged Request (Got Cached Data): " + cache.get(bookID) +
" (Fetched by User " + dbTracker.get(bookID) + ")";
}
}

private static String fetchFromDB(String bookID) throws InterruptedException {
Thread.sleep(1000); // Simulate DB delay
return "BookData-" + bookID;
}

public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(10);
AtomicInteger userID = new AtomicInteger(1);

for (int i = 0; i < 10; i++) {
final String bookID = "12345"; // All requests search for the same book
executor.execute(() -> {
try {
System.out.println(fetchBookData(userID.getAndIncrement(), "12345"));
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}

executor.shutdown();
}
}

Thursday, 30 January 2025

Comparison: Kernel-Space vs User-Space Copying

 

Comparison: Kernel-Space vs User-Space Copying

MethodSystem Calls UsedUser-Space Copying?Zero-Copy?
Files.copy()read() + write()✅ Yes❌ No
ByteBuffer (NIO)read() + write()✅ Yes❌ No
Memory-Mapped File (mmap)mmap() + memcpy() + msync()🚫 No (but may cause page faults)✅ Yes (after mapping)
transferTo() (sendfile)sendfile()🚫 No✅ Yes
transferFrom()sendfile()🚫 No✅ Yes
Buffered Streams (I/O)read() + write()✅ Yes❌ No
Apache FileUtils.copyFile()read()/write() OR sendfile()✅ Yes (if using streams) / 🚫 No (if using sendfile)✅ Possible
InputStream.transferTo()read() + write()✅ Yes❌ No

Friday, 24 January 2025

AI Roadmap

AI Ecosystem

  1. Artificial Intelligence (AI)

    • AI is the umbrella term for systems that mimic human intelligence. It includes various fields such as machine learning, robotics, computer vision, and NLP.
  2. Machine Learning (ML)

    • ML is a subset of AI that focuses on building systems that learn from data rather than being explicitly programmed.
    • NLP often relies on machine learning models to process and understand language.
  3. Deep Learning (DL)

    • A subset of ML that uses neural networks with many layers. It powers state-of-the-art NLP models like GPT and BERT.
  4. Natural Language Processing (NLP)

    • NLP is a specialized area within AI focused on enabling machines to understand, interpret, and generate human language.

 Comparison Based on Present and Future Market Growth

ConceptRelevance TodayFuture Market GrowthCareer Prospects
AIHigh (broad field)High (leadership roles)Strategic roles in AI projects
MLVery High (foundational)Very HighCore for AI/ML engineering
DLHigh (specialized cases)Very HighResearch and innovation roles
NLPVery High (popular field)Extremely HighNLP-specific engineering roles


Which AI Concepts Are Essential for You?

Here’s how you can prioritize learning AI concepts for your project:

ConceptRelevance to ProjectLearning PriorityReason
NLPVery HighHighestCentral to generating paragraph explanations.
ML BasicsHighHighForms the foundation for understanding NLP models.
Deep Learning (DL)ModerateMediumRequired for text-to-speech systems.
Reinforcement LearningOptionalLowUseful for personalization but not critical.