<?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 Sachin Sharma on Medium]]></title>
        <description><![CDATA[Stories by Sachin Sharma on Medium]]></description>
        <link>https://medium.com/@sachinsharmaa15?source=rss-5012528448c2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*SNYmtWU8St1-oBRPuTCUfg.jpeg</url>
            <title>Stories by Sachin Sharma on Medium</title>
            <link>https://medium.com/@sachinsharmaa15?source=rss-5012528448c2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 17:30:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sachinsharmaa15/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[Data Archival Solution for on-premises MongoDB]]></title>
            <link>https://medium.com/airteldigital/data-archival-solution-for-on-premises-mongodb-9181e47bec99?source=rss-5012528448c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/9181e47bec99</guid>
            <category><![CDATA[data-archival]]></category>
            <category><![CDATA[spring-batch]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[software-architecture]]></category>
            <dc:creator><![CDATA[Sachin Sharma]]></dc:creator>
            <pubDate>Mon, 17 Jul 2023 12:33:46 GMT</pubDate>
            <atom:updated>2023-07-17T12:33:46.029Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cAupj9FT7zreLtcW6FluMw.png" /></figure><h3>PREFACE</h3><p>In 2020, while revamping the complete Digital Payments System and Checkout at Airtel, we had decided to split our Monolith System into smaller blocks (microservices) each having its own granular single responsibility. In the process, we also ended up migrating from Oracle SQL Database to Mongo DB. We are using an on premises Mongo Cluster.</p><p>As we scaled to millions of transactions per day, the data grew in size and with terabytes of data, we had to start moving older data out of our live database. Now, Oracle Cloud provides Auto Archival solutions and we needed something similar for Mongo. That is when we came up with this utility that can automatically move data from one mongo instance to another.</p><h3><strong>BASIC FLOW FOR ARCHIVAL</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*4oybh8Hb2x3KofWh27wQaw.jpeg" /><figcaption>Data Archival Process</figcaption></figure><p>The idea is to use a time stamp field that contains the creation time of the document. The utility reads the data created after the last archived document, this helps in ensuring zero duplication of data and prevents loss of data in case of failures; the archival DB controls the range of data to be transferred.</p><h3><strong>IMPLEMENTATION</strong></h3><p>For implementation we decided on using <strong>Spring Batch.</strong> Spring Batch provides a robust, lightweight and reusable batch architecture to execute different batch jobs. It is easily scalable, uses core features of the Spring Framework and also maintains a Job Repository containing information of all the jobs that have been run.</p><p>Spring Batch collects metrics (such as job duration, step duration, item read and write throughput, and others) and registers them in Micrometer’s global metrics registry under the spring.batch prefix. These metrics can be sent to any <a href="https://micrometer.io/docs/concepts#_supported_monitoring_systems">monitoring system</a> supported by Micrometer.</p><p>In order to setup a job, our application needs a metaData document. A json which contains all necessary info to execute the job. Here are the parameters needed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*srJuK8egF4DFGeA743sHaA.png" /><figcaption>Archival MetaData</figcaption></figure><p>These datapoints help in creating our Archival Job. This is a Spring Batch Job which contains of 5 steps as mentioned below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rwx-f25sbBKGnbDHzg0bLQ.png" /><figcaption>Mongo Archive Utility Steps</figcaption></figure><p>Basically, it initially loads the meta data and then uses it to first establish connection with both mongo instances and then transfer data as per our flow above. We have added an extra step to ensure Mongo Connections are closed once archival is done as we won’t be needing them anymore. So, application connects with your Mongo only for the duration of the job run and ensures there is no additional load on your database. It is advisable to run the archival job in lean hours.</p><p>Our Utility is a Web Application, which on start up sets up a job having all above steps for all the meta data documents loaded in application’s context.</p><h3>EXECUTION</h3><p>Our utility exposes a REST Endpoint which can be used to launch a job. As a client, once the job is configured you can use this endpoint to launch the job as and when needed.</p><pre>curl --location &#39;{{application-end-point}}/v1/job/archive/launch&#39; \<br>--header &#39;Content-Type: application/json&#39; \<br>--header &#39;Authorization: xxxxxx&#39; \<br>--data &#39;{<br>    &quot;jobName&quot;: &quot;testJob&quot;,<br>    &quot;jobParametersMap&quot;: {}<br>}&#39;</pre><p>It is a POST API which needs only 2 attributed in request body:</p><ol><li>jobName: A mandatory attribute; The name of the job to be executed.</li><li>jobParametersMap: Optional, A map of type &lt;String,String&gt; to add any parameter to job. Can be any meta-data client may need to identify or analyse job execution. This will not be used and will just be saved to job execution context to be returned later.</li></ol><p>If the job is launched successfully, the API responds with HTTP status 200.</p><p>If the metadata of the job contains a call back URI, the execution details are sent to that endpoint once execution completes.</p><p>We have a second API, which can be used to check on the state of job execution. It returns the state of executing job instance if any, or the state of last executed job.</p><pre>curl --location &#39;{{application-end-point}}/v1/job/archive/status?jobName=testJob&#39; \<br>--header &#39;Authorization: xxxx&#39;</pre><p>This is a GET API and expects a query param jobName.</p><h3>ARCHIVAL EXECUTED JOB INFO</h3><p>This is a data class that we use to convey the State of Job Execution. After job execution, this is sent as a payload to the call back URI present in Archival Meta Data and is also returned as a response to the Job Status API. Here are the attributes present in this data class:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2sJd5u6AeDnaxHlf-9TLWg.png" /><figcaption>ArchivalExecutedJobInfo</figcaption></figure><h3><strong>MONITORING</strong></h3><p>Spring batch provides support for batch execution metrics via Micrometer. These metrics can be pushed into any time series DB from where they can be used for creating dashboards and set up alerts. We use influxDB and Grafana for the same. The metrics exposed by Spring Batch are:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yZyBSYy2V4Tl43TjIF78MA.png" /><figcaption>Spring Batch Exposed Metrics</figcaption></figure><p>Here are a couple of sample graphs, we have set up, depicting read/write counts and execution and active tasks in Batch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fq0ZUfBAD-jsFpicsJMZUA.png" /><figcaption>Grafana Dashboard using Micrometer Metrics</figcaption></figure><p>In addition to these, we also use our very own SMART API,(about which I mentioned in a previous article <a href="https://medium.com/airteldigital/how-airtel-revamped-the-in-house-api-logging-framework-e1e2a78b4dd8">here</a>) to push the Archival Executed Job Info to Elastic and then use Kibana for visualisation and alerting. Here are some sample graphs plotted using the same:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pXfaI8UNcyyxdJqGg2t1gw.png" /><figcaption>Graphs Plotted in Kibana using Data pushed via SMART API.</figcaption></figure><h3><strong>CONCLUSION</strong></h3><p>We have been using this utility to transfer data successfully to our archive data base for over 2 years now. We operate with both read size and chunk size of 10,000 documents and the time taken to archive around 3 million documents is 30 minutes with around 6–7 jobs running in parallel on a single machine.</p><p>We have configured a skip of 7 days and have schedulers in place which launch the jobs at midnight daily. Hence utility transfers one day data of T-7th day daily. If somehow the utility fails to run on any given day, it is intelligent enough to next day transfer data of T-8th and T-7th day. We have set up TTL index of 15 days in our live database to enable auto purge of data. This means our utility has at least 8 attempts to move the data.</p><p>With all dashboards and alerts in place, we can be sure of not loosing any data.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9181e47bec99" width="1" height="1" alt=""><hr><p><a href="https://medium.com/airteldigital/data-archival-solution-for-on-premises-mongodb-9181e47bec99">Data Archival Solution for on-premises MongoDB</a> was originally published in <a href="https://medium.com/airteldigital">Airtel Digital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Airtel revamped the in-house API Logging Framework]]></title>
            <link>https://medium.com/airteldigital/how-airtel-revamped-the-in-house-api-logging-framework-e1e2a78b4dd8?source=rss-5012528448c2------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1e2a78b4dd8</guid>
            <category><![CDATA[application-development]]></category>
            <category><![CDATA[api-management]]></category>
            <category><![CDATA[api-logging]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <dc:creator><![CDATA[Sachin Sharma]]></dc:creator>
            <pubDate>Fri, 14 Oct 2022 04:19:03 GMT</pubDate>
            <atom:updated>2022-10-14T04:19:03.612Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/526/1*g_i17gmD2sVgfI-g87CphQ.png" /></figure><h3>Brief about Smart API</h3><p>Every modern application based on REST APIs requires extensive logging and auditing of all the interactions.</p><p>Application logs are always there to serve the purpose, but it is tiresome and time consuming to debug via application logs. At airtel, we came up with the idea of defining a definite schema for logging all requests and responses. The idea was to be able to easily visualize all the required data by restricting it to a predefined schema and at the same time provide applications the flexibility to choose the data points as per use case basis.</p><p>The key issues which led to the building of Smart API:</p><p>· Highly unstructured nature of application logs.</p><p>· Too much noise in application logs.</p><p>· Tools like Logstash could be used but, it again was CPU intensive and had maintenance and integration overheads for every new application.</p><h3>What is Smart API?</h3><p>Smart API is a framework which leverages Kafka to push auditing logs and then persists the logs in Elastic Search to enable visualization via Kibana.</p><p>Seems almost like Logstash? Doesn’t it? Well, the difference is in the data that is pushed to Kafka. Smart API does not push unstructured application logs, instead it leverages Apache Avro Schema to push structured data.</p><p>Apache Avro™ is a data serialization system.<br> Avro relies on schemas. When Avro data is read, the schema used when writing it is always present. This permits each datum to be written with no per-value overheads, making serialization both fast and small. This also facilitates use with dynamic, scripting languages, since data, together with its schema, is fully self-describing.</p><h3>SMART API ARCHITECTURE:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*DGW7_QBIFj74LSLOWCRhUA.png" /></figure><h3>Need for Revamp?</h3><p>Though we had been using this in many of our applications for a few years now, recently we hit a bottleneck in using the core logging framework of SMART-API. To give more context this is basically a java artifact which can be included in any application and exposes an annotation that can be used to push smart logs (an AVRO schema based log) to KAFKA.</p><p>The existing Smart API has proved to be quite robust and efficient at handling audit logs, but as mentioned above we ran into a few bottlenecks:</p><ol><li>The existing framework is <strong>not able to handle Reactive Stack</strong> neither on netty nor on tomcat. We had to do a bypass of an async event for that. It will also be <strong>difficult to handle GRPC calls</strong> via existing system.</li><li>It does not support Java Modules. So, to use it in a project using java module, we will have to add <strong>— add-reads my.module=ALL_UNNAMED </strong>to JVM.</li><li>It relies extensively on Spring’s RequestContextHolder. There are scenarios where we need to do some logging asynchronously and there is no HttpContext available.</li></ol><h3>Challenges?</h3><p>While designing a generic system which can support multiple stacks, we faced some really interesting challenges:</p><ul><li>Unavailability of JAVAX in an app running on Netty using Spring WebFlux.</li><li>Our utility should be usable by both Named and Unnamed Java Modules.</li><li>Since Reactor is completely non-blocking and we do not want to interfere with actual request execution. We wanted to perform logging in doOn Hooks. But as mentioned in <a href="https://github.com/reactor/reactor-core/issues/2615">this GitHub issue</a>. DoOn Hooks do not support context propagation.</li><li>Utility should be easily extensible for new stacks and new logging strategies in future.</li><li>Utility should provide a definite structure to populate log data but should be flexible enough at the same time to allow applications to choose data ingestion sources.</li></ul><h3>Solution:</h3><p>As a solution, we decided to re-write (re-arch 😁) the smart-api utility.</p><p>This new smart API exposes an configurable annotation, which takes in two parameters:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*NiD8usuyo7AKgEVjNQP--Q.png" /></figure><ol><li>loggerName: An Enum which defines the kind of logging to use. Currently we support NON_REACTIVE_AVRO_LOGGER and REACTIVE_AVRO_LOGGER(for Mono and Flux return types).</li><li>loggingEvent: An interface which defines base operations performed as a part of all logging strategies.</li></ol><h4><strong>Logging Event:</strong></h4><p>This interface is what allows the utility to be flexible enough to handle different scenarios and provides multiple interception points at different stages of logging and api Execution.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aGjMKSad7Zwkfr1UoMjUig.png" /></figure><ol><li>getApiName: Returns a name to identify the different APIs</li><li>generateNewLoggingSchema: The type of Logging Schema is a generic. The important thing to note here is that the pipeline relies on logs to follow a definite schema. So logging events should declare their schema. The type of schema is used to determine the strategy for logging.</li><li>populateRequestFields: Here, application is expected to populate any information before the actual execution of API. The input arguments are provided in arguments array, This method enables providing an interception point before actual execution.</li><li>populateResponseFields: Here, application is expected to add any logs related to result of the API execution.</li><li>populateExceptionDetails: This interception point is invoked in case the API execution throws an error.</li><li>getLoggableRequestContext: This allows an application to provide custom request context, thereby enabling the application to control request context ingestion. If not provided the utility is smart enough to identify the server stack the application is running on and then uses in-built utilities to populate Request Context.</li></ol><p>This interface can always be extended to provide specific operations for different Logging Strategies. Currently we, have only Avro based logging Strategy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*_1NSxNVIR3KmJVE3C9umOg.png" /></figure><p>getKafkaTopic(): The Kafka topic to push API logs to.</p><h3>Challenges Resolved:</h3><ol><li>The abstractions that we have make our utility flexible enough to handle multiple logging strategies and application stacks.</li><li>We identified all our packages that will have to be used by applications, and exposed them to all modules in our java module.</li><li>For java x servlet, we included it as an optional dependency in our pom and a static requirement in our module-info.java . This ensures that the dependency is available at compile time and is not required at link time and run time. This assumes that whenever the functionality related to servlet is invoked, it will be provided by the application or system.</li><li>We use Project Reactors Mono Context and Hook with a Thread Local to maintain request context in reactive applications.</li></ol><p><strong>ReactiveRequestContextFilter</strong> uses Mono.contextWrites to write requestContext in Mono context before the execution of request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o8hSKoVf06p5QEbmGXRgTA.png" /></figure><p>a) <strong>ThreadLocalContextFilter:</strong> It implements a BiFunction which can be used as a Hook in a project reactor app and will intercept all subscriptions. Here we use ThreadLocal to preserve the context and then use this context to create our LoggableRequestContext.</p><p>b) This ContextFilter is registered in Reactor Hooks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gINnihRc9ZntOhmZiOQnrA.png" /></figure><h3>Conclusion:</h3><p>We have been able to use this across all our Spring based projects for capturing and viewing more detailed audit logs with required important business KPI’s and provide better debugging with more insightful data rather than just access LOGS. The USPs of this revamped version are</p><p>1. It can be used with both Spring Reactive and Web MVC and also non web applications.</p><p>2. The ability to provide more control to the application to log Request Context.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1e2a78b4dd8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/airteldigital/how-airtel-revamped-the-in-house-api-logging-framework-e1e2a78b4dd8">How Airtel revamped the in-house API Logging Framework</a> was originally published in <a href="https://medium.com/airteldigital">Airtel Digital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>