{"id":3283,"date":"2022-03-29T10:19:57","date_gmt":"2022-03-29T10:19:57","guid":{"rendered":"https:\/\/www.paytmmoney.com\/blog\/?p=3283"},"modified":"2022-03-29T14:03:38","modified_gmt":"2022-03-29T14:03:38","slug":"profit-and-loss-report-generation","status":"publish","type":"post","link":"https:\/\/www.paytmmoney.com\/blog\/profit-and-loss-report-generation\/","title":{"rendered":"Profit And Loss Report Generation"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">India has about 150 million active investors and 750 million demat accounts which is around 6 percent of the total population as against 50% in US, 30% in UK. With this number expected to grow in India the number of Transactions and Holdings is also expected to grow with individual users contributing at least 500 to 2000 holdings \/ realized transactions yearly. This data would be ever increasing in nature i.e. once a user has squared off his \/ her position he\/she can come anytime and see what profit or loss was made by him \/ her in a given tenure.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">It would not be an easy task to maintain the data from inception and share users their profit and loss statement. Moreover it is a compute intensive operation as details are returned as a report file. Initial system load tests and data size validated that the design of the system suits to have an asynchronous workflow. The goal was to process user requests as fast as possible and reliably deliver reports to recipients while offering strong observability and telemetry.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In this blog, we will share some of the challenges we came across and the learnings we gained in the process of implementing a highly scalable Profit and Loss Statement Report Generation system.<\/span><\/p>\n<p><b>Problem Statement<\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Generate Profit and Loss statement for holdings and Realized Detail from data scattered across multiple datasources.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Scalability: Make sure the system can handle spikes in user requests for statements during tax filing months and at the same time ensure cost effectiveness<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Resiliency: Design the system to degrade gracefully and recover in case of system failures.<\/span><\/li>\n<\/ol>\n<p><b>Architecture<\/b><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">The use case of our system was such that it should be able to deal with ever growing data, its size and still provide high availability. After multiple load tests and data analysis, we determined that asynchronous processing suffices our current and future requirements. The flow of the system was segregated into Incoming Requests, Processing and Outgoing Response tasks.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">As a result, the architecture can be broadly divided into 3 systems<\/span><\/p>\n<p><b>1. Request<\/b><span style=\"font-weight: 400;\">: The request is accepted from the user and passed onto the queueing system. In case the user requested to download the report, a unique identifier across application instances is returned as a successful initial response. If an email report was requested, a successful acceptance response is returned. The acceptance of the request triggers an AWS Lambda function responsible for the processing tasks.<\/span><\/p>\n<p><b>2. Processing<\/b>: The AWS Lambda function takes care of the processing system which involves creating a Profit and Loss excel report by using multiple source databases. In our case it was ElasticSearch and Microsoft SQL. A single connection was created per Lambda function with the underlying databases to avoid undue resource creation. All the report requests were processed in parallel threads to enhance the compute speed. After the report is generated, it is placed in Amazon S3 storage and a pre-signed URL of the file is generated.<\/p>\n<p><b>3. Response<\/b><span style=\"font-weight: 400;\">: With the pre-signed URL, the report is either emailed to the user or it is placed in Redis which can be polled as requested by the client. The polling of the report is done in arithmetic progression until a set time duration.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Usage of Async processing\u00a0 and message queuing makes sure that the delivery is guaranteed as we always preserve the request in the queue, in case of any failure during processing tasks the request will be re-processed. This helps\u00a0 make the system resilient. Segregating the request and processing systems makes it easy to implement API versioning.<\/span><\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-3290\" src=\"https:\/\/paytmmoney.wpengine.com\/wp-content\/uploads\/2022\/03\/PnL_Architechture-1.jpg\" alt=\"PnL Architechture\" width=\"1091\" height=\"625\" srcset=\"https:\/\/www.paytmmoney.com\/blog\/wp-content\/uploads\/2022\/03\/PnL_Architechture-1.jpg 1091w, https:\/\/www.paytmmoney.com\/blog\/wp-content\/uploads\/2022\/03\/PnL_Architechture-1-300x172.jpg 300w, https:\/\/www.paytmmoney.com\/blog\/wp-content\/uploads\/2022\/03\/PnL_Architechture-1-1024x587.jpg 1024w, https:\/\/www.paytmmoney.com\/blog\/wp-content\/uploads\/2022\/03\/PnL_Architechture-1-768x440.jpg 768w\" sizes=\"(max-width: 1091px) 100vw, 1091px\" \/><\/p>\n<p><b>High Throughput<\/b><span style=\"font-weight: 400;\"> : <\/span><span style=\"font-weight: 400;\">By using an asynchronous design pattern we are able to achieve high throughput with less number of application instances. Moreover we were able to control the utilization of our downstream systems by modulating the number of requests we want to process at a given time. Thereby preventing a cascading effect on overutilized systems. The\u00a0 Microsoft SQL database was a single instance read replica that was part of an external component we were integrating with, and hence didn\u2019t have direct control over the architecture \/ data model. And hence had to workaround this limitation that was a bottleneck in the flow and find a sweet spot.<\/span><\/p>\n<p><b>Persistence<\/b><span style=\"font-weight: 400;\">:\u00a0<\/span><span style=\"font-weight: 400;\">The storage of file URLs in Redis and reports generated in Amazon S3\u00a0 had their corresponding TTL (Time To Live).\u00a0<\/span><\/p>\n<p><b>Cost Effective Development<\/b><span style=\"font-weight: 400;\">: <\/span><span style=\"font-weight: 400;\">We were able to utilize the event driven, serverless computing service AWS Lambda which is charged \/ billed only for the duration the function is run. Moreover, we would be able to control the number of messages consumed and concurrent Lambda functions we want to run. Being a compute intensive process, the downside of using a Lambda function was the time involved in cold start (initial code spin up) and no control over the CPU allocation to the function.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">As per the AWS documentation, CPU allocated to lambda execution is proportional to RAM configured. Report generation however was not memory intensive hence RAM was underutilized which led to lower CPU allocation.We did however see\u00a0 improvement in the processing stage when the configured RAM was increased which in turn elevated the number of CPU cores.<\/span><\/p>\n<p><b>Gains<\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">A High throughput system ready for request bursts.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Cost Effective Infrastructure utilization\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Guaranteed response delivery due to the usage of a message queue<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Generic implementation for API versioning\u00a0<\/span><\/li>\n<\/ol>\n<p><b>Losses:<\/b><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Constant compute ability<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Cold start latency<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Reached so far !!<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Today we came across one of the system designs utilizing asynchronous flows for a compute heavy workflow of generating a report file with multiple underlying data sources. The future might hold using the same Lambda function to generate other reporting as well.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Most importantly, valuable feedback for the blog is always welcome. One can share the same on ankit9.singh@paytm.com. Users with a demat account with Paytm Money can find the utility on the Paytm Money website \/ Paytm Money application.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Cheers and Happy Learning.<\/span><\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-3289\" src=\"https:\/\/paytmmoney.wpengine.com\/wp-content\/uploads\/2022\/03\/PnLWeb-1-1.png\" alt=\"PnL Web\" width=\"1322\" height=\"421\" srcset=\"https:\/\/www.paytmmoney.com\/blog\/wp-content\/uploads\/2022\/03\/PnLWeb-1-1.png 1322w, https:\/\/www.paytmmoney.com\/blog\/wp-content\/uploads\/2022\/03\/PnLWeb-1-1-300x96.png 300w, https:\/\/www.paytmmoney.com\/blog\/wp-content\/uploads\/2022\/03\/PnLWeb-1-1-1024x326.png 1024w, https:\/\/www.paytmmoney.com\/blog\/wp-content\/uploads\/2022\/03\/PnLWeb-1-1-768x245.png 768w\" sizes=\"(max-width: 1322px) 100vw, 1322px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Team Members: <a href=\"https:\/\/in.linkedin.com\/in\/ganesh-kumar-a0a66834\">Ganesh Kumar<\/a>, <a href=\"https:\/\/in.linkedin.com\/in\/rajkumarsaroj\">Raj Saroj<\/a>, <a href=\"https:\/\/in.linkedin.com\/in\/ankit-singh-a57907123\">Ankit Singh<\/a><\/span><\/p>\n<p><span style=\"font-weight: 400;\">Blog Contribution : <a href=\"https:\/\/in.linkedin.com\/in\/ankit-singh-a57907123\">Ankit Singh<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>India has about 150 million active investors and 750 million demat accounts which is around 6 percent of the total population as against 50% in US, 30% in UK. With this number expected to grow in India the number of Transactions and Holdings is also expected to grow with individual users contributing at least 500<a href=\"https:\/\/www.paytmmoney.com\/blog\/profit-and-loss-report-generation\/\">Continue reading <span class=\"sr-only\">&#8220;Profit And Loss Report Generation&#8221;<\/span><\/a><\/p>\n","protected":false},"author":31,"featured_media":3284,"comment_status":"open","ping_status":"open","sticky":false,"template":"single-classic-ns.php","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-3283","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/posts\/3283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/comments?post=3283"}],"version-history":[{"count":0,"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/posts\/3283\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/media\/3284"}],"wp:attachment":[{"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/media?parent=3283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/categories?post=3283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.paytmmoney.com\/blog\/wp-json\/wp\/v2\/tags?post=3283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}