<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Galip on Medium]]></title>
        <description><![CDATA[Stories by Galip on Medium]]></description>
        <link>https://medium.com/@galippala?source=rss-ffc2933d9abe------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Galip on Medium</title>
            <link>https://medium.com/@galippala?source=rss-ffc2933d9abe------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 19:39:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@galippala/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[K8s Leader Election: Ensuring Single Execution in Distributed Systems]]></title>
            <link>https://medium.com/@galippala/k8s-leader-election-ensuring-single-execution-in-distributed-systems-a618d89b58c2?source=rss-ffc2933d9abe------2</link>
            <guid isPermaLink="false">https://medium.com/p/a618d89b58c2</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[multiple-instances]]></category>
            <category><![CDATA[leader-election]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[single-execution]]></category>
            <dc:creator><![CDATA[Galip]]></dc:creator>
            <pubDate>Thu, 30 Apr 2026 17:05:06 GMT</pubDate>
            <atom:updated>2026-04-30T17:05:06.124Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>“What happens when the same scheduled job runs multiple times in a distributed system?</em></blockquote><p>Duplicate payments. Corrupted data. Inconsistent state.</p><p>There is a common problem in distributed systems where the same scheduled job can be executed <strong>multiple times across different instances</strong>.</p><p>This demo simulates a distributed leader election system using <strong>multiple application instances running on a single-node</strong> Kubernetes (Minikube) cluster.</p><p>Even though this demo runs <strong>locally</strong> on a single node, it models a real distributed systems problem: coordination between multiple independent instances over shared state.</p><ul><li><strong><em>How do we ensure only one instance executes a job at any given time?</em></strong></li></ul><p>This is where Kubernetes leader election comes into play.</p><p>It acts as a distributed locking mechanism. Multiple instances share the same lease object but only one instance can hold the <strong>holderIdentity </strong>at a time.</p><p>In this demo, I solve this problem using Kubernetes leader election to ensure only one instance executes the job at any given time.</p><p>The elected leader is responsible for running critical tasks. A lease acts like a distributed lock stored in the Kubernetes API:</p><ul><li>One instance acquires the lease, becomes the LEADER</li><li>Other instances observe the lease</li><li>The leader must periodically renew the lease</li><li>If it fails to renew, the lease expires and another instance can take over</li><li><strong>Repository link:</strong></li></ul><p><a href="https://github.com/galip/k8s-leader-election">GitHub - galip/k8s-leader-election</a></p><h3>Architecture of the Demo</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VUC_PDr3wZoruFKWhqxq_g.png" /><figcaption><strong>Architecture of this demo</strong></figcaption></figure><p>This demo follows the structure below:</p><ol><li><strong>Case 1: Kubernetes not reachable</strong></li><li><strong>Start Minikube</strong></li><li><strong>Create a namespace</strong></li><li><strong>Case 2: Lease not found</strong></li><li><strong>Create a Lease object</strong></li><li><strong>Case 3: Run a single instance (leader)</strong></li><li><strong>Case 4: Run multiple instances (follower)</strong></li><li><strong>Case 5: Change the leader — Failover test</strong></li><li><strong>Case 6: Leadership is lost</strong></li></ol><h3>1. Case 1: Kubernetes not reachable</h3><p>In this case, the application cannot<strong> communicate with the Kubernetes API server.</strong></p><p>This simulates cluster availability before running the election logic:</p><pre>if (!isKubernetesAvailable()) {<br>    log.warn(&quot;Kubernetes not reachable — skipping leader election&quot;);<br>    transitionToFollower(null);<br>    return;<br>}</pre><pre>private boolean isKubernetesAvailable() {<br>    try {<br>        client.pods().list();<br>        return true;<br>    } catch (Exception e) {<br>        return false;<br>    }<br>}</pre><p>If the API server is unreachable, you will get <strong><em>KubernetesClientException</em></strong> at this point and <strong>returns false</strong>. It safely falls back to follower mode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o1JaJe1P0QYrvsyxGRvm3w.png" /></figure><pre>2026-04-26T13:35:21.571+03:00  INFO 64630 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T13:35:21.599+03:00  INFO 64630 --- [k8s-leader-election] [           main] c.g.k.K8sLeaderElectionApplication       : K8s Leader Election application started successfully.<br>2026-04-26T13:35:26.574+03:00  INFO 64630 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T13:35:31.575+03:00  INFO 64630 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T13:35:36.577+03:00  INFO 64630 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T13:35:40.747+03:00  WARN 64630 --- [k8s-leader-election] [ leader-elect-1] c.g.k.elector.Fabric8LeaderElector       : Kubernetes not reachable — skipping leader election<br>2026-04-26T13:35:41.579+03:00  INFO 64630 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T13:35:46.585+03:00  INFO 64630 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T13:35:51.589+03:00  INFO 64630 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader</pre><p>In this case, we did not see the log as “<strong>Current leader -&gt; …</strong>“ or “<strong>Leadership lost. Previous leader=…</strong>” because the instance never became a leader in the first place. These log blocks never run for this case.</p><h3><strong>2. Start Minikube</strong></h3><p>I start a local Kubernetes cluster using Minikube, a minimal configuration is enough:</p><p>This creates a single node Kubernetes cluster locally.</p><pre>minikube start - memory=4096 - cpus=2</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/687/1*kXtC7w0wIKItH9dZ5fO8FQ.png" /><figcaption><strong>minikube start</strong></figcaption></figure><p>Let’s verify the node:</p><pre>kubectl get nodes</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*DAAvu6noCMN0SOXvutzPmw.png" /><figcaption><strong>nodes</strong></figcaption></figure><h3><strong>3. C</strong>reate a namespace</h3><pre>kubectl create namespace leader-election</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*JlZHl6WrSRZZc07BoC6uJg.png" /><figcaption><strong>leader-election namespace created</strong></figcaption></figure><h3>4. Case 2: Lease not found</h3><p>Since I have not created lease yet, I see “<strong>Lease not found -&gt; leader-election / order-processor-leader</strong>” in the logs.</p><pre>Lease lease = client.leases()<br>        .inNamespace(properties.namespace())<br>        .withName(properties.leaseName())<br>        .get();<br><br>if (lease == null) {<br>    log.warn(&quot;Lease not found -&gt; {} / {}&quot;, properties.namespace(), properties.leaseName());<br>    transitionToFollower(null);<br>    return;<br>}</pre><p>If the Lease object does not exist:</p><ul><li>The instance cannot participate in leader election</li><li>It falls back to follower mode</li><li>Scheduled jobs are not executed</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pPdZMj0gq1Kb9uL_ndLoJw.png" /><figcaption><strong>Lease not found</strong></figcaption></figure><pre>2026-04-26T13:43:04.242+03:00  INFO 67800 --- [k8s-leader-election] [           main] c.g.k.K8sLeaderElectionApplication       : Started K8sLeaderElectionApplication in 1.295 seconds (process running for 1.502)<br>2026-04-26T13:43:04.245+03:00  INFO 67800 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T13:43:04.275+03:00  INFO 67800 --- [k8s-leader-election] [           main] c.g.k.K8sLeaderElectionApplication       : K8s Leader Election application started successfully.<br>2026-04-26T13:43:04.459+03:00  WARN 67800 --- [k8s-leader-election] [ leader-elect-1] c.g.k.elector.Fabric8LeaderElector       : Lease not found -&gt; leader-election / order-processor-leader<br>2026-04-26T13:43:09.250+03:00  INFO 67800 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T13:43:14.254+03:00  INFO 67800 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T13:43:14.472+03:00  WARN 67800 --- [k8s-leader-election] [ leader-elect-1] c.g.k.elector.Fabric8LeaderElector       : Lease not found -&gt; leader-election / order-processor-leader</pre><h3><strong>5. Create a Lease Object</strong></h3><p>Let’s create a lease named <strong>order-processor-leader</strong>. I define a <strong>lease.yml </strong>file in resources folder:</p><pre>apiVersion: coordination.k8s.io/v1<br>kind: Lease<br>metadata:<br>  name: order-processor-leader<br>  namespace: leader-election<br>spec:<br>  holderIdentity: &quot;&quot;<br>  leaseDurationSeconds: 30</pre><p>Then I will run the following command to apply this <strong>lease.yml</strong> file.</p><pre>kubectl apply -f lease.yml</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*IK_gWsd93GLO1xS6xsByVQ.png" /><figcaption><strong>lease created</strong></figcaption></figure><blockquote>lease.coordination.k8s.io/order-processor-leader created</blockquote><p>Also, if you want more details:</p><pre>kubectl describe lease order-processor-leader -n leader-election</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*hhnYO-Oq6pFtC32W1fYOow.png" /><figcaption><strong>lease details</strong></figcaption></figure><p>holder Identity is empty here. After we start one application and elect the LEADER, we will have holder identity here.</p><p>Let’s deep dive in.</p><h3><strong>6. Case 3: Run a single instance (leader)</strong></h3><p>I start a single application instance now. We can expect the LEADER election now.</p><pre>if (holderIdentity == null || holderIdentity.isBlank() || expired) {<br> boolean success = attemptTakeover(lease);<br>if (success) {<br> transitionToLeader();<br> }<br>}</pre><blockquote>If the lease is empty or expired, the instance attempts to become the leader.</blockquote><pre>private void transitionToLeader() {<br> if (leader.compareAndSet(false, true)) {<br>        currentLeaderIdentity = identity;<br>        log.info(&quot;I AM LEADER -&gt; {}&quot;, identity);<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2Gwq4-1EMIIxrdWYRoqCfg.png" /><figcaption><strong>leader elected</strong></figcaption></figure><pre>2026-04-26T13:50:42.168+03:00  INFO 70751 --- [k8s-leader-election] [           main] c.g.k.K8sLeaderElectionApplication       : K8s Leader Election application started successfully.<br>2026-04-26T13:50:42.369+03:00  INFO 70751 --- [k8s-leader-election] [ leader-elect-1] c.g.k.elector.Fabric8LeaderElector       : Successfully acquired leadership -&gt; 4f5b4bdf-75ea-4bc3-91c3-25f1d5f6fd7e<br>2026-04-26T13:50:42.369+03:00  INFO 70751 --- [k8s-leader-election] [ leader-elect-1] c.g.k.elector.Fabric8LeaderElector       : I AM LEADER -&gt; 4f5b4bdf-75ea-4bc3-91c3-25f1d5f6fd7e<br>2026-04-26T13:50:47.145+03:00  INFO 70751 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Leader executing scheduled job<br>2026-04-26T13:50:47.146+03:00  INFO 70751 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Processing batch step 1<br>2026-04-26T13:50:48.147+03:00  INFO 70751 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Processing batch step 2<br>2026-04-26T13:50:49.148+03:00  INFO 70751 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Processing batch step 3<br>2026-04-26T13:50:50.153+03:00  INFO 70751 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Leader completed scheduled job</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*Z3L2uUysz9kSDTusaLshwQ.png" /><figcaption><strong>holder identity</strong></figcaption></figure><p>holder-identity = “<strong>4f5b4bdf-75ea-4bc3–91c3–25f1d5f6fd7e</strong>” and we will see this in the second application instance log now.</p><h3>7. <strong>Case 4: Run multiple instances (follower)</strong></h3><p>Now let’s start the second instance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zX1o7DwRTtvr2JdmpwwZaQ.png" /><figcaption><strong>Instance-2 started successfully</strong></figcaption></figure><pre>2026-04-26T13:55:24.491+03:00  INFO 72607 --- [k8s-leader-election] [           main] c.g.k.K8sLeaderElectionApplication       : Started K8sLeaderElectionApplication in 1.111 seconds (process running for 1.27)<br>2026-04-26T13:55:24.495+03:00  INFO 72607 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T13:55:24.519+03:00  INFO 72607 --- [k8s-leader-election] [           main] c.g.k.K8sLeaderElectionApplication       : K8s Leader Election application started successfully.<br>2026-04-26T13:55:24.670+03:00  INFO 72607 --- [k8s-leader-election] [ leader-elect-1] c.g.k.elector.Fabric8LeaderElector       : Current leader -&gt; 4f5b4bdf-75ea-4bc3-91c3-25f1d5f6fd7e<br>2026-04-26T13:55:29.502+03:00  INFO 72607 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T13:55:34.508+03:00  INFO 72607 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader</pre><p>As we see that second instance detects the current leader: <strong>4f5b4bdf-75ea-4bc3–91c3–25f1d5f6fd7e<em>.</em></strong></p><p>This instance checks whether it is the current leader before executing the job. If not, it safely skips execution, ensuring only one instance performs the task.</p><pre>if (!elector.isLeader()) {<br>    log.info(&quot;Skipping execution — not leader&quot;);<br>    return;<br>}</pre><h3><strong>8. Case 5: Change the leader — Failover test</strong></h3><p>Leader election is not “<strong>one time selection</strong>”.</p><p>The leader continuously renews the lease before it expires to signal liveness.</p><p>Failover is not immediate. Kubernetes determines leader failure based on lease expiration(renewTime + leaseDuration), which introduces a small delay before a new leader is elected.</p><p>Now we have 2 application instances running. I will stop the <strong>LEADER</strong> instance now and <strong>LEADER</strong> instance will change.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3alXUSDHm9JeE6PI7lv5gg.png" /><figcaption><strong>instance-1 stopped</strong></figcaption></figure><pre>2026-04-26T14:00:02.145+03:00  INFO 70751 --- [k8s-leader-election] [          job-2] c.g.k8sleaderelection.job.LeaderOnlyJob  : Processing batch step 3<br>2026-04-26T14:00:03.149+03:00  INFO 70751 --- [k8s-leader-election] [          job-2] c.g.k8sleaderelection.job.LeaderOnlyJob  : Leader completed scheduled job<br>2026-04-26T14:00:03.199+03:00  INFO 70751 --- [k8s-leader-election] [ionShutdownHook] o.s.b.w.e.tomcat.GracefulShutdown        : Commencing graceful shutdown. Waiting for active requests to complete<br>2026-04-26T14:00:03.206+03:00  INFO 70751 --- [k8s-leader-election] [tomcat-shutdown] o.s.b.w.e.tomcat.GracefulShutdown        : Graceful shutdown complete</pre><p>The leader changed and it is second instance now, “<strong>ec24448f-c8fd-4045–9077–4d897b2145f1</strong>”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bXY-f4yYRp7d4oEo_INysg.png" /><figcaption><strong>instance-2 is the new LEADER</strong></figcaption></figure><pre>2026-04-26T14:00:29.722+03:00  INFO 72607 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T14:00:34.723+03:00  INFO 72607 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T14:00:35.390+03:00  INFO 72607 --- [k8s-leader-election] [ leader-elect-1] c.g.k.elector.Fabric8LeaderElector       : Lease expired. renewTime=2026-04-26T10:59:54.237432Z, duration=30s, now=2026-04-26T11:00:35.390275Z<br>2026-04-26T14:00:35.416+03:00  INFO 72607 --- [k8s-leader-election] [ leader-elect-1] c.g.k.elector.Fabric8LeaderElector       : Successfully acquired leadership -&gt; ec24448f-c8fd-4045-9077-4d897b2145f1<br>2026-04-26T14:00:35.416+03:00  INFO 72607 --- [k8s-leader-election] [ leader-elect-1] c.g.k.elector.Fabric8LeaderElector       : I AM LEADER -&gt; ec24448f-c8fd-4045-9077-4d897b2145f1<br>2026-04-26T14:00:39.728+03:00  INFO 72607 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Leader executing scheduled job<br>2026-04-26T14:00:39.728+03:00  INFO 72607 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Processing batch step 1<br>2026-04-26T14:00:40.733+03:00  INFO 72607 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Processing batch step 2<br>2026-04-26T14:00:41.739+03:00  INFO 72607 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Processing batch step 3<br>2026-04-26T14:00:42.744+03:00  INFO 72607 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Leader completed scheduled job</pre><p>When a node logs “<strong>I AM LEADER”</strong>, it is not just the state change. It is a successful result of a distributed lock acquisition using Lease objects.</p><p>The leader continuously proves liveness by renewing the lease. If the leader stops <strong>renewing the lease</strong>, the system does not react immediately. Other instances wait until the lease expires, then attempt to <strong>take over</strong>. This ensures consistency and prevents <strong>split-brain</strong> problem.</p><p>When I start the instance-1 again, it will acquire the new, existing leader.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0WNjfqwejGHKBsdW-Gb_-A.png" /><figcaption><strong>instance-1 started again</strong></figcaption></figure><pre>2026-04-26T14:06:24.877+03:00  INFO 76878 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T14:06:24.908+03:00  INFO 76878 --- [k8s-leader-election] [           main] c.g.k.K8sLeaderElectionApplication       : K8s Leader Election application started successfully.<br>2026-04-26T14:06:25.079+03:00  INFO 76878 --- [k8s-leader-election] [ leader-elect-1] c.g.k.elector.Fabric8LeaderElector       : Current leader -&gt; ec24448f-c8fd-4045-9077-4d897b2145f1<br>2026-04-26T14:06:29.886+03:00  INFO 76878 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader<br>2026-04-26T14:06:34.891+03:00  INFO 76878 --- [k8s-leader-election] [          job-1] c.g.k8sleaderelection.job.LeaderOnlyJob  : Skipping execution — not leader</pre><p>When I start instance-1 again, it does not automatically become the leader. It follows and observes the current leader, <strong>ec24448f-c8fd-4045–9077–4d897b2145f1. </strong>Because of this, it does not attempt to execute <strong>LEADER-only</strong> logic and skips the execution.</p><h3>9. Case 6: Leadership is lost</h3><p>After executing <strong><em>minikube stop</em></strong> the Kubernetes cluster becomes unreachable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*u4yfzGrPaYdhO8usFSWFbg.png" /><figcaption><strong>minikube stop</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GrXa8nr2y1Wg241IpbO5Vw.png" /><figcaption><strong>Leadership lost</strong></figcaption></figure><p>The application can no longer communicate Kubernetes API and it cannot read or renew the lease object. Without renewing, it cannot prove leadership anymore.</p><pre>if (!isKubernetesAvailable()) {<br> log.warn(&quot;Kubernetes not reachable - skipping leader election&quot;);<br> transitionToFollower(null);<br> return;<br>}</pre><pre>private void transitionToFollower(String newLeader) {<br><br>    if (!Objects.equals(currentLeaderIdentity, newLeader)) {<br><br>        currentLeaderIdentity = newLeader;<br><br>        if (newLeader != null) {<br>            log.info(&quot;Current leader -&gt; {}&quot;, newLeader);<br>        } else {<br>            log.info(&quot;Leader unknown&quot;);<br>        }<br>    }<br><br>    if (leader.compareAndSet(true, false)) {<br>        log.info(&quot;Leadership lost. Previous leader={}&quot;, identity);<br>    }</pre><p>Thanks for reading. Hope you find this useful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EaqKfXfXQ9F212nWDI_9_A.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a618d89b58c2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From Git Commit to Live Behavior Change: A Proven Spring Cloud Config + Bus Demo]]></title>
            <link>https://medium.com/@galippala/from-git-commit-to-live-behavior-change-a-proven-spring-cloud-config-bus-demo-3fbbb5004fcd?source=rss-ffc2933d9abe------2</link>
            <guid isPermaLink="false">https://medium.com/p/3fbbb5004fcd</guid>
            <category><![CDATA[observability]]></category>
            <category><![CDATA[spring-cloud-config]]></category>
            <category><![CDATA[spring-bot]]></category>
            <category><![CDATA[spring-cloud-bus]]></category>
            <category><![CDATA[rabbitmq]]></category>
            <dc:creator><![CDATA[Galip]]></dc:creator>
            <pubDate>Fri, 16 Jan 2026 16:05:15 GMT</pubDate>
            <atom:updated>2026-01-16T16:05:15.129Z</atom:updated>
            <content:encoded><![CDATA[<p>In this demo, the goal is to clearly demonstrate and verify how dynamic configuration refresh works in the Spring Cloud ecosystem. Specifically, it shows how a configuration change can propagate from a Git commit to running services <strong>without any application restarts</strong>.</p><p>I will focus on the most critical parts and how they actually behave at runtime.</p><p>This demo consists of three separate repositories and I will introduce them in their respective sections:</p><ul><li><strong>scb-config-repo</strong></li><li><strong>scb-config-server</strong></li><li><strong>scb-campaign-service</strong></li></ul><p>I intentionally prefixed the repository names with <strong>scb</strong> to emphasize that this demo is centered around <strong>Spring Cloud Bus</strong>.<br>The prefix stands for <strong><em>Spring Cloud Bus and is used consistently to reflect the purpose of the architecture.</em></strong></p><p>By the end of this demo, you will have a <strong>concrete</strong>, <strong>observable proof</strong> of how Spring Cloud Config and Spring Cloud Bus work together to deliver live configuration changes, backed by <strong>logs, events and runtime behavior changes</strong> rather than assumptions.</p><blockquote><em>All HTTP requests in this article use </em><em>Content-Type: application/json.</em></blockquote><h3>Architecture of This Demo</h3><p>This demo follows the architecture shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ytv8M0ufzzhfY30KF3Qmdg.png" /><figcaption>High-level flow of the demo</figcaption></figure><ol><li>A configuration value is changed in the Git repository.</li><li>Git triggers a <strong>webhook</strong> to the <strong>scb-config-server</strong>.</li><li>The <strong>scb-config-server</strong> receives the webhook on the <strong>/monitor</strong> endpoint.</li><li>The <strong>scb-config-server</strong> publishes a <strong>RefreshRemoteApplicationEvent</strong> to Spring Cloud Bus.</li><li>Spring Cloud Bus propagates this event through RabbitMQ.</li><li>All subscribed services receive the refresh event.</li><li><strong>scb-campaign-service</strong> reloads its configuration from the <strong>scb-config-server</strong> into memory.</li><li>The service behavior changes immediately, without application restart.</li></ol><h3>Deep Dive into Components</h3><h3>1. scb-config-repo</h3><p>I have created <strong>scb-config-repo</strong> as the centralized configuration repository for this demo.</p><p>The repository contains two types of configuration files:</p><p>scb-config-repo<br>├── application.yml<br>└── scb-campaign-service.yml</p><ul><li><strong>application.yml </strong>is a global config shared across services.</li><li><strong>scb-campaign-service.yml</strong> is the service-specific configuration for the campaign service.</li></ul><p>This separation allows us to clearly demonstrate how global and service-level configurations are merged and served by Spring Cloud Config.</p><p><strong>Repository link:</strong></p><p><a href="https://github.com/galip/scb-config-repo">GitHub - galip/scb-config-repo</a></p><p>Below is the configuration defined in <strong><em>scb-campaign-service.yml</em></strong>.</p><pre>campaign:<br>  enabled: true<br>  type: PREMIUM<br>  discount-rate: 45<br>  response:<br>    message-prefix: &quot;[CAMPAIGN]&quot;<br>    include-discount: true</pre><ul><li><strong>Structural config</strong> (type, discount-rate): Changing them alters what the service does.</li><li><strong>Behavioral config</strong> (message-prefix, include-discount) : Changing them alters how the service behaves, response formatting.</li></ul><p>Both are refreshed dynamically at runtime. This is intentional because this proves the refresh is not superficial, but directly affects runtime business behavior.</p><ul><li><strong>Management Endpoints Configuration</strong></li></ul><pre>management:<br>  endpoints:<br>    web:<br>      exposure:<br>        include: health,info,env</pre><p>The <strong>/refresh </strong>and <strong>/busrefresh </strong>endpoints are intentionally not exposed in this demo to avoid any manual refresh triggers.</p><p>This guarantees that any observed runtime changes are the result of a <strong>Git-driven, event-based refresh flow, not a local actuator call.</strong></p><h3>2. Run the RabbitMQ</h3><blockquote><strong><em>docker run -d — name scb-rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y-N2oV1xAFCK9Ya-QWva3w.png" /></figure><p>Open the RabbitMQ UI and check it.</p><blockquote><a href="http://localhost:15672/">http://localhost:15672</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lWJQfsR-JWXpJ1OEqoTP2w.png" /><figcaption>RabbitMQ UI</figcaption></figure><p>The management UI allows you to observe exchanges, queues and message flow.</p><h3>3. scb-config-server</h3><p><a href="https://github.com/galip/scb-config-server">GitHub - galip/scb-config-server</a></p><p>The <strong>scb-config-server </strong>is the central authority for this demo.</p><ul><li>Reads configuration from git</li><li>Serves configuration to client services</li><li>Receives Git webhook events</li><li>Publishes refresh events to the Spring Cloud Bus</li></ul><p>I intentionally keep the <strong>scb-config-server</strong> minimal but explicit.</p><ul><li><strong>Enabling Spring Cloud Config Server</strong></li></ul><pre>@SpringBootApplication<br>@EnableConfigServer<br>public class ConfigServerApplication {<br> public static void main(String[] args) {<br>  SpringApplication.run(ConfigServerApplication.class, args);<br> }<br>}</pre><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-cloud-config-server&lt;/artifactId&gt;<br>&lt;/dependency&gt;</pre><p><strong><em>@EnableConfigServer</em></strong></p><p>Makes the application act as central configuration authority for all clients. The application turns into a Spring Cloud Config Server.</p><ul><li><strong>Git based configuration source</strong></li></ul><p>This block defines where the configuration is loaded from:</p><pre>spring:<br> cloud:<br>    config:<br>      server:<br>        git:<br>          uri: https://github.com/galip/scb-config-repo<br>          default-label: master<br>          clone-on-start: true</pre><ul><li><strong>RabbitMQ Integration(Spring Cloud Bus)</strong></li></ul><p>These settings connect the config server to the RabbitMQ.</p><pre>spring:<br> rabbitmq:<br>    host: localhost<br>    port: 5672<br>    username: guest<br>    password: guest</pre><p>This connection allows the <strong>scb-config-server</strong> to</p><ul><li>Publish <strong>RefreshRemoteApplicationEvent</strong></li><li>Broadcast configuration changes to all subscribed services</li></ul><p>You can see these subscribed service instances in the <strong>RabbitMQ Management UI</strong> under the <strong>Queues and Streams</strong> tab. Spring Cloud Bus creates anonymous, exclusive queues for each running service instance. As a result, queue names do not contain service identifiers.</p><ul><li><strong>Actuator Endpoints</strong></li></ul><pre>management:<br>  endpoints:<br>    web:<br>      exposure:<br>        include: health,info,env,monitor</pre><p><strong><em>/actuator/monitor</em></strong> is critical here. Because:</p><ul><li>It is where Git webhooks are sent</li><li>It receives change events</li><li><strong>Run the scb-config-server</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kjxjML-YHiZFCCO0r2AV5Q.png" /><figcaption>scb-config-server started on port 8888</figcaption></figure><p>As we can see that the <strong>scb-config-server</strong> is started and connected to Git and RabbitMQ. You can observe this queue in the <strong>Queues and Streams</strong> tab of the RabbitMQ Management UI.<br>This confirms that <strong>scb-config-server</strong> is subscribed to Spring Cloud Bus and ready to publish refresh events.</p><pre>[scb-config-server] o.s.a.r.c.CachingConnectionFactory       : Created new connection: rabbitConnectionFactory#28f8ce2e:0/SimpleConnection@1c7fd2e0 [delegate=amqp://guest@127.0.0.1:5672/, localPort=52576]</pre><p><strong><em>/actuator/health</em></strong> endpoint also gives you information about RabbitMQ whether it is UP or not. You can check it here.</p><p><a href="http://localhost:8888/actuator/health">http://localhost:8888/actuator/health</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*sU_r0D4ZAIL8-IPnGLozOA.png" /><figcaption>RabbitMQ is UP</figcaption></figure><p>This provides the <strong>scb-config-server</strong> is connected to RabbitMQ and event publishing is possible.</p><p>If you want to explicitly verify that the <strong>scb-config-server </strong>publishes the<strong> RefreshRemoteApplicationEvent</strong>, you can add a simple local event listener inside the <strong>scb-config-server</strong> application.</p><pre>@Component<br>public class BusEventLogger {<br><br>    private static final Logger log =<br>            LoggerFactory.getLogger(BusEventLogger.class);<br><br>    @EventListener<br>    public void onRefreshRemoteEvent(RefreshRemoteApplicationEvent event) {<br>        log.info(<br>                &quot;RefreshRemoteApplicationEvent published. originService={}, destinationService={}&quot;,<br>                event.getOriginService(),<br>                event.getDestinationService()<br>        );<br>    }<br>}</pre><p>I will provide the logs about this in section <strong>6 - Triggering the Change: Testing the Application.</strong></p><h3>4. scb-campaign-service</h3><p><a href="https://github.com/galip/scb-campaign-service">GitHub - galip/scb-campaign-service</a></p><p>The <strong>scb-campaign-service</strong>:</p><ul><li>Loads configuration from Spring Cloud Config Server</li><li>Listens to Spring Cloud Bus events</li><li>Refreshes configurations at runtime, without restart</li></ul><p><strong>application.yml:</strong></p><pre>spring:<br> config:<br>    import: optional:configserver:http://localhost:8888</pre><p>This configuration makes the <strong>scb-campaign-service</strong> a Spring Cloud Config client. At startup, it fetches the configuration from the scb-config-server.</p><p>Without <strong>optional, </strong>the application would fail fast if the <strong>scb-config-server</strong> is down. But this way, if it is not available, the application still starts.</p><p>With Spring Cloud Bus:</p><ul><li>Configuration changes become event-driven</li><li>Refresh propagates automatically via RabbitMQ</li></ul><p><strong>pom.xml:</strong></p><pre>&lt;dependency&gt;<br>   &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;<br>   &lt;artifactId&gt;spring-cloud-starter-bus-amqp&lt;/artifactId&gt;<br>&lt;/dependency&gt;</pre><p>This dependency enables:</p><ul><li>Spring Cloud Bus event publishing</li><li>RabbitMQ integration</li><li>Distributed refresh behavior</li></ul><p><strong><em>@RefreshScope</em></strong> is mandatory for dynamic refresh.</p><pre>@RefreshScope<br>@Component<br>@ConfigurationProperties(prefix = &quot;campaign&quot;)<br>public class CampaignProperties {<br><br>    private static final Logger log =<br>            LoggerFactory.getLogger(CampaignProperties.class);<br><br>    private boolean enabled;<br>    private String type;<br>    private int discountRate;<br>    private Response response = new Response();<br><br>                .<br>                .<br>                .</pre><p>It ensures that:</p><ul><li>The bean is recreated when a refresh event arrives</li><li>New values are injected into memory</li><li>No application restart is required</li></ul><p>In this service, I exposed two REST endpoints.</p><ul><li>/campaign/config</li><li>/campaign/info</li></ul><p><strong>/campaign/config</strong></p><p>This endpoint is used to expose raw, in-memory configuration values.</p><pre>GET /campaign/config</pre><pre>@GetMapping(&quot;/config&quot;)<br>public Map&lt;String, Object&gt; config() {<br>    return Map.of(<br>            &quot;enabled&quot;, properties.isEnabled(),<br>            &quot;type&quot;, properties.getType(),<br>            &quot;discountRate&quot;, properties.getDiscountRate()<br>    );<br>}</pre><p><strong>/campaign/info</strong></p><p>This endpoint demonstrates runtime behavioral change, not just configuration refresh.</p><pre>GET /campaign/info</pre><pre>@GetMapping(&quot;/info&quot;)<br>public CampaignResponse getCampaignInfo() {<br> if (!properties.isEnabled()) {<br>        return new CampaignResponse(&quot;Campaign disabled&quot;);<br>    }<br>    StringBuilder message = new StringBuilder();<br>    message.append(properties.getResponse().getMessagePrefix())<br>            .append(&quot; &quot;);<br>    message.append(properties.getType())<br>            .append(&quot; campaign active&quot;);<br>    if (properties.getResponse().isIncludeDiscount()) {<br>        message.append(&quot; with &quot;)<br>                .append(properties.getDiscountRate())<br>                .append(&quot;% discount&quot;);<br>    }<br>    return new CampaignResponse(message.toString());<br>}</pre><p>It shows that business logic reacts immediately to new config values.</p><p>For example:</p><ul><li>Changing <strong>message-prefix</strong> or enabling i<strong>nclude-discount</strong> modifies the message structure.</li><li>Disabling the <strong>campaign<em>,</em></strong> changes the response entirely.</li></ul><p>Let’s run the <strong>scb-campaign-service</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X55Y2cWv3lN8q7_F_4TkcQ.png" /><figcaption>scb-campaign-service</figcaption></figure><p>The <strong>scb-campaign-service</strong> is acting as Spring Cloud Client. It connects the <strong>scb-config-server.</strong></p><pre>[scb-campaign-service] o.s.c.c.c.ConfigServerConfigDataLoader   : Fetching config from server at : http://localhost:8888<br>[scb-campaign-service] o.s.c.c.c.ConfigServerConfigDataLoader   : Located environment: name=scb-campaign-service, profiles=[default], label=null, version=bc5f2ecef0b4767c6b2777d31635e1c195541737, state=</pre><p>It requests configuration for:</p><ul><li><strong><em>application</em></strong>: scb-campaign-service</li><li><strong><em>profile</em></strong>: default</li></ul><p>Let’s check it.</p><pre>GET http://localhost:8888/scb-campaign-service/default</pre><pre>{<br>  &quot;name&quot;: &quot;scb-campaign-service&quot;,<br>  &quot;profiles&quot;: [<br>    &quot;default&quot;<br>  ],<br>  &quot;label&quot;: null,<br>  &quot;version&quot;: &quot;bc5f2ecef0b4767c6b2777d31635e1c195541737&quot;,<br>  &quot;state&quot;: &quot;&quot;,<br>  &quot;propertySources&quot;: [<br>    {<br>      &quot;name&quot;: &quot;https://github.com/galip/scb-config-repo/scb-campaign-service.yml&quot;,<br>      &quot;source&quot;: {<br>        &quot;spring.application.name&quot;: &quot;scb-campaign-service&quot;,<br>        &quot;spring.cloud.bus.enabled&quot;: true,<br>        &quot;campaign.enabled&quot;: true,<br>        &quot;campaign.type&quot;: &quot;FIXED&quot;,<br>        &quot;campaign.discount-rate&quot;: 15,<br>        &quot;campaign.response.message-prefix&quot;: &quot;[CAMPAIGN]&quot;,<br>        &quot;campaign.response.include-discount&quot;: true,<br>        &quot;management.endpoints.web.exposure.include&quot;: &quot;health,info,env&quot;<br>      }<br>    },<br>    {<br>      &quot;name&quot;: &quot;https://github.com/galip/scb-config-repo/application.yml&quot;,<br>      &quot;source&quot;: {<br>        &quot;spring.application.name&quot;: &quot;global-config&quot;,<br>        &quot;logging.level.root&quot;: &quot;INFO&quot;,<br>        &quot;logging.level.org.springframework.cloud&quot;: &quot;INFO&quot;,<br>        &quot;logging.level.org.springframework.cloud.bus&quot;: &quot;DEBUG&quot;,<br>        &quot;logging.level.org.springframework.amqp&quot;: &quot;DEBUG&quot;,<br>        &quot;management.endpoints.web.exposure.include&quot;: &quot;health, info, env&quot;<br>      }<br>    }<br>  ]<br>}</pre><p>We can see that configuration is fetched before Tomcat is initialized.</p><p>When we call this endpoint we see that <strong>scb-config-server </strong>fetches configuration files from the git repository, resolves and merges the configurations, <strong><em>scb-campaign-service.yml </em></strong>and<strong><em> application.yml.</em></strong></p><p>This makes the config server endpoint as a single source of truth for configuration resolution.</p><h3>5. Git webhook</h3><p>The Git webhook is the mechanism that triggers a configuration change to the Spring Cloud Bus refresh flow in this demo.</p><p>It acts as the starting trigger of the entire pipeline.</p><p>It will notify the <strong>scb-config-server</strong> whenever a configuration change has occurred.</p><p>Without a webhook:</p><ul><li>The <strong>scb-config-server</strong> has no idea that a config file changed.</li><li>We would need to manually call <strong><em>/busrefresh</em></strong>.</li></ul><p>But this webhook eliminates this manual step by actively notifying the <strong>scb-config-server</strong> when a change happens.</p><p>Spring Cloud Config Server exposes a built-in endpoint for Git events:</p><pre>POST /monitor</pre><p>So I used this endpoint in the webhook payload.</p><p>But if you look at the payload in the image, <strong><em>Github Webhook Settings, </em></strong>at the below, you do not see localhost:8888. Because Github cannot reach a local machine. <strong>Webhook</strong> requires a publicly accessible URL.</p><p>I used <strong>Ngrok</strong> here strictly for <strong>demo purposes only</strong> to expose a local Config Server. Production environments require different, more robust solutions, which are intentionally out of scope for this demo.</p><p>So in my case Ngrok temporarily exposes my local <strong>scb-config-server </strong>to the internet.</p><ul><li><strong>Start Ngrok</strong></li></ul><p>Run the following command:</p><pre>ngrok http 8888</pre><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*hJaJaA3YUMuFSgXugiTK8Q.png" /><figcaption>ngrok forwarding from public URL to http://localhost:8888</figcaption></figure><p>I will use this public URL: <a href="https://possessively-odontalgic-marsha.ngrok-free.dev/"><strong>https://possessively-odontalgic-marsha.ngrok-free.dev/</strong></a></p><p>Since Spring Cloud Config listens for webhook events on <strong><em>/monitor,</em></strong> I append it to the Ngrok URL:</p><p>Payload URL: <a href="https://possessively-odontalgic-marsha.ngrok-free.dev/monitor"><strong>https://possessively-odontalgic-marsha.ngrok-free.dev/monitor</strong></a></p><pre>https://possessively-odontalgic-marsha.ngrok-free.dev/monitor</pre><ul><li><strong>Add the Webhook in Github</strong></li></ul><p>In <strong>scb-config-repo<em>:</em></strong></p><p><strong>Settings → Webhooks → Add Webhook → Set the Payload URL(</strong>which Ngrok produced the public URL)</p><p>And also you need to configure other settings too, content type, SSL, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*EAaFY3iEo3JKHZj4CVTgAg.png" /><figcaption>Github Webhook Settings</figcaption></figure><p>Now, everything is ready. Let’s change the config values in <strong>scb-config-repo</strong>. One commit will trigger full refresh flow.</p><h3>6. Triggering the Change: Testing the Application</h3><p>I change the values as in the following lines:</p><pre>campaign:<br>  enabled: true<br>  type: PREMIUM<br>  discount-rate: 45<br>  response:<br>    message-prefix: &quot;[CAMPAIGN]&quot;<br>    include-discount: true</pre><p>This change is committed and pushed to Git.</p><p>Without any restart, I check the endpoints and see the values are changed as I expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*AvQOJ1r-ATwOLCliE60EDw.png" /><figcaption>/campaign/config</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/1*zVI2hsMe_jkqihUMYjG3Pg.png" /><figcaption>/campaign/info</figcaption></figure><ul><li><strong><em>Recent Deliveries</em></strong></li></ul><p>We can see the details in Github <strong>webhooks</strong> → <strong><em>Recent Deliveries</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JWdkpTB24Ybx4j2x4m6WLA.png" /><figcaption>Webhook Recent Deliveries</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MRX2bs5jJwZo_6cKFbiCmg.png" /><figcaption>Webhook Recent Deliveries</figcaption></figure><p>If you open the delivery details, you can inspect both the request and the response sent to the <strong>scb-config-server.</strong></p><p>You can see the <strong><em>after</em></strong> <strong>version</strong>, commit SHA, <strong>c2077459b5b2fca4e21ff28adf1eb91705ed0cef, </strong>in <strong>scb-campaign-service</strong> log.</p><p>You also can see these via Ngrok UI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Dhe8fieg0W5qwy7OEBjOdA.png" /><figcaption>Ngrok UI</figcaption></figure><ul><li><strong>Verifying the Commit SHA in Service Logs</strong></li></ul><pre>[scb-campaign-service] o.s.c.c.c.ConfigServerConfigDataLoader   : Located environment: name=scb-campaign-service, profiles=[default], label=null, version=c2077459b5b2fca4e21ff28adf1eb91705ed0cef</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dzTO94ONLP_hhv0l_hfCSw.png" /><figcaption>The configuration in use exactly with this Git commit</figcaption></figure><p>That proves Spring Cloud Server successfully resolved and served the configuration from Git.</p><p>It uses default profile and configuration version corresponds exactly to the specific Git commit SHA.</p><ul><li><strong>Verifying Refreshed Keys</strong></li></ul><p>The following log shows exactly which keys were refreshed. Only the <strong>affected keys were refreshed</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ZK8e_VIK1i-RveOmDXiiA.png" /><figcaption>Keys refreshed in scb-campaign-service</figcaption></figure><pre>[scb-campaign-service] o.s.cloud.bus.event.RefreshListener      : Keys refreshed [config.client.version, campaign.type, campaign.enabled, campaign.response.include-discount, campaign.discount-rate]</pre><ul><li><strong>Published event message log</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7LT_FDGpLUFLxJqcopnfcA.png" /><figcaption>Published remote event from scb-config-server, RefreshRemoteApplicationEvent</figcaption></figure><pre>[scb-config-server] c.s.configserver.bus.BusEventLogger      : RefreshRemoteApplicationEvent published. originService=scb-config-server:8888:1b5ae149473e3bacb9cea737910fccc3, destinationService=scb:**</pre><ul><li><strong>Received message log</strong></li></ul><p>We can see the incoming message from Spring Cloud bus in the <strong>scb-campaign-service</strong> logs:</p><pre>[scb-campaign-service] o.s.a.r.listener.BlockingQueueConsumer   : Received message: (Body:&#39;[B@4d076d53(byte[210])&#39; MessageProperties [headers={}, timestamp=Sat Jan 10 01:15:14 TRT 2026, messageId=fa34079e-3458-3454-57cd-3d42d28eebee, contentType=application/json, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=springCloudBus, receivedRoutingKey=springCloudBus, deliveryTag=36, consumerTag=amq.ctag-hSSFqnvTpS5-LXVD_5nmNQ, consumerQueue=springCloudBus.anonymous.Gk2qp7fcR9uLJfRmR6H0xA])</pre><p>You also can see this in in RabbitMQ UI with the same queue name, consumerQueue=springCloudBus.anonymous.Gk2qp7fcR9uLJfRmR6H0xA</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c5GUfE4tkmXIZw3MJ7Lw3g.png" /><figcaption>RabbitMQ UI shows an event came</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8guloQOCeJkxmZWPxDLgsw.png" /><figcaption>Received remote event, RefreshRemoteApplicationEvent</figcaption></figure><pre>[scb-campaign-service] o.springframework.cloud.bus.BusConsumer  : Received remote event from bus: [RefreshRemoteApplicationEvent@1843186b id = &#39;07eb19cc-f15b-4379-b442-cf7301ecea5f&#39;, originService = &#39;scb-config-server:8888:1b5ae149473e3bacb9cea737910fccc3&#39;, destinationService = &#39;scb:**&#39;]</pre><p>After receiving the <strong>RefreshRemoteApplicationEvent</strong>, Spring reloads the configuration in memory.</p><ul><li><strong>Recreated Bean Proof</strong></li></ul><p>If you want to see the bean being recreated, you can add a simple logging directly inside the bean which is annotated by @RefreshScope in my example. Because lifecycle callbacks such as <strong>@PostConstruct</strong> and <strong>@PreDestroy</strong> will fire on the refresh scoped bean itself.</p><p>So this allows us to track whether the same instance is reused or a new one is created.</p><pre>    @PostConstruct<br>    public void init() {<br>        log.info(&quot;CampaignProperties bean CREATED. instanceId={}&quot;,<br>                System.identityHashCode(this));<br>    }<br><br>    @PreDestroy<br>    public void destroy() {<br>        log.info(<br>                &quot;CampaignProperties bean DESTROYED. instanceId={}&quot;,<br>                System.identityHashCode(this)<br>        );<br>    }</pre><p>Whenever you push the new values to the <strong>scb-config-repo</strong>, you will see the previously created bean is destroyed. <strong>instanceId=1506844659</strong> is created first and then after the git push it is destroyed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pNeBSIyQoBIpXHIL_qVYPg.png" /><figcaption>CREATED and DESTROYED bean instance</figcaption></figure><p>Whenever you request the new values, you will see the new bean instance is created with the new instanceId again.</p><pre>[scb-campaign-service] c.s.c.config.CampaignProperties          : CampaignProperties bean CREATED. instanceId=1819129291</pre><ul><li><strong>Local Git Working Copy (Config Server)</strong></li></ul><pre>[scb-config-server] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: Config resource &#39;file [/var/folders/8f/wlvhdz8x3tscyh3t9cg9wswm0000gn/T/config-repo-7975916063049639024/application.yml]&#39; via location &#39;file:/var/folders/8f/wlvhdz8x3tscyh3t9cg9wswm0000gn/T/config-repo-7975916063049639024/&#39;</pre><p>This log confirms that the <strong>scb-config-server</strong> is not reading configuration directly from the remote Git repository on every request. It fetches configuration from <strong>its local Git working copy, temporary clone </strong>of the remote repository.</p><p>If we inspect the local directory:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/893/1*Gdy1yaob2xAlUHYkhvkL5A.png" /><figcaption>local Git working copy</figcaption></figure><p>This directory is a temporary local git clone created by <strong>scb-config-server</strong>. It reads configuration files directly from this working copy, not from the remote Git repository on every request.</p><p>If you request this endpoint:</p><pre>GET http://localhost:8888/scb-campaign-service/default</pre><p>Both files are resolved,<strong> scb-campaign-service.yml</strong> and <strong>application.yml.</strong> I will see two lines here in the log as <strong>Adding property source.</strong></p><pre>[scb-config-server] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: Config resource &#39;file [/var/folders/8f/wlvhdz8x3tscyh3t9cg9wswm0000gn/T/config-repo-7975916063049639024/scb-campaign-service.yml]&#39; via location &#39;file:/var/folders/8f/wlvhdz8x3tscyh3t9cg9wswm0000gn/T/config-repo-7975916063049639024/&#39;<br>[scb-config-server] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: Config resource &#39;file [/var/folders/8f/wlvhdz8x3tscyh3t9cg9wswm0000gn/T/config-repo-7975916063049639024/application.yml]&#39; via location &#39;file:/var/folders/8f/wlvhdz8x3tscyh3t9cg9wswm0000gn/T/config-repo-7975916063049639024/&#39;</pre><p>But If I change a value in the .yml in <strong>scb-config-repo</strong> and push it then it will fetch from the remote server again.</p><pre> [scb-config-server] .c.s.e.MultipleJGitEnvironmentRepository : Fetched for remote master and found 1 updates</pre><p><strong>No fetch happens unless a new commit is detected.</strong></p><ul><li><strong>Changing Response Behavior</strong></li></ul><p>Last thing. Let’s change the behavior of response.</p><pre>campaign:<br>  enabled: false<br>  type: PREMIUM<br>  discount-rate: 45<br>  response:<br>    message-prefix: &quot;[CAMPAIGN]&quot;<br>    include-discount: true</pre><p>This time we can check it from Postman.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*eMr3wHcPfJtPXyvRexNOPw.png" /><figcaption><strong>enabled</strong> changed as false, campaign being disabled</figcaption></figure><p>Let’s remove the discount from response.</p><pre>campaign:<br>  enabled: true<br>  type: PREMIUM<br>  discount-rate: 45<br>  response:<br>    message-prefix: &quot;[CAMPAIGN]&quot;<br>    include-discount: false</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*CW0sXSjU_cpdGgUBa7OGFA.png" /><figcaption><strong>include-discount</strong> changed as false, discount value is removed in response</figcaption></figure><p>The message structure changes dynamically.</p><p>That concludes the demo.</p><p>Live configuration refresh is only valuable when it is observable. Everything else is an assumption.</p><p>Thank you for your time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3fbbb5004fcd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Elasticsearch — Indexes, Documents]]></title>
            <link>https://medium.com/@galippala/elasticsearch-indexes-documents-7efd86512c70?source=rss-ffc2933d9abe------2</link>
            <guid isPermaLink="false">https://medium.com/p/7efd86512c70</guid>
            <category><![CDATA[elasticsearch-crud]]></category>
            <category><![CDATA[elasticsearch-index]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <dc:creator><![CDATA[Galip]]></dc:creator>
            <pubDate>Fri, 21 Feb 2025 21:42:25 GMT</pubDate>
            <atom:updated>2025-02-22T13:18:57.597Z</atom:updated>
            <content:encoded><![CDATA[<h3>Elasticsearch — Indexes, Documents</h3><p>In Elasticsearch, we store documents within indexes. We can not store them without indexes. Indexes are collections of documents. So, first we need to create an index.</p><h3>Create Index</h3><pre>PUT /orders</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j6bB6mqHFeXhBFfIqNnT4Q.png" /></figure><p>Well, we have orders index now.</p><p>If you do not explicitly tell the index settings to Elasticsearch then the default shards and replicas number will be <strong>1</strong>.</p><p>You can easily see this with the following request:</p><pre>GET /orders/_settings</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*szdJLRSri_DWMJpd6mqvZQ.png" /></figure><p>But if you want to specify the settings explicitly then during the creation of index you would define with settings property at first before you created the index.</p><pre>PUT /orders/_settings<br>{<br>  &quot;settings&quot;: {<br>    &quot;number_of_shards&quot;: 3,<br>    &quot;number_of_replicas&quot;: 2<br>  }<br>}</pre><p>But do not forget, after you created an index you can not modify its number of shards. You can reindex it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NMcjK6M4quzXld9nPe83RA.png" /></figure><p>At the beginning, you could create index with settings like that.</p><pre>PUT /orders_new<br>{<br>  &quot;settings&quot;: {<br>    &quot;number_of_shards&quot;: 3,<br>    &quot;number_of_replicas&quot;: 2<br>  },<br>  &quot;mappings&quot;: {<br>    &quot;properties&quot;: {<br>      &quot;id&quot;: { &quot;type&quot;: &quot;keyword&quot; },<br>      &quot;order_date&quot;: { &quot;type&quot;: &quot;date&quot; },<br>      &quot;price&quot;: { &quot;type&quot;: &quot;float&quot; },<br>      &quot;status&quot;: { &quot;type&quot;: &quot;keyword&quot; }<br>    }<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/912/1*wviVCWuR-qCDqWiwZ80RZg.png" /></figure><h3>Add Document to The Index</h3><p>I keep orders fields simple.</p><pre>POST /orders/_doc<br>{<br>  &quot;id&quot;: &quot;12345&quot;,<br>  &quot;order_date&quot;: &quot;2025-02-12&quot;,<br>  &quot;price&quot;: 53.21,<br>  &quot;status&quot;: &quot;PENDING&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SIwAE6xxstpG8ORyHdDHLA.png" /></figure><p>There are 2 shards totally, one is primary and the other one is replica.</p><p>_seq_no is the number which increments in every operation.</p><p>_primary_term is first generation of shard and 1 when it is first created.</p><p>I created the another document on the same shard and I get incremented _seq_no but the same _primary_term.</p><pre>POST /orders/_doc<br>{<br>  &quot;id&quot;: &quot;12345678&quot;,<br>  &quot;order_date&quot;: &quot;2025-02-12&quot;,<br>  &quot;price&quot;: 53.21,<br>  &quot;status&quot;: &quot;PENDING&quot;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uEBP0o5EJR0MY1U7OWZhSw.png" /></figure><h3>Shard Information</h3><pre>GET /_cat/shards?v</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N-V9J5G-r7HNsfpIC_mIYw.png" /></figure><p>Since I run elasticsearch on single node, the replica shard of <strong>orders</strong> index is <strong>UNASSIGNED</strong>. Replica shards need different node than the primary shard to be stored.</p><h3>Retrive Document By Id</h3><pre>GET /orders/_doc/dDodKpUBJGJLkLIAl-Nf</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/1*_STpY4JcR--E98ZkisSdWQ.png" /></figure><h3>Get All Documents</h3><pre>GET /orders/_search<br>{<br>  &quot;query&quot;: {<br>    &quot;match_all&quot;: {}<br>  }<br>}</pre><p>You can see the added 2 documents.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*50pmE0K3vF5Jqyy3ATsUPQ.png" /></figure><h3>Update the Document</h3><p>We updated the price as 58.</p><pre>POST /orders/_update/IQoL_JQB2kvQTSUzGBWQ<br>{<br>  &quot;doc&quot;: {<br>    &quot;price&quot; : 58<br>  }<br>}</pre><p>And check the document again. We see the price is updated.</p><pre>GET /orders/_doc/IQoL_JQB2kvQTSUzGBWQ</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/1*CcgNPLpQw5TDqxBfl7l_EA.png" /></figure><h3><strong>Delete Document By ID</strong></h3><pre>DELETE /orders/_doc/dDodKpUBJGJLkLIAl-Nf</pre><p>That will delete the document with specific id in orders index.</p><h3><strong>Delete All Documents</strong></h3><blockquote><em>“match_all”: “{}” </em>matches the every document in the index.</blockquote><p>So the following query will delete all documents in the orders index.</p><pre>POST /orders/_delete_by_query<br>{<br>  &quot;query&quot;: {<br>    &quot;match_all&quot;: {}<br>  }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/935/1*DtSKESWtOOCAyaYmr78fnA.png" /></figure><p>As you see, in 82 miliseconds 2 documents deleted. There are many more operations you can explore with these queries.</p><h3><strong>What if you want to delete documents by status?</strong></h3><pre>POST /orders/_delete_by_query<br>{<br>  &quot;query&quot;: {<br>    &quot;match&quot;: {<br>      &quot;status&quot;: &quot;PENDING&quot;<br>    }<br>  }<br>}</pre><p>Hope you enjoyed with it. 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7efd86512c70" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Apache Kafka Installation on Mac]]></title>
            <link>https://medium.com/@galippala/apache-kafka-installation-on-mac-ff6cafc41750?source=rss-ffc2933d9abe------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff6cafc41750</guid>
            <category><![CDATA[kafka-producer]]></category>
            <category><![CDATA[kafka-installation-on-mac]]></category>
            <category><![CDATA[kafka-topic]]></category>
            <category><![CDATA[kafka-consumer]]></category>
            <dc:creator><![CDATA[Galip]]></dc:creator>
            <pubDate>Sun, 01 Sep 2024 21:26:57 GMT</pubDate>
            <atom:updated>2024-09-01T21:26:57.897Z</atom:updated>
            <content:encoded><![CDATA[<p>We will see how to install kafka, create topic, list topics, send message to producer and read it with consumer.</p><p>Use this link: <a href="https://kafka.apache.org/quickstart">https://kafka.apache.org/quickstart</a></p><p>Download the .tgz file and go to kafka folder.</p><pre>$ bin/zookeeper-server-start.sh config/zookeeper.properties</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eMMmmqdmyqpfeRI3ukhluw.png" /></figure><pre>$ bin/kafka-server-start.sh config/server.properties</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8mGtcbuJz-nnXB7-PDKhDg.png" /></figure><h3><strong>Create a topic</strong></h3><blockquote>bin/kafka-topics.sh — create — topic demo — bootstrap-server localhost:9092</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*beFG1D_4eYP9tPhVbwc3yw.png" /></figure><h3>List topics</h3><blockquote>bin/kafka-topics.sh — bootstrap-server=localhost:9092 — list</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g9UNGnskMFe7bjU2RNmRgA.png" /></figure><h3><strong>Send message from producer</strong></h3><blockquote>bin/kafka-console-producer.sh — topic demo — bootstrap-server localhost:9092</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p3QxKm6zNO4-rtZeuXgFeQ.png" /></figure><h3><strong>Read message from consumer</strong></h3><blockquote>bin/kafka-console-consumer.sh — topic demo — from-beginning — bootstrap-server localhost:9092</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yZJSMn9n74pITOxlexegEw.png" /></figure><p>That is all for now.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff6cafc41750" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Elasticsearch-Kibana Installation Windows]]></title>
            <link>https://medium.com/@galippala/elasticsearch-kibana-installation-00d3439efd74?source=rss-ffc2933d9abe------2</link>
            <guid isPermaLink="false">https://medium.com/p/00d3439efd74</guid>
            <category><![CDATA[kibana]]></category>
            <category><![CDATA[elasticsearch]]></category>
            <dc:creator><![CDATA[Galip]]></dc:creator>
            <pubDate>Sat, 11 May 2024 22:29:45 GMT</pubDate>
            <atom:updated>2024-05-12T10:10:01.840Z</atom:updated>
            <content:encoded><![CDATA[<p>Download Elasticsearch, elasticsearch-8.12.2-windows-x86_64 when I write this blog.</p><p><a href="https://www.elastic.co/downloads/elasticsearch">Download Elasticsearch</a></p><p>Go to elastic folder and run the below script in command shell.</p><blockquote>bin\elasticsearch.bat</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*y9Y9rV451y_0gkresU0meA.png" /></figure><p>During the installation logs, you will see elastic password and enrollment token for Kibana.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tH8VZQqAJURQqBDLotJhcQ.png" /></figure><p>Save this <strong>enrollment token(valid for 30 minutes) </strong>and after Kibana installation you will use it.</p><p>Download Kibana, kibana-8.12.2-windows-x86_64 when I write this article.</p><blockquote>bin\kibana.bat</blockquote><p>Start Kibana and go to the link which is created but not configured yet.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6OzIjf-YD-VmYowHrdqNPg.png" /></figure><p>Use the token you have already got it in elastic console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OE7eMhh4UL2lg-RHmDKzDA.png" /></figure><p>Then you should press <strong>Configure Elastic</strong> button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YPezLSdNKZn9ve2W2TYTNw.png" /></figure><p>Let’s see the health check. Click the Dev Tools under Management section in the menu.</p><blockquote><strong><em>GET /_cluster/health</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zqJlbMThKRQQfWEq7O8r5w.png" /></figure><p>Elasticsearch and Kibana is ready now.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=00d3439efd74" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring Data Redis Demo]]></title>
            <link>https://medium.com/@galippala/spring-data-redis-demo-0ecf37cc9c7e?source=rss-ffc2933d9abe------2</link>
            <guid isPermaLink="false">https://medium.com/p/0ecf37cc9c7e</guid>
            <category><![CDATA[spring-data-redis]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Galip]]></dc:creator>
            <pubDate>Fri, 15 Mar 2024 18:22:22 GMT</pubDate>
            <atom:updated>2024-03-15T18:49:05.164Z</atom:updated>
            <content:encoded><![CDATA[<p>I keep this project structure simple and the main goal for this demo is only to demonstrate spring data redis cache usage.</p><p>I add project github repo here: <a href="https://github.com/galip/SpringDataRedis">https://github.com/galip/SpringDataRedis</a></p><p>We will have only 3 endpoints for demo purpose.</p><p>Swagger doc: <a href="http://localhost:8080/swagger-ui/index.html">http://localhost:8080/swagger-ui/index.html</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0t4djf4K3ycotEmNdi4ePw.png" /><figcaption><strong>endpoints in demo</strong></figcaption></figure><h3>Redis-stack</h3><p>I will run <strong>redis-stack</strong> on my machine because redis also advices it to use when it is for local development purposes.</p><p><strong>redis-stack-server</strong> is for production purposes.</p><p>You can check here: <a href="https://redis.io/docs/install/install-stack/docker/">https://redis.io/docs/install/install-stack/docker/</a></p><pre>docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*YOQoyxnGP0jkbPCnolEuxw.png" /></figure><p>Let’s go to this url <a href="http://localhost:8001/redis-stack/browser"><strong><em>http://localhost:8001/redis-stack/browser</em></strong></a></p><p>redis-stack database is ready to use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h79aEgIXtZRY6GaV8bRsAw.png" /></figure><h3>Redis-cli</h3><p>Make the redis-cli ready with the below docker command.</p><pre>docker exec -it redis-stack redis-cli</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*-qty5uLUpXo15ZXQsmNOyA.png" /></figure><p>redis-cli is ready also.</p><p>Then we start the spring boot application.</p><p>As you can see I use Cacheable for both services getAllProducts() and getProductById() but not for the save(). This will make a problem but we will solve this.</p><pre>@Service<br>@Slf4j<br>public class ProductServiceImpl implements ProductService {<br><br> @Autowired<br> ProductRepository productRepository;<br><br> @Override<br> @Cacheable(value = &quot;product&quot;)<br> public List&lt;Product&gt; getAllProducts() {<br>  getLongProcess();<br>  return productRepository.findAll();<br> }<br><br> @Override<br> public Product save(CreateProductRequest productRequest) {<br>  Product product = ProductMapper.convertToProduct(productRequest);<br>  return productRepository.save(product);<br> }<br><br> @Override<br> @Cacheable(value = &quot;product&quot;, key = &quot;#productId&quot;)<br> public Product getProductById(long productId) {<br>  getLongProcess();<br>  Optional&lt;Product&gt; productOpt = productRepository.findById(productId);<br>  return productOpt.isPresent() ? productOpt.get() : new Product();<br> }<br><br> private void getLongProcess() {<br>  try {<br>   Thread.sleep(5000);<br>  } catch (InterruptedException e) {<br>   log.error(&quot;Exception occured: &quot; + e);<br>  }<br> }<br>}</pre><p>I put getLongProcess() only for the demo purpose, to see the time difference with first and second endpoint calls. You can also prefer to write a simple logger inside the cacheable services and see that the log will not be written for the second call.</p><h3>Let’s see the cache works</h3><p>I save 3 different datas and now will check the first call for the endpoint and its duration.</p><p>→ First call for the <strong>/api/products/all</strong> service takes 5.17 seconds.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rD4-R87h3Eda38hjg0Nm_Q.png" /><figcaption><strong>/api/products/all first call</strong></figcaption></figure><pre>http://localhost:8080/api/products/all</pre><p>→ Second call, this time cache works and it fetches the data from cache only within few ms.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JCN2jf5fGfi3cZ3q5-8pnA.png" /><figcaption><strong>/api/products/all second call</strong></figcaption></figure><p>In the below, you can see <strong>/api/products/byId</strong> service for the first and second calls.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z_ReHdllJ8GTteFwraoUVg.png" /><figcaption><strong>/api/products/byId first call</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UA57LNtj49OVMur51Z-LCw.png" /><figcaption><strong>/api/products/byId second call</strong></figcaption></figure><h3>Check the keys on redis-stack</h3><p>We did 2 calls and we see these 2 keys on redis stack database. Since we did not define any ttl(time-to-live) for the cache, it is No limit by default.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wSOkA-qf8pLoXNq-GQw5MA.png" /></figure><h3>Check the keys on redis-cli</h3><p>Use the command on redis-cli</p><pre>KEYS *</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/227/1*gnvZu0umG6I-sfCQEjQAsg.png" /><figcaption><strong>Two cache keys can be seen</strong></figcaption></figure><p>Well, what happens if we add one more data and then try to fecth it.</p><p>Since we did not clear the cache when we add the new data, we can not see the new added data in this endpoint call, <strong>/api/products/all</strong>. That is why we need to clear the cache when we call our create service (<strong>/api/products/create</strong>).</p><p>We add CacheEvict here.</p><pre> @Override<br> @Caching(evict = { @CacheEvict(value = &quot;product&quot;, allEntries = true) })<br> public Product save(CreateProductRequest productRequest) {<br>  Product product = ProductMapper.convertToProduct(productRequest);<br>  return productRepository.save(product);<br> }</pre><p>After this modification, whenever you add the new item with the save() method, you can see old caches are removed.</p><p>After this call <strong>/api/products/create,</strong> you can check redis-stack database and redis-cli again to see the cache is clear. You should be seeing empty caches.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/202/1*Ht1p5p0Te6pHWDgcPj2I2Q.png" /><figcaption><strong>After added new item, cache is clear</strong></figcaption></figure><h4>Stop redis container and check the logs.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*62CSar4HmA43779PyntboA.png" /></figure><pre>2024-03-06T01:27:55.739+03:00  WARN 20056 --- [ioEventLoop-4-1] i.l.core.protocol.ConnectionWatchdog     : Cannot reconnect to [localhost/&lt;unresolved&gt;:6379]: Connection refused: no further information: localhost/127.0.0.1:6379<br>2024-03-06T01:28:25.826+03:00  INFO 20056 --- [xecutorLoop-1-4] i.l.core.protocol.ConnectionWatchdog     : Reconnecting, last destination was localhost/&lt;unresolved&gt;:6379<br>2024-03-06T01:28:25.833+03:00  WARN 20056 --- [ioEventLoop-4-2] i.l.core.protocol.ConnectionWatchdog     : Cannot reconnect to [localhost/&lt;unresolved&gt;:6379]: Connection refused: no further information: localhost/127.0.0.1:6379<br>2024-03-06T01:28:55.926+03:00  INFO 20056 --- [xecutorLoop-1-2] i.l.core.protocol.ConnectionWatchdog     : Reconnecting, last destination was localhost/&lt;unresolved&gt;:6379<br>2024-03-06T01:28:55.931+03:00  WARN 20056 --- [ioEventLoop-4-3] i.l.core.protocol.ConnectionWatchdog     : Cannot reconnect to [localhost/&lt;unresolved&gt;:6379]: Connection refused: no further information: localhost/127.0.0.1:6379<br>2024-03-06T01:29:26.031+03:00  INFO 20056 --- [xecutorLoop-1-4] i.l.core.protocol.ConnectionWatchdog     : Reconnecting, last destination was localhost/&lt;unresolved&gt;:6379</pre><p>Start the redis container again and the application will reconnect to the redis.</p><pre>2024-03-06T01:38:27.835+03:00  INFO 20056 --- [xecutorLoop-1-4] i.l.core.protocol.ConnectionWatchdog     : Reconnecting, last destination was localhost/&lt;unresolved&gt;:6379<br>2024-03-06T01:38:28.309+03:00  INFO 20056 --- [ioEventLoop-4-2] i.l.core.protocol.ReconnectionHandler    : Reconnected to localhost/&lt;unresolved&gt;:6379Thank you for reading.</pre><p>That is all for now. Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0ecf37cc9c7e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kafka Notes]]></title>
            <link>https://medium.com/@galippala/kafka-notes-fb04d957d35b?source=rss-ffc2933d9abe------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb04d957d35b</guid>
            <category><![CDATA[kafka]]></category>
            <dc:creator><![CDATA[Galip]]></dc:creator>
            <pubDate>Sun, 05 Nov 2023 11:20:25 GMT</pubDate>
            <atom:updated>2023-11-05T11:20:25.892Z</atom:updated>
            <content:encoded><![CDATA[<p>Download kafka in via this link: <a href="https://kafka.apache.org/downloads">https://kafka.apache.org/downloads</a></p><p>I use 3.5.1 binary sources when I write this article. Extract the binary source file. Now you are ready to use kafka.</p><h3>List topics:</h3><blockquote><strong>.\kafka-topics.bat --bootstrap-server=localhost:9092 --list</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/853/1*kbNSiiC5TuCAt8JI9f7vog.png" /></figure><p>__consumer_offsets is the default internal topic which is used by Kafka to store consumer offsets.</p><h3>Create a topic:</h3><blockquote><strong>.\kafka-topics.bat — create — replication-factor 1 — partitions 1 — topic test — bootstrap-server=localhost:9092</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1CeOLrAisXATnQs4jPQcWA.png" /></figure><p>Now you may list the topics again see the test topic.</p><h3>Describe the topic:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BSsfsBLviE15kRwDwtpTyw.png" /></figure><p>Let’s run the producer and consumer now and send a message to newly created test topic by producer and consume it by consumer which is subscribed to the test topic.</p><h3>Run the producer:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*ux1Q78a1bWnRc7nGZJMNOQ.png" /></figure><h3>Run the consumer:</h3><p>We get the message in consumer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t-kqJdT7nT8Sc70sIdks7Q.png" /></figure><p>You can send more messages from producer and see it on consumer.</p><h3>Delete a topic:</h3><p>set the <strong><em>delete.topic.enable=true</em></strong><em> </em>to server.properties files.</p><p>.\kafka-topics.bat — delete — bootstrap-server=localhost:9092 — topic test</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb04d957d35b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>