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?