April 12, 2025

๐Ÿง  Classification Loss Functions: A Deep Dive (From Basics to Mastery)

๐ŸŒŸ Introduction: Why Study Loss Functions?

Every machine learning model, at its heart, is trying to minimize a mistake.

This mistake is quantified using something called a loss function.

๐Ÿ‘‰ Loss functions are the heartbeat of learning.
Without them, your model has no direction, no feedback, no improvement.

In classification tasks, choosing the right loss function:

  • Improves model performance dramatically,

  • Speeds up convergence during training,

  • Enhances generalization to unseen data.

In exams, interviews, and real-world applications, understanding loss functions is a non-negotiable skill.


๐Ÿ›ค️ Evolution of Classification Loss Functions

Let's walk through how loss functions evolved over time, step-by-step:


1. 0-1 Loss — The First Attempt ๐ŸŽฏ

Definition:
If prediction is correct → loss = 0, else loss = 1.

Formula:

L(u)={0if u>01otherwiseL(u) = \begin{cases} 0 & \text{if } u > 0 \\ 1 & \text{otherwise} \end{cases}

where u=yf(x)u = y \cdot f(x),
yy is the true label (+1 or -1),
f(x)f(x) is the model’s raw score.

Example:

  • True label: +1

  • Model prediction: +0.8
    → Correct → Loss = 0

  • Model prediction: -0.3
    → Incorrect → Loss = 1

Problems:

  • Not differentiable ๐Ÿ›‘

  • Not continuous ๐Ÿ›‘

  • Optimization becomes NP-hard → impossible for big datasets.

Lesson: Theory sounds great, but practice demands something smoother.


2. Squared Loss — Borrowed from Regression ๐Ÿ“‰

Definition:
Penalizes based on squared distance from the true value.

Formula:

L(y,y^)=(yy^)2L(y, \hat{y}) = (y - \hat{y})^2

Example:

  • True label: +1

  • Predicted score: 0.7
    → Loss = (10.7)2=0.09(1 - 0.7)^2 = 0.09

Good for Regression, but for classification?

  • Sensitive to outliers ๐Ÿ”ฅ

  • No concept of "margin" between classes.

Issue in Classification:
Small wrongs are treated the same as big wrongs.


3. Hinge Loss — The SVM Revolution ๐Ÿฅ‹

Definition:
Encourages not just correct classification but also a confidence margin.

Formula:

L(u)=max(0,1u)L(u) = \max(0, 1 - u)

Interpretation:

  • If u1u \geq 1, no loss (safe margin ✅)

  • If u<1u < 1, linear penalty (danger zone ❌)

Example:

  • True label: +1

  • Predicted score: 0.5
    → Loss = max(0,1(1)(0.5))=0.5\max(0, 1 - (1)(0.5)) = 0.5

  • Predicted score: 1.2
    → Loss = 0 (safe)

Visual intuition:

Prediction ConfidenceLoss
High (Safe Margin)0
Low (Near Decision Boundary)>0
Wrong SideHigher Loss

Impact:

  • Made Support Vector Machines (SVMs) dominant in the 90s-2000s.


4. Logistic Loss — Probability and Deep Learning Era ๐Ÿ”ฅ

Definition:
Smoothly penalizes wrong predictions and interprets outputs as probabilities.

Formula:

L(u)=log(1+eu)L(u) = \log(1 + e^{-u})

Example:

  • True label: +1

  • Predicted score: 0.5
    → Loss = log(1+e0.5)0.474\log(1 + e^{-0.5}) \approx 0.474

  • Predicted score: 2
    → Loss = log(1+e2)0.126\log(1 + e^{-2}) \approx 0.126

Advantages:

  • Smooth gradients ✅

  • Convex ✅

  • Great for gradient descent optimization ✅

  • Probabilistic outputs (sigmoid connection) ✅

Today’s deep learning networks (classification heads) still often use Cross-Entropy Loss (a multi-class generalization of logistic loss).


๐Ÿ“ˆ Comparison of Loss Functions

Feature0-1 LossSquared LossHinge LossLogistic Loss
Convex
Smooth
Probabilistic
Margin-BasedKind of (soft margin)
OptimizationVery hardEasyEasy (piecewise)Very easy

๐Ÿ”ฅ Practical Example: How Loss Impacts Training

Suppose you are building a spam classifier.

Prediction ScoreTrue Label0-1 LossHinge LossLogistic Loss
0.21 (Spam)10.80.598
1.51 (Spam)000.105
-0.51 (Spam)11.50.974

Notice:

  • Logistic Loss always provides small but non-zero gradients (good for learning).

  • Hinge Loss enforces a hard threshold.

  • 0-1 Loss just "correct/wrong", no learning signal.


๐Ÿ’ฌ Important Concept: Risk and Surrogate Loss

  • Bayes Optimal Classifier minimizes the 0-1 loss.

  • But since optimizing 0-1 directly is impossible,
    we use surrogate losses (hinge, logistic) that are easier to optimize.

๐Ÿ‘‰ If your surrogate loss is good, you still approach Bayes optimality.

This theory is called Consistency of Surrogate Losses.

Exam Tip:
You must mention "Surrogate Loss" if asked about why hinge or logistic losses are used instead of 0-1 loss.


๐Ÿ“š Some Important Variants

VariantIdea
Cross EntropyLogistic Loss generalized for multi-class
Softmax LossSpecial cross-entropy for softmax output
Exponential LossUsed in AdaBoost (focuses more on misclassified points)
Huberized Hinge LossSmooths hinge loss to make it fully differentiable

๐Ÿง  Summary

"You are not just minimizing errors — you are shaping how your model thinks about errors."

Understand this:

  • 0-1 Loss: Pure but impractical.

  • Squared Loss: Regression friend, classification enemy.

  • Hinge Loss: Margin fighter.

  • Logistic Loss: Smooth probability guide.

Your Study Checklist ✅

  • Know the loss functions' formulas and graphs.

  • Understand which models use which loss.

  • Know advantages and disadvantages.

  • Practice examples and visualize curves.


๐ŸŽฏ Final Thoughts

Learning loss functions isn’t just about passing exams.

๐Ÿ‘‰ It’s about thinking like an algorithm designer.
๐Ÿ‘‰ It's about building better models that learn smarter and faster.

You’re no longer a student once you understand how mistakes drive learning
You are now a master of machine learning thinking.


April 8, 2025

๐Ÿง  Understanding Axon Framework (CQRS & Event Sourcing) – In Simple Words

 For developers, architects, and curious minds – including your tech-loving uncle! ๐Ÿ˜„


๐Ÿ” What is Axon Framework?

Imagine you're managing a huge library. Every time someone borrows or returns a book, you log it in a diary. Later, if you want to know which books were borrowed most, or which user has never returned a book, you just flip through the diary.

That's event sourcing. Instead of storing the current state, you store every change (event). Axon Framework helps you do this with Java and Spring Boot in a clean and scalable way.


๐Ÿ› ️ Core Concepts (With Analogies)

1. CQRS – Command Query Responsibility Segregation

In normal apps, one class both updates and fetches data.

With CQRS, we split that into:

  • Command: "Please change something" (e.g., borrow a book)

  • Query: "Tell me something" (e.g., list all books borrowed by Jatin)

This separation helps us scale better and move faster.

2. Event Sourcing – Every Action Is Recorded

Instead of updating a database row, we append an event:

  • "Book borrowed by Jatin at 2 PM"

  • "Book returned by Jatin at 5 PM"

Want to know who had the book on Jan 1st? Just replay the events!

3. Aggregates

Think of these as mini-managers for each type of data.

  • A LibraryAggregate ensures no one borrows the same book twice.

4. Sagas

These are like long conversations.

  • "User borrowed book -> Notify system -> Send reminder -> Handle return"

Axon automates these flows with reliability.


๐Ÿ“š In-depth Topics to Know

๐Ÿ”„ 1. Command Bus vs Event Bus

  • Command Bus is like a single delivery truck taking your message to the right person. Only ONE handler can process a command.

  • Event Bus is like a loudspeaker. When an event happens, everyone listening can respond.

Axon provides both out of the box and lets you plug in distributed versions.

๐Ÿ“– 2. Snapshotting

Over time, an aggregate may have thousands of events. Replaying all of them might get slow.

With snapshotting, Axon stores a recent snapshot of the state, so it only replays newer events. Think of it like saving your progress in a video game.

๐Ÿ” 3. Query Side with Projections

In CQRS, your read side often has its own database (like MongoDB or PostgreSQL).

  • Axon lets you build projections by reacting to events and updating read models.

  • You can have multiple projections for different use cases: dashboards, reports, etc.

๐Ÿ” 4. Replay Events

Did your logic change? Want to rebuild your reports?

Axon allows event replay:

  • Clears the projection DB

  • Replays all events to rebuild data accurately

You don't need to mess with old code or data — just replay and regenerate.

๐Ÿ” 5. Security in CQRS

With commands and queries separated, security must be enforced separately:

  • Use Spring Security to protect REST endpoints

  • Inside Axon, use interceptors to validate commands or restrict queries

This fine-grained control improves robustness.


๐Ÿš€ Why Use Axon?

✅ Scales well – easy to split across microservices
✅ Maintains audit logs – every change is recorded
✅ Fits into Spring Boot easily
✅ Built-in tools for commands, events, queries, sagas
✅ Comes with Axon Server (a native event store & router)


๐Ÿ†š Axon vs Others – Who Are Its Competitors?

1. Eventuate

  • ๐Ÿ”น Java + Microservices

  • ๐Ÿ”น Event sourcing + distributed sagas

  • ๐Ÿ”ธ Less tooling and documentation compared to Axon

2. Lagom (by Lightbend)

  • ๐Ÿ”น Scala-first, supports Java

  • ๐Ÿ”น Reactive + event-sourced

  • ๐Ÿ”ธ Complex for beginners

3. JHipster + Blueprints

  • ๐Ÿ”น Quick scaffolding with optional CQRS support

  • ๐Ÿ”ธ Not true event sourcing

4. Kafka / RabbitMQ (Custom builds)

  • ๐Ÿ”น DIY event-driven systems

  • ๐Ÿ”ธ Requires heavy lifting to get to Axon's level


๐Ÿงพ Summary Table

Feature Axon Eventuate Lagom JHipster Kafka
CQRS Support ✅ Full ✅ Full ✅ Full ⚠️ Partial
Event Sourcing ✅ Yes ✅ Yes ✅ Yes ⚠️ Basic ⚠️ Custom
Spring Boot Ready ✅ Yes ✅ Yes ⚠️ Limited ✅ Yes
UI Tools ✅ Axon Server ⚠️ Basic ⚠️ Basic ✅ Dev UI ⚠️ Plugins
Learning Curve ⚠️ Moderate ⚠️ High ⚠️ High ✅ Easy ⚠️ Medium

๐ŸŽฏ Should You Use Axon?

Use Axon if:

  • You’re building a complex Java system (microservices or monolith)

  • You want event history, audit trails, and saga flows

  • You use Spring Boot and want out-of-the-box support

Avoid if:

  • You prefer very simple CRUD apps

  • You need ultra-low latency (CQRS adds slight delay)


๐Ÿ‘ต A Word for Non-Techies

Think of Axon as a really smart notebook where:

  • You record everything

  • You don’t lose any data

  • You can always replay events to see what happened

  • And it has a brain that makes sure everything happens correctly!


๐Ÿ“ฆ Bonus: What’s Axon Server?

It’s a free server by the Axon team.

  • Stores events

  • Routes commands and queries

  • Has a nice dashboard to monitor everything

Optional enterprise version adds clustering, scaling, and backup.


๐Ÿ“š Final Thoughts

Axon Framework isn’t just a tool — it’s a well-thought-out platform for building reliable, event-driven Java applications.

If you’re an architect or backend developer and you haven’t tried Axon yet — now’s the time.

Happy coding! ๐Ÿ’ป


Was this blog helpful? Let me know — or share with someone who’s exploring CQRS/Event Sourcing! ๐Ÿงก

April 7, 2025

Mastering Keycloak Client Access Settings – A Complete Guide with Real Use Cases & Best Practices

๐Ÿ” Mastering Keycloak Client Access Settings – A Complete Guide with Real Use Cases & Best Practices


✨ Why Understanding Keycloak Client URLs Matters

Imagine you have a secure web application. You want users to:

  • Log in via Keycloak

  • Get redirected to the right page after login

  • Be returned to a nice page after logout

  • Avoid CORS issues in SPAs

  • Handle backend logout events when a session ends

All of this is controlled via Keycloak Client Access Settings.


๐Ÿ”‘ Let’s Break Down the URLs with a Story

๐Ÿง‘‍๐Ÿ’ผ Meet Aditi, who is logging in to your app:

App:

https://tenant-123.example.com

Keycloak:

https://auth.example.com

What happens?

1. Aditi opens: https://tenant-123.example.com ➡️
2. App redirects to Keycloak for login ➡️
3. Keycloak checks if redirect URL is allowed (Valid Redirect URIs) ➡️
4. After login, Keycloak redirects her back to: https://tenant-123.example.com/login/oauth2/code/keycloak
5. After logout, she’s taken to: https://tenant-123.example.com/logout-success

๐Ÿงฉ Client URL Types — Explained with Examples

URL Type Purpose Example Required?
Root URL Base URL of your app, used by Keycloak as default https://tenant-123.example.com ✅ Yes
Home URL Where “Back to App” points https://tenant-123.example.com/dashboard ๐Ÿ”„ Optional
Valid Redirect URIs Where to return users after login https://tenant-*.example.com/login/oauth2/code/keycloak ✅ Yes
Valid Post Logout Redirect URIs Where to redirect after logout https://tenant-*.example.com/logout-success ✅ Yes
Web Origins Trusted domains for browser-based requests https://tenant-*.example.com ✅ Yes (for SPAs)
Admin URL Where to send backchannel logout (server to server) https://tenant-123.example.com/backchannel-logout ๐Ÿงช Optional

๐Ÿ” Flow Diagram (Text-based Arrows)

๐Ÿ” Login Flow:

User ➡️ https://tenant-123.example.com
      ➡️ (App redirects to Keycloak)
      ➡️ https://auth.example.com/realms/demo/protocol/openid-connect/auth
      ➡️ (User logs in)
      ➡️ Redirects to: https://tenant-123.example.com/login/oauth2/code/keycloak
      ➡️ App handles token + navigates to: /dashboard

๐Ÿšช Logout Flow:

User clicks Logout ➡️
      App calls: https://auth.example.com/realms/demo/protocol/openid-connect/logout
      ➡️ Keycloak clears session
      ➡️ Redirects to: https://tenant-123.example.com/logout-success

๐Ÿ›ฐ️ Backchannel Logout (Optional)

Keycloak (server) ➡️ POST to Admin URL
                   https://tenant-123.example.com/backchannel-logout
                   (App terminates session silently)

๐Ÿ’ก Best Practices (Updated)

๐Ÿ” Security Tips:

  • Avoid using * in any URL setting in production.

  • Use wildcards like https://tenant-*.example.com/* only when you have DNS control.

  • Test each environment (localhost, dev, staging, prod).

⚙️ Wildcard Examples:

Use Case URI Pattern
Dev environment http://localhost:3000/*
Multi-tenant https://tenant-*.example.com/*
Logout page https://tenant-*.example.com/logout-success
Web origin for SPA https://tenant-*.example.com

๐Ÿง˜ Final Thoughts

These settings might look technical, but they're your app's gatekeepers. A properly configured Keycloak client:

  • Protects users from phishing

  • Prevents CORS headaches

  • Creates a seamless login/logout experience

Now that you’re equipped with:

  • URL meanings ✅

  • Flow diagrams ✅

  • Real-world story ✅

  • Best practices ✅

You’re ready to master Keycloak like a pro.


Would you like me to convert this blog into a Markdown/HTML file for publishing?