<?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 Danylo Halaiko on Medium]]></title>
        <description><![CDATA[Stories by Danylo Halaiko on Medium]]></description>
        <link>https://medium.com/@d9nich?source=rss-f50118a3e717------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Xrj_EwCVS5OZUorRN1K8yw.jpeg</url>
            <title>Stories by Danylo Halaiko on Medium</title>
            <link>https://medium.com/@d9nich?source=rss-f50118a3e717------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 09:58:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@d9nich/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[Back-of-the-Envelope Estimation: Simplifying System Design with Quick Approximations]]></title>
            <link>https://d9nich.medium.com/back-of-the-envelope-estimation-simplifying-system-design-with-quick-approximations-a3a9e96da4af?source=rss-f50118a3e717------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3a9e96da4af</guid>
            <category><![CDATA[analysis]]></category>
            <category><![CDATA[design-thinking]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[estimations]]></category>
            <category><![CDATA[planning]]></category>
            <dc:creator><![CDATA[Danylo Halaiko]]></dc:creator>
            <pubDate>Mon, 29 May 2023 07:01:54 GMT</pubDate>
            <atom:updated>2023-05-29T07:01:54.394Z</atom:updated>
            <content:encoded><![CDATA[<p>Have you ever needed to make a quick estimation or gain a rough understanding of a problem or scenario without delving into complex calculations or precise data? That’s where back-of-the-envelope estimates come to the rescue. Also known as envelope math or back-of-the-napkin estimations, this approach allows individuals to make quick and rough estimations using simplified assumptions and basic arithmetic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_g6zEJ2zr-a6blXt" /><figcaption>Photo by <a href="https://unsplash.com/@joannakosinska?utm_source=medium&amp;utm_medium=referral">Joanna Kosinska</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><blockquote>“<strong>Back-of-the-envelope calculations</strong> are estimates you create using a combination of thought experiments and common performance numbers to get a good feel for which designs will meet your requirements,” says o <a href="https://www.linkedin.com/in/jeff-dean-8b212555/">Jeff Dean, Google Senior Fellow</a>.</blockquote><p>Back-of-the-envelope calculations, or back-of-the-napkin or envelope math, refer to quick and rough estimations using simplified assumptions and basic arithmetic. This approach allows individuals to gain a general understanding or ballpark figure for a problem or scenario without relying on complex calculations or precise data.</p><p>The term “back-of-the-envelope” originates from making calculations on the back of an envelope, often in informal or impromptu situations where a pen and paper are readily available. It emphasizes the simplicity and roughness of the calculations, as they are not meant to provide precise results but relatively quick approximations.</p><p>Back-of-the-envelope calculations can be used in various fields, such as science, engineering, business, and everyday life. These calculations are handy when time or resources are limited and precise answers are not essential. They are a valuable tool for making initial assessments, exploring feasibility, evaluating options, and gaining a preliminary understanding of a problem.</p><p>Performing a back-of-the-envelope calculation typically involves breaking down a complex problem into simpler components and making reasonable assumptions to simplify the calculations. For example, you want to estimate the total cost of a construction project. In that case, you might individually approximate the materials, labour, and overhead expenses and then add them together for a rough estimation.</p><p>While back-of-the-envelope calculations can be quick and convenient, it’s essential to recognize their limitations. Due to their nature, they are prone to <strong>significant inaccuracies</strong> and may <strong>not account</strong> for <strong>all</strong> relevant <strong>factors</strong>. Therefore, these calculations should be used cautiously, and their results should be interpreted as rough estimates rather than precise values.</p><p>Back-of-the-envelope calculations can be a valuable starting point for further analysis or decision-making processes despite their limitations. They <strong>help to identify the key drivers</strong> or variables <strong>affecting a problem</strong>, assess the feasibility of an idea, or determine whether a more detailed and precise analysis is warranted.</p><p>In conclusion, back-of-the-envelope calculations are a practical and accessible method for making rough estimations or approximations in various fields. They allow individuals to quickly understand problems, explore possibilities, and make initial assessments. While they have their limitations regarding accuracy and precision, they serve as a valuable tools for decision-making when time and resources are limited.</p><h4>Latency numbers every programmer should know.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5HU95OLN226xWrK5avf10w.png" /><figcaption><a href="https://colin-scott.github.io/personal_website/research/interactive_latency.html">Latency numbers every programmer should know</a>.</figcaption></figure><p>In 2010 Dr Dean working at Google, revealed the typical computer operation.</p><p>Some information can be outdated, but some facts are still valid:</p><ul><li>Ram is faster than disk</li><li>Disk seeks are expensive</li><li>The simple compression algorithm is fast</li><li>Compressing data before sending it over the Internet is a good idea</li><li>Data centres take time to send data between them.</li></ul><h3>Example</h3><h4>Assumptions:</h4><ol><li>The average number of users: 100,000 per day</li><li>The average number of requests per user: 10</li><li>Average response time per request: 50 milliseconds (ms)</li></ol><h4>Calculation:</h4><p><strong>Step 1:</strong> Estimate the total number of requests per day.</p><p><em>Total requests per day = Average number of users × Average number of requests per user</em></p><p>Total requests per day = 100,000 × 10 = <strong>1,000,000</strong> requests</p><p><strong>Step 2:</strong> Estimate the average requests per second.</p><p><em>Total requests per second = Total requests per day / (24 hours × 60 minutes × 60 seconds)</em></p><p>Total requests per second = 1,000,000 / (24 × 60 × 60) ≈ <strong>11.6</strong> requests per second</p><p><strong>Step 3:</strong> Estimate the server capacity required. To estimate the server capacity, we must consider the maximum number of requests a single server can handle per second. Let’s assume a conservative estimate of 100 requests per second per server.</p><p><em>Number of servers required = Total requests per second / Maximum requests per second per server</em></p><p>Number of servers required = 11.6 / 100 ≈ <strong>0.12</strong> servers</p><p>Since we can’t have a fraction of a server, we must round it up to at least one server.</p><p>Based on this back-of-the-envelope calculation, we would require at least one server to handle the estimated high-load traffic for the product.</p><p>It’s important to note that this is a simplified estimation, and there are several other factors to consider in a real-world scenario, such as redundancy, load balancing, database requirements, network bandwidth, and server performance. This calculation provides only a rough estimation and would need further analysis and refinement for an accurate system design.</p><p>In conclusion, back-of-the-envelope calculations provide a practical and accessible method for making rough estimations or approximations in various fields. While their accuracy and precision may be limited, they serve as valuable tools for decision-making processes, enabling individuals to gain initial insights and make informed choices when faced with time constraints or limited resources. By understanding their purpose and limitations, practitioners can leverage back-of-the-envelope calculations as a valuable tool in their problem-solving arsenal.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3a9e96da4af" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Questions and Answers for Middle Go Developer Interviews: A Comprehensive Guide]]></title>
            <link>https://d9nich.medium.com/questions-and-answers-for-middle-go-developer-interviews-a-comprehensive-guide-ab52b6aaecf0?source=rss-f50118a3e717------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab52b6aaecf0</guid>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[questions]]></category>
            <dc:creator><![CDATA[Danylo Halaiko]]></dc:creator>
            <pubDate>Mon, 22 May 2023 07:01:57 GMT</pubDate>
            <atom:updated>2023-05-22T07:01:57.470Z</atom:updated>
            <content:encoded><![CDATA[<p>As the field of software development continues to evolve, so does the demand for skilled professionals in various programming languages. Go (or Golang) has gained significant popularity among these languages due to its simplicity, efficiency, and powerful concurrency support. With companies increasingly adopting Go for their projects, the need for capable Go developers has never been higher.</p><p>If you aspire to pursue a career as a Go developer or are preparing for a middle-level Go developer interview, this article is for you. This comprehensive guide will explore some commonly asked questions and provide detailed answers to help you prepare effectively.</p><p>The interview process for a middle-level Go developer position typically involves assessing your understanding of Go language fundamentals, problem-solving abilities, familiarity with Go’s concurrency patterns, and knowledge of best practices in Go development. By familiarizing yourself with these commonly asked questions, you can confidently approach your interview and demonstrate your Go programming expertise.</p><p>This article will equip you with the knowledge and insights to tackle middle-level Go developer interview questions effectively. We will cover various topics, including Go language basics, data structures, algorithms, error handling, testing, and concurrency patterns. Each question will be accompanied by a detailed explanation, ensuring a comprehensive understanding of the underlying concepts.</p><p>Whether you are new to Go development or looking to enhance your existing skills, this guide will be a valuable resource to help you navigate the interview process successfully. By studying these questions and their answers, you will strengthen your technical prowess and gain valuable insights into the best practices and idiomatic patterns of Go programming.</p><p>Remember, the interview process is not solely about finding the “<strong>right</strong>” answer but also <em>evaluating your problem-solving approach, analytical thinking, and ability to apply your knowledge in real-world scenarios</em>. Therefore, while we provide sample answers, it is essential to understand the underlying concepts and adapt them to address specific problem statements during the interview.</p><p>So, let’s dive into the world of middle-level Go developer interview questions and equip ourselves with the tools necessary to excel in these interviews. Whether seeking to bolster your knowledge or preparing for an upcoming interview, this guide will help you navigate the challenges and emerge as a confident and skilled Go developer.</p><p>⚠️: The questions and answers in this article are meant to serve as a <strong>reference </strong>and learning resource. The specific questions asked in an interview may vary depending on the company, interviewer, and the role you are applying for. It is crucial to thoroughly research the company and its requirements to tailor your preparation accordingly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uAAIvvm48ZmbFv5x" /><figcaption>Photo by <a href="https://unsplash.com/@craftedbygc?utm_source=medium&amp;utm_medium=referral">Unseen Studio</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3><strong>Software engineering</strong></h3><h4>1. What are the advantages and disadvantages of using protobuf compared to JSON?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*42agE8CVthtFL6R1" /><figcaption>Photo by <a href="https://unsplash.com/@douglasamarelo?utm_source=medium&amp;utm_medium=referral">Douglas Lopes</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Protocol Buffers (protobuf) and JSON are popular data interchange formats in modern software development. Here are some advantages and disadvantages of using protobuf compared to JSON:</p><p>Advantages of protobuf:</p><ul><li>Smaller message size: protobuf messages are binary encoded, resulting in a smaller size than JSON messages. This can be especially important for network communication and storage, where bandwidth and disk space are at a premium.</li><li>Faster serialization and deserialization: protobuf messages can be serialized and deserialized much faster than JSON messages due to binary encoding.</li><li>Efficient schema evolution: protobuf supports schema evolution, which means you can change your messages&#39; schema without breaking compatibility with older clients that may still be using the previous schema.</li><li>Strongly typed messages: protobuf messages are strongly typed, meaning that the message&#39;s structure and types are defined in a separate schema file. This can help catch errors early and provide more explicit documentation.</li></ul><p>Disadvantages of protobuf:</p><ul><li>Harder to read and debug: protobuf messages are binary encoded, making them harder to read and debug than JSON messages.</li><li>Requires schema definition: to use protobuf, you need to define the schema of your messages in a separate file, which adds some overhead to the development process.</li><li>Limited tooling: JSON has more widespread support and tooling compared to protobuf, which can make it easier to work with in some cases.</li></ul><p>In summary, protobuf can provide significant performance benefits over JSON, especially when message size and serialization speed are critical factors. However, the added complexity of defining and maintaining a schema can make it more difficult to work with, especially for smaller projects or those prioritising developer productivity over performance. Ultimately, the choice between protobuf and JSON depends on your project&#39;s specific needs and constraints.</p><p>📗: Read more enhanced about storing formats in <a href="https://medium.com/@d9nich/json-xml-protobuf-thrift-avro-or-everything-you-need-to-know-about-encoding-data-6077a7e769e2">my previous article</a>.</p><h4>2. Name some of the principles of the 12-factor app. What is graceful shutdown used for?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gB_0i98xeuqJR0eA" /><figcaption>Photo by <a href="https://unsplash.com/@janesky?utm_source=medium&amp;utm_medium=referral">Antonio Janeski</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The 12-factor app is a methodology for building modern, scalable, and portable web applications. Some of the principles of the 12-factor-app are:</p><ol><li><strong>Codebase</strong>: One codebase tracked in revision control, many deploys.</li><li><strong>Dependencies</strong>: Explicitly declare and isolate dependencies.</li><li><strong>Config</strong>: Store configuration in the environment.</li><li><strong>Backing Services</strong>: Treat backing services as attached resources.</li><li><strong>Build, Release, Run</strong>: Strictly separate build and run stages.</li><li><strong>Processes</strong>: Execute the app as one or more stateless processes.</li><li><strong>Port Binding</strong>: Export services via port binding.</li><li><strong>Concurrency</strong>: Scale out via the process model.</li><li><strong>Disposability</strong>: Maximize robustness with fast startup and graceful shutdown.</li><li><strong>Dev/prod parity</strong>: Keep development, staging, and production as similar as possible.</li><li><strong>Logs</strong>: Treat logs as event streams.</li><li><strong>Admin processes</strong>: Run admin/management tasks as one-off processes.</li></ol><p>Graceful shutdown allows a running application to finish processing any outstanding requests or tasks before it stops completely. This ensures that the application does not abruptly terminate, which can cause data loss or other issues. A graceful shutdown involves sending a signal to the running application to stop accepting new requests and then waiting for the existing requests to finish processing before shutting down the application completely.</p><h4>3. What is dependency injection? And dependency inversion?</h4><blockquote>See this article <a href="https://www.daedtech.com/dependency-injection-or-inversion/">here</a></blockquote><blockquote>The author differentiates these two in simple words. <strong>Dependency Injection == “Gimme it” and Dependency Inversion == “Someone take care of this for me, somehow.”</strong>. In dependency inversion principle, high level module is the owner of the abstraction. Thus the detail(implementation of the abstraction) is depends on the abstraction and hence depends on the high level module. Dependency inverted!.. Dependency injection is different. The abstraction might not be kept by the high level module. Thus an abstraction given to the higher level object might not be limited to the needs of the high level module.</blockquote><blockquote>Dependency inversion :</blockquote><blockquote>You have a higher level module X and an abstraction Y which is defined by X. Z implements Y and is given to X. Thus Z is dependent of X(via the abstraction Y defined by X).</blockquote><blockquote>Dependency injection:</blockquote><blockquote>You have an higher level module X which needs functionalities A and B. Y is an abstraction which contains the functionalities A, B and C. Z implements Y. Since Z implements Y and hence have functionalities A and B, Z is given to X. Now X is dependent of Y.</blockquote><p>🔗: Original post in <a href="https://stackoverflow.com/a/46710071/13321534">StackOverflow</a></p><h4>4. You have ssh access to the Linux server running the web server. Unplanned restarts occur periodically with the server. The server writes logs to files (log file size &gt; 10 Mb). What *nix commands can you use to analyze the problem?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rYkNk3rTxlyK1tOQ" /><figcaption>Photo by <a href="https://unsplash.com/@6heinz3r?utm_source=medium&amp;utm_medium=referral">Gabriel Heinzer</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Several *nix commands can be used to analyze the problem of unplanned restarts on a Linux server running a web server and writing logs to files. Here are some of them:</p><ol><li>tail: This command can be used to monitor the web server log files in real time and display the last 10 lines of the file. For example, to monitor the Apache access log file, you can run the command tail -f /var/log/httpd/access_log.</li><li>grep: This command can search for a specific string or pattern in the log files. For example, to search for all the entries in the Apache error log file that contain the string &quot;500&quot;, you can run the command grep &quot;500&quot; /var/log/httpd/error_log.</li><li>top: This command can monitor the system resources, such as CPU and memory usage, and identify processes that consume too many resources. For example, to monitor the CPU usage, you can run the command top -o %CPU.</li><li>ps: This command can be used to display the processes that are currently running on the system. For example, to display the Apache processes, you can run the command ps aux | grep httpd.</li><li>df: This command can display the disk space usage on the system. For example, to display the disk space usage of the /var/log directory, you can run the command df -h /var/log.</li><li>dmesg: This command can display the kernel ring buffer messages containing information about hardware or software problems. For example, to display the last 50 kernel messages, you can run the command dmesg | tail -50.</li><li>systemctl: This command can manage the system services, such as starting, stopping, or restarting the web server. For example, to restart the Apache service, you can run the command systemctl restart httpd.</li></ol><p>To analyze the problem of unplanned restarts, it is recommended to start with the system resource usage, such as CPU, memory, and disk space, and then move on to the web server logs to identify any errors or issues. The system services can be restarted if necessary, or the kernel messages can be checked for hardware or software issues.</p><h4>5. How does the TLS handshake work?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VDvIt_aNATilX-72" /><figcaption>Photo by <a href="https://unsplash.com/@flyd2069?utm_source=medium&amp;utm_medium=referral">FLY:D</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The Transport Layer Security (TLS) handshake is a process that establishes a secure communication channel between two devices, typically a client and a server. The TLS handshake involves several steps:</p><ol><li><strong>Client hello</strong>: The client initiates the handshake by sending the server a “hello” message. The message includes the TLS version number, a list of supported cypher suites, and a random string (known as the “client random”).</li><li><strong>Server hello</strong>: The server responds with its own “hello” message. The message includes the TLS version number, the selected cypher suite, and another random string (known as the “server random”).</li><li><strong>Certificate exchange</strong>: The server sends its digital certificate to the client. The certificate contains the server’s public key, which will be used to establish an encrypted communication channel.</li><li><strong>Client key exchange</strong>: The client generates a random string and encrypts it using the server’s public key. The encrypted string is sent back to the server.</li><li><strong>Session key generation</strong>: The client and the server use the client random, server random, and exchanged key to generate a session key. This key will be used to encrypt and decrypt data during the communication.</li><li><strong>Change cypher spec</strong>: Both the client and the server send a message to indicate that they are switching to the agreed-upon cypher suite and encryption algorithm.</li><li><strong>Finished</strong>: Both the client and the server send a message to indicate that the handshake is complete and that they are ready to start communicating securely.</li></ol><p>Once the TLS handshake is complete, the client and the server can communicate securely using the agreed-upon encryption algorithm.</p><h4>6. What is the difference between TCP &amp; UDP? In which situation is UDP better?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XDYWNxQ5Gkm-jByc" /><figcaption>Photo by <a href="https://unsplash.com/@linkedinsalesnavigator?utm_source=medium&amp;utm_medium=referral">LinkedIn Sales Solutions</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) are the most commonly used protocols for sending data over the internet. The main difference is that TCP is a connection-oriented protocol, while UDP is connectionless.</p><p>A connection-oriented protocol must connect two endpoints before any data can be sent. TCP achieves this by establishing a three-way handshake between the two endpoints, exchanging SYN (synchronize) and ACK (acknowledge) packets to establish the connection. Once the connection is established, data is sent in a stream and is guaranteed to be received in the same order it was sent.</p><p>In contrast, UDP is a connectionless protocol, meaning data can be sent without establishing a connection. Instead, each packet is sent individually, and the receiver does not acknowledge receipt of each packet. This makes UDP faster and more lightweight than TCP but less reliable. Packets may be lost or arrive out of order, and it is up to the application to handle these errors.</p><p>UDP is better suited for situations where speed is more important than reliability. For example, online games often use UDP because it is faster and can handle many small packets, which is important for real-time gameplay. Video and audio streaming services also often use UDP, as the application can handle dropped packets without causing too much disruption. However, TCP is usually the better choice for applications where reliability is more important, such as file transfers or email.</p><h4>7. Explain in detail what happens when a client sends a request to a server, and the server receives the request</h4><p>When a client sends a request to a server, the following steps generally occur:</p><ol><li>The client initiates a connection to the server by sending a SYN packet to the server. This is known as the three-way handshake, where the client and server agree on a common protocol and set up a reliable connection between them.</li><li>Once the connection is established, the client sends a request line, headers, and, optionally, a message body to the server.</li><li>The server receives and parses the request, extracting the request method, headers, and message body as necessary.</li><li>The server determines which handler should handle the request based on the request method. The handler could be a function that generates a response or a piece of middleware that processes the request before passing it on to another handler.</li><li>The handler generates a response to the request. The response typically consists of a status line, headers, and, optionally, a message body.</li><li>The server sends the response back to the client.</li><li>The client receives the response and processes it, displaying the response to the user or taking some other appropriate action.</li></ol><p>Throughout this process, various layers of the network stack are involved, including the transport layer (TCP or UDP), the network layer (IP), and the application layer (HTTP or another protocol). Each layer adds its headers and metadata to the transmitted data, allowing it to be routed correctly and ensuring it arrives intact at its destination.</p><h4>8. What is Clean Architecture? Give an example</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ip2VWc-LO770oqlh" /><figcaption>Photo by <a href="https://unsplash.com/@umityildirim?utm_source=medium&amp;utm_medium=referral">Ümit Yıldırım</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Clean Architecture is a software architecture pattern that emphasizes the separation of concerns and the independence of any specific technology or framework. The main goal of Clean Architecture is to create a system that is easy to understand, test, and maintain. It consists of several layers, each with its own responsibilities and boundaries.</p><p>The most common layers in Clean Architecture are:</p><ol><li>Presentation layer: responsible for handling user input and output, including the user interface and web interface.</li><li>Use case layer: where the business logic of the application resides.</li><li>Domain layer: contains the application’s business entities and logic.</li><li>Infrastructure layer: contains all the technical details that make the system work, such as databases, servers, and external APIs.</li></ol><p>One example of Clean Architecture is a web application for a restaurant that allows customers to order food online. The presentation layer would handle the web interface and user input, and the use case layer would manage the order processing and delivery logic, the domain layer would define the order and payment entities and logic, and the infrastructure layer would handle the database and any external APIs used for payment processing or delivery tracking.</p><p>By separating the concerns and responsibilities of each layer, Clean Architecture allows developers to easily modify and maintain the codebase without affecting other parts of the system. It also makes it easier to test the application since each layer can be tested independently.</p><h4>9. What is RAM?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*8lSBXtEAK0q77nYQ" /><figcaption>Photo by <a href="https://unsplash.com/@luangjokaj?utm_source=medium&amp;utm_medium=referral">Luan Gjokaj</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>RAM</strong> stands for <strong>R</strong>andom <strong>A</strong>ccess <strong>M</strong>emory, a type of computer memory that allows data to be accessed randomly, i.e., without needing to read all the previous data in a specific sequence. It is a volatile memory, which means that the data stored in it is lost when the power to the computer is turned off. RAM is used by the computer’s operating system and software applications to temporarily store data and program code that is actively being used or processed. The more RAM a computer has, the more data and programs it can handle simultaneously without slowing down or crashing.</p><h4>10. What is the difference between stack &amp; heap?</h4><p>In computer programming, the stack and heap are two memory allocation techniques programs use during their execution.</p><p>The stack is a linear data structure that stores temporary variables created by a program during its execution. It is used for static memory allocation, which means that the memory is allocated during the compile-time phase of the program. The stack follows a last-in, first-out (LIFO) order, meaning the most recently added item will be the first to be removed. The operating system manages the stack, and its size is limited.</p><p>On the other hand, the heap is a more dynamic data structure used for dynamic memory allocation. It is a large pool of memory that can be allocated and deallocated at runtime. The heap is used for variables unknown at compile time or whose size is variable. Unlike the stack, the heap does not follow a specific order of allocation and deallocation. The operating system also manages the heap.</p><p>In summary, the main differences between the stack and the heap are:</p><ul><li>The stack is used for static memory allocation, while the heap is used for dynamic memory allocation.</li><li>The operating system manages the stack and has a limited size, while the heap is a large pool of memory that can be allocated and deallocated at runtime.</li><li>The stack follows a LIFO order, while the heap does not follow a specific order.</li></ul><h4>11. Why Docker and Kubernetes?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jOZFohCyxjURRBht" /><figcaption>Photo by <a href="https://unsplash.com/@growtika?utm_source=medium&amp;utm_medium=referral">Growtika</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>Docker </strong>and <strong>Kubernetes </strong>are two powerful technologies revolutionising software development, deployment, and management.</p><p><strong>Docker </strong>is a containerization platform that allows developers to create, package, and run their applications in isolated environments known as containers. Containers are lightweight and portable, providing a consistent runtime environment for applications regardless of the underlying infrastructure. Docker provides a way to package applications and their dependencies into a single unit, which can be deployed on any machine that supports Docker, making it a popular choice for building and deploying microservices-based architectures.</p><p><strong>Kubernetes</strong>, on the other hand, is an open-source container orchestration platform that automates containerised applications&#39; deployment, scaling, and management. It provides a way to manage multiple containers across multiple hosts, ensuring high availability, fault tolerance, and scalability. Kubernetes abstracts away the underlying infrastructure, making deploying applications on any cloud platform or on-premise data centre easy.</p><p>The combination of <strong>Docker </strong>and <strong>Kubernetes </strong>provides a powerful solution for building and deploying modern, cloud-native applications. Docker provides a way to package and deploy applications in containers, while Kubernetes provides a way to manage and scale those containers across multiple hosts, ensuring high availability and fault tolerance. Together, they provide a consistent and reliable way to deploy applications in modern, cloud-native environments.</p><h4>12. Tell us about the Kubernetes abstractions you worked with?</h4><p>Kubernetes is a container orchestration platform that provides a set of abstractions to manage containerized applications. Some of the Kubernetes abstractions are:</p><ol><li><strong>Pod</strong>: A Pod is the smallest deployable unit in Kubernetes. It represents a single instance of an application and can contain one or more containers.</li><li><strong>Service</strong>: A Service is an abstraction that defines a logical set of Pods and a policy to access them. It provides a stable IP address and DNS name that can be used to access the Pod.</li><li><strong>Deployment</strong>: A Deployment is an abstraction that provides declarative updates to Pods and ReplicaSets. It ensures that the desired number of replicas are running and provides rolling updates for the application.</li><li><strong>ReplicaSet</strong>: A ReplicaSet is an abstraction that ensures that a specified number of replicas of a Pod are running at any given time. Deployments use it to maintain the desired state.</li><li><strong>StatefulSet</strong>: A StatefulSet is an abstraction that manages the deployment and scaling of a set of stateful Pods. It provides stable network identifiers and persistent storage for each Pod.</li><li><strong>ConfigMap</strong>: A ConfigMap is an abstraction that provides a way to store configuration data for an application. It can store environment variables, command-line arguments, and configuration files.</li><li><strong>Secret</strong>: A Secret is an abstraction that provides a way to store sensitive data, such as passwords and API keys. It is stored in an encrypted format and can be mounted as a volume in a Pod.</li></ol><p>These abstractions provide a way to manage containerized applications in a scalable and reliable way. By using these abstractions, Kubernetes can provide features such as automatic scaling, self-healing, and rolling updates, which makes it easier to manage containerized applications.</p><h4>13. What is a Pod? How is it arranged?</h4><p>In Kubernetes, a Pod is the smallest deployable unit that consists of one or more containers that share a network and a storage namespace and are scheduled together on the same node.</p><p>A Pod is organized as follows:</p><ul><li>Each Pod has a unique IP address, which allows containers within the Pod to communicate with each other over the loopback network interface.</li><li>Each Pod can have one or more containers running inside it, which share the same network namespace and can communicate with each other via local inter-process communication (IPC) mechanisms and shared storage.</li><li>Each Pod has a unique hostname, the Pod’s metadata name and is used for DNS resolution within the Kubernetes cluster.</li><li>Pods are ephemeral and can be created, destroyed, and recreated at any time, so their storage is also ephemeral, but they can use volumes to store data beyond the life cycle of the Pod.</li><li>Pods can be horizontally scaled by creating multiple replicas of the same Pod template, and they can be managed and orchestrated by a higher-level Kubernetes object, such as a Deployment or a StatefulSet.</li></ul><h4>14. Tell us about Data structures: stack, queue, linked list, trie, balanced tree</h4><p>Data structures are ways of organizing data to efficiently perform operations such as search, insertion, deletion, and sorting. Here are brief explanations of some common data structures:</p><ol><li><strong>Stack</strong>: A stack is a linear data structure that follows the LIFO (Last In First Out) principle. Elements can only be added or removed from the top of the stack. It is useful when an order of execution is important, such as in a function call stack.</li><li><strong>Queue</strong>: A queue is a linear data structure that follows the FIFO (First In First Out) principle. Elements are inserted at the rear end and removed from the front end. It is useful when an order of processing is important, such as in a print job queue.</li><li><strong>Linked List</strong>: A linked list is a data structure consisting of a sequence of nodes containing data and a reference to the next node. Unlike arrays, linked lists do not have a fixed size and can be easily resized. They are commonly used in dynamic memory allocation.</li><li><strong>Trie</strong>: A trie is a tree-like data structure that stores associative arrays where the keys are strings. It allows for fast data retrieval based on partial matches of the keys.</li><li><strong>Balanced Tree</strong>: A balanced tree is a data structure where the height of any node&#39;s left and right subtrees differs by at most one. This ensures that the tree remains balanced, ensuring that search, insertion, and deletion operations have a worst-case time complexity of O(log n).</li></ol><p>These data structures have various applications in computer science and are used extensively in algorithms and software development.</p><h3>Databases</h3><h4>15. What are the ways to find slow SELECT queries in RDBMS? What are the ways to speed up such requests?</h4><p>There are several ways to find slow SELECT queries in a relational database management system (RDBMS):</p><ol><li>Use the database’s query <strong>profiler </strong>or performance-tuning tools to identify slow queries. Most databases have built-in profiling tools that can help identify slow queries, including the time they take to execute, the number of rows they return, and the amount of CPU and memory resources they consume.</li><li>Enable slow query logging to identify queries that take a long time to execute. Many databases allow you to configure logging to record slow queries, which can help identify bottlenecks and performance issues.</li><li>Monitor resource usage to identify queries consuming too much CPU or memory. You can use system monitoring tools to identify queries that consume a disproportionate amount of resources, which may indicate that they need to be optimized or restructured.</li></ol><p>To speed up slow SELECT queries in RDBMS, here are some best practices:</p><ol><li>Optimize database schema and indexes: Ensure that the database schema and indexes are designed to support the application’s query patterns and workload.</li><li>Tune database configuration parameters: Adjust database configuration parameters, such as buffer and cache sizes, to optimize performance for the specific workload.</li><li>Use appropriate indexing strategies: Use appropriate indexing strategies to speed up SELECT queries. Ensure that indexes support the application’s query patterns and workload.</li><li>Use query optimization tools: Many databases have built-in query optimization tools to help identify and fix slow queries.</li><li>Reduce the data set: Reduce the amount of data returned by SELECT queries by using filters or aggregations to limit the data set returned by the query.</li><li>Use caching: Use caching to reduce the number of SELECT queries that need to be executed. Cached data can serve subsequent requests, reducing the overhead of executing SELECT queries.</li></ol><h4>16. What are database normal forms?</h4><p>Database normalization is a process used to organize a database to reduce redundancy and dependency, making it more efficient and easier to maintain. Normalization is usually divided into different levels called normal forms.</p><p>The most common normal forms are:</p><ol><li>First Normal Form (1NF): This level requires that each column in a table must contain atomic values, which means that each value in the column is indivisible.</li><li>Second Normal Form (2NF): At this level, the table must meet the requirements of 1NF, and each non-key column must depend on the primary key.</li><li>Third Normal Form (3NF): At this level, the table must meet the requirements of 2NF, and there should be no transitive dependency between the non-key columns.</li></ol><p>There are also other normal forms, such as Boyce-Codd Normal Form (BCNF), Fourth Normal Form (4NF), and Fifth Normal Form (5NF).</p><p>The benefits of normalization are that it reduces data redundancy, improves data consistency, and makes it easier to maintain and update the database. However, normalization can also increase complexity and decrease performance, especially in larger databases. Therefore, it is important to balance normalization and performance optimization.</p><h4>17. What are indexes? What are their disadvantages?</h4><p>Indexes are database objects that help speed up the data retrieval process by providing quick access to the data. They are created on one or more table columns, allowing the database management system (DBMS) to locate the required data without scanning the entire table.</p><p>Indexes have the following advantages:</p><ol><li>They speed up data retrieval by providing quick access to the data.</li><li>They help to enforce data uniqueness and integrity by allowing the DBMS to check for duplicates or null values.</li><li>They can improve query performance by reducing the amount of data that needs to be processed.</li></ol><p>However, indexes also have some disadvantages:</p><ol><li>They take up space on disk and in memory.</li><li>They can slow down data modification operations, such as inserts, updates, and deletes, as the DBMS must update the index and table.</li><li>They can become outdated if the underlying table is modified, leading to query performance degradation.</li><li>They can increase the complexity of the database schema and query optimization, leading to maintenance issues.</li></ol><p>Therefore, using indexes judiciously based on the application&#39;s specific requirements and the database workload is important.</p><h4>18. What data structures can be used in database indexes? How do they work?</h4><p>In databases, indexes are used to improve the speed of data retrieval operations. An index is a data structure that is created based on the values in one or more columns of a table. The index allows the database to locate the rows that match a particular query condition more quickly than it would be able to if it had to scan the entire table.</p><p>The most commonly used data structures for database indexes are B-trees and hash tables.</p><p>A B-tree is a balanced tree data structure for storing data on a disk. B-trees are used for indexing in many databases because they allow fast data retrieval even when the data set is too large to fit entirely in memory. The data is stored in nodes organized into a hierarchy in a B-tree. Each node contains a range of keys and pointers to the child nodes that contain the keys that fall within that range. The tree&#39;s root node contains pointers to the child nodes that contain the smallest and largest keys in the tree.</p><p>A hash table is a data structure designed for fast lookups based on a key. In a hash table, the keys are hashed to a value used as an array index. The array contains pointers to the data that corresponds to each key. The advantage of hash tables is that they provide very fast lookups for a single key value. However, they can be less efficient than B-trees for range queries or when the data is stored on disk.</p><p>Both B-trees and hash tables have advantages and disadvantages, and which one to use depends on the application&#39;s specific requirements and the data being indexed.</p><h4>19. What is the difference between foreign &amp; primary keys?</h4><p>A primary key is a column or a combination of columns in a database table that uniquely identifies each row. It ensures that each row in a table can be uniquely identified and accessed efficiently. A primary key constraint is used to enforce the uniqueness of the primary key values in the table.</p><p>On the other hand, a foreign key is a column or a combination of columns in a table that is used to establish a link between two tables. It is used to enforce referential integrity between the related tables. A foreign key in one table is linked to the primary key of another table, which creates a parent-child relationship between the two tables.</p><p>In simpler terms, a primary key is used to uniquely identify a row in a table, while a foreign key is used to link two tables and maintain the referential integrity between them.</p><h4>20. The concept of migration in the context of databases. What are the advantages of the migration approach?</h4><p>In the context of databases, migration refers to the process of managing changes to the structure of a database schema over time. Migrations can involve adding or modifying tables, columns, or other database objects and manipulating data within the database.</p><p>The main advantage of migrations is that they provide a way to manage changes to the database schema in a controlled and repeatable way. By keeping track of each change to the schema, migrations ensure that all developers working on the project are working with the same version of the database schema and that all necessary changes are applied consistently across all database instances.</p><p>Another advantage of migrations is that they make it easier to roll back changes to the database schema if something goes wrong. By keeping track of each change, migrations allow developers to easily reverse a specific change or roll back to a previous schema version.</p><p>Migrations also help to ensure the consistency and integrity of the database schema by enforcing rules for how changes should be made. For example, migrations might require that certain fields or columns are always present in a table or that certain types of changes are only made during specific stages of development.</p><p>Overall, the use of migrations can help to ensure that database changes are managed in a consistent and controlled way, which can help to minimize errors and ensure the long-term stability and reliability of the database.</p><h4>21. What is data replication? What replication options are there for the databases you’ve worked with?</h4><p>Data replication is copying and distributing data from a database to one or more other databases. The primary goal of replication is to increase data availability, reliability, and fault tolerance.</p><p>In the databases I’ve worked with, such as MySQL and PostgreSQL, there are several replication options available:</p><ol><li>Master-slave replication: In this setup, one database server acts as the master and another as a slave. The master server receives all write requests, and these changes are then propagated to the slave servers. This type of replication is often used to improve read scalability, as the slaves can handle read requests while the master handles write requests.</li><li>Master-master replication: In this setup, two or more database servers act as masters, and each can handle both read and write requests. Changes made on one master are then replicated in the other masters. This type of replication is often used for high availability as if one master goes down, the other master(s) can continue to handle requests.</li><li>Multi-source replication: This feature in some databases, such as MySQL, allows replication from multiple sources into a single slave server. This can be useful in cases where data is distributed across multiple servers and needs to be consolidated.</li></ol><p>The advantages of using data replication include:</p><ol><li><strong>Improved data availability</strong>: By replicating data across multiple servers, the likelihood of data becoming unavailable due to server failure is reduced.</li><li><strong>Improved read scalability</strong>: By allowing read requests to be handled by slave servers, the master server is freed up to handle write requests, which can improve overall system performance.</li><li><strong>Improved fault tolerance</strong>: By having multiple copies of data, replication can provide fault tolerance in case of data corruption or other types of failures.</li><li><strong>Improved disaster recovery</strong>: Replicating data in multiple locations makes recovering from natural disasters or server failures easier.</li><li><strong>Improved geographic distribution</strong>: By replicating data across servers in different geographic locations, data can be made available to users in different regions, improving performance and reducing latency.</li></ol><h4>22. What is the difference between SQL and NoSQL?</h4><p>SQL and NoSQL are two different types of database management systems. SQL (Structured Query Language) is a relational database management system, while NoSQL (Not Only SQL) is a non-relational database management system.</p><p>SQL databases are based on the relational model, where data is stored in tables with a fixed structure defined by a schema. SQL databases use the SQL language to interact with the database, which provides a standard syntax for querying, updating, and managing data. SQL databases are known for their strong consistency, data integrity, and transaction support.</p><p>On the other hand, NoSQL databases are designed to store unstructured or semi-structured data, such as documents, graphs, and key-value pairs. NoSQL databases are often schemaless and provide a flexible data model that can be adapted to changing requirements. NoSQL databases typically provide high scalability and performance but may sacrifice some consistency in favour of availability and partition tolerance.</p><p>In summary, the main differences between SQL and NoSQL databases are:</p><ul><li><strong>Data model</strong>: SQL databases use a relational data model, while NoSQL databases use a variety of non-relational data models.</li><li><strong>Schema</strong>: SQL databases have a fixed schema, while NoSQL databases may have a dynamic or flexible schema.</li><li><strong>Query language</strong>: SQL databases use SQL as the standard query language, while NoSQL databases may use different query languages or APIs.</li><li><strong>Consistency</strong>: SQL databases typically provide strong consistency guarantees, while NoSQL databases may provide weaker consistency models.</li><li><strong>Scalability</strong>: NoSQL databases are often designed for high scalability and performance, while SQL databases may have more limitations.</li></ul><h4>23. What types of NoSQL databases do you know? Give examples. What is the difference between them?</h4><p>There are four main types of NoSQL databases, which are as follows:</p><ol><li><strong>Document-oriented databases</strong>: Store data in documents, usually using formats like JSON or XML. Each document represents an instance of an object, and each object can have its own unique schema. Examples of document-oriented databases include MongoDB and CouchDB.</li><li><strong>Key-value stores</strong>: These databases store data as key-value pairs, where each key is unique and maps to a value. They are optimized for fast reads and writes, making them useful for caching and session management. Examples of key-value stores include Redis and Riak.</li><li><strong>Column-family stores</strong>: These databases store data in a tabular format but with a twist: each row can have a different set of columns, making them more flexible than traditional SQL databases. They are optimized for handling large amounts of data and running complex analytics queries. Examples of column-family stores include Apache Cassandra and HBase.</li><li><strong>Graph databases</strong>: These databases are designed to store and query data that has complex relationships. They are optimized for traversing large and complex data graphs, making them useful for applications like social networks and recommendation engines. Examples of graph databases include Neo4j and OrientDB.</li></ol><p>The main difference between these databases is how they store and access data. Each type has its own strengths and weaknesses, so the choice of the database depends on the application&#39;s specific needs. For example, document-oriented databases are good for storing complex, hierarchical data, while key-value stores are good for caching and session management. Column-family stores are good for handling large amounts of data, and graph databases are good for handling complex relationships between data.</p><h4>24. What is a columnar database? Advantages and disadvantages</h4><p>A columnar database is a type of database that stores data in columns rather than in rows. In traditional row-oriented databases, data is stored in rows, meaning all the data for a given record is stored together. Each column is stored separately in a columnar database, allowing faster queries and more efficient storage.</p><p>Advantages of columnar databases:</p><ol><li><strong>Faster query performance</strong>: Columnar databases can query large amounts of data faster than traditional row-oriented databases, as they only need to read the columns required for a given query.</li><li><strong>Efficient storage</strong>: Columnar databases are highly efficient in terms of storage space, as they only need to store the values for each column once.</li><li><strong>Compression</strong>: Columnar databases are highly compressible, as the data is stored in a column-oriented manner.</li></ol><p>Disadvantages of columnar databases:</p><ol><li><strong>Limited flexibility</strong>: Columnar databases are not as flexible as traditional row-oriented databases, as they require the data to be structured in a column-oriented manner.</li><li><strong>Complexity</strong>: Columnar databases can be more complex than traditional row-oriented databases, requiring more sophisticated algorithms for storing and querying data.</li><li><strong>Limited support for transactional data</strong>: Columnar databases are not as well-suited to transactional data as traditional row-oriented databases, as they are optimized for analytical queries rather than transactional processing.</li></ol><h4>25. What is a document-oriented database? Advantages and disadvantages</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*faZp2Cy1Z9ir-VL-" /><figcaption>Photo by <a href="https://unsplash.com/@rubaitulazad?utm_source=medium&amp;utm_medium=referral">Rubaitul Azad</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>A document-oriented database is a NoSQL database that stores and manages data in semi-structured or unstructured documents. Each document contains key-value pairs or key-array pairs where arrays and nested documents can be used to represent complex data structures. Examples of document-oriented databases include MongoDB, Couchbase, and Amazon DocumentDB.</p><p>Advantages of document-oriented databases:</p><ol><li><strong>Flexible data schema</strong>: Document-oriented databases are schemaless, which means you can store any type of data without a predefined schema. Storing and retrieving complex, nested, and hierarchical data structures is easy.</li><li><strong>Horizontal scalability</strong>: Document-oriented databases can be scaled horizontally by adding more nodes to the cluster. This makes it easy to handle high-volume and high-velocity data.</li><li><strong>Query flexibility</strong>: Document-oriented databases support various query languages, including SQL-like languages, MapReduce, and full-text search. This allows for flexible and powerful querying of the data.</li><li><strong>High performance</strong>: Because documents are stored in a self-contained format, document-oriented databases can retrieve data quickly and efficiently, especially when compared to relational databases.</li></ol><p>Disadvantages of document-oriented databases:</p><ol><li><strong>Limited transactional support</strong>: Document-oriented databases do not support ACID transactions, which can lead to inconsistent data.</li><li><strong>Lack of standardization</strong>: There is no standard for document-oriented databases, meaning each database may have its unique syntax and query language.</li><li><strong>Not suitable for all data</strong>: While document-oriented databases are great for storing unstructured and semi-structured data, they are not always the best option for storing highly structured data, such as financial data.</li><li><strong>Increased storage requirements</strong>: Because each document contains its own schema, document-oriented databases can use more storage space than other databases, especially when dealing with highly structured data.</li></ol><h4>26. Tell us about working with key-value databases (preferably from your own experience)</h4><p>In my experience, I have worked with Redis, which is a popular in-memory key-value database. Here are some key points about working with key-value databases:</p><p>Advantages:</p><ul><li><strong>Fast data access</strong>: Key-value databases offer high-speed data access since the data is stored in memory and can be retrieved quickly by its key.</li><li><strong>Simple data model</strong>: Key-value databases have a simple data model that is easy to understand and work with. They are ideal for storing simple data structures like counters, lists, and sets.</li><li><strong>Horizontal scalability</strong>: Key-value databases can be easily scaled horizontally to handle increasing loads by adding more nodes to a cluster.</li></ul><p>Disadvantages:</p><ul><li><strong>Limited data querying</strong>: Key-value databases lack the ability to query data beyond simple key lookups, which can be a limitation for complex applications that require more advanced querying.</li><li><strong>Data persistence</strong>: Many key-value databases only store data in memory, meaning data is lost if the server crashes or is restarted. Persistence can be added with disk-based storage, but this can impact performance.</li><li><strong>Data integrity</strong>: Key-value databases do not enforce relationships or constraints between data items, so maintaining data integrity is left to the application layer.</li></ul><p>When working with key-value databases, it is important to design your data model carefully and choose the appropriate data structures for your use case. You also need to ensure that your application can handle the limitations of key-value databases, such as the lack of advanced querying and data integrity checks.</p><h4>27. What Redis data types do you have hands-on experience with? Give examples of when it is appropriate to use them.</h4><p>Redis supports various data types, including:</p><ol><li><strong>Strings</strong>: Stores a string or binary data, such as an image or serialized object.</li><li><strong>Hashes</strong>: A hash table-like data structure that allows the storage of key-value pairs.</li><li><strong>Lists</strong>: Stores a list of strings in the order they were added.</li><li><strong>Sets</strong>: A collection of unique, unordered strings.</li><li><strong>Sorted Sets</strong>: A set ordered by a score associated with each element.</li><li><strong>Bitmaps</strong>: Stores a compact bitmap for each key. This is useful for storing compressed boolean information.</li><li><strong>HyperLogLogs</strong>: Approximate set membership counting data structure.</li></ol><p>The choice of which data type to use depends on the use case. Here are some examples:</p><ol><li><strong>Strings</strong>: Storing session IDs or caching objects.</li><li><strong>Hashes</strong>: Storing user profiles or a product catalogue.</li><li><strong>Lists</strong>: Storing logs or messages in a queue.</li><li><strong>Sets</strong>: Storing tags or following relationships between users.</li><li><strong>Sorted Sets</strong>: Storing leaderboards or ranking products based on popularity.</li><li><strong>Bitmaps</strong>: Tracking user activity or online presence.</li><li><strong>HyperLogLogs</strong>: Counting unique visitors or unique search queries.</li></ol><p>The advantage of Redis data types is that they are fast, flexible and provide a simple key-value interface. However, the disadvantage is that Redis is an in-memory database, so it may not be suitable for storing large amounts of data that cannot fit in memory.</p><h3>Go</h3><h4>28. What Go libraries have you used to access the RDBMS? What are their positive and negative sides?</h4><ol><li>database/sql: This is the standard Go library for accessing SQL databases. It provides a generic interface to access a wide variety of SQL databases. It is simple and easy to use but lacks some advanced features and has limited performance optimizations.</li><li>gorm: This ORM (Object-Relational Mapping) library provides a higher-level interface to work with databases. It simplifies database interactions by providing a clear and concise syntax for executing queries and working with results. However, it may be less performant than raw SQL queries and require more configuration and setup.</li><li>sqlx: This is a library that extends the standard database/sql package with some additional features, such as named parameters, struct scanning, and query-building helpers. It aims to be a lightweight and easy-to-use library that can be used as a drop-in replacement for the standard library.</li><li>pgx: This PostgreSQL driver provides a low-level, high-performance interface to work with PostgreSQL databases. It is designed to be faster and more efficient than the standard database/sql library but may require more configuration and setup.</li></ol><p>The choice of the library depends on the specific use case and requirements. If simplicity and ease of use are more important, the standard library or gorm may be a good choice. If performance is critical, pgx may be a better option. If a balance between performance and simplicity is required, sqlx may be a good choice.</p><h4>29. What is Context used for? What are the options for cancelling contexts?</h4><p>Context is a Go package that provides a way to manage and propagate request-scoped values, cancellation signals, and deadlines across API boundaries and between processes and threads. It allows you to control the lifecycle of a request and any related operations, such as handling timeouts and cancelling in-flight requests.</p><p>Contexts can be used to carry metadata and deadlines across API boundaries. This is especially useful in networked environments where latency and timeouts are critical. For example, a context can cancel an in-flight HTTP request after reaching a certain timeout. In this way, the context provides a way to propagate a cancellation signal from the top-level caller to all its sub-calls.</p><p>The context package provides several ways to cancel a context:</p><ol><li>Call the CancelFunc returned by the context.WithCancel() function when you want to cancel the context.</li><li>Call the context.WithTimeout() or context.WithDeadline() functions to create a context automatically cancelled after a certain amount of time or at a certain time.</li><li>Pass a context.Context value down through the call stack, and have each function check whether the context has been cancelled.</li></ol><p>Contexts can be used to carry any request-scoped values. For example, you could use a context to carry authentication credentials for a request. When a handler receives a request, it can extract the authentication credentials from the context and use them to authenticate the user.</p><p>One of the popular use cases of the context package in Go is for the graceful shutdown of servers. When the server receives a shutdown signal, it cancels the context. All in-flight requests are given a chance to finish up before the server is shut down. This ensures that the server is shut down gracefully and all requests are handled before the server goes down.</p><h4>30. How can you exclude (hide) structure fields during JSON serialization?</h4><p>In Go, you can exclude or hide structure fields during JSON serialization by using struct tags. Struct tags are metadata that can be added to the fields of a struct to provide additional information about the field.</p><p>To exclude a field during JSON serialization, you can add a struct tag json:&quot;-&quot; to that field. Here&#39;s an example:</p><pre>type User struct {<br>    ID        int    `json:&quot;-&quot;`<br>    FirstName string `json:&quot;first_name&quot;`<br>    LastName  string `json:&quot;last_name&quot;`<br>    Email     string `json:&quot;email&quot;`<br>}</pre><p>In this example, the ID field is excluded from the JSON serialization by adding the json:&quot;-&quot; struct tag. The FirstName, LastName, and Email fields are included in the JSON serialization and are mapped to the JSON keys first_name, last_name, and email, respectively.</p><p>You can exclude a field based on a specific condition using a struct tag. For example, you can exclude a field if its value is empty or nil by adding the omitempty option to the json struct tag. Here&#39;s an example:</p><pre>type User struct {<br>    ID        int     `json:&quot;-&quot;`<br>    FirstName string  `json:&quot;first_name,omitempty&quot;`<br>    LastName  string  `json:&quot;last_name,omitempty&quot;`<br>    Email     *string `json:&quot;email,omitempty&quot;`<br>}</pre><p>In this example, the FirstName and LastName fields will be omitted from the JSON output if their values are empty. The Email field is also omitted from the JSON output if its value is nil, but will be included if it has a non-nil value.</p><p>☝️: Note that struct tags are only applicable during JSON encoding and decoding and do not affect the behaviour of the struct otherwise.</p><h4>31. Name the primitives of the sync package of the standard library. What are the purpose and usage examples of sync.WaitGroup?</h4><p>The sync package of the Go standard library provides several primitives for synchronization and concurrency. Some of the primitives in the sync package are:</p><ol><li>Mutex: used to provide exclusive access to a shared resource by multiple goroutines.</li><li>RWMutex: allows multiple readers or a single writer to access a shared resource.</li><li>WaitGroup: used to wait for a collection of goroutines to finish their execution.</li><li>Cond: provides a way for one goroutine to signal one or more waiting goroutines that a particular event has occurred.</li><li>Once: used to ensure that a particular function is executed only once, regardless of the number of times it is called.</li></ol><p>The sync.WaitGroup type is used to wait for a collection of goroutines to finish their execution. It has two methods: Add and Done. The Add method increments the WaitGroup counter, while the Done method decrements it. When the WaitGroup counter becomes zero, the Wait method returns, allowing the program to continue.</p><p>Usage example:</p><pre>var wg sync.WaitGroup<br><br>func main() {<br>    for i := 0; i &lt; 5; i++ {<br>        wg.Add(1)<br>        go func(id int) {<br>            defer wg.Done()<br>            fmt.Println(&quot;Worker&quot;, id, &quot;started&quot;)<br>            time.Sleep(time.Second)<br>            fmt.Println(&quot;Worker&quot;, id, &quot;finished&quot;)<br>        }(i)<br>    }<br>    wg.Wait()<br>    fmt.Println(&quot;All workers finished&quot;)<br>}</pre><p>In this example, we create 5 goroutines and use the Add method to increment the WaitGroup counter before starting each goroutine. Inside each goroutine, we call Done to decrement the counter when the work is finished. Finally, we call the Wait method to block until all goroutines have finished, after which the program continues.</p><h4>32. What is the difference between Mutex and RWMutex?</h4><p>Mutex and RWMutex are synchronization primitives provided by the sync package in Go. <strong>Mutex </strong>(short for mutual exclusion) provides exclusive access to a shared resource, meaning that only one goroutine can access the resource at a time. <strong>RWMutex</strong> (short for read-write mutex) provides shared access to a resource for reading but exclusive access for writing.</p><p>The main difference between Mutex and RWMutex is the level of concurrency they provide. With a <strong>Mutex</strong>, only one goroutine can access the resource at a time, regardless of whether it is reading or writing. This means that if one goroutine is currently holding the Mutex, all other goroutines needing access to the resource must wait until the Mutex is released.</p><p>With an <strong>RWMutex</strong>, multiple goroutines can access the resource simultaneously for reading, but only one goroutine can access it for writing. This means that if multiple goroutines only need to read the resource, they can all proceed concurrently. However, if a goroutine needs to modify the resource, it must acquire exclusive access to the resource by obtaining a write lock, and all other goroutines that need access to the resource (whether for reading or writing) must wait until the write lock is released.</p><p>In summary, Mutex provides exclusive access to a shared resource, while RWMutex provides shared access to reading and exclusive access to writing. RWMutex is more efficient when multiple goroutines only need to read the resource, but it may not be appropriate if many goroutines need to write to the resource, as write locks are exclusive and can block all other goroutines.</p><h4>33. What are the ways to stop N goroutines running at the same time (e.g. worker pool)?</h4><p>There are several ways to stop N goroutines running at the same time in Go, including:</p><ol><li><strong>Using a channel to signal termination</strong>: Create a channel that signals termination, and pass it to all the goroutines. When the main function wants to stop the goroutines, it can close the channel, causing them to exit.</li><li><strong>Using a context</strong>: A context can signal cancellation to multiple goroutines. When a context is cancelled, all the goroutines that are listening to it will receive the signal and can terminate.</li></ol><p>The best approach depends on the application&#39;s specific use case and requirements.</p><h4>34. What is function closure?</h4><p>In Go, a closure is a function value that references variables from outside its body. The function can access and modify the values of those variables, even after the function has returned. In other words, the closure “closes over” the variables it uses.</p><p>Closures are created by defining a function inside another function. The inner function can access the outer function’s variables, which will be available for the lifetime of the inner function, even if the outer function has completed execution and its variables are no longer in scope.</p><p>Here is an example of a closure in Go:</p><pre>func counter() func() int {<br>    i := 0<br>    return func() int {<br>        i++<br>        return i<br>    }<br>}<br><br>func main() {<br>    c1 := counter()<br>    fmt.Println(c1()) // Output: 1<br>    fmt.Println(c1()) // Output: 2<br>    <br>    c2 := counter()<br>    fmt.Println(c2()) // Output: 1<br>}</pre><p>In this example, the counter function returns a closure that keeps track of a counter variable i. Each time the closure is called, it increments the counter and returns its value. Multiple closures can be created with the counter function, each with its own independent counter.</p><h4>35. Explain the difference between switch and select?</h4><p>switch and select are two different control flow statements in Go.</p><p>switch statement is used to select one of many possible paths to execute based on the value of a given expression. It allows testing a value against a list of expressions to find a match and execute the corresponding code block. Here&#39;s an example:</p><pre>func main() {<br>    day := &quot;Monday&quot;<br><br>    switch day {<br>    case &quot;Monday&quot;:<br>        fmt.Println(&quot;Today is Monday&quot;)<br>    case &quot;Tuesday&quot;:<br>        fmt.Println(&quot;Today is Tuesday&quot;)<br>    default:<br>        fmt.Println(&quot;It&#39;s not Monday or Tuesday&quot;)<br>    }<br>}</pre><p>In this example, the switch statement tests the value of the variable day and executes the corresponding block of code.</p><p>select statement is used to wait for multiple channel operations at the same time. It allows waiting for multiple channel operations to complete and execute the corresponding code block. Here&#39;s an example:</p><pre>func main() {<br>    ch1 := make(chan int)<br>    ch2 := make(chan int)<br><br>    go func() {<br>        ch1 &lt;- 1<br>    }()<br><br>    go func() {<br>        ch2 &lt;- 2<br>    }()<br><br>    select {<br>    case res := &lt;-ch1:<br>        fmt.Println(res)<br>    case res := &lt;-ch2:<br>        fmt.Println(res)<br>    }<br>}</pre><p>In this example, the select statement waits for data to be received from either ch1 or ch2 channels, and executes the corresponding block of code.</p><p>So, the main difference between switch and select statements is that switch selects one of many possible paths based on the value of a given expression, whereas select waits for multiple channel operations to complete and executes the corresponding code block.</p><h4>36. What are the ways to get data from JSON?</h4><p>In Go, there are different ways to get data from JSON, depending on the use case and the desired data structure. Here are a few common methods:</p><ul><li><strong>Unmarshal</strong>: Unmarshaling converts JSON data into a Go data structure. The json.Unmarshal function can be used to unmarshal JSON data into a struct or a map. For example:</li></ul><pre>type Person struct {<br>    Name string `json:&quot;name&quot;`<br>    Age  int    `json:&quot;age&quot;`<br>}<br><br>func main() {<br>    jsonStr := `{&quot;name&quot;:&quot;Alice&quot;, &quot;age&quot;:30}`<br>    var p Person<br>    err := json.Unmarshal([]byte(jsonStr), &amp;p)<br>    if err != nil {<br>        log.Fatal(err)<br>    }<br>    fmt.Println(p.Name, p.Age)<br>}</pre><ul><li><strong>Decode</strong>: If the JSON data is coming from an io.Reader like an HTTP response body, the json.Decoder can be used to read and decode the data into a Go data structure. For example:</li></ul><pre>type Person struct {<br>    Name string `json:&quot;name&quot;`<br>    Age  int    `json:&quot;age&quot;`<br>}<br><br>func main() {<br>    resp, err := http.Get(&quot;https://example.com/person.json&quot;)<br>    if err != nil {<br>        log.Fatal(err)<br>    }<br>    defer resp.Body.Close()<br>    var p Person<br>    err = json.NewDecoder(resp.Body).Decode(&amp;p)<br>    if err != nil {<br>        log.Fatal(err)<br>    }<br>    fmt.Println(p.Name, p.Age)<br>}</pre><ul><li><strong>Parsing</strong>: If you just need to extract a specific value from a JSON string without parsing the entire structure, you can use the jsonparser package. This package provides functions to extract specific values from a JSON string. For example:</li></ul><pre>func main() {<br>    jsonStr := `{&quot;name&quot;:&quot;Alice&quot;, &quot;age&quot;:30}`<br>    name, err := jsonparser.GetString([]byte(jsonStr), &quot;name&quot;)<br>    if err != nil {<br>        log.Fatal(err)<br>    }<br>    fmt.Println(name)<br>}</pre><p>These are just a few examples of how to get data from JSON in Go. The best approach depends on the specific use case and data structure.</p><h4>37. How are loop constructs implemented in Go?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LM6fSyhDrupMk50t" /><figcaption>Photo by <a href="https://unsplash.com/@tine999?utm_source=medium&amp;utm_medium=referral">Tine Ivanič</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Go programming language has a for loop construct that can be used for various types of looping. The basic syntax of the for loop in Go is:</p><pre>for initialization; condition; post {<br>    // loop body<br>}</pre><p>where initialization is the initial statement, condition is the loop condition, and post is the post-loop statement. All three parts are optional, but the semicolons must be present.</p><p>In addition to the basic for loop, Go also provides a range loop construct that can loop over arrays, slices, maps, and channels. The syntax of the range loop is:</p><pre>for index, value := range collection {<br>    // loop body<br>}</pre><p>where index is the index or key of the current element, value is the value of the current element, and collection is the array, slice, map, or channel being looped over.</p><p>Both for and range loops can be used to implement different loops, such as infinite loops, while loops, and do-while loops. In addition, Go also provides a goto statement, which can transfer control to a different point in the code, but its usage is generally discouraged as it can lead to hard-to-understand and hard-to-debug code.</p><h4>38. How is the map type arranged?</h4><p>In Go, the map type is a reference type representing an unordered collection of key-value pairs, where each key must be unique.</p><p>Internally, a map is implemented as a hash table. A hash table is an array of buckets, and each bucket is a linked list of key-value pairs. When a key is added to the map, its hash code is computed, and the key-value pair is added to the bucket corresponding to that hash code. If there are collisions (i.e., two or more keys have the same hash code), the key-value pairs are added to the same bucket and stored as a linked list.</p><p>The key&#39;s hash code is computed to find a value associated with a particular key, and the bucket associated with that hash code is searched for the key-value pair. If the key is found, its corresponding value is returned. If the key is not found, nil is returned.</p><p>When a key-value pair is added or deleted from a map, the hash table is reorganized to maintain an efficient distribution of keys.</p><h4>39. What is the iteration order of the map?</h4><p>In Go, the iteration order of a map is not defined, and it is not guaranteed to be the same across multiple iterations. This is because the Go runtime randomizes the hash seed used for the map during its creation, which leads to the different ordering of keys across different executions of the program. Therefore, it is important not to rely on any specific iteration order when working with maps.</p><h4>40. What is serialization? Where is it used?</h4><p>Serialization is converting a data object or a complex data structure into a format that can be stored or transmitted easily over a network or between different programming languages. It enables objects to be saved to a file or sent across a network or other communication channels in a language-agnostic format. Once serialized, the object can be reconstructed at any time by deserializing it back into an object.</p><p>Serialization is commonly used in client-server communication, caching, and storing application state. In client-server communication, data sent by the client to the server is serialized into a format that can be transmitted over the network and then deserialized by the server. In caching, serialized objects are stored in a cache and retrieved later for faster access. In storing the application state, serialized objects are stored in a database or on disk and then loaded and deserialized back into memory when the application is started again.</p><p>Serialization can be performed using a variety of formats, including JSON, XML, YAML, and binary formats such as Protocol Buffers and Apache Thrift.</p><h4>41. Can nil be used to initialize a variable?</h4><p>Yes, in Go, a variable can be initialized to the nil value. However, the applicability of this depends on the type of the variable.</p><p>For example, a variable of pointer type can be initialized to nil. This often indicates that the pointer does not currently point to any valid memory address. Here&#39;s an example:</p><pre>var p *int // p is a pointer to int<br>p = nil    // p is initialized to nil</pre><p>Similarly, slices, maps, channels, and interfaces can also be initialized to nil. When a slice, map, or channel is nil, attempting to access or modify its elements will result in a runtime error. When an interface is nil, it cannot be used to call any methods.</p><p>It’s worth noting that variables of numeric or boolean types cannot be initialized to nil, as they are not pointer types.</p><h4>42. Is it possible to set the map capacity? Is it possible to get the capacity map?</h4><p>Yes, it is possible to set the capacity of a map in Go using the built-in make function. The syntax is as follows:</p><pre>make(map[&lt;key-type&gt;]&lt;value-type&gt;, &lt;capacity&gt;)</pre><p>For example, to create a map with a capacity of 100 for int keys and string values, you can use:</p><pre>m := make(map[int]string, 100)</pre><p>This creates a map with an initial capacity of 100.</p><p>However, getting a map&#39;s capacity in Go is <strong>impossible</strong>. Maps in Go are implemented using a hash table, and the capacity of the hash table is automatically adjusted as the number of elements in the map grows or shrinks. The size of the hash table is always a power of 2, and the language does not expose the exact capacity of the hash table.</p><h4>43. What is code generation, and why is it needed?</h4><p>Code generation is a technique used in software development to automatically generate code based on predefined templates, models, or specifications. It can be used for various purposes, such as reducing the amount of boilerplate code, improving productivity, enforcing coding standards, or ensuring consistency across a project.</p><p>In code generation, a tool or a program is used to create source code based on rules or specifications. The generated code can be in any programming language and can be used to create various software components, such as classes, methods, interfaces, or entire applications.</p><p>Code generation can be used in various scenarios, such as:</p><ol><li>ORM frameworks: Object-relational mapping frameworks like Hibernate or Entity Framework generate code to map database tables to objects.</li><li>Protocol Buffers: Protocol Buffers is a language-agnostic binary serialization format. Code generation tools generate code in different programming languages to work with Protocol Buffers.</li><li>REST APIs: Code generation can be used to generate client-side code to access RESTful APIs based on a specification like OpenAPI.</li><li>GUI design: Some GUI design tools use code generation to generate UI code for different platforms based on a single design.</li></ol><p>The advantages of code generation include reducing the amount of manual coding, improving consistency and maintainability, and reducing the risk of errors. However, it can also introduce additional complexity and require specialized knowledge and tools.</p><h4>44. What is the difference between a goroutine and an OS thread?</h4><p>In Go, a goroutine is a lightweight thread managed by the Go runtime, while the operating system manages an OS thread.</p><p>Goroutines are multiplexed onto a smaller number of OS threads by the Go runtime, which allows them to be created and destroyed much more quickly and with less overhead than OS threads. Goroutines are scheduled cooperatively by the Go runtime, which means that the scheduler decides when to switch between goroutines rather than being interrupted by the operating system.</p><p>In contrast, OS threads are scheduled by the operating system’s scheduler, which can be less efficient regarding context-switching overhead. Creating and destroying OS threads also has a higher overhead than creating and destroying goroutines.</p><p>Overall, goroutines are designed to be a lightweight and efficient way to achieve concurrency in Go, while OS threads are a lower-level construct that provides more control over system resources at the cost of increased overhead and complexity.</p><h4>45. How and for what use io.Reader and io.Writer?</h4><p>In Go, io.Reader and io.Writer are interfaces that define how data is read from or written to an input or output stream. These interfaces are used extensively in Go&#39;s standard library to consistently handle input and output across different sources and destinations.</p><p>The io.Reader interface has a single method, Read, which reads data from an input stream and writes it into a byte slice. The method returns the number of bytes read and an error value, which is io.EOF if the end of the input stream has been reached.</p><pre>type Reader interface {<br>    Read(p []byte) (n int, err error)<br>}</pre><p>The io.Writer interface, on the other hand, has a single method, Write, which writes data to an output stream. The method returns the number of bytes written and an error value.</p><pre>type Writer interface {<br>    Write(p []byte) (n int, err error)<br>}</pre><p>These interfaces are used in a variety of places in the Go standard library, including os.File, which implements both the io.Reader and io.Writer interfaces. For example, to read data from a file, you can create a File object and then pass it to a function that takes an io.Reader:</p><pre>file, err := os.Open(&quot;filename.txt&quot;)<br>if err != nil {<br>    log.Fatal(err)<br>}<br>defer file.Close()<br><br>buffer := make([]byte, 1024)<br>n, err := file.Read(buffer)<br>if err != nil &amp;&amp; err != io.EOF {<br>    log.Fatal(err)<br>}</pre><p>In this example, we open a file named “filename.txt” and create a buffer to read data into. We then call the Read method on the file object, passing in the buffer. The Read method reads up to len(p) bytes from the file and stores them in the buffer, returning the number of bytes read and an error value. We check the error value to make sure that we haven&#39;t reached the end of the file (io.EOF) or encountered any other errors.</p><h4>46. How to convert []io.ReadWriter to []io.Reader?</h4><p>To convert []io.ReadWriter to []io.Reader, we need to iterate over the slice and append the result of the type conversion to []io.Reader.</p><p>Here’s an example code snippet:</p><pre>package main<br><br>import (<br> &quot;fmt&quot;<br> &quot;io&quot;<br> &quot;os&quot;<br>)<br><br>func main() {<br> // Create a slice of io.ReadWriter<br> readWriters := []io.ReadWriter{<br>  os.Stdout,<br>  os.Stderr,<br> }<br><br> // Create a slice of io.Reader<br> readers := make([]io.Reader, 0, len(readWriters))<br> for _, readWriter := range readWriters {<br>  // Convert io.ReadWriter to io.Reader<br>  reader := io.Reader(readWriter)<br>  // Append to the slice of io.Reader<br>  readers = append(readers, reader)<br> }<br><br> // Print the slice of io.Reader<br> fmt.Println(readers)<br>}</pre><p>In this code snippet, we first create a slice of io.ReadWriter containing os.Stdout and os.Stderr. Then we create a slice of io.Reader with the same length as the slice of io.ReadWriter. We iterate over the slice of io.ReadWriter and convert each element to io.Reader using a type conversion. Finally, we append the converted element to the slice of io.Reader. We print the slice of io.Reader to verify that the conversion was successful.</p><h4>47. How to tell the main goroutine to wait for all worker goroutines to finish?</h4><p>To tell the main goroutine to wait for all worker goroutines to finish, we can use a sync.WaitGroup. Here&#39;s an example:</p><pre>package main<br><br>import (<br> &quot;fmt&quot;<br> &quot;sync&quot;<br>)<br><br>func worker(id int, wg *sync.WaitGroup) {<br> defer wg.Done()<br> fmt.Printf(&quot;Worker %d starting\n&quot;, id)<br> // Do some work...<br> fmt.Printf(&quot;Worker %d done\n&quot;, id)<br>}<br><br>func main() {<br> var wg sync.WaitGroup<br> for i := 1; i &lt;= 5; i++ {<br>  wg.Add(1)<br>  go worker(i, &amp;wg)<br> }<br><br> // Wait for all workers to finish<br> wg.Wait()<br><br> fmt.Println(&quot;All workers done&quot;)<br>}</pre><p>In this example, the worker function simulates doing some work by printing a message and then waiting for a short period of time. The wg.Add(1) call adds a worker to the WaitGroup, and the wg.Done() call signals that the worker is finished. Finally, wg.Wait() blocks the main goroutine until all workers are done.</p><h4>48. Will passing a Pointer as a function argument always be faster?</h4><p>Passing a pointer as a function argument <strong>won&#39;t always be faster</strong>. Whether it is faster depends on several factors, such as the size of the data being passed, how frequently the data is accessed, and whether the data needs to be modified by the function.</p><p>When you pass a value to a function in Go, a copy of that value is made. The overhead of copying the value is negligible for small values like integers or floats. However, for larger values like arrays or structs, passing by value can be expensive in terms of time and memory.</p><p>In such cases, passing a pointer to the data can be more efficient than copying it. This way, the function can directly access and modify the original data without making a copy.</p><p>However, passing a pointer has its own overhead. Dereferencing a pointer to access the value it points to can be slower than accessing a value directly. Additionally, if the pointer is not used carefully, it can introduce bugs such as data races.</p><p>Therefore, the decision to pass a pointer or a value as a function argument should be based on the specific use case and the size of the data being passed.</p><h4>49. What is a variadic variable of a function? How to work with this variable?</h4><p>A variadic variable is a special parameter of a function in Go that allows the function to accept an arbitrary number of arguments. An ellipsis denotes it (...) followed by the parameter type. For example:</p><pre>func foo(args ...int) {<br>    // ...<br>}</pre><p>The args parameter is a variadic variable of type []int. It allows the function foo to accept any number of int arguments.</p><p>To work with a variadic variable, you can treat it like a slice. For example, you can iterate over its elements using a for loop:</p><pre>func foo(args ...int) {<br>    for _, arg := range args {<br>        // do something with arg<br>    }<br>}</pre><p>You can also pass a slice of values to a function that accepts a variadic variable by using the ellipsis (...) operator to expand the slice:</p><pre>func foo(args ...int) {<br>    // ...<br>}<br><br>func main() {<br>    s := []int{1, 2, 3}<br>    foo(s...)  // expand the slice and pass it to foo<br>}</pre><p>In this example, the slice s is expanded using the ellipsis (...) operator and passed as individual arguments to the function foo. The function foo receives the expanded arguments as a variadic variable.</p><h4>50. How to work with the internal package?</h4><p>The internal package is a special type of package in Go that is used for code only intended to be used within the package in which it is defined. It is not visible to code outside of the package, so it can be used for implementing private or implementation-specific functionality that should not be exposed to other packages.</p><p>To work with the internal package, you can define it in the same way as any other Go package, with the internal directory located inside the main package directory. For example, if you have a package called mypackage, you can define an internal package within it by creating a directory called internal at the same level as the mypackage.go file.</p><p>Once you have defined the internal package, you can use it to define any functions, variables, or types that should only be used within the mypackage package. You can import the internal package in the same way as any other package, using the path to the internal directory.</p><p>For example, if you have defined an internal package called myinternal within the mypackage package, you can import it using the following statement:</p><pre>import &quot;mypackage/internal/myinternal&quot;</pre><p>Note that this import statement is only valid within the mypackage package, and it will not work outside of it. This means that any functions, variables, or types defined in the myinternal package will only be visible within the mypackage package.</p><p>Working with the internal package can be useful for encapsulating implementation details and creating clean and maintainable code. Restricting access to implementation-specific functionality ensures that your package is easier to understand and use and less prone to errors and bugs.</p><h4>51. How does dot import work, and why is it a bad practice?</h4><p>In Go, a dot import is a way to import a package so that its exported names can be accessed without prefixing them with the package name. This is done by using a dot . symbol before the package name while importing.</p><p>For example, if we have a package called math with a function called Abs, we can import it using a dot import as follows:</p><pre>import . &quot;math&quot;<br><br>func main() {<br>    x := Abs(-10) // No need to prefix with &quot;math&quot;<br>    fmt.Println(x)<br>}</pre><p>While this may seem convenient, it can be a bad practice because it can make the code less readable and harder to understand. It can also lead to naming conflicts if multiple packages have the same name, as all the exported names of the imported packages are available in the current namespace.</p><p>It is generally recommended to avoid using dot imports and instead use the full package name or an alias for clarity and to avoid naming conflicts.</p><h4>52. How does underlining import work?</h4><p>The underscore (_) character indicates that a package is being imported for its side effects only, without being used in the code. This is sometimes called a blank import.</p><p>For example, consider the following Go code:</p><pre>package main<br><br>import (<br>    &quot;fmt&quot;<br>    _ &quot;github.com/some/package&quot;<br>)<br><br>func main() {<br>    fmt.Println(&quot;Hello, World!&quot;)<br>}</pre><p>In the above example, the package &quot;github.com/some/package&quot; is being imported, but it is not being used anywhere in the code. The underscore character indicates that the package is being imported only for its side effects, including registering a driver for a database/sql package or initializing a global variable.</p><p>The use of blank imports can be useful in some cases, but it can also make the code less clear and harder to maintain. It can also make it difficult to determine which packages are used in the code, leading to unnecessary dependencies and larger binary sizes. Therefore, it is generally considered a bad practice to use blank imports unless they are absolutely necessary.</p><h4>53. What is defer()?</h4><p>In Go, defer is a keyword that is used to delay the execution of a function or statement until the surrounding function returns. It allows a programmer to ensure that an action is performed after a function call, regardless of whether it succeeds or fails.</p><p>The defer statement is typically used to release resources acquired by a function, such as closing files or network connections, or to perform some clean-up tasks before a function returns. It can also be used to simplify code by ensuring that certain actions are performed in the correct order.</p><p>For example, consider the following code that opens a file, writes some data to it, and then closes the file:</p><pre>func writeToFile(filename string, data []byte) error {<br>    f, err := os.Create(filename)<br>    if err != nil {<br>        return err<br>    }<br>    defer f.Close()<br><br>    _, err = f.Write(data)<br>    return err<br>}</pre><p>In this code, the defer f.Close() statement ensures that the file will always be closed, even if an error occurs while writing to the file.</p><h4>54. How to work with goto?</h4><p>In Go, goto statement allows to transfer control to another part of the program using a label. The label is an identifier followed by a colon and should be placed before the statement to which you want to transfer control.</p><p>Here is a simple example of using goto statement:</p><pre>package main<br><br>import &quot;fmt&quot;<br><br>func main() {<br>    i := 0<br><br>loop:   // label<br>    fmt.Println(i)<br>    i++<br>    if i &lt; 10 {<br>        goto loop   // jump to the label<br>    }<br>}</pre><p>In this example, goto is used to create an infinite loop. The loop label marks the beginning of the loop, and the goto loop statement transfers control back to the beginning.</p><p>While goto can be useful in certain situations, but it can also make the code hard to read and understand. It is generally considered bad practice to use goto in Go, and there are usually better ways to write the same code without using it.</p><h4>55. What are reflect.DeepEqual() and reflect.TypeOf()?</h4><p>reflect.DeepEqual() and reflect.TypeOf() are functions provided by the reflect package in Go, which is used to perform reflection in Go programs.</p><p>reflect.TypeOf() returns the actual type of a value represented by a reflect.Type object. This function is commonly used in Go libraries that require runtime type checking and introspection. For example, you could use it to determine the type of a variable at runtime or to check if a value implements a certain interface.</p><p>reflect.DeepEqual() is a function that compares two values for deep equality. This function is useful when you need to compare two values of arbitrary types or when you need to compare nested values (e.g. a struct that contains slices or maps). However, it&#39;s important to note that DeepEqual() has some limitations, and it may not always behave as expected. For example, it doesn&#39;t work with types that contain unexported fields or cyclic data structures.</p><p>It’s worth noting that reflection in Go is often discouraged due to its performance overhead and lack of type safety. Avoiding reflection and using static typing and interfaces is recommended in most cases. However, reflection may be the only option in some cases, such as in generic programming libraries or serialization/deserialization.</p><h4>56. How to parse the time?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*u7klRPYo2RpEE-3C" /><figcaption>Photo by <a href="https://unsplash.com/@loic?utm_source=medium&amp;utm_medium=referral">Djim Loic</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In Go, you can parse time using the time.Parse() function. The time.Parse() function takes a format string and a string representation of time as input and returns a time.Time value. Here&#39;s an example:</p><pre>package main<br><br>import (<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>)<br><br>func main() {<br>    dateStr := &quot;2022-05-05 10:30:00&quot;<br>    layout := &quot;2006-01-02 15:04:05&quot;<br>    date, err := time.Parse(layout, dateStr)<br>    if err != nil {<br>        fmt.Println(&quot;Error parsing date:&quot;, err)<br>    } else {<br>        fmt.Println(&quot;Parsed date:&quot;, date)<br>    }<br>}</pre><p>In this example, we define a string representation of the time in dateStr and the corresponding layout in layout. We then call time.Parse() with these arguments to get a time.Time value representing the parsed time. The if statement checks for errors, and if there are none, the parsed time is printed to the console.</p><h4>57. How to compare two dates?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SY2zBUWwB0qtMJp7" /><figcaption>Photo by <a href="https://unsplash.com/@towfiqu999999?utm_source=medium&amp;utm_medium=referral">Towfiqu barbhuiya</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>To compare two dates in Go, you can use the time.Time type and its various methods. Here&#39;s an example:</p><pre>package main<br><br>import (<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>)<br><br>func main() {<br>    date1, _ := time.Parse(&quot;2006-01-02&quot;, &quot;2022-05-01&quot;)<br>    date2, _ := time.Parse(&quot;2006-01-02&quot;, &quot;2022-05-02&quot;)<br><br>    if date1.Before(date2) {<br>        fmt.Println(&quot;date1 is before date2&quot;)<br>    } else if date1.After(date2) {<br>        fmt.Println(&quot;date1 is after date2&quot;)<br>    } else {<br>        fmt.Println(&quot;date1 is the same as date2&quot;)<br>    }<br><br>    if date1.Equal(date2) {<br>        fmt.Println(&quot;date1 and date2 are equal&quot;)<br>    } else {<br>        fmt.Println(&quot;date1 and date2 are not equal&quot;)<br>    }<br>}</pre><p>In this example, we create two time.Time values by parsing two strings that represent dates using the time.Parse() function. Then we use the Before(), After(), and Equal() methods to compare the dates and print the results.</p><h4>58. What is a pointer, and how to work with it?</h4><p>In Go, a pointer is a variable that stores the memory address of another variable. Pointers are used to access and modify the value of a variable indirectly by accessing its memory location rather than its value directly.</p><p>In Go, the &amp; operator is used to get the address of a variable, and the * operator is used to access the value that a pointer points to. Here is an example:</p><pre>// Declare a variable of type int<br>var num int = 42<br><br>// Declare a pointer variable that points to the address of `num`<br>var ptr *int = &amp;num<br><br>// Print the value of `num` and the value pointed to by `ptr`<br>fmt.Println(num)    // Output: 42<br>fmt.Println(*ptr)   // Output: 42<br><br>// Modify the value of `num` indirectly using the pointer `ptr`<br>*ptr = 24<br>fmt.Println(num)    // Output: 24<br>fmt.Println(*ptr)   // Output: 24</pre><p>Go uses pointers extensively to pass variables by reference, allocate memory dynamically, and optimize memory usage. However, they can also lead to bugs and memory leaks if used incorrectly, so it’s important to use them carefully and follow best practices.</p><h4>59. How to check if a variable implements an interface?</h4><p>In Go, you can check if a variable implements an interface by using the type assertion syntax with the .(type) keyword.</p><p>Here’s an example:</p><pre>type MyInterface interface {<br>    Method1() string<br>    Method2() int<br>}<br><br>type MyStruct struct {<br>    Field1 string<br>    Field2 int<br>}<br><br>func (s *MyStruct) Method1() string {<br>    return s.Field1<br>}<br><br>func (s *MyStruct) Method2() int {<br>    return s.Field2<br>}<br><br>func main() {<br>    var i interface{} = &amp;MyStruct{&quot;foo&quot;, 42}<br><br>    if _, ok := i.(MyInterface); ok {<br>        fmt.Println(&quot;i implements MyInterface&quot;)<br>    } else {<br>        fmt.Println(&quot;i does not implement MyInterface&quot;)<br>    }<br>}</pre><p>In this example, we define an interface MyInterface and a struct MyStruct that implements it. We then create a variable i of type interface{} that holds a pointer to a MyStruct instance.</p><p>To check if i implements MyInterface, we use the type assertion syntax i.(MyInterface). If the assertion succeeds, the value of ok will be true, indicating that i does implement MyInterface. Otherwise, ok will be false.</p><h4>60. What is embedding?</h4><p>In Go, embedding is a mechanism that allows a struct to include (embed) fields and methods of another struct within its own definition. This is similar to inheritance in object-oriented programming but with a different approach.</p><p>When a struct embeds another, it gains access to all the fields and methods of the embedded struct as if they were defined in the embedding struct itself. This allows for code reuse and abstraction.</p><p>There are two ways to embed a struct in Go:</p><ul><li><strong>Struct embedding</strong> involves defining a new struct with the embedded struct as a field. The fields and methods of the embedded struct can be accessed using dot notation from the embedding struct.</li></ul><pre>type Embedded struct {<br>    field1 int<br>    field2 string<br>}<br><br>type MyStruct struct {<br>    Embedded<br>    field3 float64<br>}<br><br>func main() {<br>    s := MyStruct{<br>        Embedded: Embedded{1, &quot;test&quot;},<br>        field3:   3.14,<br>    }<br>    fmt.Println(s.field1, s.field2, s.field3) // Output: 1 test 3.14<br>}</pre><ul><li><strong>Interface embedding</strong>: This involves embedding an interface type within a struct and then implementing the methods of that interface on the struct. The struct can then be used as an implementation of the interface.</li></ul><pre>type MyInterface interface {<br>    DoSomething()<br>}<br><br>type MyStruct struct {<br>    MyInterface<br>}<br><br>func (s *MyStruct) DoSomething() {<br>    fmt.Println(&quot;Doing something&quot;)<br>}<br><br>func main() {<br>    s := MyStruct{}<br>    s.DoSomething() // Output: Doing something<br>}</pre><p>Both struct and interface embedding can create more expressive and reusable code in Go.</p><h4>61. Describe the steps in the testing process</h4><p>In Go, the testing process follows a similar process as in other programming languages but with some specificities related to the Go language and its testing package.</p><ol><li><strong>Writing test functions</strong>: The first step is to write test functions that will test the different parts of the code. Test functions are prefixed with the word Test, and their signature is func TestXxx(*testing.T).</li><li><strong>Running tests</strong>: To run the tests, you can use the go test command, which will automatically discover and run all the test functions in your package. The command can be executed from the command line, producing output indicating the status of the tests.</li><li><strong>Assertions</strong>: Within the test functions, you can use assertions to verify that the code being tested is producing the expected results. Go’s testing package provides several assertion functions that can be used for this purpose, such as t.Errorf(), t.Fatalf(), and t.Logf().</li><li><strong>Benchmarking</strong>: Go also has a built-in benchmarking framework that can be used to measure code performance. Benchmark functions are prefixed with the word Benchmark, and their signature is func BenchmarkXxx(*testing.B).</li><li><strong>Code coverage</strong>: Go’s testing package also provides tools for measuring code coverage. By running the tests with the -cover flag, you can see which lines of code are being executed during the tests and which are not. This can help identify areas of the code that are not being tested adequately.</li><li><strong>Integration testing</strong>: In addition to unit testing, it is also important to perform integration testing to ensure that all the different components of the system are working together as expected. This can involve setting up a test environment that closely resembles the production environment and running a suite of tests against it.</li><li><strong>Continuous integration and deployment</strong>: To ensure that the code continues to work as expected over time, it is important to set up a continuous integration and deployment (CI/CD) pipeline. This involves automating the testing and deployment process to quickly and easily test and deploy changes to production. Several CI/CD tools can be used to accomplish this, such as Travis CI, CircleCI, and Jenkins.</li></ol><h4>62. How to write tests? What are table tests?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*B7NhzxEhBH7f0Uwn" /><figcaption>Photo by <a href="https://unsplash.com/@alexkondratiev?utm_source=medium&amp;utm_medium=referral">Alex Kondratiev</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In Go, tests are written as separate files with the _test.go suffix and are located in the same package as the code being tested. The Go testing package provides the testing package to write unit tests and the go test command to run them.</p><p>To write a test in Go, you need to create a function with a name starting with Test that takes a single argument of type *testing.T. This function will contain the test code and use the t argument to report the test result.</p><p>Here is an example of a simple test function in Go:</p><pre>func TestAdd(t *testing.T) {<br>    result := Add(2, 3)<br>    expected := 5<br>    if result != expected {<br>        t.Errorf(&quot;Add(2, 3) returned %d, expected %d&quot;, result, expected)<br>    }<br>}</pre><p>Table-driven tests are a technique in which multiple test cases are defined in a table, and the test function loops over the table to run each test case. This can be useful for testing a function with various inputs and expected outputs.</p><p>Here is an example of a table test in Go:</p><pre>func TestAddTableDriven(t *testing.T) {<br>    tests := []struct {<br>        a, b, expected int<br>    }{<br>        {2, 3, 5},<br>        {0, 0, 0},<br>        {-1, 1, 0},<br>        {-5, -5, -10},<br>    }<br><br>    for _, test := range tests {<br>        result := Add(test.a, test.b)<br>        if result != test.expected {<br>            t.Errorf(&quot;Add(%d, %d) returned %d, expected %d&quot;, test.a, test.b, result, test.expected)<br>        }<br>    }<br>}</pre><p>In this example, the tests variable is a slice of structs that define the test cases. The test function loops over the slice and runs each test case, using the struct fields to define the inputs and expected output. If any of the test cases fail, the t.Errorf function is used to report the failure.</p><h4>63. What is a memory leak? What are the ways to detect it? How to get rid of it?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*B5Y1JLWvjCRGoj5e" /><figcaption>Photo by <a href="https://unsplash.com/@sanatoga?utm_source=medium&amp;utm_medium=referral">Joe Zlomek</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>A memory leak is when a program continuously allocates memory but fails to release it after it is no longer needed, causing the program to consume more and more memory until it crashes or becomes unresponsive.</p><p>Detecting memory leaks can be tricky, but there are a few common symptoms to look for, such as:</p><ul><li>The program consumes more memory over time and doesn’t release it.</li><li>The program slows down or becomes unresponsive over time.</li><li>The program crashes due to a lack of memory.</li></ul><p>To detect memory leaks in Go, there are several tools available. Some of the popular ones are:</p><ul><li>go tool pprof: This is a built-in tool in Go that can be used to analyze memory usage. It can be used to generate memory profiles that show the memory usage of a Go program over time.</li><li>go-torch: This is a visualization tool that can be used to generate flame graphs from memory profiles generated by go tool pprof.</li><li>memviz: This is another visualization tool that can be used to generate a graph of the memory usage of a Go program.</li></ul><p>To get rid of memory leaks, you first need to identify the root cause of the problem. Once you’ve identified the source of the memory leak, you can take the following steps to fix it:</p><ul><li><strong>Release unused memory</strong>: Make sure you’re releasing any memory you no longer need. For example, ensure you’re closing any files or network connections you’re done with.</li><li><strong>Use garbage collection</strong>: In Go, memory is managed through a garbage collector. If you’re not manually managing memory, ensure you use the garbage collector correctly. For example, ensure you’re not holding onto references to objects you no longer need.</li><li><strong>Avoid circular references</strong>: Circular references occur when two or more objects reference each other. This can cause the garbage collector to be unable to free the memory associated with these objects. Avoid circular references by breaking the references between the objects.</li><li><strong>Use profiling tools</strong>: The abovementioned tools help you identify and fix memory leaks.</li></ul><h4>64. What is a race condition? What are the ways to detect it? How to get rid of it?</h4><p>A race condition is a software bug that occurs when two or more threads access a shared resource and try to modify it simultaneously. The program&#39;s behaviour is unpredictable and can vary depending on the order in which the threads execute.</p><p>Race conditions can be difficult to detect and reproduce because they are usually timing-dependent and may only occur under specific circumstances. They can cause crashes, data corruption, and other unexpected behaviour.</p><p>The ways to detect race conditions include:</p><ol><li><strong>Manual code review</strong>: The code can be reviewed manually to identify potential race conditions.</li><li><strong>Static analysis tools</strong>: Several static analysis tools can detect race conditions automatically.</li><li><strong>Dynamic analysis tools</strong>: Dynamic analysis tools such as race detectors can be used to detect race conditions at runtime.</li></ol><p>To eliminate race conditions, we need to synchronize access to shared resources. This can be done using synchronization primitives such as locks, mutexes, and semaphores. In Go, we can use the sync package to implement synchronization. For example, we can use a mutex to protect access to a shared variable:</p><pre>import &quot;sync&quot;<br><br>var mu sync.Mutex<br>var sharedVar int<br><br>func updateSharedVar(newValue int) {<br>    mu.Lock()<br>    sharedVar = newValue<br>    mu.Unlock()<br>}</pre><p>In the above example, the mu mutex is used to protect access to the sharedVar variable. The Lock() method is called before modifying the variable, and the Unlock() method is called after modifying it. This ensures that only one thread can modify the variable simultaneously, preventing race conditions.</p><h3>Practical tasks</h3><h4>65. Subset Sum Problem</h4><p>Given: the set of positive integers and the value sum. Determine whether a subset of the given set exists with a sum equal to the sum.</p><pre>package main<br><br>import &quot;fmt&quot;<br><br>func main() {<br> arr := []int{3, 34, 4, 12, 5, 2}<br> fmt.Println(findSubsetSum(arr, 9))<br>}<br><br>func findSubsetSum(arr []int, sum int) bool {<br> if sum == 0 {<br>  return true<br> }<br> if len(arr) == 0 {<br>  return false<br> }<br> if arr[0] &gt; sum {<br>  return findSubsetSum(arr[1:], sum)<br> }<br> return findSubsetSum(arr[1:], sum-arr[0]) || findSubsetSum(arr[1:], sum)<br>}</pre><h4>66. There is a code. What will be displayed on the screen? Why?</h4><p>🔗: <a href="https://go.dev/play/p/-3o2gp3enIG">The code is here</a></p><h4>67. There is a code. Can you predict what will appear on the screen? Why?</h4><p>🔗: <a href="https://go.dev/play/p/qwC_nJNFdEy">The code is here</a></p><h4>68. Implement Stack (LIFO)</h4><pre>package main<br><br>import &quot;fmt&quot;<br><br>// Stack represents a stack data structure<br>type Stack struct {<br>    items []interface{}<br>}<br><br>// Push adds an element to the top of the stack<br>func (s *Stack) Push(item interface{}) {<br>    s.items = append(s.items, item)<br>}<br><br>// Pop removes and returns the element at the top of the stack<br>func (s *Stack) Pop() interface{} {<br>    if s.IsEmpty() {<br>        return nil<br>    }<br>    lastIndex := len(s.items) - 1<br>    item := s.items[lastIndex]<br>    s.items = s.items[:lastIndex]<br>    return item<br>}<br><br>// IsEmpty checks if the stack is empty<br>func (s *Stack) IsEmpty() bool {<br>    return len(s.items) == 0<br>}<br><br>// Size returns the number of elements in the stack<br>func (s *Stack) Size() int {<br>    return len(s.items)<br>}<br><br>// Peek returns the element at the top of the stack without removing it<br>func (s *Stack) Peek() interface{} {<br>    if s.IsEmpty() {<br>        return nil<br>    }<br>    lastIndex := len(s.items) - 1<br>    return s.items[lastIndex]<br>}<br><br>func main() {<br>    stack := Stack{}<br><br>    stack.Push(10)<br>    stack.Push(20)<br>    stack.Push(30)<br><br>    fmt.Println(&quot;Stack size:&quot;, stack.Size())<br>    fmt.Println(&quot;Top element:&quot;, stack.Peek())<br><br>    poppedItem := stack.Pop()<br>    fmt.Println(&quot;Popped element:&quot;, poppedItem)<br><br>    fmt.Println(&quot;Stack size:&quot;, stack.Size())<br>    fmt.Println(&quot;Is stack empty?&quot;, stack.IsEmpty())<br>}</pre><p>This code defines a Stack struct with methods to push, pop, check if it&#39;s empty, get its size, and peek at the top element. In the main function, we create a stack, push some elements onto it, and demonstrate the usage of the stack&#39;s methods.</p><p>When you run this code, you’ll see the output:</p><pre>Stack size: 3<br>Top element: 30<br>Popped element: 30<br>Stack size: 2<br>Is stack empty? false</pre><p>This shows the size of the stack, the top element (without removing it), the popped element, the updated size of the stack, and whether the stack is empty.</p><h4>69. Implement a linked list</h4><pre>package main<br><br>import &quot;fmt&quot;<br><br>// Node represents a node in the linked list<br>type Node struct {<br>    data interface{}<br>    next *Node<br>}<br><br>// LinkedList represents a singly linked list<br>type LinkedList struct {<br>    head *Node<br>}<br><br>// InsertFront inserts an element at the front of the linked list<br>func (ll *LinkedList) InsertFront(data interface{}) {<br>    newNode := &amp;Node{<br>        data: data,<br>        next: ll.head,<br>    }<br>    ll.head = newNode<br>}<br><br>// InsertBack inserts an element at the back of the linked list<br>func (ll *LinkedList) InsertBack(data interface{}) {<br>    newNode := &amp;Node{<br>        data: data,<br>        next: nil,<br>    }<br>    if ll.head == nil {<br>        ll.head = newNode<br>        return<br>    }<br>    current := ll.head<br>    for current.next != nil {<br>        current = current.next<br>    }<br>    current.next = newNode<br>}<br><br>// Display prints the elements of the linked list<br>func (ll *LinkedList) Display() {<br>    current := ll.head<br>    for current != nil {<br>        fmt.Print(current.data, &quot; &quot;)<br>        current = current.next<br>    }<br>    fmt.Println()<br>}<br><br>func main() {<br>    linkedList := LinkedList{}<br><br>    linkedList.InsertFront(10)<br>    linkedList.InsertFront(20)<br>    linkedList.InsertBack(30)<br>    linkedList.InsertBack(40)<br><br>    linkedList.Display()<br>}</pre><p>This code defines a Node struct that represents a node in the linked list, and a LinkedList struct that represents the linked list itself. The LinkedList struct has methods to insert an element at the front or back of the list, as well as a method to display the elements of the list.</p><p>In the main function, we create a linked list, insert some elements at the front and back, and then display the elements.</p><p>When you run this code, you’ll see the output:</p><pre>20 10 30 40</pre><p>This shows the elements of the linked list as they were inserted. In this case, the elements are 20, 10, 30, and 40.</p><p>In conclusion, preparing for a middle-level Go developer interview requires a solid understanding of Go language fundamentals, problem-solving skills, and familiarity with Go’s concurrency patterns. By studying and practising the questions and answers in this guide, you have equipped yourself with valuable insights and knowledge to excel in your interview.</p><p>Remember to focus not only on memorizing the answers but also on understanding the underlying concepts. The interview process assesses your problem-solving abilities and ability to apply your knowledge to real-world scenarios. Therefore, it is important to approach each question logically and systematically, demonstrating your analytical thinking and adaptability.</p><p>Additionally, continue to explore and expand your Go programming skills beyond the scope of this guide. Engage in personal projects, contribute to open-source projects, and participate in coding challenges to enhance your proficiency and stay up-to-date with the evolving Go ecosystem.</p><p>Lastly, remember that interviews evaluate your technical skills and are an opportunity to assess the company culture, work environment, and how well you align with their goals. Use this opportunity to ask thoughtful questions about the company, team dynamics, and growth opportunities to ensure a good fit for both parties.</p><p>By combining your technical knowledge, problem-solving abilities, and positive attitude, you are well on your way to securing a middle-level Go developer position. Best of luck with your interview preparation, and may your journey as a Go developer be filled with continuous learning and success.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab52b6aaecf0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Master Your Junior Go Developer Interview: Essential Questions and Answers to Boost Your…]]></title>
            <link>https://d9nich.medium.com/master-your-junior-go-developer-interview-essential-questions-and-answers-to-boost-your-334b09f13280?source=rss-f50118a3e717------2</link>
            <guid isPermaLink="false">https://medium.com/p/334b09f13280</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[preparation]]></category>
            <category><![CDATA[interview]]></category>
            <category><![CDATA[junior-developer]]></category>
            <category><![CDATA[questions]]></category>
            <dc:creator><![CDATA[Danylo Halaiko]]></dc:creator>
            <pubDate>Mon, 15 May 2023 07:01:59 GMT</pubDate>
            <atom:updated>2023-05-15T07:01:59.347Z</atom:updated>
            <content:encoded><![CDATA[<h3>👦Master Your Junior Go Developer Interview: Essential Questions and Answers to Boost Your Confidence</h3><p>As the demand for Go (Golang) continues to rise, many companies seek junior developers with a solid foundation in the language and its ecosystem. As a junior Go developer, you may prepare for an interview and wonder what questions you can expect to encounter.</p><p>This article will provide a comprehensive list of questions and answers to help you prepare for your next junior Go developer interview. We will cover various topics, including the basics of Go syntax, best practices for writing efficient and effective Go code, common data structures and algorithms used in Go development, and much more.</p><p>Whether you are just starting out in your Go development career or looking to take the next step in your professional journey, this article will provide you with the tools and knowledge you need to succeed in your next interview and beyond.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6zcZMZyBMb82Qs4g" /><figcaption>Photo by <a href="https://unsplash.com/@vantaymedia?utm_source=medium&amp;utm_medium=referral">Van Tay Media</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Software engineering</h3><h4>1. What are the memory areas of the program? What are their features and differences?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NJFeV_umNqylpzA0" /><figcaption>Photo by <a href="https://unsplash.com/@possessedphotography?utm_source=medium&amp;utm_medium=referral">Possessed Photography</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In computer programming, there are typically four types of memory areas used by a program: stack, heap, data, and code. Each memory area has distinct features and differences, and understanding them is crucial for efficient and secure programming.</p><ol><li><strong>Stack</strong>: The stack is a Last-In-First-Out (LIFO) memory area used for local variables and function calls. When a function is called, its local variables and parameters are pushed onto the stack, and when the function returns, they are popped off. The stack is typically fixed and can be quickly accessed, making it ideal for managing temporary data.</li><li><strong>Heap</strong>: The heap is a dynamic memory area used for storing objects and data structures that do not have a fixed size or lifetime. Memory allocation and deallocation on the heap are done manually using functions like malloc() and free(), making it more flexible than the stack. However, the heap can become fragmented, leading to memory leaks and slowing the program’s execution.</li><li><strong>Data</strong>: The data area stores global and static variables with a fixed size and lifetime throughout the program’s execution. Unlike the stack and heap, the data area is initialized before the program starts and has a predefined size. It is usually used to store constants, arrays, and structures.</li><li><strong>Code</strong>: The code area contains the program’s executable code and is typically read-only. The CPU fetches and executes instructions from the code area, making it the most critical memory area. The code area is usually located in a separate memory section, and its size is determined at compile time.</li></ol><p>In summary, the stack is used for temporary data and function calls, the heap for dynamically allocated memory, the data area for global and static variables, and the code area for executable code. Each memory area has different features and trade-offs, and understanding them is crucial for efficient and secure programming.</p><h4>2. Tell me what you know about the HTTP protocol. What are the components of a request? What status codes do you know? Which groups can be distinguished?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WOicZjiWZIK-MJGk" /><figcaption><a href="https://namtech.com.au/what-is-http-https-learn-about-http-https-protocols/">What is HTTP, HTTPS? Learn about HTTP &amp; HTTPS protocols. — (namtech.com.au)</a></figcaption></figure><p>HTTP stands for Hypertext Transfer Protocol, a protocol for communication between web servers and web clients, such as browsers. HTTP is based on a client-server architecture, where the client sends a request to the server, and the server responds with a message containing the requested resource or an error message.</p><p>The components of an HTTP request are:</p><ol><li><strong>Request line</strong>: It contains the HTTP method, the URL of the requested resource, and the HTTP version.</li><li><strong>Request headers</strong>: These are optional metadata sent in the request that provides additional information about the request, such as the type of data the client can accept and the type of encoding used.</li><li><strong>Request body</strong>: This is optional data that can be sent along with the request, such as data entered in a form.</li></ol><p>The HTTP protocol uses status codes to indicate the outcome of a request. The status codes are grouped into 5 classes based on the first digit of the code:</p><ol><li><strong>1xx </strong>(<em>Informational</em>): These status codes indicate that the request has been received and is being processed.</li><li><strong>2xx </strong>(<em>Successful</em>): These status codes indicate that the request was successful and that the server has sent the requested resource.</li><li><strong>3xx </strong>(<em>Redirection</em>): These status codes indicate that the requested resource has been moved to a different location or that the client needs to take additional action to complete the request.</li><li><strong>4xx </strong>(<em>Client Error</em>): These status codes indicate that the server could not fulfil the request due to an error on the client’s side, such as an invalid request or a lack of authorization.</li><li><strong>5xx </strong>(<em>Server Error</em>): These status codes indicate that the server could not fulfil the request due to an error on the server’s side, such as a server crash or a timeout.</li></ol><p>Some common status codes are:</p><ul><li><em>200 OK</em>: Indicates that the request was successful and that the server has sent the requested resource.</li><li><em>404 Not Found</em>: This indicates that the requested resource was not on the server.</li><li><em>500 Internal Server Error</em>: This indicates an error occurred on the server while processing the request.</li><li><em>302 Found</em>: Indicates that the requested resource has been moved to a different location, and the client should follow the redirect.</li></ul><p>In summary, the HTTP protocol is used for communication between web servers and clients. HTTP requests include a request line, headers, and an optional body. HTTP status codes are used to indicate the outcome of a request, and they are grouped into 5 classes based on the first digit of the code.</p><h4>3. Name and describe any programming pattern (of your choice). When should it be used, and why?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/0*wH5DXACnKNcvfiL7.png" /><figcaption>The publisher<a href="https://refactoring.guru/design-patterns/observer"><em> notifies subscribers by calling the specific notification method on their objects.</em></a></figcaption></figure><p>One popular programming pattern is the Observer pattern. The Observer pattern is a behavioural pattern that defines a one-to-many dependency between objects so that all its dependents are notified and updated automatically when one object changes state.</p><p>The Observer pattern consists of two main components:</p><ol><li><strong>Subject</strong>: This object has a state and is observed by other objects. The subject maintains a list of its observers and notifies them when its state changes.</li><li><strong>Observer</strong>: This is the object that is notified when the state of the subject changes. The observer implements an update() method that the subject calls to notify it of the state change.</li></ol><p>The Observer pattern can be used when we have a scenario where there is a need to maintain consistency between related objects, but it is not desirable for these objects to be tightly coupled. Using the Observer pattern, we can decouple the subject and observer objects, allowing them to evolve independently without affecting each other.</p><p>For example, consider a weather monitoring application where multiple display screens should show the same weather information. In this case, the WeatherData object can be the subject, and the display screens can be the observers. When the weather data changes, the WeatherData object can notify all the display screens, and they can update themselves accordingly. This way, we can add or remove display screens without affecting the WeatherData object.</p><p>In summary, the Observer pattern is useful for maintaining consistency between related objects while decoupling them. It consists of a subject that maintains a list of its observers and notifies them when its state changes. Observers who are notified of the state change can update themselves accordingly. The Observer pattern can be used when there is a need to maintain consistency between related objects, but it is not desirable for these objects to be tightly coupled.</p><h4>4. What are processes and threads in an operating system? Describe their relationship in the context of program execution.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*upqePf1c19eRkidB" /><figcaption>Photo by <a href="https://unsplash.com/@lukash?utm_source=medium&amp;utm_medium=referral">Lukas</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In an operating system, a process is an instance of a program being executed. A process consists of a program code, data, and the resources allocated to it, such as memory, file handles, and network connections. Each process has its own address space and runs independently of other processes. Processes can communicate with each other using inter-process communication mechanisms provided by the operating system.</p><p>A thread is a lightweight unit of execution within a process. A thread shares the same address space as the process that created it and can access the same resources as other threads within the same process. Each thread within a process has its own stack and program counter, but they share the same heap and data section.</p><p>The relationship between processes and threads is that a process can contain one or more threads. When a program is executed, the operating system creates a process for it, and the process starts with a single thread of execution. The program can then create additional threads within the same process to perform tasks concurrently.</p><p>Threads within a process share the same memory space, which allows them to communicate and share data easily. This can increase performance and efficiency in programs that take advantage of concurrency.</p><p>However, it is important to note that multiple threads within a process can also introduce synchronization and race condition issues, where two or more threads simultaneously compete for the same resource or data. Therefore, it is important for programmers to use synchronization mechanisms such as locks, semaphores, and mutexes to ensure that threads access shared resources in a safe and orderly manner.</p><p>In summary, a process is an instance of a program being executed, and a thread is a lightweight unit of execution within a process. A process can contain one or more threads, and threads within a process share the same memory space, which allows them to communicate and share data easily. However, multiple threads within a process can introduce synchronization and race condition issues, which can be mitigated using synchronization mechanisms provided by the operating system.</p><h4>5. What is a design pattern?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/280/0*Mj_haQo74I1KnjNV.png" /><figcaption><a href="https://refactoring.guru/design-patterns">Design Pattern</a></figcaption></figure><p>A design pattern is a general repeatable solution to a commonly occurring problem in software design. It represents the best practices, solutions, and templates software designers and developers can use to solve design problems they encounter during software development.</p><p>Design patterns provide a way to communicate and document commonly used software design practices and solutions in a standardized way. They also help to ensure that software solutions are scalable, maintainable, and extensible over time.</p><p>There are three main categories of design patterns:</p><ol><li><strong>Creational patterns</strong>: These patterns focus on how objects are created, instantiated, and composed, and they help to ensure that the process of creating objects is flexible, extensible, and easy to maintain.</li><li><strong>Structural patterns</strong>: These patterns focus on how objects are composed and work together to form larger structures. They help to ensure that the structure of the software is scalable, easy to understand, and maintainable.</li><li><strong>Behavioural patterns</strong>: These patterns focus on how objects interact and communicate with each other to accomplish specific tasks and behaviours. They help ensure the software is flexible, easy to maintain, and extensible over time.</li></ol><p>Examples of design patterns include the Singleton pattern, which ensures that only one instance of an object is created, the Observer pattern, which defines a one-to-many relationship between objects so that when one object changes state, all its dependents are notified and updated automatically, and the Strategy pattern, which defines a family of algorithms, encapsulates each one and makes them interchangeable.</p><p>Design patterns are important for software designers and developers, providing a common language and solutions for addressing common software design problems. By using design patterns, developers can ensure that their software is well-designed, maintainable, and scalable and that it meets the needs of their users over time.</p><h4>6. What sorting algorithms do you know?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BqBJXzgCISio6eII" /><figcaption>Photo by <a href="https://unsplash.com/@traxing?utm_source=medium&amp;utm_medium=referral">Soraya Irving</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>There are many sorting algorithms in computer science, each with its own strengths and weaknesses. Here are some of the most commonly used sorting algorithms:</p><ol><li><strong>Bubble Sort</strong>: This algorithm repeatedly swaps adjacent elements in the wrong order until the list is sorted.</li><li><strong>Selection Sort</strong>: This algorithm selects the smallest element in the list and swaps it with the first element. Then it selects the second smallest element and swaps it with the second element, and so on, until the list is sorted.</li><li><strong>Insertion Sort</strong>: This algorithm inserts each list element into its correct position in a sorted sub-list until the entire list is sorted.</li><li><strong>Merge Sort</strong>: This algorithm divides the list into smaller sub-lists, sorts the sub-lists recursively, and then merges the sub-lists back together to produce a sorted list.</li><li><strong>Quick Sort</strong>: This algorithm selects a “pivot” element from the list and partitions the list into two sub-lists, one with elements less than the pivot and one with elements greater than the pivot. The algorithm then recursively sorts the sub-lists.</li><li><strong>Heap Sort</strong>: This algorithm uses a heap data structure to sort the list. It repeatedly removes the root element from the heap (the maximum element in a max heap) and places it at the end of the list until the entire list is sorted.</li><li><strong>Radix Sort</strong>: This algorithm sorts the list by grouping elements by their individual digits, starting with the least significant digit and working up to the most significant digit.</li></ol><p>Each sorting algorithm has its own time and space complexity, and the choice of algorithm depends on the application’s specific requirements. For example, insertion sort may be the best choice if the list is small or nearly sorted. If the list is large and the available memory is limited, merge sort may be the best choice.</p><h4>7. What is Big-O notation?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jmQZreEelA5jcB49" /><figcaption>Photo by <a href="https://unsplash.com/@barkiple?utm_source=medium&amp;utm_medium=referral">John Barkiple</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Big-O notation is a mathematical notation used to describe the asymptotic behaviour of a function or algorithm. In computer science, Big-O notation is used to describe the worst-case time complexity of an algorithm.</p><p>In Big-O notation, a function f(n) is said to be O(g(n)) if there exists a positive constant c and a value n0 such that f(n) is less than or equal to c * g(n) for all n greater than or equal to n0.</p><p>In other words, Big-O notation describes the upper bound on the growth rate of a function or algorithm. For example, if the worst-case time complexity of an algorithm is O(n), the algorithm’s running time grows linearly with the input size.</p><p>Some common Big-O notations and their corresponding growth rates include:</p><ul><li>O(1) — constant time</li><li>O(log n) — logarithmic time</li><li>O(n) — linear time</li><li>O(n log n) — linearithmic time</li><li>O(n²) — quadratic time</li><li>O(2^n) — exponential time</li></ul><p>Big-O notation is useful for analyzing and comparing the performance of algorithms, as it provides a way to describe how an algorithm’s running time grows with the input size. By choosing an algorithm with a lower Big-O complexity, it is possible to improve the performance of a system, especially for large inputs.</p><h4>8. What are the differences between a unit test and an integration test?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vns2j_Ffesd9Xtut" /><figcaption>Photo by <a href="https://unsplash.com/@hansreniers?utm_source=medium&amp;utm_medium=referral">Hans Reniers</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Unit tests and integration tests are two different types of tests used in software development to ensure the quality and correctness of software. The main differences between unit tests and integration tests are as follows:</p><ol><li><strong>Scope</strong>: Unit tests are designed to test the functionality of a single, isolated code unit, such as a function or method, in isolation from the rest of the codebase. On the other hand, integration tests are designed to test the interactions and integration between multiple code units, such as modules, components, or services.</li><li><strong>Dependencies</strong>: Unit tests typically use mocking or stubbing techniques to isolate the code being tested from its dependencies so that the focus is on testing the unit’s behaviour. Integration tests, however, require the presence of these dependencies and test the integration between them.</li><li><strong>Execution</strong>: Unit tests are usually executed quickly and frequently during the development process, as they only test a small piece of code and can be run in isolation. Integration tests, on the other hand, are usually executed less frequently and take longer to run, as they test multiple components or services and require more setup and configuration.</li><li><strong>Feedback</strong>: Unit tests provide fast and specific feedback on the behaviour of a single code unit, making it easier to identify and fix issues. Integration tests, on the other hand, provide feedback on the system&#39;s overall behaviour, which is useful for identifying issues with the integration between components or services.</li></ol><p>In summary, unit tests test individual code units in isolation, while integration tests test the interactions and integration between multiple code units. Both types of tests are important for ensuring the quality and correctness of software and should be used in combination as part of a comprehensive testing strategy.</p><h4>9. What is a mock, and what is it used for?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*K9X3Uzj2ygzcEQOL" /><figcaption>Mock</figcaption></figure><p>In software development, a mock is a simulated object or component used to replace the real implementation of that object or component to test or simulate the system’s behaviour.</p><p>Mocks are often used in unit testing to isolate the code being tested from its dependencies, such as external APIs, databases, or services. Using a mock object to simulate the dependency’s behaviour, the test’s focus can be on the behaviour of the unit being tested rather than on the behaviour of the dependency.</p><p>Mocks can also be used in other types of testing, such as integration or acceptance testing, to simulate the behaviour of components or services that are not yet implemented or unavailable during testing.</p><p>Mock objects can be created manually or with the help of a mocking framework, which provides tools and libraries for creating and managing mock objects.</p><p>Mocking can be particularly useful when the real implementation of a component or service is complex, expensive to set up, or unreliable or when testing a specific edge case that is difficult to reproduce with the real implementation.</p><p>In summary, a mock is a simulated object or component used in software development to replace the real implementation of a dependency to test or simulate the system’s behaviour.</p><h4>10. What is SDLC? What are the stages, and why are they needed?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nZNsBsgxNcWV991M" /><figcaption>Photo by <a href="https://unsplash.com/@marvelous?utm_source=medium&amp;utm_medium=referral">Marvin Meyer</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>SDLC stands for Software Development Life Cycle. It is a process used in software engineering to design, develop, and maintain software products. SDLC is a systematic approach to software development that consists of several stages, each with its own set of activities and deliverables.</p><p>The stages of SDLC are as follows:</p><ol><li><strong>Planning</strong>: This stage involves defining the software product’s goals, scope, and requirements. This includes determining the project&#39;s feasibility, identifying the stakeholders, and defining the project plan.</li><li><strong>Analysis</strong>: In this stage, the requirements are gathered, analyzed, and documented. This includes identifying the user needs, analyzing the business processes, and defining the functional and non-functional requirements.</li><li><strong>Design</strong>: This stage involves creating a high-level design of the software product, including the architecture, interfaces, data models, and algorithms. This stage also includes creating detailed designs for individual components or modules.</li><li><strong>Implementation</strong>: This stage involves writing the code, integrating the components, and testing the software product. This includes coding, debugging, testing each module or component and then integrating them into the overall system.</li><li><strong>Testing</strong>: In this stage, the software product is tested to ensure it meets the requirements and is free from defects. This includes unit testing, integration testing, system testing, and acceptance testing.</li><li><strong>Deployment</strong>: In this stage, the software product is released to the users or customers. This includes preparing the software for deployment, creating documentation, and training the users.</li><li><strong>Maintenance</strong>: In this stage, the software product is maintained and updated to ensure it meets the requirements and remains usable. This includes fixing bugs, adding new features, and updating the documentation.</li></ol><p>The stages of SDLC are needed to ensure that software products are developed structured and systematically, focusing on meeting the requirements, delivering quality products, and managing risks. Each stage has its own set of activities, deliverables, and objectives, and the output of one stage becomes the input for the next stage. By following a structured SDLC process, software development teams can ensure that software products are developed efficiently, focusing on quality and customer satisfaction.</p><h4>11. What is SOLID?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*blr1lHG_FPS-E4bX.jpg" /><figcaption><a href="https://www.boxpiper.com/posts/solid-software-design-principles">SOLID principles</a></figcaption></figure><p><strong>SOLID </strong>is a set of principles for object-oriented software development intended to make software systems more understandable, maintainable, and scalable. The SOLID acronym stands for:</p><p><strong>S </strong>— Single Responsibility Principle (SRP) <strong>O</strong> — Open/Closed Principle (OCP) <strong>L </strong>— Liskov Substitution Principle (LSP) <strong>I</strong> — Interface Segregation Principle (ISP) <strong>D</strong> — Dependency Inversion Principle (DIP)</p><p>Here is a brief description of each of these principles:</p><ol><li><strong>Single Responsibility Principle (SRP)</strong>: This principle states that each class or module should have only one reason to change. In other words, a class should have only one responsibility, and any change to that responsibility should be isolated to that class.</li><li><strong>Open/Closed Principle (OCP)</strong>: This principle states that software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. This means you should be able to add new functionality to a system without modifying existing code.</li><li><strong>Liskov Substitution Principle (LSP)</strong>: This principle states that objects of a superclass should be able to be replaced with objects of a subclass without affecting the correctness of the program. In other words, subclasses should be able to be used in place of their superclass without any unexpected behaviour.</li><li><strong>Interface Segregation Principle (ISP)</strong>: This principle states that clients should not be forced to depend on interfaces they do not use. Interfaces should be small and focused, with only the needed methods for each client.</li><li><strong>Dependency Inversion Principle (DIP)</strong>: This principle states that high-level modules should not depend on low-level modules. Instead, both should depend on abstractions (interfaces). This means that the implementation details of a module should be hidden behind an interface so that changes to the implementation do not affect the higher-level modules that depend on it.</li></ol><p>By following these principles, developers can create easier software systems to maintain, extend, and scale. The SOLID principles promote good design practices, such as loose coupling, high cohesion, and modularity, which can lead to more robust, flexible, and reusable code.</p><h4>What is MVC?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wf5sHw2jPLSv0iHk" /><figcaption>Photo by <a href="https://unsplash.com/@victoriano?utm_source=medium&amp;utm_medium=referral">Victoriano Izquierdo</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>MVC stands for Model-View-Controller, a software architectural pattern for developing user interfaces. It separates the application logic into three interconnected components: the model, the view, and the controller.</p><p>Here is a brief overview of each of these components:</p><ol><li><strong>Model</strong>: The model represents the application’s data and business logic. It is responsible for managing the data, processing user input, and responding to queries from the view or controller.</li><li><strong>View</strong>: The view is responsible for rendering the user interface. It displays the data from the model to the user and may also handle user input, such as button clicks or form submissions.</li><li><strong>Controller</strong>: The controller is an intermediary between the view and the model. It handles user input, such as button clicks or form submissions, and updates the model accordingly. It also retrieves data from the model and updates the view to display it to the user.</li></ol><p>The main benefit of using the MVC pattern is that it separates the application logic into distinct, modular components, which makes it easier to develop, test, and maintain. It also promotes the principles of separation of concerns and loose coupling, which can lead to more scalable and flexible code.</p><p>The MVC pattern is widely used in web development frameworks, such as Ruby on Rails, Laravel, and ASP.NET, as well as in desktop and mobile application development.</p><h4>13. What is a DBMS? What is the difference between a DBMS and a database?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JyFR9lS43v1T08sm" /><figcaption>Photo by <a href="https://unsplash.com/pt-br/@campaign_creators?utm_source=medium&amp;utm_medium=referral">Campaign Creators</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>A DBMS, or Database Management System, is software used to manage and manipulate databases. The software system provides an interface to create, store, retrieve, and update data in a database. A DBMS manages data storage, retrieval, security, and integrity in a database.</p><p>On the other hand, a database is a collection of data organized in a specific way. A database is a structured data collection stored and managed by a DBMS. It can contain one or more tables, which are used to store data in a structured format.</p><p>In other words, a DBMS is a software system that manages one or more databases, while a database is a data collection that a DBMS manages. The DBMS provides the means to access and manipulate the data stored in the database, while the database itself is simply a collection of data.</p><p>In summary, a DBMS is the software that allows you to interact with a database, while a database is the actual collection of data that the DBMS manages.</p><h4>14. What are the types of databases?</h4><p>There are several types of databases, each with its own strengths and weaknesses. Here are some of the most common types:</p><ol><li><strong>Relational database</strong>: This type of database is organized according to the relational model, which uses tables to store data. A relational database is structured so that each table represents a different data type and is linked based on common fields.</li><li><strong>NoSQL database</strong>: This type of database does not use the traditional table-based structure of a relational database. Instead, it uses a more flexible data model for faster and more scalable data processing. NoSQL databases are often used for big data and real-time applications.</li><li><strong>Object-oriented database</strong>: This database stores data in objects containing data and behaviour. An object-oriented database is well-suited for complex data structures and can be used for CAD/CAM, multimedia, and artificial intelligence applications.</li><li><strong>Hierarchical database</strong>: This type of database stores data in a tree-like structure, with each record having one parent record and zero or more child records. A hierarchical database is useful for managing data with a clear hierarchical structure, such as an organization’s personnel records.</li><li><strong>Network database</strong>: This type of database is similar to a hierarchical database but allows each record to have multiple parent and child records. A network database is useful for managing complex relationships between data, such as in a supply chain or manufacturing process.</li><li><strong>Graph database</strong>: This database stores data in a graph structure, with nodes representing entities and edges representing their relationships. A graph database is well-suited for applications involving complex data relationships, such as social networks or recommendation engines.</li></ol><p>Each database type has advantages and disadvantages, and the choice of database type will depend on the application&#39;s specific needs.</p><h4>15. What is Docker?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*h30_1B4DlDdaG5rg" /><figcaption>Photo by <a href="https://unsplash.com/@rubaitulazad?utm_source=medium&amp;utm_medium=referral">Rubaitul Azad</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Docker is a software platform for building, deploying, and running container applications. A container is a lightweight, standalone executable package of software that includes everything needed to run the application, such as code, libraries, and dependencies. Docker provides a standardized way to package and deploy applications, making moving applications between different environments and infrastructures easier.</p><p>With Docker, developers can package their applications into containers, which can then be deployed to any Docker-compatible environment. This allows developers to build and test their applications in a consistent environment without worrying about differences in infrastructure between development, testing, and production environments.</p><p>Docker also provides tools for managing containers, including starting, stopping, and restarting containers and scaling applications up and down as needed. Docker can run on various operating systems and infrastructures, including on-premises data centres, public clouds, and hybrid cloud environments.</p><p>Overall, Docker provides a powerful and flexible platform for building, deploying, and managing modern applications, enabling developers and organizations to streamline their application development and deployment processes.</p><p>📗: Read about Docker and Containers in <a href="https://medium.com/@d9nich/virtualization-or-all-you-need-to-know-about-containers-vm-docker-compose-k8s-and-b1d500c6e12a">my previous article</a></p><h3>Databases</h3><h4>16. Describe the ACID properties of relational databases.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fNzsgwsK4CgVkA6Q" /><figcaption>Photo by <a href="https://unsplash.com/de/@naipo_de?utm_source=medium&amp;utm_medium=referral">naipo.de</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>ACID </strong>is an acronym for a desirable set of properties in a database system to ensure reliability, consistency, and transactional integrity. The properties are as follows:</p><ol><li><strong>Atomicity</strong>: This property ensures that a transaction is treated as a single, indivisible operation. In other words, if a transaction consists of multiple steps, all of them must succeed, or none should be applied to the database. This ensures that the database remains consistent even in the event of a failure.</li><li><strong>Consistency</strong>: This property ensures that the database is always in a valid state. When a transaction is executed, it should leave the database consistent. If a transaction violates any constraints or rules, it should be rolled back, and the database should be restored to its previous consistent state.</li><li><strong>Isolation</strong>: This property ensures that each transaction is executed independently of other transactions. Each transaction should be isolated from other transactions to prevent conflicts and ensure that each transaction is executed as if it were the only transaction running on the database.</li><li><strong>Durability</strong>: This property ensures that once a transaction is committed, its changes are permanent and will survive any subsequent failures or system crashes. This is typically achieved by writing transaction logs or using other techniques to ensure that the changes made by a transaction are stored reliably and can be recovered in the event of a system failure.</li></ol><p>Together, the ACID properties ensure that database transactions are executed reliably, consistently, and without interference from other transactions. This helps ensure that the database remains consistent despite failures or other issues, making it a critical foundation for many modern applications.</p><p>📗: <a href="https://medium.com/@d9nich/understanding-b-trees-the-most-common-indexing-structure-in-databases-5d6a51d35312">In my previous article</a>, you can read about B-trees, a structure used for transactions.</p><h4>17. There is a table in a relational database with 100 thousand rows. A SELECT request from such a table takes time t. How will the query time change if the number of rows is one million? What are the options depending on the query/table?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*n8UZrdeSYl9j3JRn" /><figcaption>Photo by <a href="https://unsplash.com/@tsvetoslav?utm_source=medium&amp;utm_medium=referral">Tsvetoslav Hristov</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The time taken for a SELECT query from a table with 100 thousand rows can be denoted as t. When the number of rows in the table increases to one million, the time taken for the same SELECT query will depend on several factors.</p><p>If the query is a simple SELECT * FROM table, the time taken to fetch the data will increase linearly with the number of rows. In this case, the time taken for the SELECT query from a table with one million rows can be estimated as follows:</p><p>t’ = t * (1,000,000 / 100,000) = 10t</p><p>In other words, the time taken for the query to complete will increase tenfold as the table size has increased by a factor of ten.</p><p>However, the time taken for a SELECT query can vary significantly depending on the query itself and the table&#39;s structure. If the query includes WHERE clauses or JOINs, for example, the time taken will depend on the selectivity of the query and the efficiency of the indexing on the table. If the table is properly indexed, the time taken may not increase linearly with the number of rows.</p><p>In addition, the hardware and configuration of the database server can also affect the query time. For example, increasing the amount of memory or adding more processors to the server can help to improve query performance.</p><p>Therefore, depending on the query and the structure of the table, the time taken for a SELECT query from a table with one million rows can vary significantly. However, in general, the time taken will increase as the size of the table increases unless the query or the table structure is optimized to improve query performance.</p><h4>18. What database indexes are used for, and why (what data structures) do they work?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-qXvFPpUJBDt4fha" /><figcaption>Photo by <a href="https://unsplash.com/@redaquamedia?utm_source=medium&amp;utm_medium=referral">Denny Müller</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Database indexes are used to improve the speed of querying data from tables. They work by providing a data structure that allows the database to quickly locate specific rows in a table without the need to scan the entire table. This can result in significant performance improvements for queries that involve large tables or complex queries that join multiple tables.</p><p>Indexes in databases are typically implemented using B-tree or hash data structures. B-trees are commonly used in most relational database systems and are optimized for range queries and sorting operations. They are effective for handling large datasets where the number of rows is too large to be stored in memory.</p><p>Hash indexes, however, are optimized for equality-based queries, where a specific value is being searched for. They effectively handle small datasets and have a lower memory footprint than B-trees.</p><p>When a database table is indexed, the database system creates a separate data structure containing a copy of the indexed columns and pointers to the corresponding rows in the table. This allows the database to quickly locate rows matching a specific query condition.</p><p>For example, consider a database table containing customer records, with columns for customer name, address, and phone number. If a query is performed to retrieve all customers with a specific phone number, the database system can use an index on the phone number column to quickly locate the matching rows rather than scanning the entire table.</p><p>In summary, indexes in databases are used to improve query performance by providing a data structure that allows the database system to quickly locate specific rows in a table. They are typically implemented using B-tree or hash data structures and can result in significant performance improvements for large tables or complex queries.</p><p>📗: You can read about advanced indexing structures in <a href="https://medium.com/@d9nich/exploring-advanced-indexing-structures-in-databases-c4e5e3ce44bd">my previous article</a>.</p><h4>19. What NoSQL databases did you work with? What are their advantages over relational databases?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CqKHkSdvJI_k4hsq" /><figcaption>Photo by <a href="https://unsplash.com/@jankolar?utm_source=medium&amp;utm_medium=referral">Jan Antonin Kolar</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>NoSQL databases are a class of databases that use non-relational data models and are designed to handle large volumes of structured and unstructured data. Some popular types of NoSQL databases include:</p><ol><li><strong>Document databases</strong> — These databases store data in a document-oriented format, typically JSON or BSON. They are highly scalable and flexible and are used in applications that require a high degree of flexibility in the data model.</li><li><strong>Key-value stores</strong> — These databases store data as key-value pairs, where the key is a unique identifier for the data item. They are highly scalable and performant, making them popular for high-traffic web applications.</li><li><strong>Column-family stores </strong>— These databases store data in column families, groups of related columns. They are highly scalable and can handle large volumes of data.</li></ol><p>The advantages of NoSQL databases over relational databases include:</p><ol><li><strong>Scalability </strong>— NoSQL databases are highly scalable and can handle large volumes of data, making them ideal for big data applications.</li><li><strong>Flexibility</strong> — NoSQL databases have flexible data models that allow for easy schema changes, making them ideal for applications requiring frequent data model updates.</li><li><strong>Performance </strong>— NoSQL databases are designed for high performance and can handle large volumes of data with high throughput and low latency.</li><li><strong>Cost-effectiveness </strong>— NoSQL databases are often open-source and can be deployed on commodity hardware, making them more cost-effective than relational databases.</li><li><strong>Availability</strong> — NoSQL databases are often designed for high availability, with features such as replication and automatic failover, making them ideal for applications that require high uptime.</li></ol><p>However, it’s important to note that NoSQL databases are not a one-size-fits-all solution, and their advantages come with trade-offs, such as decreased consistency and increased complexity in managing the data. The choice of database type should depend on the specific requirements and constraints of the application.</p><h3>Go</h3><h4>20. In your opinion, what is the advantage of Go over other languages?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*McdM0w4vWajGh4fB" /><figcaption>Photo by <a href="https://unsplash.com/@enginakyurt?utm_source=medium&amp;utm_medium=referral">engin akyurt</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Go, also known as Golang, is a statically typed, compiled language developed by Google. Some advantages of Go over other languages include:</p><ol><li><strong>Concurrency </strong>— Go has built-in support for concurrency, allowing developers to write efficient, parallel programs that can take full advantage of modern multi-core CPUs.</li><li><strong>Fast compilation </strong>— Go compiles quickly, making building and deploying large projects easy. This is especially important in web development, where rapid iteration is crucial.</li><li><strong>Garbage collection</strong> — Go has a garbage collector that automatically manages memory allocation and deallocation, freeing developers from manually managing memory.</li><li><strong>Simple syntax</strong> — Go has a simple and concise syntax, which makes it easy to learn and read and reduces the likelihood of errors.</li><li><strong>Cross-platform support</strong> — Go supports multiple platforms, including Linux, macOS, and Windows, and can be compiled to run on other operating systems.</li><li><strong>Standard library</strong> — Go has a rich library with many useful features like networking, encryption, and compression.</li><li><strong>Strong typing </strong>— Go is a statically typed language which can help prevent bugs and improve code quality.</li></ol><p>Combining concurrency, fast compilation, garbage collection, simple syntax, cross-platform support, strong typing, and a rich standard library makes Go a popular choice for building efficient and scalable applications.</p><h4>21. What are goroutines, and what are they for?</h4><p>Goroutines are lightweight, concurrent execution units in Go used to achieve concurrent programming. Goroutines are similar to threads but are much lighter regarding memory and resource usage and can be spawned in much greater numbers.</p><p>A Go program can have multiple goroutines executing simultaneously, and the Go runtime scheduler manages the execution of these goroutines. Goroutines are created using the “go” keyword followed by a function call like this:</p><pre>go myFunction()</pre><p>When a goroutine is created, it runs independently of the main program flow, allowing a program to perform multiple tasks concurrently. Goroutines can communicate with each other using channels, which are a mechanism for safely passing data between goroutines.</p><p>Goroutines are useful for concurrent programming tasks like parallel processing, event handling, and network programming. Goroutines can help improve program performance by allowing multiple tasks to be executed simultaneously without the overhead of creating and managing multiple threads.</p><p>In summary, goroutines are a key feature of Go that enable concurrent programming, allowing developers to write efficient, scalable, and concurrent code.</p><h4>22. What are GOROOT and GOPATH?</h4><p><strong>GOROOT </strong>and <strong>GOPATH </strong>are environment variables used by the Go programming language.</p><p><strong>GOROOT </strong>is an environment variable that specifies the root directory of the Go installation on the local machine. This is where Go’s standard library and other Go-related files are located. When a Go program is compiled, it refers to the GOROOT directory to access the standard library.</p><p><strong>GOPATH </strong>is an environment variable that specifies the root directory of the Go workspace. This is where a developer’s Go code and dependencies are stored. When a Go program is compiled, it looks in the GOPATH directory to find the necessary source code and packages.</p><p>The GOPATH directory is divided into three subdirectories: “src”, “bin”, and “pkg”. The “src” directory contains the source code of the developer’s Go projects, while the “pkg” directory contains compiled packages used by those projects. The “bin” directory contains executable binaries generated by the “go install” command.</p><p>Developers can organise their Go workspace by setting the GOPATH environment variable to suit their needs. This helps make it easier to manage dependencies and share code between projects.</p><p>In summary, GOROOT specifies the root directory of the Go installation, while GOPATH specifies the root directory of the Go workspace. Understanding and properly configuring these environment variables is important for developing and compiling Go programs.</p><h4>23. What data types are used in Go?</h4><p>Go has several built-in data types, including:</p><p>1. <em>Boolean </em>— represents true/false values<br>2. <em>Numeric</em> — includes integer, floating-point, and complex numbers<br>3. <em>String </em>— represents a sequence of characters<br>4. <em>Array </em>— a fixed-size collection of elements of the same type<br>5. <em>Slice </em>— a dynamic, resizable collection of elements of the same type<br>6. <em>Map </em>— an unordered collection of key-value pairs<br>7. <em>Struct</em> — a composite data type that groups together zero or more named values of different types<br>8. <em>Interface </em>— a type that specifies a set of methods that a concrete type must implement</p><p>In addition to these built-in data types, Go also supports pointers, which are variables that hold the memory address of another variable. Go uses pointers extensively to pass references to data structures and values to functions and manage memory allocation and deallocation.</p><p>Go also provides several composite data types used for concurrent and functional programming, including channels and functions.</p><p>Overall, Go provides a rich set of data types that support a wide range of programming paradigms and applications.</p><h4>24. What does the init() function do? Give examples where it should be used. Give examples of when to avoid.</h4><p>In Go, the init() function is a special function that can be used to perform initialization tasks when a package is loaded. The init() function is called automatically by the Go runtime before the main() function is executed, and it can be defined in any package.</p><p>The init() function is useful for performing initialization tasks that need to be done before the program starts executing, such as initializing global variables, registering signal handlers, or setting up database connections. Here is an example of using the init() function to initialize a global variable:</p><pre>var myGlobalVar int<br><br>func init() {<br> myGlobalVar = 42<br>}<br><br>func main() {<br> fmt.Println(myGlobalVar)<br>}</pre><p>In this example, the init() function sets the value of the myGlobalVar variable to 42 before the main() function is executed.</p><p>The init() function can be used in any package, but it should be used sparingly, as it can make the code harder to understand and maintain. In general, it is best to use the init() function for simple initialization tasks and to avoid complex initialization logic that can lead to subtle bugs.</p><p>In addition, the init() function should not be used for tasks that can be done lazily, as it can slow down program startup time. For example, if you need to initialize a large data structure, it may be better to do so lazily, on-demand, rather than in the init() function.</p><h4>25. Explain the difference between error and panic</h4><p>In Go, errors and panics are two mechanisms for handling exceptional conditions, but they serve different purposes and implications.</p><p>An error is a value that represents an exceptional condition that can be handled gracefully. In Go, errors are represented by the error interface, which has a single method, Error() string, which returns a string describing the error. Functions that may encounter exceptional conditions return an error value as the last return value, which the calling function can check and handle accordingly.</p><p>For example, a function that opens a file may return an error if the file does not exist or cannot be opened for some reason. The calling function can check the error value and take appropriate action, such as reporting the error to the user or retrying the operation.</p><p>In contrast, panic is an exceptional condition that cannot be handled gracefully and typically indicates a bug or a programming error. When panic occurs, the program stops executing immediately and starts unwinding the call stack until it reaches a recover() function, which can capture the panic and resume execution.</p><p>Panic can be used in exceptional cases, for example, when an invariant is violated. However, it should be used cautiously and only when there is no better way to handle the situation. Generally, it is better to use error handling for expected exceptional conditions and avoid panic as much as possible.</p><p>Here is an example of how to use error handling and panic in Go:</p><pre>func openFile(filename string) (*os.File, error) {<br>    f, err := os.Open(filename)<br>    if err != nil {<br>        return nil, err<br>    }<br>    return f, nil<br>}<br><br>func main() {<br>    f, err := openFile(&quot;nonexistent_file.txt&quot;)<br>    if err != nil {<br>        log.Fatal(err) // handle error gracefully<br>    }<br>    // do something with f<br>    // ...<br>    <br>    err = f.Close()<br>    if err != nil {<br>        panic(err) // panic if an unexpected error occurs<br>    }<br>}</pre><p>In this example, the openFile() function returns an error if the file cannot be opened, which the main() function checks and handles gracefully using the log.Fatal() function. If an unexpected error occurs when closing the file, such as if the file handle is already closed, the program panics using the panic() function.</p><h4>26. How to catch panics?</h4><p>In Go, panics can be caught and handled using the built-in recover() function, which allows the program to recover from panic and continue executing.</p><p>The recover() function should be called inside a deferred function to ensure that it is called even if the function containing the panic returns early. The recover() function returns the value that was passed to the panic() function if a panic occurred or nil if no panic occurred.</p><p>Here is an example of how to catch panics using the recover() function:</p><pre>func safeDivide(x, y int) (result int, err error) {<br>    defer func() {<br>        if p := recover(); p != nil {<br>            err = fmt.Errorf(&quot;panic occurred: %v&quot;, p)<br>        }<br>    }()<br>    result = x / y<br>    return result, nil<br>}<br><br>func main() {<br>    result, err := safeDivide(10, 0)<br>    if err != nil {<br>        log.Fatal(err) // handle error gracefully<br>    }<br>    fmt.Println(result)<br>}</pre><p>In this example, the safeDivide() function divides two integers and uses the recover() function to catch and handle any panics that occur. If a panic occurs, the defer function returns the error variable. The main() function checks the error value and handles it gracefully.</p><p>☝️: Note that catching panics using recover() is generally considered a last resort and should only be used in exceptional cases where there is no better way to handle the situation. Generally, it is better to use error handling for expected exceptional conditions and avoid panic as much as possible.</p><h4>27. How do we get the current time?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4T5Jr1xKIXCkxZMN" /><figcaption>Photo by <a href="https://unsplash.com/@fortheloveofbry5tahh?utm_source=medium&amp;utm_medium=referral">Bryce Barker</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In Go, you can get the current local time using the time.Now() function from the time package. Here is an example:</p><pre>package main<br><br>import (<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>)<br><br>func main() {<br>    currentTime := time.Now()<br>    fmt.Println(&quot;Current time is:&quot;, currentTime)<br>}</pre><p>This will output the current local time in a format like 2022-01-01 12:30:45.678912345 +0000 UTC. You can format the time into a string with a specific layout using the Time.Format() method. Here is an example:</p><pre>package main<br><br>import (<br>    &quot;fmt&quot;<br>    &quot;time&quot;<br>)<br><br>func main() {<br>    currentTime := time.Now()<br>    formattedTime := currentTime.Format(&quot;2006-01-02 15:04:05&quot;)<br>    fmt.Println(&quot;Current time is:&quot;, formattedTime)<br>}</pre><p>This will output the current local time in the format 2006-01-02 15:04:05. The format string specifies the layout of the output, using special placeholders for different parts of the time value (e.g. 2006 represents the year, 01 represents the month, 02 represents the day, etc.).</p><h4>28. What is iota?</h4><p>In Go, iota is a predeclared identifier that represents an integer constant. It is used in const declarations to generate a sequence of related values.</p><p>Each use of iota within a const declaration increments its value by one. If multiple const declarations exist in the same block, each has its own separate sequence of iota values.</p><p>Here’s an example that demonstrates how iota can be used:</p><pre>package main<br><br>import &quot;fmt&quot;<br><br>const (<br>    Sunday = iota // 0<br>    Monday        // 1<br>    Tuesday       // 2<br>    Wednesday     // 3<br>    Thursday      // 4<br>    Friday        // 5<br>    Saturday      // 6<br>)<br><br>func main() {<br>    fmt.Println(Sunday)   // 0<br>    fmt.Println(Monday)   // 1<br>    fmt.Println(Tuesday)  // 2<br>    fmt.Println(Saturday) // 6<br>}</pre><p>In this example, iota is used to generate a sequence of integer constants representing the days of the week. Each constant is assigned a value greater than the previous constant&#39;s value. The first iota value is zero, and subsequent values are incremented by one.</p><h4>29. What is the difference between a slice and an array?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*upylkzXj2PZoG4mk" /><figcaption>Photo by <a href="https://unsplash.com/@pawel_czerwinski?utm_source=medium&amp;utm_medium=referral">Pawel Czerwinski</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In Go, an array is a fixed-size sequence of elements of the same type, while a slice is a dynamically-sized view into a contiguous segment of an underlying array. Here are some key differences between arrays and slices:</p><ol><li><strong>Size</strong>: The size of an array is fixed and cannot be changed once it is created, while the size of a slice can grow or shrink dynamically as elements are added or removed.</li><li><strong>Declaration</strong>: An array is declared with a specific size, like var a [5]int, while a slice is declared using the make() function or a slice literal, like var s []int or s := []int{1, 2, 3}.</li><li><strong>Passing to functions</strong>: When an array is passed to a function, a copy of the entire array is made, while a slice is passed by reference. Any changes to a slice inside a function will affect the original slice passed in.</li><li><strong>Performance</strong>: Because arrays have a fixed size and are allocated on the stack, they can be more efficient than slices, which are allocated on the heap and require extra indirection to access elements. However, slices are more flexible and easier to work with in many situations.</li></ol><p>Here’s an example that illustrates some of these differences:</p><pre>package main<br><br>import &quot;fmt&quot;<br><br>func main() {<br>    // create an array of integers<br>    var a [5]int = [5]int{1, 2, 3, 4, 5}<br><br>    // create a slice of integers from the array<br>    var s []int = a[1:3]<br><br>    // print the array and slice<br>    fmt.Println(&quot;Array:&quot;, a)<br>    fmt.Println(&quot;Slice:&quot;, s)<br><br>    // modify the slice<br>    s[0] = 10<br><br>    // print the modified array and slice<br>    fmt.Println(&quot;Array:&quot;, a)<br>    fmt.Println(&quot;Slice:&quot;, s)<br>}</pre><p>In this example, we create an array of integers with five elements and then a slice of integers from the array containing elements 2 and 3. We then modify the first element of the slice and print both the array and the slice before and after the modification. Note that the modification to the slice affects the corresponding element in the original array since the slice is just a view into the array.</p><h4>30. What parts does a slice-type variable consist of?</h4><p>A slice-type variable in Go consists of three parts: a pointer to the underlying array, a length, and a capacity.</p><p>The pointer points to the first element of the underlying array that the slice refers to. The length is the number of elements in the slice, and the capacity is the maximum number of elements the slice can hold without allocating additional memory.</p><p>These three parts are stored in a data structure created when a slice is initialized, and the slice variable is essentially a reference to this data structure.</p><h4>31. How does append work?</h4><p>In Go, append is a built-in function used to add one or more elements to a slice. When append is called, it checks if the underlying array of the slice has enough capacity to store the new elements.</p><p>If there is enough capacity, append simply adds the new element(s) to the slice by modifying the length of the slice and updating the values in the underlying array.</p><p>However, if there is not enough capacity in the underlying array, append creates a new array with twice the capacity of the original array (or a larger capacity if the new size exceeds a certain threshold) copies the elements from the original array to the new array and then adds the new element(s) to the end of the new array. Finally, append returns a new slice that points to the new array.</p><p>In either case, the append function always returns a new slice that represents the modified slice since the capacity of the original slice may have changed.</p><h4>32. What are len and capacity of a slice?</h4><p>In Go, len and cap are built-in functions used to determine the length and capacity of a slice, respectively.</p><p>The len function returns the number of elements in the slice, which is the length of the slice. It simply returns the value of the slice&#39;s length field.</p><p>The cap function returns the capacity of the slice, which is the maximum number of elements that the slice can hold without allocating additional memory. The capacity is determined by the length of the underlying array of the slice and can be obtained using the cap function as well.</p><p>It’s important to note that the capacity of a slice can be greater than its length. This means that there is extra space in the underlying array of the slice to add more elements to the slice without having to allocate additional memory.</p><h4>33. What are packages?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*el2uAt4M61meXHYx" /><figcaption>Photo by <a href="https://unsplash.com/@fempreneurstyledstock?utm_source=medium&amp;utm_medium=referral">Leone Venter</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In Go, a package is a way to organize code into reusable and maintainable units. A package is a collection of Go source files in the same directory that are compiled together to form a single binary or library.</p><p>Packages are used to:</p><ol><li><strong>Encapsulate </strong>and <strong>modularize </strong>functionality into reusable components</li><li>Provide access to functionality from other packages through exported identifiers</li><li><strong>Manage dependencies</strong> between different packages</li></ol><p>Every Go program must have a main package containing the main function that serves as the program’s entry point. All other packages can be imported into the main package using the import keyword.</p><p>Go provides a standard library that contains many useful packages for common tasks such as string manipulation, file I/O, networking, and more. In addition to the standard library, many third-party packages available on the web can be easily integrated into your Go programs using the go get command.</p><h4>34. What are interfaces, and how do they work?</h4><p>In Go, an interface is a set of method signatures defining a behaviour contract. An interface specifies what a type can do but not how it does it. Any type that implements all the methods of an interface is said to satisfy the interface and can be used interchangeably wherever that interface is expected.</p><p>Here is an example interface definition:</p><pre>type Shape interface {<br>    Area() float64<br>}</pre><p>This interface specifies a single method called Area() that returns a float64. Any type with a method with the same signature can satisfy the Shape interface. For example, we can define a struct called Circle that satisfies the Shape interface:</p><pre>type Circle struct {<br>    Radius float64<br>}<br><br>func (c Circle) Area() float64 {<br>    return math.Pi * c.Radius * c.Radius<br>}</pre><p>The Circle struct has a method called Area() that takes no arguments and returns a float64. This method has the same signature as the Area() method specified by the Shape interface, so the Circle type satisfies the Shape interface.</p><p>We can now create a function that takes a Shape interface as an argument and use it to calculate the area of any type that satisfies the Shape interface:</p><pre>func PrintArea(s Shape) {<br>    fmt.Printf(&quot;Area: %f\n&quot;, s.Area())<br>}</pre><p>Here, the PrintArea() function takes a Shape interface as its argument and calls its Area() method to print the area of the shape.</p><p>Interfaces are a powerful tool in Go that enable polymorphic behaviour and abstraction. They allow you to write code independent of specific types and can be used with any type that satisfies the interface.</p><h4>35. What are interfaces in Golang for?</h4><p>Interfaces in Golang are used to define a set of methods that a type should implement. In other words, an interface is a contract that specifies what methods a type should have to be considered “compatible” with the interface.</p><p>An interface is declared using the interface keyword followed by a set of method signatures. For example:</p><pre>type MyInterface interface {<br>    Method1() string<br>    Method2() int<br>}</pre><p>Here, MyInterface is an interface that specifies two methods: Method1, which should return a string, and Method2, which should return an int. Any type that has these two methods will be considered &quot;compatible&quot; with the MyInterface interface.</p><p>In Golang, interfaces are satisfied <strong>implicitly</strong>. This means that a type doesn’t need to declare that it implements an interface explicitly. Instead, if a type has all the methods specified in an interface, it is considered to implement that interface.</p><p>Interfaces are widely used in Golang for <strong>abstraction </strong>and <strong>decoupling</strong>. They allow you to write code that depends on an interface rather than a concrete implementation, which makes it easier to change the implementation without affecting the rest of the code. This makes Golang programs more <strong>modular </strong>and easier to maintain.</p><h4>36. What is the string data type?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*u7PqiZwdmp4AB3eI" /><figcaption>Photo by <a href="https://unsplash.com/@glencarrie?utm_source=medium&amp;utm_medium=referral">Glen Carrie</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In Golang, the string data type represents a sequence of characters. It is a built-in type implemented as a read-only slice of bytes. This means that each character in a string is represented by a byte in memory.</p><p>In Golang, strings are enclosed in double quotes (e.g. &quot;hello world&quot;). They can also contain escape sequences, such as \n for a newline character, \t for a tab character, and \&quot; for a double quote character.</p><p>Strings in Golang are immutable, which means that once a string is created, it cannot be changed. However, you can create a new string by concatenating two or more strings using the + operator, or by using the fmt.Sprintf function to format a string.</p><p>Golang also provides a wide range of built-in functions and methods for working with strings, such as len to get the length of a string, strings.Contains to check if a string contains a substring, strings.ToUpper to convert a string to uppercase, and many more.</p><p>Overall, the string data type is a fundamental part of Golang and is used extensively in Golang programs for representing text and other types of data that can be represented as a sequence of characters.</p><h4>37. What is the difference between quotes in Go (double, single, back)?</h4><p>In Go, three types of quotes can be used to represent string literals:</p><ol><li>Double quotes (&quot;&quot;) are used to represent a string literal in which escape sequences like \n or \t are replaced by their respective special characters.</li><li>Single quotes (&#39;&#39;) are used to represent a single character or rune. Unlike double quotes, single quotes cannot represent a string containing escape sequences.</li><li>Back quotes (``) are used to represent a raw string literal in which escape sequences are not interpreted. Back quotes are often used to define regular expressions, multi-line strings, or strings that include backslashes and other special characters.</li></ol><p>Here is an example of how each of these quotes can be used:</p><pre>package main<br><br>import &quot;fmt&quot;<br><br>func main() {<br>    // Double quotes<br>    fmt.Println(&quot;Hello, World!&quot;)  // Output: Hello, World!<br>    // Single quotes<br>    fmt.Println(&#39;A&#39;)  // Output: 65<br>    // Back quotes<br>    fmt.Println(`\t\n`)  // Output: \t\n<br>}</pre><h4>38. What is a rune?</h4><p>In Go, a rune is a data type that represents a Unicode code point. It is an alias for the int32 type and is used to represent a single Unicode character.</p><p>Unicode is a universal character encoding standard that defines a unique number for every character used in most of the world’s writing systems. A rune is a value that can hold any Unicode code point, which means it can represent any character in the Unicode standard.</p><p>In Go, a rune literal is enclosed in single quotes, like this:</p><pre>var r rune = &#39;a&#39;</pre><p>Rune literals can represent characters from the ASCII character set and characters from other Unicode blocks, such as Cyrillic, Arabic, Chinese, and many more.</p><h4>39. Can a variable of type string accept a nil value?</h4><p>Yes, a variable of type string can accept a nil value. In Go, the zero value of the string type is the empty string (&quot;&quot;), not nil. However, a string variable can be assigned a nil value explicitly by using a pointer to a string (*string) or an interface that contains a nil value.</p><p>Here is an example of assigning a nil value to a string variable using a pointer:</p><pre>var s *string<br>fmt.Println(s) // prints &quot;nil&quot;</pre><p>It’s worth noting that attempting to access a nil string variable will result in a runtime panic. Therefore, checking for nil values before using a string variable is important.</p><h4>40. Can multiple values be returned from a function?</h4><p>Yes, Go allows returning multiple values from a function. A function can return any number of values by specifying them in a comma-separated list in the function signature. Here’s an example:</p><pre>func swap(x, y int) (int, int) {<br>    return y, x<br>}<br><br>func main() {<br>    a, b := 10, 20<br>    a, b = swap(a, b)<br>    fmt.Println(a, b) // Output: 20 10<br>}</pre><p>In the above example, the swap function returns two integer values, which are swapped values of the input parameters. These two values are then assigned to variables a and b in the main function.</p><h4>41. What happens when strings are concatenated?</h4><p>When strings are concatenated in Go using the + operator or the fmt.Sprintf function, a new string is created with the concatenated values.</p><p>For example:</p><pre>str1 := &quot;hello&quot;<br>str2 := &quot;world&quot;<br>result := str1 + &quot; &quot; + str2<br>fmt.Println(result) // Output: &quot;hello world&quot;</pre><p>In the above example, a new string is created by concatenating the strings “hello” and “world” with a space between them. The resulting string is assigned to the result variable.</p><p>☝️: It’s important to note that a new string is created in memory each time strings are concatenated, which can be inefficient for large strings or many concatenations. In such cases, it is recommended to use a bytes.Buffer or strings.Builder to concatenate strings more efficiently.</p><h4>42. How to write to a file?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*50RMwbIuxD7hfB1w" /><figcaption>Photo by <a href="https://unsplash.com/@ilyapavlov?utm_source=medium&amp;utm_medium=referral">Ilya Pavlov</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In Go, you can write a file using the WriteFile function from the os package, or the File type from the os package.</p><p>Here’s an example using WriteFile:</p><pre>import (<br>    &quot;os&quot;<br>)<br><br>func main() {<br>    data := []byte(&quot;Hello, world!&quot;)<br>    err := os.WriteFile(&quot;output.txt&quot;, data, 0644)<br>    if err != nil {<br>        panic(err)<br>    }<br>}</pre><p>This will write the string “Hello, world!” to a file named “output.txt”. The 0644 argument specifies the file permissions.</p><p>Here’s an example using the File type:</p><pre>import (<br>    &quot;os&quot;<br>)<br><br>func main() {<br>    file, err := os.Create(&quot;output.txt&quot;)<br>    if err != nil {<br>        panic(err)<br>    }<br>    defer file.Close()<br>    data := []byte(&quot;Hello, world!&quot;)<br>    _, err = file.Write(data)<br>    if err != nil {<br>        panic(err)<br>    }<br>}</pre><p>This will also write the string “Hello, world!” to a file named “output.txt”. The os.Create function creates the file, and the defer file.Close() statement ensures that the file is closed when the function returns. The file.Write method writes the data to the file.</p><h4>43. What is structure?</h4><p>In Go, a struct is a user-defined composite data type that groups together zero or more variables of different types under a single name. It is similar to a class in object-oriented programming languages.</p><p>A struct definition consists of the keyword “type”, followed by the name of the struct, the keyword “struct”, and a list of fields enclosed in curly braces. Each field is a variable with a name and a type.</p><p>Here’s an example of a struct definition:</p><pre>type Person struct {<br>    Name string<br>    Age int<br>    Address string<br>}</pre><p>In this example, we define a struct named Person with three fields: Name, Age, and Address. The Name field is of type string, the Age field is of type int, and the Address field is of type string.</p><p>We can create a new instance of the Person struct by using the struct literal syntax:</p><pre>person := Person{Name: &quot;Alice&quot;, Age: 30, Address: &quot;123 Main St&quot;}</pre><p>We can access the fields of the struct using the dot notation:</p><pre>fmt.Println(person.Name)    // Output: Alice<br>fmt.Println(person.Age)     // Output: 30<br>fmt.Println(person.Address) // Output: 123 Main St</pre><h4>44. What happens if you call log.Fatal?</h4><p>If you call log.Fatal, it will write the message to the log using log.Print, then call os.Exit(1) to terminate the program with a non-zero exit code. This function often reports fatal errors, where the program cannot continue execution. The log message typically provides information about the error that caused the program to terminate.</p><h4>45. Explain the difference between concurrency and parallelism?</h4><p>Concurrency and parallelism are related but distinct concepts in computer science.</p><p>Concurrency refers to the ability of a program to manage multiple tasks simultaneously by interleaving the execution of different tasks. Concurrency does not necessarily imply that tasks are executed simultaneously but rather that they are executed in an overlapping manner, sharing resources such as CPU time, memory, and I/O operations. Concurrency is typically achieved through threads, coroutines, or other mechanisms that allow multiple tasks to run concurrently.</p><p>Parallelism, on the other hand, refers to the ability of a program to execute multiple tasks simultaneously by distributing them across multiple processing units, such as multiple cores, processors, or computers. Parallelism requires that tasks be executed independently and divided into smaller subtasks that can be executed in parallel. Parallelism is typically achieved through multiprocessing, multithreading, or distributed computing frameworks.</p><p>In short, concurrency refers to managing multiple tasks that are executed in an overlapping manner, while parallelism refers to executing multiple tasks simultaneously by distributing them across multiple processing units.</p><h4>46. How to announce a defer call?</h4><p>In Go, the defer statement is used to schedule a function call to be executed after the current function completes but before the function returns. The defer statement is often used to simplify functions that perform various clean-up actions, such as closing files or unlocking mutexes.</p><p>To announce a defer call, simply use the defer keyword followed by the function call that you want to defer. For example, suppose we have a function that opens a file and returns a pointer to it:</p><pre>func OpenFile(filename string) *File {<br>    f, err := os.Open(filename)<br>    if err != nil {<br>        log.Fatal(err)<br>    }<br>    defer f.Close()<br>    return f<br>}</pre><p>In this example, we use the defer statement to schedule a call to f.Close() after the OpenFile function completes, regardless of whether an error occurs or not. This ensures the file will always be closed properly, even if an error occurs during the opening process.</p><p>Note that deferred functions are executed in LIFO (last in, first out) order. This means that the last deferred function to be scheduled is the first to be executed, and the first to be scheduled is the last to be executed.</p><h4>47. What is a channel? What types of channels do you know? What are they needed for?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pqFeUbBodmeEAq5d" /><figcaption>Photo by <a href="https://unsplash.com/@quinoal?utm_source=medium&amp;utm_medium=referral">Quino Al</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In Go, a channel is a concurrency mechanism that allows communication and synchronization between goroutines. It provides a way for one goroutine to send a message to another and for the receiving goroutine to block until it receives the message. Channels can be considered a conduit connecting two goroutines, enabling them to share information and coordinate their activities.</p><p>There are two types of channels in Go: <strong>buffered </strong>and <strong>unbuffered</strong>. Unbuffered channels are synchronous, meaning that when a goroutine sends a value on the channel, it blocks it until another goroutine receives it. On the other hand, Buffered channels have a fixed capacity and can store multiple values. When a goroutine sends a value on a buffered channel, it blocks only if the channel is full, and when a goroutine receives a value from a buffered channel, it blocks only if the channel is empty.</p><p>Channels are needed to facilitate communication and synchronization between goroutines, which is essential for concurrent programming. They provide a safe and efficient way for goroutines to communicate and share data without the need for explicit locking or other synchronization mechanisms. This simplifies the development of concurrent programs, making them easier to reason about and less error-prone.</p><h4>48. What is the difference between buffered and unbuffered channels?</h4><p>In Go, a channel is a data structure that allows communication between goroutines. There are two types of channels: buffered and unbuffered.</p><p>An unbuffered channel is a channel with a capacity of 0. This means that it can only hold one value at a time. When a value is sent on an unbuffered channel, the sending goroutine blocks until another goroutine receives the value. Similarly, when a value is received from an unbuffered channel, the receiving goroutine blocks until another goroutine sends a value.</p><p>On the other hand, a buffered channel has a capacity greater than 0. This means it can hold multiple values simultaneously, up to its capacity. When a value is sent on a buffered channel, the sending goroutine will not block as long as the buffer is not full. If the buffer is full, the sending goroutine will block until there is space. Similarly, when a value is received from a buffered channel, the receiving goroutine will not block as long as values are in the buffer. If the buffer is empty, the receiving goroutine will block until the buffer has a value.</p><p>In summary, the main difference between buffered and unbuffered channels is that a buffered channel can hold multiple values at a time and allows non-blocking sends and receives as long as the buffer is not full or empty, while an unbuffered channel can only hold one value at a time and requires synchronous communication between goroutines.</p><h4>49. What will happen if you read from a closed channel? What will happen if you write to a closed channel?</h4><p>If you try to read from a closed channel, the operation will not block, and the returned value will be the zero value for the channel’s element type. This is because a closed channel can still be read from until it is empty.</p><p>A panic will occur if you try to write to a closed channel. This is because a closed channel cannot be written to, and attempting to do so indicates a bug in the code.</p><h4>50. How to check that a variable of type map has a stored value for a specific key?</h4><p>You can use the comma-ok idiom to check if a map has a stored value for a specific key. Here is an example:</p><pre>myMap := map[string]int{&quot;foo&quot;: 1, &quot;bar&quot;: 2}<br><br>if val, ok := myMap[&quot;foo&quot;]; ok {<br>    // The value is present in the map<br>    fmt.Println(&quot;Value for key &#39;foo&#39;:&quot;, val)<br>} else {<br>    // The value is not present in the map<br>    fmt.Println(&quot;Key &#39;foo&#39; not found&quot;)<br>}</pre><p>In this example, we check if the map myMap has a value for the key &quot;foo&quot;. If the value is present, we print it. Otherwise, we print a message indicating that the key is not found. The ok variable is a boolean that indicates whether the key is present in the map.</p><h4>51. What are standard env variables in Go?</h4><p>Several standard environment variables in Go can be used to modify the behaviour of certain packages or to configure certain aspects of the Go runtime:</p><ol><li>GOOS - specifies the target operating system for cross-compiling Go programs</li><li>GOARCH - specifies the target architecture for cross-compiling Go programs</li><li>GOROOT - specifies the location of the Go installation directory</li><li>GOPATH - specifies the location of the workspace directory</li><li>GOBIN - specifies the location where executable files should be installed</li><li>GOMOD - specifies the location of the current module&#39;s go.mod file</li><li>GO111MODULE - enables or disables Go modules support</li></ol><p>These environment variables can be set using the export command in Unix-based systems or the set command in Windows. For example, to set the GOPATH variable to /home/user/go, you would run export GOPATH=/home/user/go in a Unix-based system, or set GOPATH=C:\Users\user\go in a Windows system.</p><h4>52. What is the difference between value and pointer receiver?</h4><p>In Go, methods can have either value or pointer receivers. A value receiver means the method operates on a copy of the value, while a pointer receiver operates on the original value itself.</p><p>When a method has a value receiver, it operates on a copy of the value, which means that any changes made to the value inside the method will not affect the original value. On the other hand, when a method has a pointer receiver, it operates on the original value itself, which means that any changes made to the value inside the method will affect the original value.</p><p>For example, consider the following code:</p><pre>type Person struct {<br>    Name string<br>}<br><br>func (p Person) SetName(name string) {<br>    p.Name = name<br>}<br><br>func (p *Person) SetNamePtr(name string) {<br>    p.Name = name<br>}<br><br>func main() {<br>    person1 := Person{&quot;Alice&quot;}<br>    person1.SetName(&quot;Bob&quot;)<br>    fmt.Println(person1.Name) // Output: Alice<br>    person2 := &amp;Person{&quot;Charlie&quot;}<br>    person2.SetNamePtr(&quot;David&quot;)<br>    fmt.Println(person2.Name) // Output: David<br>}</pre><p>In this code, the Person struct has two methods, SetName and SetNamePtr, one with a value receiver and the other with a pointer receiver. When SetName is called on person1, a copy of person1 is made, and the Name field of the copy is set to &quot;Bob&quot;. However, this does not affect the original person1, which still has the Name field set to &quot;Alice&quot;. When SetNamePtr is called on person2, the method operates on the original person2, setting its Name field to &quot;David&quot;. As a result, person2.Name is &quot;David&quot;.</p><h4>53. How to make a type assertion?</h4><p>A type assertion is a way to check an interface value&#39;s dynamic type at runtime and access the value of the underlying concrete type. In Go, a type assertion has the following syntax:</p><pre>x.(T)</pre><p>where x is an expression of interface type and T is a type. The expression x.(T) asserts that the value of x is of type T, and returns two values: the value of x converted to type T, and a boolean value that indicates whether the assertion succeeded or not.</p><p>Here’s an example:</p><pre>var x interface{} = &quot;hello&quot;<br>s, ok := x.(string)<br>if ok {<br>    fmt.Println(&quot;x is a string:&quot;, s)<br>} else {<br>    fmt.Println(&quot;x is not a string&quot;)<br>}</pre><p>In this example, the variable x is an interface value with a string value. We use a type assertion x.(string) to assert that the value of x is a string, and to assign the value to the variable s. The boolean variable ok is set to true if the assertion succeeds, and to false otherwise. If the assertion succeeds, we print the string value. If not, we print a message saying that x is not a string.</p><p>Note that if the assertion fails, a runtime panic will occur. To avoid this, you can use a type switch, which is a safer way to test the type of interface value.</p><h4>54. How to write a benchmark?</h4><p>In Go, you can write a benchmark function using the testing package. A benchmark function should start with the name “Benchmark” followed by a descriptive name of the benchmarked function. The function should take a pointer to a testing.B struct as its only parameter. The benchmark function should use the testing.B.N argument to determine the data size used in the benchmark. The benchmark function should be repeated b.N times using a loop.</p><p>Here’s an example of a simple benchmark function that measures the performance of the built-in append function:</p><pre>func BenchmarkAppend(b *testing.B) {<br>    data := make([]int, b.N)<br>    b.ResetTimer()<br><br>    for i := 0; i &lt; b.N; i++ {<br>        data = append(data, i)<br>    }<br>}</pre><p>The BenchmarkAppend function takes a pointer to a testing.B struct creates a slice of integers of length b.N, and then repeatedly appends a value to the slice in a loop that runs b.N times.</p><p>The b.ResetTimer() call resets the benchmark timer so that the time spent on any setup code outside the loop is not included in the benchmark results.</p><p>To run this benchmark, you can use the go test command with the -bench flag followed by the name of the benchmark function, like this:</p><pre>go test -bench=BenchmarkAppend</pre><p>The benchmark output will show the number of iterations, the total time taken, and the average time per iteration.</p><h4>55. In what order are cases executed in select?</h4><p>The order in which cases are executed is not defined. If multiple cases are ready to proceed, then one of them will be chosen at random. If none of the cases is ready, and there is a default case, then that case will be executed. If there is no default case, the select statement will block until at least one of the cases becomes ready.</p><h3>Practical tasks</h3><h4>56. Implement the binary search algorithm for an element in a slice</h4><pre>package main<br><br>import (<br> &quot;fmt&quot;<br>)<br><br>func main() {<br> arr := []int{1, 2, 3, 4, 5, 6, 7, 8}<br> target := 5<br><br> index := binarySearch(arr, target)<br><br> if index != -1 {<br>  fmt.Println(&quot;index of&quot;, target, &quot;is&quot;, index)<br> } else {<br>  fmt.Println(target, &quot;not found&quot;)<br> }<br>}<br><br>func binarySearch(arr []int, target int) int {<br> low := 0<br> high := len(arr) - 1<br><br> for low &lt;= high {<br>  middle := (low + high) / 2<br><br>  if arr[middle] == target {<br>   return middle<br>  } else if arr[middle] &lt; target {<br>   low = middle + 1<br>  } else {<br>   high = middle - 1<br>  }<br><br> }<br> return -1<br>}</pre><h4>57. There is a code. What is displayed on the screen? What must you do to see the proposed conclusions of Foo1 and Foo2?</h4><p>👨‍💻: The code can be <a href="https://go.dev/play/p/a-JIesxdCQ7">found here</a></p><p>🔑: The solution can be <a href="https://go.dev/play/p/-Hu--FqtTGG">found here</a></p><h4>58. There is a code. What will be displayed on the screen? How to display letters on the screen?</h4><p>👨‍💻: The code can be <a href="https://go.dev/play/p/6CiZvQp7r3t">found here</a></p><p>🔑: The solution can be <a href="https://go.dev/play/p/0n7vBmVsqPd">found here</a></p><h4>59. Swap the values of two variables without a temporary helper variable.</h4><pre>a, b := 1, 2<br>a, b = b, a<br>fmt.Println(a, b)</pre><h4>60. Reverse the slice</h4><pre>func reverseSlice(arr []int) []int {<br> for i := 0; i &lt; len(arr)/2; i++ {<br>  arr[i], arr[len(arr)-1] = arr[len(arr)-1], arr[i]<br> }<br> return arr<br>}</pre><h4>61. Move all zero values to the end of the array</h4><pre>func moveZeroValuesToTheEnd(arr []int) []int {<br> zeroIndex := 0<br> for i := 0; i &lt; len(arr); i++ {<br>  if arr[i] != 0 {<br>   arr[zeroIndex], arr[i] = arr[i], arr[zeroIndex]<br>   zeroIndex++<br>  }<br> }<br> return arr<br>}</pre><p>In conclusion, preparing for a junior Go developer interview can be daunting, but it doesn’t have to be. With the right knowledge and preparation, you can confidently navigate any interview and showcase your skills as a junior Go developer.</p><p>We hope this article has provided valuable insights and resources to help you prepare for your next interview. You can demonstrate your expertise and potential as a junior developer by mastering Go development&#39;s fundamental concepts and best practices.</p><p>Remember, interviews allow employers to evaluate your skills and learn more about the company and the role you are applying for. Take the time to research the company and ask thoughtful questions during the interview to show your interest and enthusiasm.</p><p>We wish you the best of luck in your interview and future endeavours as a junior Go developer!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=334b09f13280" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Evolution of System Design: A Journey from Single Server to a Scalable Infrastructure]]></title>
            <link>https://d9nich.medium.com/the-evolution-of-system-design-a-journey-from-single-server-to-a-scalable-infrastructure-4936428f594c?source=rss-f50118a3e717------2</link>
            <guid isPermaLink="false">https://medium.com/p/4936428f594c</guid>
            <category><![CDATA[infrastructure]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[load-balancing]]></category>
            <category><![CDATA[evolution]]></category>
            <category><![CDATA[scalability]]></category>
            <dc:creator><![CDATA[Danylo Halaiko]]></dc:creator>
            <pubDate>Mon, 08 May 2023 07:01:56 GMT</pubDate>
            <atom:updated>2023-05-08T07:01:56.415Z</atom:updated>
            <content:encoded><![CDATA[<p>System design is an essential part of building any scalable application, and it is the process of designing a large and complex software system. A scalable system can handle increasing traffic and data without performance degradation or service downtime. The application must scale up as the user base grows to accommodate the traffic. This article will discuss the steps to scale an application from zero to a million users, including adding servers, separating the database, adding a load balancer, and replication. Following these steps, you can design a scalable system that can handle millions of users without issues.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sKOvyHo5oKe4GA4v" /><figcaption>Photo by <a href="https://unsplash.com/@kellysikkema?utm_source=medium&amp;utm_medium=referral">Kelly Sikkema</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>1. Single server setup</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/0*0jU3TgCX3yZO7tJm" /><figcaption><strong>Single </strong>server</figcaption></figure><ol><li>Users access websites through <strong>domain names</strong>, such as <em>site.com</em></li><li>Internet Protocol (<strong>IP</strong>) address is returned to the browser or mobile app</li><li>Once the IP address is obtained, Hypertext Transfer Protocol (<strong>HTTP</strong>) <strong>requests </strong>are sent directly to a web server</li><li>The web server returns <strong>HTML </strong>pages or <strong>JSON </strong>responses for rendering</li></ol><h4>Common trafic sources</h4><p>Typical there are <strong>two </strong>types of traffic sources:</p><ul><li><strong>Web application</strong>: it uses a combination of server-side languages (Go, Python, JavaScript, etc.) to handle business logic, storage, etc., and client-side languages (HTML and JavaScript) for presentation.</li><li><strong>Mobile application</strong>: HTTP is the communication protocol between the mobile app and the web server. JavaScript Object Notation (<strong>JSON</strong>) is a commonly used API response format to transfer data due to its simplicity.</li></ul><p>📗: More about different storage formats you can read in <a href="https://medium.com/@d9nich/json-xml-protobuf-thrift-avro-or-everything-you-need-to-know-about-encoding-data-6077a7e769e2">my previous article</a></p><h3>2. Seperating Database</h3><p>With the growth of the user base, one server is not enough, and we need multiple servers: one for web/mobile traffic and the other for the <strong>database</strong>. Separating web/mobile traffic (web tier) and database (data tier) servers allows them to be scaled independently.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/0*JbkaSVuUGo1jjGAb" /><figcaption>Adding database</figcaption></figure><h4>Which database to choose?</h4><p>You can choose between a traditional relational database and a non-relational database.</p><p><strong>Relational databases</strong> are also called relational database management systems (RDBMS) or SQL databases. The most popular ones are MySQL, Oracle database, PostgreSQL, etc. Relational databases represent and store data in tables and rows. You can perform join operations using SQL across different database tables.</p><p>📗: You can read about relational database core in <a href="https://medium.com/@d9nich/understanding-b-trees-the-most-common-indexing-structure-in-databases-5d6a51d35312">my previous article</a>.</p><p><strong>Non-Relational databases</strong> are also called NoSQL databases. Popular ones are CouchDB, Neo4j, Cassandra, HBase, Amazon DynamoDB, etc. These databases are grouped into four categories: <em>key-value stores, graph stores, column stores, and document stores</em>. Join operations are generally not supported in non-relational databases.</p><p>📗: <a href="https://medium.com/@d9nich/sstable-database-or-big-ideas-behind-the-scene-1d449a12fb44">In my previous article</a>, you can read about SSTable (the core of Cassandra).</p><p>For most developers, <strong>relational databases are the best option</strong> because they have been around for over 40 years, and historically, they have worked well. However, if relational databases are unsuitable for your specific use cases, it is critical to explore beyond relational databases.</p><p>Non-relational databases might be the right choice if:</p><ul><li>Your application requires super-low latency</li><li>Your data are unstructured, or you do not have any relational data</li><li>You only need to serialize and deserialize data (JSON, XML, YAML, etc.)</li><li>You need to store a massive amount of data</li></ul><h3>3. Adding Load Balancer</h3><h4>Vertical vs horizontal scaling</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/0*hgGDWRp6sQGieU9w.jpg" /><figcaption>Difference between Horizontal and Vertical Scaling</figcaption></figure><p><strong>Vertical scaling</strong>, called “scale up”, means adding more power (CPU, RAM, etc.) to your servers. Horizontal scaling, called “scale-out”, allows you to scale by adding more servers to your pool of resources.</p><p>When traffic is low, vertical scaling is a great option, and the simplicity of vertical scaling is its main advantage. Unfortunately, it comes with serious limitations:</p><ul><li>Vertical scaling has a hard limit. It is impossible to add unlimited CPU and memory to a single server.</li><li>Vertical scaling does not have failover and redundancy. If one server goes down, the website/app goes down with it completely.</li></ul><p><strong>Horizontal scaling</strong> is more desirable for large-scale applications due to the limitations of vertical scaling.</p><p>In the previous design, users are connected to the web server directly. Users cannot access the website if the web server is offline. In another scenario, if many users access the web server simultaneously and it reaches its load limit, users generally experience slower responses or fail to connect to the server. A <strong>load balancer</strong> is the best technique to address these problems.</p><h4>Adding Load balancer</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/0*cfy2Y1z0Ctq6zm_a" /><figcaption>Load Balancer concept</figcaption></figure><p>Users connect to the public IP of the load balancer directly. With this setup, web servers are unreachable directly by clients. For better security, private IPs are used for communication between servers. A private IP is an IP address reachable only between servers in the same network; however, it is unreachable over the Internet. The load balancer communicates with web servers through private IPs.</p><p>After adding a load balancer and a second web server, we successfully solved the no-failover issue and improved the availability of the web tier. Details are explained below:</p><ul><li>If <em>“Server 1”</em> goes offline, all the traffic will be routed to <em>“Server 2”</em>. This prevents the website from going offline. We will also add a new healthy web server to the pool to balance the load.</li><li>If the website traffic overgrows, and two servers are not enough to handle the traffic, the load balancer can handle this problem gracefully. You only need to <strong>add more servers</strong> to the web server pool, and the load balancer automatically starts to send requests to them.</li></ul><p>Now the web tier looks good; what about the data tier? The current design has one database, so it does not support failover and redundancy. Database replication is a common technique to address those problems. Let us take a look.</p><h3>4. Database replication</h3><p>The typical approach is master-slave. A master database generally only supports write operations. A slave database gets copies of the data from the master database and only supports read operations. All the data-modifying commands like insert, delete, or update must be sent to the master database. Most applications require a much higher ratio of reads to writes; thus, the number of slave databases in a system is usually larger than the number of master databases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/0*HrvkOGDONTjEVU0T" /><figcaption>master database with two slave databases</figcaption></figure><p>Advantages of database replication:</p><ul><li>💯<strong>Better performance</strong>: In the master-slave model, all writes and updates happen in master nodes, whereas read operations are distributed across slave nodes. This model improves performance by allowing more queries to be processed in parallel.</li><li>🧐<strong>Reliability</strong>: Data is still preserved if one of your database servers is destroyed by a natural disaster, such as a typhoon or an earthquake. You do not need to worry about data loss because data is replicated across multiple locations.</li><li>🆒<strong>High availability</strong>: By replicating data across different locations, your website remains in operation even if a database is offline, as you can access data stored in another database server.</li></ul><p>In the previous section, we discussed how a load balancer helped to improve system availability. We ask the same question: what if one of the databases goes offline? The architectural design discussed in the figure above can handle this case:</p><ul><li>If only one slave database is available and it goes offline, read operations will be directed to the master database temporarily. When the issue is found, a new slave database will replace the old one. If multiple slave databases are available, read operations are redirected to other healthy slave databases. A new database server will replace the old one.</li><li>If the master database goes offline, a slave database will be promoted to be the new master. All the database operations will be temporarily executed on the new master database. A new slave database will replace the old one for data replication immediately. In production systems, promoting a new master is more complicated as the data in a slave database might not be up to date. The missing data needs to be updated by running data recovery scripts.</li></ul><h3>5. Cache</h3><p>A cache is a temporary storage area that stores the result of expensive responses or frequently accessed data in memory so that subsequent requests are served more quickly. As illustrated, every time a new web page loads, one or more database calls are executed to fetch data. The application performance is greatly affected by calling the database repeatedly. The cache can mitigate this problem.</p><h4>Cache tier</h4><p>The cache tier is a temporary data store layer, much faster than the database. The benefits of having a separate cache tier include better system performance, reduced database workloads, and scaling the cache tier independently.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/609/0*cbYuY6fBYKmmtvqn" /><figcaption>setup of a cache server</figcaption></figure><p>After receiving a request, a web server checks if the cache has the available response. If it has, it sends data back to the client. If not, it queries the database, stores the response in the cache, and sends it back to the client. This caching strategy is called a read-through cache. Other caching strategies are available depending on the data type, size, and access patterns.</p><h4>Consideration for using cache</h4><p>Here are a few considerations for using a cache system:</p><ul><li>🤔<strong>Decide when to use the cache</strong>: Consider using a cache when data is read frequently but modified infrequently. Since cached data is stored in volatile memory, a cache server is not ideal for persisting data. For instance, if a cache server restarts, all the data in memory is lost. Thus, important data should be saved in persistent data stores.</li><li>⏰<strong>Expiration policy</strong>: Implementing an expiration policy is a good practice. Once cached data is expired, it is removed from the cache. When there is no expiration policy, cached data will be stored in the memory permanently. It is advisable not to make the expiration date too short, as this will cause the system to reload data from the database too frequently. Meanwhile, it is advisable not to make the expiration date too long as the data can become stale.</li><li>🤝<strong>Consistency</strong>: This involves storing the data and cache in sync. Inconsistency can happen because data-modifying operations on the data store and cache are not in a single transaction. Maintaining consistency between the data store and cache is challenging when scaling across multiple regions.</li><li>🔧<strong>Mitigating failures</strong>: A single cache server represents a potential single point of failure (SPOF).</li><li>🚪<strong>Eviction Policy</strong>: Once the cache is full, any requests to add items to the cache might cause existing items to be removed. This is called cache eviction. Least-recently-used (LRU) is the most popular cache eviction policy. Other eviction policies, such as the Least Frequently Used (LFU) or First in First Out (FIFO), can be adapted to satisfy different use cases.</li></ul><h3>6. CDN</h3><p>A Content Delivery Network (CDN) is a distributed network of servers strategically located worldwide to deliver content, such as images, videos, HTML pages, and other web assets, to end-users with high availability and performance.</p><p>When a user requests content from a website that uses a CDN, the request is automatically routed to the closest CDN server based on the user’s geographical location, which reduces latency and improves load times. The CDN server then serves the requested content to the user directly without returning to the origin server where the content is hosted.</p><p>CDNs work by caching the content on their servers, which allows them to serve content more quickly and efficiently. They also employ load balancing, traffic routing, and dynamic content optimization techniques to ensure users receive the best possible experience.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/893/0*npSsCdPfNrpVZfei" /><figcaption>CDN improves load time</figcaption></figure><ol><li>User A tries to get index.html by using a page URL</li><li>If the CDN server does not have image.png in the cache, the CDN server requests the file from the origin, which can be a web server or online storage like Amazon S3.</li><li>The origin returns image.png to the CDN server, which includes the optional HTTP header Time-to-Live (TTL), which describes how long the page is cached.</li><li>The CDN caches the page and returns it to User A. The page remains cached in the CDN until the TTL expires.</li><li>User B sends a request to get the same page.</li><li>The page is returned from the cache if the TTL has not expired.</li></ol><h4>Considerations</h4><ul><li>💰<strong>Cost</strong>: CDNs are run by third-party providers, and you are charged for data transfers in and out of the CDN. Caching infrequently used assets provides no significant benefits, so you should consider moving them out of the CDN.</li><li>⏳<strong>Setting an appropriate cache expiry</strong>: Setting a cache expiry time is important for time-sensitive content. The cache expiry time should neither be too long nor too short. If it is too long, the content might no longer be fresh. If it is too short, it can cause repeat reloading of content from the origin servers to the CDN.</li><li>🗑️<strong>Invalidating files</strong>: You can remove a file from the CDN before it expires</li></ul><p>📗: Read <a href="https://medium.com/%40d9nich/%EF%B8%8F-how-to-cache-aws-lambda-responses-with-amazon-cloudfront-and-lambda-edge-3d937679ff14">How to Cache AWS Lambda Responses with Amazon CloudFront and Lambda@Edge</a></p><h3>7. Message queue</h3><p>A message queue is a durable component stored in memory that supports asynchronous communication. It serves as a buffer and distributes asynchronous requests. The basic architecture of a message queue is simple. Input services, called producers/publishers, create and publish messages to a message queue. Other services or servers, called consumers/subscribers, connect to the queue and perform actions defined by the messages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/946/0*sdmP3HfezJBfXXPL" /><figcaption>Consumer/Producer workflow</figcaption></figure><p>Decoupling makes the message queue a preferred architecture for building a scalable and reliable application. With the message queue, the producer can post a message to the queue when the consumer cannot process it. The consumer can read messages from the queue even when the producer is unavailable.</p><p>📗: Read about <a href="https://medium.com/%40d9nich/%EF%B8%8Fevent-driven-development-and-everything-you-should-know-about-it-3dc5c7e94cb6">Event Driven Development in my previous article</a></p><p>Consider the following use case: your application supports photo customization, including cropping, sharpening, blurring, etc. Those customization tasks take time to complete. Web servers publish photo processing jobs to the message queue. Photo processing workers pick up jobs from the message queue and perform photo customization tasks asynchronously. The producer and the consumer can be scaled independently. When the queue becomes large, more workers are added to reduce the processing time. However, the number of workers can be reduced if the queue is empty.</p><h3>8. Logging, metrics, automation</h3><p>When working with a small website that runs on a few servers, logging, metrics, and automation support are good practices but are unnecessary. However, now that your site has grown to serve a large business investing in those tools is essential.</p><p>📝Logging: Monitoring error logs is important because it helps to identify errors and problems in the system. You can monitor error logs per server level or use tools to aggregate them to a centralized service for easy search and viewing.</p><p>📊Metrics: Collecting different metrics helps us gain business insights and understand the system&#39;s health status. Some of the following metrics are useful:</p><ul><li>Host level metrics: CPU, Memory, disk I/O, etc.</li><li>Aggregated level metrics: for example, the performance of the entire database tier, cache tier, etc.</li><li>Key business metrics: daily active users, retention, revenue, etc.</li></ul><p>🤖Automation: When a system gets big and complex, we need to build or leverage automation tools to improve productivity. Continuous integration is a good practice in which each code check-in is verified through automation, allowing teams to detect problems early. Besides, automating your build, test, deploy process, etc., could improve developer productivity significantly.</p><p>📗: Read about <a href="https://medium.com/@d9nich/advanced-git-github-actions-dependabot-a822b2463077">GitHub Actions and Dependabot automation in my previous article</a>.</p><p>In conclusion, scaling a system from 0 to millions of users requires careful planning and design. Starting with a single server setup, separating the database, adding a load balancer, and implementing database replication are crucial steps in achieving high availability, fault tolerance, and scalability. Developers must also consider the pros and cons of different database types, whether relational or non-relational and decide which one best fits their specific use cases. Vertical and horizontal scaling are important concepts to understand and choose appropriately based on traffic load and limitations. By following these steps and understanding the underlying concepts, developers can ensure that their system can handle rapid growth and increasing traffic, ultimately leading to a successful and scalable product.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4936428f594c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[️Building Robust, Scalable, and Event-Driven Orchestration Applications with AWS Step Functions…]]></title>
            <link>https://d9nich.medium.com/%EF%B8%8Fbuilding-robust-scalable-and-event-driven-orchestration-applications-with-aws-step-functions-c7104b8abe2b?source=rss-f50118a3e717------2</link>
            <guid isPermaLink="false">https://medium.com/p/c7104b8abe2b</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[event-driven-architecture]]></category>
            <category><![CDATA[step-functions]]></category>
            <category><![CDATA[orchestration]]></category>
            <category><![CDATA[scalability]]></category>
            <dc:creator><![CDATA[Danylo Halaiko]]></dc:creator>
            <pubDate>Mon, 01 May 2023 07:01:48 GMT</pubDate>
            <atom:updated>2023-05-01T07:01:48.933Z</atom:updated>
            <content:encoded><![CDATA[<h3>🏗️Building Robust, Scalable, and Event-Driven Orchestration Applications with AWS Step Functions and EventBridge</h3><p>In today’s world, building complex, distributed applications that require the coordination of multiple services is becoming more common. AWS Step Functions is a fully managed service that allows developers to coordinate and visualize distributed applications and microservices using state machines. AWS EventBridge, on the other hand, is a serverless event bus service that enables building event-driven architectures. In this article, we will explore how to leverage these two AWS services to build a robust, scalable, and event-driven orchestration application. We will examine how to build our application, the different steps involved, the configurations required, and the benefits of using AWS Step Functions and AWS EventBridge. By the end of this article, you will have a better understanding of how to build and manage complex, distributed applications using these two powerful AWS services.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7CoJJ1ghWbXFikoTU0Hdvg.jpeg" /><figcaption>AWS Step Function is managing resources</figcaption></figure><h4>AWS Step Functions</h4><p>AWS Step Functions is a fully managed service that allows you to coordinate and visualize distributed applications and microservices using state machines. With Step Functions, you can easily create workflows that represent the steps of your application, and visualize them as a state machine diagram.</p><p>Step Functions provides a simple way to build and scale workflows by separating the logic of your application into smaller, easier-to-manage components. You can use Step Functions to build and run complex, multi-step workflows that are reliable, scalable, and easy to debug.</p><p>Step Functions supports a wide range of AWS services and third-party APIs, allowing you to easily integrate with other AWS services like AWS Lambda, Amazon S3, Amazon DynamoDB, and Amazon SQS. With Step Functions, you can define your workflows using AWS CloudFormation templates or the AWS Management Console.</p><p>Step Functions provides a number of useful features to help you build and manage your workflows, including:</p><ul><li>🔄Automatic retries: Step Functions automatically retry failed steps in your workflow, so you don’t have to worry about handling errors manually.</li><li>🗺️State machine visualization: You can visualize your workflows as state machine diagrams, making it easy to understand the structure of your application and troubleshoot issues.</li><li>🚀Parallel processing: Step Functions allow you to run steps in parallel, improving the performance of your workflows.</li><li>💾Step-level input and output data: You can pass input and output data between steps in your workflow, allowing you to build more complex applications.</li></ul><p>Overall, AWS Step Functions is a powerful tool for building and managing complex, distributed applications. With its simple, visual interface and support for a wide range of AWS services, Step Functions can help you build scalable, reliable workflows that are easy to maintain and debug.</p><h4>What is the difference between the Express and Standard Step Functions?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sv0s1e2L6wpFIBfB-6TkZg.png" /><figcaption>Standard VS Express Workflows</figcaption></figure><p>The primary difference between Standard and Express Step Functions in AWS is the level of scalability and performance that they offer.</p><p>Standard Step Functions are designed to handle high-scale workflows that require long-term state management, coordination between multiple services, and support for complex state machine structures. Standard Step Functions are ideal for building enterprise-grade applications that require high availability, durability, and scalability.</p><p>Express Step Functions, on the other hand, are designed for applications that require extremely low latency and high throughput. Express Step Functions use an optimized execution engine that enables sub-millisecond state transitions, making them ideal for building real-time applications that require immediate responses to events and actions.</p><p>In terms of limitations, Standard Step Functions have a maximum execution time of one year and a maximum state size of 262,144 bytes, while Express Step Functions have a maximum execution time of five minutes and a maximum state size of 64 KB. Standard Step Functions also have a higher pricing tier than Express Step Functions.</p><p>When choosing between Standard and Express Step Functions, it’s important to consider your application’s specific requirements for scalability, performance, and state management. If your application requires long-term state management, coordination between multiple services, and support for complex state machine structures, Standard Step Functions may be the best choice. If your application requires extremely low latency and high throughput, and you’re willing to trade off some of the advanced state management capabilities of Standard Step Functions, Express Step Functions may be the better choice.</p><p>🔗: More read in <a href="https://docs.aws.amazon.com/step-functions/latest/dg/concepts-standard-vs-express.html">Standard vs. Express Workflows — AWS Step Functions (amazon.com)</a></p><h4>AWS Event Bridge</h4><p>AWS EventBridge is a serverless event bus service that makes it easy to build event-driven applications at scale. With EventBridge, you can connect various AWS services, SaaS applications, and custom applications and receive events in near real-time as they happen. You can also route events to multiple targets like AWS Lambda, Amazon SNS, and Amazon SQS, enabling you to build event-driven architectures that are decoupled and scalable.</p><p>EventBridge uses a publish/subscribe model to send and receive events. Events are published to an event bus and are routed to targets based on rules that you define. You can use these rules to filter events based on event pattern matching, specific data values, or a combination of both.</p><p>EventBridge integrates with over 100 AWS services, including Amazon EC2, Amazon S3, AWS Lambda, Amazon ECS, Amazon EKS, and many more. You can also integrate with SaaS applications like Zendesk, Datadog, and PagerDuty, and custom applications using custom integrations.</p><p>One of the key benefits of EventBridge is its ability to simplify event-driven architectures. You can use EventBridge to build event-driven workflows that integrate multiple services without the need for complex code or manual configurations. This allows you to build scalable, reliable applications that can handle large volumes of events and respond to them in near real time.</p><p>Overall, AWS EventBridge is a powerful tool for building event-driven architectures that can help you build scalable, reliable, and flexible applications. With its easy-to-use interface and support for a wide range of AWS services and SaaS applications, EventBridge can simplify the development and management of event-driven architectures, enabling you to focus on building great applications.</p><h4>Why event-driven architectures are useful?</h4><p>Event-driven architectures are useful because they enable decoupling and asynchronous communication between different components or services of a system. This allows for more scalability, flexibility, and resilience in handling different types of events, as well as easier integration of new services or changes to existing ones. Event-driven architectures also provide a clear separation of concerns between producers and consumers of events and can be implemented using a variety of tools and technologies, including message brokers, pub/sub systems, and serverless computing platforms.</p><p>📗: More about Event-Driven Development read in <a href="https://medium.com/%40d9nich/%EF%B8%8Fevent-driven-development-and-everything-you-should-know-about-it-3dc5c7e94cb6">my previous article.</a></p><h3>Building our orchestration application</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*nDpjxNwoGVbVUznonE4XyQ.png" /><figcaption>Our final orchestration application</figcaption></figure><p>The scenario of the application will be the following:</p><ol><li>We upload files to S3</li><li>EventBridge triggers step function</li><li>Step function run ECS task and wait for callback result(either successful or not)</li><li>If everything is ok, we publish the result to an external database.</li></ol><p>📗: About integration ECS with Dockerhub read in <a href="https://medium.com/@d9nich/understanding-amazon-elastic-container-service-ecs-and-its-integration-with-docker-hub-9813ac278978">my previous article</a></p><p>⚠️: We will skip the part about creating ECS and Lambda and highlight only the main difference in integrating them into Step Functions.</p><h4>Create a step function</h4><ol><li>Go to “StepFunctions”</li><li>Click “Create State Machine”</li><li>Select “Design workflow visually”</li><li>Select “Standard type”</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XMOrjEcZkvBFyTmybUgEMQ.png" /><figcaption>Choosing authoring method</figcaption></figure><p>In design workflow, you should provide the config of your workflow. Graphic modelling is excellent for development but poor for sharing, so I’ll provide JSON config.</p><pre>{<br>  &quot;Comment&quot;: &quot;A description of my state machine&quot;,<br>  &quot;StartAt&quot;: &quot;Check Work&quot;,<br>  &quot;States&quot;: {<br>    &quot;Check Work&quot;: {<br>      &quot;Type&quot;: &quot;Task&quot;,<br>      &quot;Resource&quot;: &quot;arn:aws:states:::ecs:runTask.waitForTaskToken&quot;,<br>      &quot;Parameters&quot;: {<br>        &quot;LaunchType&quot;: &quot;FARGATE&quot;,<br>        &quot;Cluster&quot;: &quot;YOUR_CLUSTER_ARN&quot;,<br>        &quot;TaskDefinition&quot;: &quot;YOUR_TASK_DEFINITION_ARN&quot;,<br>        &quot;NetworkConfiguration&quot;: {<br>          &quot;AwsvpcConfiguration&quot;: {<br>            &quot;AssignPublicIp&quot;: &quot;ENABLED&quot;,<br>            &quot;SecurityGroups&quot;: [<br>              &quot;sg-65ea7c00&quot; // you need to change it to your values<br>            ],<br>            &quot;Subnets&quot;: [<br>              &quot;subnet-a7eb46cd&quot;,<br>              &quot;subnet-6ec12e22&quot;,<br>              &quot;subnet-6cc8d011&quot;<br>            ]<br>          }<br>        },<br>        &quot;Overrides&quot;: {<br>          &quot;ContainerOverrides&quot;: [<br>            {<br>              &quot;Name&quot;: &quot;analyzer&quot;,<br>              &quot;Environment&quot;: [<br>                {<br>                  &quot;Name&quot;: &quot;TASK_TOKEN&quot;, // providing task token as env variable<br>                  &quot;Value.$&quot;: &quot;$$.Task.Token&quot;<br>                },<br>                {<br>                  &quot;Name&quot;: &quot;SOURCE_FILE_PATH&quot;, // we also need S3 object key<br>                  &quot;Value.$&quot;: &quot;$.detail.object.key&quot;<br>                }<br>              ]<br>            }<br>          ]<br>        }<br>      },<br>      &quot;Next&quot;: &quot;Publish Check Result&quot;<br>    },<br>    &quot;Publish Check Result&quot;: {<br>      &quot;Type&quot;: &quot;Task&quot;,<br>      &quot;Resource&quot;: &quot;arn:aws:states:::lambda:invoke&quot;,<br>      &quot;OutputPath&quot;: &quot;$.Payload&quot;,<br>      &quot;Parameters&quot;: {<br>        &quot;Payload.$&quot;: &quot;$&quot;,<br>        &quot;FunctionName&quot;: &quot;YOUR_LAMBDA_ARN&quot;<br>      },<br>      &quot;Retry&quot;: [<br>        {<br>          &quot;ErrorEquals&quot;: [<br>            &quot;Lambda.ServiceException&quot;,<br>            &quot;Lambda.AWSLambdaException&quot;,<br>            &quot;Lambda.SdkClientException&quot;,<br>            &quot;Lambda.TooManyRequestsException&quot;<br>          ],<br>          &quot;IntervalSeconds&quot;: 2,<br>          &quot;MaxAttempts&quot;: 6,<br>          &quot;BackoffRate&quot;: 2<br>        }<br>      ],<br>      &quot;End&quot;: true<br>    }<br>  }<br>}</pre><p>Specify the state machine name and click “Create State Machine”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zIm2OTxhjMj_IBt2984T5w.png" /><figcaption>Specify state machine settings</figcaption></figure><h4>Adding policies to StepFunction to trigger Lambda and ECS</h4><p>StepFunction does not add policies for Lambda and ECS automatically.</p><ul><li>To execute lambda you need to add “InvokeFunction”</li></ul><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>        {<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Action&quot;: [<br>                &quot;lambda:InvokeFunction&quot;<br>            ],<br>            &quot;Resource&quot;: [<br>                &quot;YOUR_LAMBDA_ARN&quot;<br>            ]<br>        }<br>    ]<br>}</pre><ul><li>To run ECS you need to add “RunTask” and “PassRole”</li></ul><pre>{<br>    &quot;Version&quot;: &quot;2012-10-17&quot;,<br>    &quot;Statement&quot;: [<br>        {<br>            &quot;Sid&quot;: &quot;VisualEditor0&quot;,<br>            &quot;Effect&quot;: &quot;Allow&quot;,<br>            &quot;Action&quot;: [<br>                &quot;iam:PassRole&quot;,<br>                &quot;ecs:RunTask&quot;<br>            ],<br>            &quot;Resource&quot;: [<br>                &quot;ECS_TASK_ARN&quot;,<br>                &quot;ECS_EXECUTION_ROLE_ARN&quot;,<br>                &quot;ECS_TASK_ROLE_ARN&quot;<br>            ]<br>        }<br>    ]<br>}</pre><p>☝️: You can find out what roles you need a role in the error logs of a step function.</p><h4>Passing context from ECS</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RGaR7yP9PXsaOgK5-SVeGQ.png" /><figcaption>Enabled Wait For Callback option</figcaption></figure><p>By default, you can not pass any information outside the container, but if you enable the “Wait for a callback” option in StepFunction(as we did), you can pass parameters outside the container via callback.</p><p>☝️: To execute a callback you’ll need a token</p><p>A simple module for sending a callback on Python can be the following:</p><pre>import boto3<br>from os import environ<br>from json import dumps<br><br>sfn = boto3.client(&#39;stepfunctions&#39;)<br>taskToken = environ[&quot;TASK_TOKEN&quot;]<br><br><br>def send_output(output) -&gt; None:<br>    sfn.send_task_success(<br>        taskToken=taskToken,<br>        output=dumps(output)<br>    )<br><br><br>def send_error(error: str) -&gt; None:<br>    sfn.send_task_failure(<br>        taskToken=taskToken,<br>        error=error<br>    )</pre><p>Also, you need to catch any errors and send failure to report failure and not hang StepFunction.</p><pre>if __name__ == &#39;__main__&#39;:<br>    source_file_path = os.getenv(&#39;SOURCE_FILE_PATH&#39;, &#39;&#39;)<br>    try:<br>        # main logic goes here<br>        pass<br>    except Exception as e:<br>        send_error(str(e))<br>        raise e</pre><h4>Create a trigger for S3 events</h4><p>Go to your bucket -&gt; “Properties” -&gt; “Amazon EventBridge” -&gt; “Edit” -&gt; On</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nCfZben3TCV9csbnkcvXCw.png" /><figcaption>Enabled S3 to EventBridge notification</figcaption></figure><p>Next, go to “<strong>EventBridge</strong>” and press “Create a new rule”.</p><p>Enter a name and select “Rule with event pattern”. Click “Next”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P2PMOYK_qRKRmIbEso8nMw.png" /><figcaption>Creating a new rule</figcaption></figure><p>Remain “Evente source” to “AWS events or EventBridge partner events”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/1*rQXxEq2BbjpoEii9u9CG_A.png" /><figcaption>Events source set to AWS events</figcaption></figure><p>Move to the end.</p><ol><li>Select “Creation method” as “Use pattern form”.</li><li>Move to the “Event pattern section”</li><li>Select “Event source” as “AWS services”.</li><li>Select “AWS service” as S3</li><li>Select type as “Amazon S3 event notification”</li><li>Select “Specific event(s)” as “Object Created”</li></ol><p>Click “Next”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*B-RSR_1zx2e-VRRrav0jTw.png" /><figcaption>Creating EventBridge event pattern</figcaption></figure><p>☝️: You can customize the event pattern more. For example, you can filter only events with the specific key beginning. More read in <a href="https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html">Amazon EventBridge event patterns — Amazon EventBridge</a>.</p><p>Next, we will create a target for our event.</p><ol><li>Select “Target type” as “AWS service”</li><li>Select “Select a target” as “Step Functions state machine”</li><li>Select State Machine you’ve created</li><li>Move to the review and create and save it.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1017/1*ugXd2ZAZvfbngncnadaRbA.png" /><figcaption>Adding a target service</figcaption></figure><h4>Debugging StepFunction</h4><p>If you go to StepFunction, you’ll see a list of all execution events.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*35kBcEEQEOEdhwEcCvDEZQ.png" /><figcaption>List of all executions</figcaption></figure><p>If you click on one, you’ll see input and output data and resources.</p><p>By clicking resource, you’ll navigate to the page of a resource to see more advanced logs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dkwAuAhWtwSZNFFLv_1QxA.png" /><figcaption>Execution details</figcaption></figure><p>In conclusion, AWS Step Functions and AWS EventBridge are two powerful services that developers can use to build scalable, reliable, and event-driven architectures. AWS Step Functions allow developers to coordinate and visualize distributed applications and microservices using state machines. It is a fully managed service that provides a simple way to build and scale workflows, making it easy to integrate with other AWS services. AWS EventBridge is a serverless event bus service that makes it easy to build event-driven architectures at scale. It integrates with over 100 AWS services and SaaS applications, simplifying event-driven architectures and enabling developers to build scalable, reliable, and flexible applications. When choosing between AWS Step Functions and AWS EventBridge, it is essential to consider your application’s specific requirements for scalability, performance, and state management.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7104b8abe2b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Maximizing Efficiency: A Guide to Cost-Optimizing Your DynamoDB Population]]></title>
            <link>https://d9nich.medium.com/maximizing-efficiency-a-guide-to-cost-optimizing-your-dynamodb-population-3f4c5cbbb90c?source=rss-f50118a3e717------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f4c5cbbb90c</guid>
            <category><![CDATA[price]]></category>
            <category><![CDATA[dynamodb]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[population]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Danylo Halaiko]]></dc:creator>
            <pubDate>Mon, 24 Apr 2023 07:01:22 GMT</pubDate>
            <atom:updated>2023-04-24T07:01:22.173Z</atom:updated>
            <content:encoded><![CDATA[<p>DynamoDB is a NoSQL database service that provides fast and flexible performance, scalability, and high availability for applications that require low-latency data access. One of the critical aspects of using DynamoDB is populating it with data. DynamoDB allows for different ways of data ingestion, such as manual insertion through the AWS Management Console, SDKs, and CLI. However, for larger datasets or automated ingestion, it’s essential to have an efficient and reliable population strategy. In this article, we will explore different approaches to the DynamoDB population, their advantages and disadvantages, and best practices for managing data ingestion in DynamoDB.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jwRiSx_0MpoQyuVE" /><figcaption>Photo by <a href="https://unsplash.com/es/@michael75?utm_source=medium&amp;utm_medium=referral">Michael</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>What is DynamoDB?</h3><p>DynamoDB is a fully managed NoSQL database service provided by Amazon Web Services (AWS). It is designed for applications that require high performance, scalability, and low latency. DynamoDB can store and retrieve any amount of data and is a great option for building web, mobile, gaming, and IoT applications.</p><p>DynamoDB is a key-value and document database that supports flexible data models, including document, key-value, and graph data models. It allows users to store and retrieve structured and semi-structured data and provides features such as automatic scaling, high availability, and data encryption.</p><p>DynamoDB uses a partitioned architecture, which allows it to scale horizontally to handle any amount of data and traffic. It also supports multiple data centres, which provide high availability and fault tolerance.</p><p>DynamoDB provides a rich set of APIs and SDKs for several programming languages, which makes it easy to integrate with other AWS services and build applications quickly. It also integrates with other AWS services, such as AWS Lambda, Amazon S3, and Amazon Redshift, to enable the building of fully functional and scalable applications.</p><h4>Use Cases</h4><p>DynamoDB is a versatile and highly scalable database that can be used for a wide range of use cases. Here are some examples of how DynamoDB is commonly used:</p><ol><li>🌐📱Web and Mobile Applications: DynamoDB is commonly used to store user data, session data, and other application data for web and mobile applications. It provides fast and reliable access to data and can handle millions of requests per second.</li><li>🎮🕹️Gaming Applications: Many online gaming applications use DynamoDB to store game state, player data, and other game-related information. It&#39;s low latency and high throughput make it an ideal choice for real-time gaming applications.</li><li>🌐📈Internet of Things (IoT): DynamoDB is used to store and process data from IoT devices such as sensors and cameras. It can handle large volumes of data in real time and allows for fast and reliable analysis of IoT data.</li><li>📊📈Ad Tech and Marketing Applications: Many ad tech and marketing applications use DynamoDB to store and analyze user behaviour data such as clicks, impressions, and conversions. Its flexible data model and high performance make it an ideal choice for storing and processing large volumes of data.</li><li>💰🏦Financial Services: DynamoDB is used by many financial services companies to store and analyze financial data such as transaction records, customer data, and fraud detection data. Its scalability, reliability, and security features make it an ideal choice for financial services applications.</li></ol><p>Overall, DynamoDB is a popular choice for any application that requires a highly scalable, flexible, and reliable database that can handle large volumes of data and traffic.</p><h3>Context of application</h3><p>My application (plagiarism detection) needs to store 100 million key/value hashes. Where the key is word hash and value document where this word hash has occurred. In future, I want to store more than 1 billion to make results more precise.</p><p>Let’s talk about approaches, we can take to saturate DB.</p><h4>What is WCU?</h4><p>In DynamoDB, <strong>WCU </strong>stands for Write Capacity Unit. It is a unit of measurement that represents the amount of write capacity available for a DynamoDB table or index. One <strong>WCU </strong>represents the capacity to write <strong>one </strong>item of up to 1KB in size per second.</p><p>When you create a DynamoDB table, you need to provision the desired read and write capacity for the table. You can do this by specifying the number of read capacity units (RCUs) and write capacity units (WCUs) for the table. The provisioned capacity determines the number of reads and writes that can be performed on the table per second.</p><p>For example, if you provision a table with 10 WCUs, you can write up to 10 items of up to 1KB in size per second, or fewer items if they are larger than 1 KB. If you need to write more items per second or larger items, you can increase the number of WCUs for the table.</p><p>DynamoDB automatically scales a table&#39;s read and write capacity based on traffic and usage patterns. It can also provide burst capacity to handle sudden spikes in traffic. The cost of using DynamoDB is based on the provisioned capacity and the actual usage, and you pay only for what you use.</p><p>🔗: More read in <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.ReadWriteCapacityMode.html">Read/write capacity mode — Amazon DynamoDB</a>.</p><h4>Challenges of storing 100 million key/value hashes</h4><p>Storing 100 million key/value hashes can present several challenges, such as:</p><ol><li>💪🚀Scalability: Traditional relational databases may struggle to handle such a large volume of data and may require additional hardware to scale up. This can be costly and time-consuming.</li><li>🛡️High availability: It’s important to ensure that the data is always available and accessible, even in the event of hardware failures or other issues.</li><li>💨Performance: Retrieving and updating data at scale can be time-consuming, especially if the database is distributed across multiple servers.</li><li>🔄Data consistency: With a distributed database, it can be challenging to ensure that data is consistent across all nodes.</li></ol><h4>How does DynamoDB address those challenges?</h4><p>DynamoDB addresses these challenges by providing a highly scalable, highly available NoSQL database that can store and retrieve large amounts of data quickly and efficiently. Some of the specific ways DynamoDB addresses these challenges include:</p><ol><li>💪🚀Scalability: DynamoDB is designed to scale horizontally, meaning that it can easily add more servers to handle additional data as needed. This allows it to handle large volumes of data without requiring additional hardware.</li><li>🛡️High availability: DynamoDB is designed to be highly available, with built-in redundancy and automatic failover to ensure that data is always accessible.</li><li>💨Performance: DynamoDB is optimized for fast data retrieval and updates, with support for low-latency queries and high-throughput data access.</li><li>🔄Data consistency: DynamoDB supports multiple levels of consistency, allowing developers to choose the level of consistency that best suits their application’s needs.</li></ol><p>Overall, DynamoDB is a powerful database solution for storing and retrieving large volumes of data quickly and efficiently, while also providing high availability and scalability.</p><h3>Calculating each mode price</h3><h4>Calculate the price for on-demand mode(very expensive)</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EhKcsbpltwyBW79o2O-Vgg.png" /><figcaption><strong>Official </strong>information about WRU prices</figcaption></figure><p>For every million writes we pay $1,25 in that mode.</p><p>For our scenario, 100 million rows will cost 125 dollars.</p><p>ℹ️: Make notice, that <strong>read </strong>operations are <strong>5 times cheaper</strong>.</p><p>🔗: Look at the prices of <a href="https://aws.amazon.com/dynamodb/pricing/on-demand/?nc1=h_ls">Amazon DynamoDB Pricing for On-Demand Capacity</a></p><h4>Calculate the price for provisioned mode</h4><p>For provisioned mode, we can scale up resources for the time of population and scale down for the time of the end.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OsFsoxPrPemksplbuXJNIg.png" /><figcaption><strong>Official </strong>information about WCU prices</figcaption></figure><p>In this case, we should evenly divide our data across specific times, because you pay per hour. You get 1 WCU every second.</p><p>For example,</p><p>100 million rows / 3600 seconds = 27778 WCU</p><p>In real situations, we will need more than what math says. In my experience 1,4 times more. So, we get 40k WCU.</p><p>Price for 40k WCU = $27.</p><p>Not bad, but creating an even population is <strong>hard to achieve</strong>.</p><p>Compared to on-demand mode, provisioned can be <strong>4 times cheaper.</strong></p><p>⚠️: You should test it on your dataset and not take this information for granted.</p><p>🔗: Look at the prices of <a href="https://aws.amazon.com/dynamodb/pricing/provisioned/">Amazon DynamoDB Pricing for Provisioned Capacity</a></p><h4>Import from S3 — the cheapest way</h4><p>⚠️: Disclaimer:</p><blockquote>“During the Amazon S3 import process, DynamoDB creates a <strong>new target table that will be imported</strong> into. Import into existing tables is not currently supported by this feature” — <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/S3DataImport.HowItWorks.html">DynamoDB data import from Amazon S3: how it works — Amazon DynamoDB</a></blockquote><p>For the population <strong>existing</strong> table, use previous methods.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jekuBjfhFhRgdNuWZomyFA.png" /><figcaption>Price of <strong>import data from S3</strong> per GB</figcaption></figure><blockquote>“Import from Amazon S3 <strong>does not consume write capacity</strong> on the new table, so you do not need to provision any extra capacity for importing data into DynamoDB. Data import pricing is based on the <strong>uncompressed size of the source</strong> data in Amazon S3, that is processed as a result of the import” — <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/S3DataImport.HowItWorks.html">DynamoDB data import from Amazon S3: how it works — Amazon DynamoDB</a></blockquote><p>Let’s take every row as 1 KB.</p><p>1Kb*100ML rows = 100GB</p><p>100GB will cost us $15.</p><h3>DynamoDB Importing from S3</h3><p>DynamoDB allows you to import data from Amazon S3 into a DynamoDB table using the AWS Management Console, the AWS Command Line Interface (CLI), or the DynamoDB API. When importing data from S3, you must specify the source S3 bucket and the format of the data.</p><h4>Import formats</h4><ul><li><strong>CSV </strong>(Comma-Separated Values): CSV is a popular format for importing and exporting data. When importing CSV data into DynamoDB, each line in the file represents a single item in the table. The first line of the file contains the column names, and each subsequent line contains the values for each column.</li><li><strong>JSONL (</strong>JSON Lines) is a format for storing structured data in a text file, where each line of the file contains a single JSON object. The format is commonly used for logging, data exchange, and batch processing of large datasets.</li></ul><p>JSONL files are similar to JSON files, but each line of the file must contain a valid JSON object, separated by a newline character. Unlike JSON, which requires the entire file to be parsed as a single object, JSONL files can be processed line by line, making them ideal for large datasets that do not fit into memory.</p><p>JSONL files can be created and processed using various tools and programming languages, including Python, Node.js, and Ruby. Many modern data processing frameworks and libraries, such as Apache Spark and Pandas, support reading and writing JSONL files.</p><p>Overall, JSONL provides a lightweight and flexible format for storing and processing structured data in a line-by-line fashion. Its simplicity and compatibility with a wide range of tools and programming languages make it a popular choice for many data processing applications.</p><ul><li><strong>Amazon Ion</strong> is a richly-typed, self-describing, hierarchical data serialization format that is designed to be both human-readable and efficient for machine processing. It was developed by Amazon Web Services (AWS) and is used as a data interchange format in various AWS services such as DynamoDB, S3, and Kinesis.</li></ul><p>At its core, Ion is a superset of JSON that includes additional data types such as timestamps, binary data, and symbol tables. It also supports advanced features such as annotations, which allow users to add metadata to data elements, and symbol tables, which allow for efficient representation of repeated values.</p><p>One of the key advantages of Ion is its ability to represent complex and nested data structures in a compact and efficient format. The self-describing nature of Ion means that data elements are accompanied by type information, making it easier to validate and process the data. Additionally, Ion supports partial parsing, which allows users to parse and process portions of a larger Ion document without having to parse the entire document.</p><p>Ion is supported by various programming languages, including Python, Java, and JavaScript, and there are several open-source libraries available for working with Ion data.</p><p>Overall, Amazon Ion is a flexible and efficient data serialization format that is particularly well-suited for complex and nested data structures. Its support for advanced features and self-describing nature make it a popular choice for various data processing and interchange use cases.</p><p>🔗: Read more in official documentation <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/S3DataImport.Format.html">Amazon S3 import formats for DynamoDB — Amazon DynamoDB</a></p><p>📗: More about popular file formats read in <a href="https://medium.com/@d9nich/json-xml-protobuf-thrift-avro-or-everything-you-need-to-know-about-encoding-data-6077a7e769e2">my previous article</a>.</p><h4>Encryption and number of files</h4><blockquote>“Data can be compressed in ZSTD or GZIP format, or can be directly imported in uncompressed form. Source data can either be a single Amazon S3 object or multiple Amazon S3 objects that use the same prefix” - <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/S3DataImport.HowItWorks.html">DynamoDB data import from Amazon S3: how it works — Amazon DynamoDB</a></blockquote><p>☝️: Encryption can reduce the price of storing a big file on S3</p><p>In conclusion, DynamoDB is a powerful database that offers high performance, scalability, and low latency for a wide range of use cases. It is an excellent option for applications that require a highly scalable, flexible, and reliable database that can handle large volumes of data and traffic. When it comes to populating DynamoDB, there are two modes to consider: on-demand and provisioned. While the on-demand mode is very expensive, the provisioned mode offers a more cost-effective solution, especially for large-scale data populations. However, it can be challenging to create evenly populated data in provisioned mode. In summary, by understanding the different modes and approaches for populating DynamoDB, you can make the most of this powerful database technology to build scalable and reliable applications.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f4c5cbbb90c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploring Advanced Indexing Structures in Databases]]></title>
            <link>https://d9nich.medium.com/exploring-advanced-indexing-structures-in-databases-c4e5e3ce44bd?source=rss-f50118a3e717------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4e5e3ce44bd</guid>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[index]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[techniques]]></category>
            <dc:creator><![CDATA[Danylo Halaiko]]></dc:creator>
            <pubDate>Mon, 17 Apr 2023 07:01:43 GMT</pubDate>
            <atom:updated>2023-04-17T07:01:43.733Z</atom:updated>
            <content:encoded><![CDATA[<p>In the world of databases, indexing plays a crucial role in improving performance and query efficiency. While primary key indexes are commonly used to identify unique rows, secondary indexes are often necessary to optimize searches for frequently queried columns. In this article, we will explore advanced indexing structures beyond basic key/value indexes, including clustered and nonclustered indexes, multi-column indexes, multi-dimensional indexes, and fuzzy indexes. We will also delve into the world of in-memory databases and their increasing popularity due to the decreasing cost of RAM. With a focus on real-world examples and use cases, this article aims to provide a comprehensive overview of advanced indexing structures and their benefits.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-G_upkz3NdValwhM" /><figcaption>Photo by <a href="https://unsplash.com/@diesektion?utm_source=medium&amp;utm_medium=referral">Robert Anasch</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Secondary indexes</h3><p>In previous articles, we have discussed key/value indexes. That can be represented as a <strong>primary key index</strong> in the relational database.</p><p>📗: Read about B-Tree in <a href="https://medium.com/@d9nich/understanding-b-trees-the-most-common-indexing-structure-in-databases-5d6a51d35312">my previous article</a></p><p><strong>Primary key</strong> — is a row in a relational database or document in document DB, that can uniquely identify a row.</p><p>In the table below, it would be right to create a <strong>secondary index</strong> for the column “location”, because we can predict that many people would search for jobs near them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*FAREAV6NiaclLWLddk5fjQ.png" /><figcaption>Example of the relational table</figcaption></figure><p>We can construct a <strong>secondary index </strong>using our key/value structures from previous articles, but we should be ready so that our key <strong>wouldn’t </strong>be <strong>unique</strong>. We can use both B-tree and log-structured indexes to solve this problem.</p><h4>Store values in the index</h4><p>There are two types of indexes you can create:</p><ul><li><strong>Clustered </strong>index — we store rows directly near the index</li><li><strong>Nonclustered </strong>index — store only references to the data</li></ul><p>☝️: Data duplication speeds up <strong>reads</strong> but needs more <strong>space </strong>and you will get slow down on <strong>writes</strong>.</p><h3>Multi-column indexes</h3><p>Indexes discussed so far can only be used with a single key/column, but in some circumstances, we need multi-column indexes. For example, it would be a good idea to search jobs in the previous example by “location” and “hiring” status or maybe by a company name too.</p><h4>Concatenated index</h4><p>The most common index is called <strong>concatenated index</strong>, which simply appends one column to another. The order of columns you specify on the declaration. Similar works phone book📞:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*hqwr4uHdZLESZ7yF" /><figcaption>Old-fashioned <strong>phone book</strong></figcaption></figure><p>For instance, you can find a person with a last name and name, or only with the last name, but you <strong>can’t find</strong> a person having their name.</p><h4>Multi-dimensional index</h4><p>The multi-dimensional index is more flexible. The best use case is to find a cafe near you having <a href="https://en.wikipedia.org/wiki/Geographic_coordinate_system">latitude and longitude</a>. SQL will be the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a0b44a07705cb6b03bbb2be04b55fbd/href">https://medium.com/media/0a0b44a07705cb6b03bbb2be04b55fbd/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/0*vQ9AN6D9lmT9LxlU.png" /><figcaption><strong>Latitude </strong>and <strong>longitude </strong>for the USA</figcaption></figure><p>We can not use simple B-trees or LSM-tree, because we can only apply them to a latitude or longitude, but not both at the same time.</p><p>Of course, we can convert multi-dimensional to single-dimensional using a <a href="https://en.wikipedia.org/wiki/Space-filling_curve">space-filling curve</a> and then use B-trees.</p><blockquote>“It acts like a <strong>thread </strong>that passes through every cell element (or pixel) in the multi-dimensional space so that every cell is visited <strong>exactly once</strong>”. <em>— Mokbel, M.F., Aref, W.G. (2009). Space-Filling Curves</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/0*S8LWHdSQSimSVXoy.gif" /><figcaption>Example of a <a href="https://www.dcs.bbk.ac.uk/~jkl/BNCOD2000/slides.html">space-filling curve</a></figcaption></figure><p>However, more commonly specialized indexes are used — <strong>R-trees</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*91lH8_r-UkuAwgC6gAK7_w.png" /><figcaption><a href="https://www.researchgate.net/figure/Example-of-an-R-tree_fig1_33030343">2D R-tree example</a></figcaption></figure><p>ℹ️: <a href="https://postgis.net/">PostGIS</a> uses PostgreSQL <a href="https://en.wikipedia.org/wiki/GiST">GiST</a>(Generalized Search Tree) index facility to implement geospatial indexes</p><h3>Fuzzy indexes(Text indexes)</h3><p>All indexes discussed before used certain searches, but if we want to find <em>similar</em>(misspelt words)<em> </em>keys they are useless.</p><p>ℹ️: Searching for similarity is called <strong>fuzzy </strong>searching</p><p>Full-text search allows one to search words without counting grammatical variations, and synonyms and supports various other linguistic analysis techniques. To cope with typos, such indexes can search with a certain <strong>edit distance</strong>(<a href="https://lucene.apache.org/">Apache Lucene</a>).</p><p>ℹ️: 📏<strong>Edit distance</strong> of <em>1</em> means one letter can be replaced, removed or added</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/0*50y2PTWaVFLCLUE-" /><figcaption><a href="https://www.elastic.co/blog/found-fuzzy-search">Example of <strong>edit distance</strong></a></figcaption></figure><p>Lucene uses SSTable to store its term dictionary(key — term, value — list of documents ids), but this structure requires an<em> in-memory index</em> that will tell offset in a sorted file. This index is a <strong>trie tree</strong>, that can be easily converted to <a href="https://en.wikipedia.org/wiki/Levenshtein_automaton"><strong>Levenshtein </strong>automaton</a>. Levenshtein automaton supports the efficient search for words in edit distance.</p><p>📗: More about SSTable in Lucene and ElaticSearch you can <a href="https://medium.com/@d9nich/sstable-database-or-big-ideas-behind-the-scene-1d449a12fb44">read here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mKoh4gnMfBNp04M5.png" /><figcaption><a href="https://en.wikipedia.org/wiki/Trie">Trie tree</a></figcaption></figure><p>Other techniques require document classification or ML. More about retrieval you can <a href="https://nlp.stanford.edu/IR-book/">read here</a>.</p><h3>In memory database🧠</h3><p>All that we have been talking about in this chapter is the cause of the limitations of disks (magnetic and SSD) compared to RAM. We need to think about how we store our data to achieve good read and write performance, but instead, we get cheap gigabyte cost and persistence.</p><p>Nowadays, RAM memory become cheaper, so the cost-per-gigabyte is not an argument anymore. And if we are talking about datasets, the majority of data is not so big to not fit in memory. This leads us to <strong>in-memory databases</strong>.</p><h4>⌚What about durability?</h4><p>Some databases are created for caching purposes (<a href="https://memcached.org/">Memcached</a>) — data will be lost after restart. However, some databases achieve durability in two ways:</p><ul><li><strong>programmatically </strong>— writing periodic snapshots, logs or replicating to other instances</li></ul><p>ℹ️: Despite writing to disk, it’s still an in-memory database, because disk usage is rare</p><ul><li><strong>hardware </strong>— <a href="https://electronics.stackexchange.com/questions/461861/can-we-not-simply-connect-a-battery-to-a-ram-to-prevent-data-loss-during-power-c">battery-powered RAM</a>. Non-volatile memory database is a new area, so it’s not so widely adopted now, but it can become soon, so it is worth to keep eye on it.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*N4VvkKaiu10Y8DVy.jpg" /><figcaption>512K battery backup <strong>SRAM</strong> Module</figcaption></figure><p>☝️: By writing snapshots/logs to disk you can easily backup DB or analyze them with external tools.</p><h4>Real examples of In-memory databases</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*bi72yZUacPCzy4Y1.png" /><figcaption><strong>MemSQL</strong> is a<strong> relational in-memory</strong> database</figcaption></figure><ul><li>Vendors with <strong>relational</strong> model: <a href="https://en.wikipedia.org/wiki/VoltDB">VoltDB</a>, <a href="https://en.wikipedia.org/wiki/SingleStore">MemSQL</a>, <a href="https://en.wikipedia.org/wiki/TimesTen">Oracle TimesTen</a></li><li><strong>Key-value</strong> store — <a href="https://ramcloud.atlassian.net/wiki/spaces/RAM/overview">RAMCloud</a>(support both <em>log </em>structure and <em>snapshot </em>on disk)</li><li><strong>Weak durability</strong>(write to disk asynchronously): <a href="https://redis.io/">Redis</a> and <a href="https://www.couchbase.com/">Couchbase</a></li></ul><h4>Why in-memory databases are faster than regular ones?</h4><p>It’s a common mistake to think, that performance advantage is due to disk reading. If you have enough memory, the operating system can cache disk blocks in memory. The <strong>main reason</strong> is the time needed to encode in a form that needs to be written to disk. More you can <a href="https://hstore.cs.brown.edu/papers/hstore-lookingglass.pdf">read in study</a>.</p><p>Another problem is maintaining difficult data structures on a hard drive. For example, sets and priority queues in Redis.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*D9xjBuQtjC_u_nJK.jpeg" /><figcaption>Available <strong>types </strong>in Redis</figcaption></figure><p>In conclusion, advanced indexing structures can significantly improve the performance and efficiency of database systems. Secondary indexes, clustered and non-clustered indexes, multi-column indexes, multi-dimensional indexes, and fuzzy indexes are all powerful tools that can be used to optimize database queries. In-memory databases have become more popular as RAM memory has become cheaper, offering a way to achieve faster read and write performance. Additionally, the use of battery-powered RAM and non-volatile memory databases is an exciting area for future development. Overall, by carefully selecting and implementing indexing structures and utilizing in-memory databases, businesses can improve the speed, scalability, and reliability of their database systems.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4e5e3ce44bd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Integrating Firebase Auth with AWS API Gateway Lambda Authorizer: A Secure and Scalable Solution…]]></title>
            <link>https://d9nich.medium.com/integrating-firebase-auth-with-aws-api-gateway-lambda-authorizer-a-secure-and-scalable-solution-d2622607862b?source=rss-f50118a3e717------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2622607862b</guid>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Danylo Halaiko]]></dc:creator>
            <pubDate>Mon, 10 Apr 2023 07:02:13 GMT</pubDate>
            <atom:updated>2023-04-10T07:02:13.481Z</atom:updated>
            <content:encoded><![CDATA[<h3>🔐Integrating Firebase Auth with AWS API Gateway Lambda Authorizer: A Secure and Scalable Solution for User Authentication and Access Control</h3><p>In today’s digital age, user authentication and access control are critical components of any secure and scalable application. One popular approach is to use Firebase Authentication for user authentication and AWS API Gateway Lambda Authorizer for access control. In this article, we will discuss how to integrate these two services to create a secure and scalable solution for user authentication and access control. We will dive into the technical details of how to implement this solution and provide step-by-step instructions to help you get started. By the end of this article, you will have a clear understanding of how to integrate Firebase Authentication with AWS API Gateway Lambda Authorizer and be able to implement this solution in your applications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HDgX4cA69fQHGKC5" /><figcaption>Photo by <a href="https://unsplash.com/@towfiqu999999?utm_source=medium&amp;utm_medium=referral">Towfiqu barbhuiya</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4>API Gateway Lambda authorizer</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*bUeNsLvXun-uPh0c.png" /><figcaption><a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html">Use API Gateway Lambda authorizers — Amazon API Gateway</a></figcaption></figure><p>API Gateway Lambda authorizer is an authorization mechanism that enables developers to control access to APIs deployed in Amazon API Gateway using AWS Lambda functions. With API Gateway Lambda authorizer, developers can<strong> authenticate and authorize clients before</strong> allowing them to access protected resources.</p><p>API Gateway Lambda authorizer works by passing the client’s credentials (such as API keys, IAM roles, or custom tokens) to a Lambda function that determines whether the client is authorized to access the requested resource.</p><p>API Gateway Lambda authorizer provides a flexible and scalable authorization solution for APIs deployed in API Gateway. Developers can use API Gateway Lambda authorizer to implement complex authorization scenarios, such as multi-factor authentication, social login, and fine-grained access control.</p><p>One of the benefits of API Gateway Lambda authorizer is that it enables developers to implement authorization logic using programming languages supported by AWS Lambda, such as <em>Node.js, Python, Java, and Go</em>. This allows developers to implement custom authorization logic that is tailored to the specific needs of their application.</p><p>Another benefit of API Gateway Lambda authorizer is that it provides a centralized authorization mechanism for APIs deployed in API Gateway. This means that developers can use a single authorization mechanism to control access to multiple APIs, simplifying the management of authorization policies and <strong>reducing the risk of errors.</strong></p><p>In summary, API Gateway Lambda authorizer is an authorization mechanism that enables developers to control access to APIs deployed in Amazon API Gateway using AWS Lambda functions. It provides a flexible and scalable authorization solution that can be used to <strong>implement complex authorization scenarios</strong> and supports multiple programming languages.</p><p>🔗: Read more in <a href="https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html">Use API Gateway Lambda authorizers — Amazon API Gateway</a></p><h4>Firebase auth</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*hFo2Wo47qDk9vc2PIDTAXQ.png" /><figcaption>Firebase Authentication</figcaption></figure><p>Firebase Auth is a powerful and easy-to-use authentication system that enables developers to add authentication to their applications <strong>quickly</strong>. Firebase Auth is a product of Google’s Firebase platform, which provides a suite of backend services for building mobile and web applications.</p><p>Firebase Auth provides a range of authentication options, including <strong>email</strong> and <strong>password</strong>, <strong>phone number</strong>, <strong>social media</strong>, and federated identity providers such as <strong>Google, Facebook, Twitter, and GitHub</strong>. This means that developers can provide their users with a wide range of authentication options, depending on their preferences and needs.</p><p>One of the most significant benefits of using Firebase Auth is that it <strong>handles all aspects of user authentication</strong> and management, including user <em>creation</em>,<em> password reset, and email verification</em>. This means that <strong>developers can focus on building their application’s core</strong> functionality, rather than worrying about authentication and security.</p><p>Firebase Auth also provides robust security features, including <strong>brute-force protection</strong>,<strong> account takeover protection</strong>, and <strong>multi-factor authentication</strong>. Developers can configure these security features to meet the needs of their applications and users.</p><p>Another benefit of Firebase Auth is that it i<strong>ntegrates seamlessly with other Firebase services</strong>, such as Firebase Cloud <strong>Firestore</strong><em> </em>and Firebase Cloud <strong>Functions</strong>. This means that developers can easily secure their application’s data and add authentication to their serverless functions.</p><p>Finally, Firebase Auth is straightforward to set up and use. Developers can integrate it into their applications using the Firebase SDKs for iOS, Android, and the web. The Firebase SDKs provide a range of APIs and pre-built UI components that make it easy to add authentication to an application.</p><p>In summary, Firebase Auth is a powerful and easy-to-use authentication system that provides developers with a range of authentication options, robust security features, and seamless integration with other Firebase services. By using Firebase Auth, developers can quickly and easily add authentication to their applications, allowing them to focus on building their application’s core functionality.</p><p>🔗: Read more in <a href="https://firebase.google.com/docs/auth/">Firebase Authentication (google.com)</a></p><h4>Examples and use cases for API Gateway Lambda Authorizer integration with Firebase Auth</h4><ol><li>🔒<strong>Secure API endpoints</strong>: You can use API Gateway Lambda Authorizer with Firebase Auth to secure your API endpoints. The Lambda function can check if the user is authorized to access the API by verifying the Firebase Auth token.</li><li>🚪<strong>User authentication</strong>: Firebase Auth provides user authentication features that can be used to authenticate users. You can use API Gateway Lambda Authorizer to verify the user’s Firebase Auth token and allow access to the API only if the user is authenticated.</li><li>🔑<strong>Role-based access control</strong>: Firebase Auth provides user roles and permissions that can be used to control access to different parts of your API. You can use API Gateway Lambda Authorizer to check the user’s role and allow access only to users with the required role.</li><li>🔐<strong>Custom authentication</strong>: Firebase Auth provides a way to implement custom authentication mechanisms using Firebase Authentication APIs. You can use API Gateway Lambda Authorizer to integrate your custom authentication mechanism with your API.</li></ol><p>Overall, API Gateway Lambda Authorizer integration with Firebase Auth provides a secure and scalable way to implement user authentication and access control for your API.</p><h3>Set up integration Firebase Auth with AWS API Authorizer</h3><p>We will build a simple API for storing data in a user folder on S3.</p><h4>🔑Obtain Firebase Admin SDK credentials</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wr376aBaAqvoSfENHT-Zcw.png" /><figcaption>Generating new <strong>admin SDK key</strong></figcaption></figure><p>To obtain Firebase Admin SDK credentials, you need to follow these steps:</p><ol><li>🔥🧑‍💻🌟<strong>Create a Firebase project</strong>: Go to the Firebase Console and create a new project or use an existing one.</li><li>🔒🔑👤<strong>Set up Firebase Authentication</strong>: To use the Firebase Admin SDK, you need to enable Firebase Authentication for your project. Go to the Firebase Console and enable the authentication method you want to use. The Firebase Admin SDK supports several authentication methods, including email and password, Google, Facebook, and others.</li><li>🔑💼🔐<strong>Generate a service account key</strong>: Go to the Firebase Console and navigate to the project you want to use. Click on the gear icon in the top left corner and select “<em>Project settings</em>.” In the “<em>Service Accounts</em>” tab, click on the “<em>Generate new private key</em>” button. This will download a JSON file that contains the service account credentials.</li><li>🔒💾🔐Store the credentials securely. You’ll need it in the next step.</li></ol><p>⚠️: The service account key contains <strong>sensitive information</strong> that should be kept <strong>secret</strong>. Store the JSON file securely and <strong>do not share it</strong> with unauthorized parties.</p><h4>🔒Store the credentials in AWS Secrets Manager</h4><p>📗: We’ve already discussed the need and usage of AWS Secrets Manager in <a href="https://medium.com/@d9nich/understanding-amazon-elastic-container-service-ecs-and-its-integration-with-docker-hub-9813ac278978">my previous article about integrating DockerHub with AWS ECS</a></p><p>Let’s summarize the main points:</p><ol><li>AWS Secrets Manager is a fully managed service that enables you to store and retrieve secrets, such as API keys, passwords, and database credentials, <strong>securely </strong>in the AWS Cloud.</li><li>AWS Secrets Manager <strong>encrypts </strong>and <strong>decrypts </strong>secrets using industry-standard AES-256 encryption and stores them in a <strong>secure</strong>, <strong>isolated environment</strong>. This helps to protect your sensitive information from unauthorized access.</li><li>You can use AWS Secrets Manager to store any type of secret data, including binary data such as SSH keys, certificates, and binary files.</li><li>AWS Secrets Manager provides a <strong>simple API</strong> that you can use to retrieve secrets securely from your applications. This makes it easy to integrate with your existing applications and services.</li><li>AWS Secrets Manager enables you to <strong>rotate secrets automatically</strong> according to a user-defined schedule. This helps to reduce the risk of unauthorized access to your secrets.</li><li>AWS Secrets Manager <strong>integrates with other AWS services</strong>, such as AWS <em>Lambda</em>, Amazon <em>RDS</em>, and Amazon <em>DocumentDB,</em> making it easy to use your secrets with other AWS services.</li></ol><p>In summary, using AWS Secrets Manager for storing sensitive information like API keys provides a secure, scalable, and easy-to-use solution for managing secrets in the AWS Cloud. It offers strong encryption, and automatic rotation, and integrates with other AWS services, making it an ideal solution for storing and retrieving sensitive information securely and efficiently.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*tTmAQv_Eq6ORfsqJ.png" /><figcaption>Creating a new secret in AWS<strong> Secret Manager</strong></figcaption></figure><p>Go to AWS Secrets Manager and select “Store a new secret”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*26HAMTy4DJPJQ79IKAy_lg.png" /><figcaption><strong>Put secret</strong> in store</figcaption></figure><p>Choose “<em>Other type of secret</em>”.</p><p>Choose “<em>Plaintext</em>”.</p><p>In the form below, provide the content of the <strong>credentials file</strong> you obtained in the <strong>previous step</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z7aRmvlQIcD9mCsTol08bw.png" /><figcaption><strong>Provide name and description</strong> for secret</figcaption></figure><p>Provide a meaningful name and description for the secret.</p><p>For the “rotation” and “review” click “next” and “save” correspondingly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M_-gDchPd4iRCyqaRZfUHw.png" /><figcaption>Copy sample <strong>code</strong></figcaption></figure><p>Open your secret and copy the code example. You’ll need it in the following steps.</p><h4>📝Writing Authorization Lambda logic</h4><p>I will write authorization on Go, but you can select your preferred language.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f81130cef7f5921b8fc88c7aeb4daf7/href">https://medium.com/media/2f81130cef7f5921b8fc88c7aeb4daf7/href</a></iframe><p>This lambda will do the following, extract and verify the token, app check the token and whether the user email is verified. If everything is ok, it’ll provide uid down to the target lambda.</p><p>☝️: For a real system your logic should include more checks.</p><p>Upload Lambda to AWS and add permission to access <strong>Secrets Manager</strong>.</p><h4>👨‍💻Creating a regular lambda function</h4><p>This function will be executed if authorization is successful.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7306609f013745d05352c41c0a1e9e8/href">https://medium.com/media/a7306609f013745d05352c41c0a1e9e8/href</a></iframe><p>Our lambda will store content sent by the Internet to the S3 bucket.</p><p>☝️: Replace bucket name with real one</p><p>☝️: Notice, it will use uid provided by authorization lambda.</p><p>Upload Lambda to AWS and add permission to access <strong>S3</strong>.</p><h4>🔑Add Lambda authorizer to API Gateway</h4><p>In the API Gateway console, create a new Lambda authorizer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AY7sSSAYhInlAH1_mMwNHA.png" /><figcaption>Adding our lambda as <strong>Lambda Authorizer</strong></figcaption></figure><p>You can specify caching if it applies to your scenario. Don’t forget to add the “<em>Firebase-AppCheck</em>” header to the required parameters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hBdSlyGN2yT1fj3l8qTKng.png" /><figcaption>Removing TTL and adding authorization required header</figcaption></figure><h4>🛠️Configure the API Gateway method</h4><p>Finally, configure the API Gateway method that you want to protect with the Lambda authorizer. Set the authorization type to “Lambda authorizer”, and specify the name of the authorizer you created in the previous step.</p><p>Once you’ve completed these steps, API Gateway will invoke your Lambda function whenever a client tries to access the protected resource. Your Lambda function will be responsible for verifying the client’s credentials and returning an authorization policy that determines what actions the client is authorized to perform on the resource.</p><h3>Conclusion</h3><p>In conclusion, integrating Firebase Auth with AWS API Gateway Lambda Authorizer can provide a secure and scalable solution for user authentication and access control. By leveraging the power of Firebase’s authentication service and AWS Lambda’s authorization functionality, developers can build a robust authentication and authorization system that is both reliable and cost-effective.</p><p>Some key takeaways from this article include the importance of user authentication and access control in modern web applications, the benefits of using Firebase Auth and AWS API Gateway Lambda Authorizer, and the step-by-step process of integrating these two services. Additionally, we have discussed some common challenges and pitfalls that developers may encounter during the integration process and provided tips to overcome them.</p><p>As a final thought, it is crucial to ensure that user data is handled securely and with the utmost care. Building a robust authentication and authorization system is just one aspect of this. Developers should also follow best practices for securing data at rest and in transit and regularly test their systems for vulnerabilities. By doing so, they can ensure that their applications are not only functional but also safe for users to use.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2622607862b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding B-Trees: The Most Common Indexing Structure in Databases]]></title>
            <link>https://d9nich.medium.com/understanding-b-trees-the-most-common-indexing-structure-in-databases-5d6a51d35312?source=rss-f50118a3e717------2</link>
            <guid isPermaLink="false">https://medium.com/p/5d6a51d35312</guid>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[relational-databases]]></category>
            <category><![CDATA[trees]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[storage]]></category>
            <dc:creator><![CDATA[Danylo Halaiko]]></dc:creator>
            <pubDate>Mon, 03 Apr 2023 07:01:46 GMT</pubDate>
            <atom:updated>2023-04-03T07:01:46.294Z</atom:updated>
            <content:encoded><![CDATA[<p>Welcome to the world of database indexes! In the vast landscape of database technologies, indexes are crucial components that make data retrieval fast and efficient. In this article, we will explore <strong>B-trees</strong>, one of the most common index implementations used in relational and non-relational databases. B-trees have stood the test of time and are widely used in modern database systems. We’ll dive into the inner workings of B-trees, how they are implemented, how they maintain data consistency and reliability, and some tuning techniques to optimize their performance. So, get ready to learn about <strong>B-trees</strong> and how they help databases process queries and transactions faster.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mXYY4kTMMOmLBoLZ" /><figcaption>Photo by <a href="https://unsplash.com/@toddquackenbush?utm_source=medium&amp;utm_medium=referral">Todd Quackenbush</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Terminology</h3><p>Some users may be unfamiliar with concepts used in this article.</p><h4>🔒Latches</h4><p>In the context of databases, a latch is a lightweight synchronization mechanism used to protect shared data structures, such as B-trees, from concurrent access by multiple threads or processes. Latches can be thought of as locks that are held for a short period of time, just long enough for the operation to complete. Unlike heavier locks, such as mutexes, latches are designed to be held for a short time and are released as soon as possible to minimize contention and allow other threads to access the shared data structure. Latches are commonly used in database systems to ensure the consistency and correctness of data in multi-threaded environments.</p><h4>🌳LSM-tree</h4><p>LSM-tree stands for Log-Structured Merge Tree, which is a data structure used in computer science and database systems for efficient storage and retrieval of data. It is an alternative to B-trees and is especially well-suited for write-intensive workloads.</p><p>In an LSM-tree, data is initially written to an in-memory buffer called a memtable, which is periodically flushed to disk as an immutable sstable (sorted string table). Multiple sstables are eventually merged into larger sstables in the background, using a process called compaction. The compaction process reduces the number of sstables and eliminates duplicate and obsolete data. This allows for more efficient querying and retrieval of data.</p><p>LSM-trees are commonly used in NoSQL databases, such as Apache Cassandra and LevelDB, and have also been adopted by some relational databases, such as MySQL and PostgreSQL, as an alternative to B-trees for certain workloads.</p><p>🔗: Read about LSM tree in <a href="https://medium.com/@d9nich/lets-implement-the-log-database-or-big-ideas-behind-the-scene-694d86bdc2a6">my previous article</a></p><h3>QA</h3><h4>What do B-trees have in common with SSTables?</h4><p>🔗: Read about SSTable in <a href="https://medium.com/@d9nich/sstable-database-or-big-ideas-behind-the-scene-1d449a12fb44">my previous article</a></p><p>The only thing in common is keeping key/value pairs sorted.</p><h4>How do B-trees are implemented?</h4><p>B-trees break the database into fixed-sized <strong>blocks </strong>(AKA <em>pages</em>) and read or write one page at a time.</p><p>ℹ️: <strong>Pages<em> </em></strong>are commonly 4KB in size</p><p>Each page can be identified by the <strong>address </strong>(or path). It allows you to reference one page from another — similar to a pointer in memory, but on a disk.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GQ4z80db5SqJAdvpcLDNqQ.png" /><figcaption><strong>B-Tree</strong> example</figcaption></figure><p>One page is called root. We start our key search here. The <strong>page </strong>contains keys and <strong>references </strong>to child <strong>pages</strong>.</p><p>Example. Let’s find 140 value:</p><ol><li>We start at the <em>root </em>(yellow block).</li><li>Our value is bigger than 100, so we use the right reference.</li><li>Our searched value is between 130 and 180, so we go by middle reference.</li><li>Now, we’re at the blue block with available values 140 and 160.</li><li>We find a “140” key.</li></ol><p>ℹ️: The number of references on one page is called <strong>branching factors</strong>. In our example <em>branching factor </em>is equal to 5. In practice, the branching factor is several hundred.</p><h4>What do I need to do to update the value?</h4><p>First, you search for the leaf containing a key(the same algorithm we use to find the number 140) and write a new value to the file.</p><h4>How to insert new key/value pair?</h4><ol><li>Find a page, whose <strong>range </strong>includes your <em>new key</em></li><li>Add a <em>new key</em> to that <em>page</em></li><li>If after insertion we don’t match page <strong>limits</strong>, then <strong>split </strong>it into <strong>two </strong>half pages and update the <strong>parent page</strong> according to the division of the child page.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9Ruyl5Hwm5rPyoUJ" /><figcaption><strong>B-Tree</strong> insertion</figcaption></figure><p>ℹ️: B-tree is a <strong>balanced tree </strong>— depth is always equal to O(log n). Most databases fit into <strong>three/four</strong> levels deep(4KB pages &amp; branching factor 500 can store 256TB of data).</p><h3>What about B-trees reliability?</h3><h4>✍Writing problem</h4><p>💣A dangerous operation in B-trees is <strong>writing </strong>especially if after that you need to <strong>split </strong>the page(write two pages, update parent reference, remove old page). Crash in the middle of this operation can lead to corrupted <strong>indexes </strong>or pages, that have no parents(<strong>orphans</strong>).</p><p>🧾To solve this issue, we use <strong>WAL</strong>(<em>write-ahead log</em> AKA <em>redo log</em>) — append-only file. Before you start the action that modifies the database, you should write it to the WAL.</p><p>🔗: Read about append only file in <a href="https://medium.com/@d9nich/lets-implement-the-log-database-or-big-ideas-behind-the-scene-694d86bdc2a6">my previous article</a></p><h4>🔀Concurrency</h4><p>If multiple threads try to access DB, they can see an <em>inconsistent state</em>(in the middle of writing). To protect ourselves from these issues we use <strong>latches</strong>(lightweight locks).</p><h3>🖌B-trees tuning</h3><h4>Omit full key</h4><p>We can store only part of the key to identify boundary ranges of the page. For example, “ac” except for “acxvd”. This leads to having more keys on the page, a higher branching factor and thus fewer levels.</p><h4>👉Additional pointers</h4><p>You can improve performance by adding additional pointers. For example, each leaf can reference the neighbour leaf without the need to go up to the root. This can be very helpful in the case of range scanning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*EMb1ukerTj447qF_" /><figcaption>B-tree with <strong>sibling linking</strong> modification</figcaption></figure><h4>📝Copy-on-write instead of WAL</h4><p>Modifying the existing page introduces a lot of problems with concurrency and consistency(that’s why we should use <strong>WAL</strong>). The algorithm is to write pages to a different location and create a new version of parent pages the reference modification.</p><h3>B-Trees 🆚 LSM-Tree</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2DtiZnCDbbfHhlFM.png" /><figcaption><a href="https://www.yugabyte.com/blog/a-busy-developers-guide-to-database-storage-engines-the-basics/">Database <strong>classification</strong> on the storage engine</a></figcaption></figure><h4>Pros of LSM-tree👍</h4><ul><li>✍fast write (especially on the magnetic hard drive)</li><li>📦better compression</li></ul><h4>Cons of LSM-tree👎</h4><ul><li>📥compaction process can slow down reading and writing</li><li>🔀Not so popular in transaction implementation (B-trees offer this out of the box using attached-to tree locks)</li></ul><p>In conclusion, B-trees are the most common index implementation used in relational and some non-relational databases. They break the database into fixed-sized blocks and read or write one page at a time, with each page containing keys and references to child pages. B-trees are a balanced tree, which means their depth is always equal to O(log n). However, writing can be a dangerous operation, so databases use write-ahead logs (WAL) to solve this issue. To protect from concurrency issues, databases use latches. B-trees can be tuned by omitting full keys, adding additional pointers, and using copy-on-write instead of WAL. In the next article, the author plans to compare LSM-trees and B-trees. Overall, B-trees have stood the test of time and remain a reliable and widely used indexing implementation in databases.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5d6a51d35312" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rev up your Payment Processing with PayPal Integration on Firebase Functions]]></title>
            <link>https://d9nich.medium.com/rev-up-your-payment-processing-with-paypal-integration-on-firebase-functions-385e35f5f7ee?source=rss-f50118a3e717------2</link>
            <guid isPermaLink="false">https://medium.com/p/385e35f5f7ee</guid>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[integration]]></category>
            <category><![CDATA[payments]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[paypal]]></category>
            <dc:creator><![CDATA[Danylo Halaiko]]></dc:creator>
            <pubDate>Mon, 27 Mar 2023 07:01:47 GMT</pubDate>
            <atom:updated>2023-03-27T07:01:47.763Z</atom:updated>
            <content:encoded><![CDATA[<p>Are you looking to add online payment functionality to your web or mobile application? Look no further than integrating PayPal Checkout with Firebase Functions! This powerful combination allows you to create a seamless payment page for your users while taking advantage of the serverless computing power of Firebase Functions. In this article, we’ll guide you through the steps of integrating PayPal Checkout with Firebase Functions so you can start accepting payments in no time.</p><p>In this guide, we will create a payment page for buying “check pages” on our plagiarism detection site.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HB32RWTwjnKT4WpE" /><figcaption>Photo by <a href="https://unsplash.com/@querysprout?utm_source=medium&amp;utm_medium=referral">Marques Thomas</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>💰Paypal</h3><p>PayPal is an online payment system that allows individuals and businesses to send and receive payments electronically. It was founded in December 1998 and is headquartered in San Jose, California. PayPal enables users to transact in multiple currencies and provides a secure and convenient way to make online payments, transfer money, and accept payments for goods and services. Users can link their PayPal accounts to their bank accounts, credit cards, or debit cards, making it easy to transfer funds between accounts. PayPal is widely used for online shopping, e-commerce, and peer-to-peer payments.</p><h3>🔥Firebase</h3><p>Firebase is a mobile and web application development platform that provides a suite of backend services, such as real-time database, authentication, hosting, storage, and analytics. It was acquired by Google in 2014 and has since become one of the most popular platforms for building web and mobile applications.</p><p>Firebase allows developers to easily add features to their applications without building their backend infrastructure. For example, developers can use Firebase’s real-time database to store and synchronize data across multiple clients in real time or use Firebase Authentication to handle user authentication and authorization.</p><p>Firebase also offers a range of tools and integrations with other Google services, such as Google Cloud Functions and Google Analytics, which makes it easier for developers to build, test, and deploy their applications.</p><p>Overall, Firebase provides developers with a powerful and flexible platform for building modern web and mobile applications, allowing them to focus on building great user experiences rather than worrying about backend infrastructure.</p><h4>⚡Firebase functions</h4><p>Firebase Functions is a serverless computing platform provided by Firebase, a mobile and web application development platform powered by Google. Firebase Functions allows developers to write serverless functions that run in response to events such as changes in data in the Firebase Realtime Database, Firebase Authentication, and Firebase Cloud Storage. Firebase Functions can be written in JavaScript, TypeScript, and Node.js, and they are hosted on the Google Cloud Platform.</p><p>The main features of Firebase Functions are:</p><ol><li>🎟️Event-driven programming model: Firebase Functions allow developers to write code that runs in response to events that occur in Firebase services, making it easy to build serverless applications that respond to changes in data and user behaviour.</li><li>🚀Scalability and reliability: Firebase Functions are hosted on the Google Cloud Platform, which provides automatic scaling and load balancing, ensuring that your functions can handle high volumes of traffic and operate reliably.</li><li>🤝Easy integration with Firebase and other Google Cloud services: Firebase Functions can be easily integrated with other Firebase services such as Cloud Firestore, Cloud Storage, and Cloud Pub/Sub, as well as other Google Cloud Platform services such as Cloud Vision API and Cloud Translation API.</li><li>🤖Automated deployment and monitoring: Firebase Functions can be deployed and managed using the Firebase CLI or through the Firebase Console. Firebase also provides real-time monitoring and logging capabilities, making it easy to track the performance and behaviour of your functions.</li></ol><p>🔗: More about <a href="https://medium.com/%40d9nich/%EF%B8%8Fevent-driven-development-and-everything-you-should-know-about-it-3dc5c7e94cb6">event-driven architecture</a> read in my previous article</p><p>Overall, Firebase Functions provides a simple and flexible way to build serverless applications that respond to changes in data and user behaviour, while also providing scalability, reliability, and integration with other Firebase and Google Cloud Platform services.</p><h3>Integrating Paypal Checkout to Firebase App</h3><p>PayPal Checkout is a payment processing service provided by PayPal that allows customers to make purchases using their PayPal accounts, credit cards, or debit cards on merchant websites. With PayPal Checkout, customers can securely enter their payment information on a merchant’s website, without having to leave the website to complete the transaction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/992/0*PcRxbRgZ3Gu019kr.png" /><figcaption><a href="https://developer.paypal.com/docs/checkout/">Paypal Checkout Let the user pay in your shop and after processing payment at your server</a></figcaption></figure><p>☝️: In this article, I will use a sandbox account, but for a production system you should register a business account to go live.</p><h4>Paypal checkout steps</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*C2vM8awEatJWJFCkjwjy1w.png" /><figcaption>Steps involved in the PayPal<strong> Checkout order process</strong></figcaption></figure><p>The steps involved in the PayPal Checkout order process are as follows:</p><ol><li>Browser load PayPal script</li><li>Customer clicks on checkout: The customer clicks on the checkout button to start the checkout process.</li><li>A client sends a request to the Orders API with information about the cart(items, count). The backend calculates the item&#39;s price based on provided cart items and creates Paypal checkout(retrieve <strong>order id</strong>). Store the cart information and <strong>order id</strong> in the database. The backend returns the <strong>order id </strong>to the front end.</li><li>The PayPal form is displayed</li><li><strong>Customer selects payment method</strong>: The customer selects their preferred payment method, such as PayPal, credit card, or debit card. <strong>Customer reviews and confirms order</strong>: The customer reviews their order, including the items they are purchasing, the total cost, and the shipping information. They then confirm their order.<strong> Customer completes payment</strong>: The customer completes the payment process, either on the PayPal website or on the merchant’s website.</li><li>The merchant receives payment: Once the payment is complete, the merchant receives the payment from PayPal. The merchant can then process the order and ship the items to the customer.</li><li>“<em>Thanks for payment</em>” message, etc. can be displayed on the page.</li></ol><h3>Obtain Paypal App credentials</h3><p>To obtain PayPal app credentials, you will need to follow these steps:</p><ol><li>Go to the PayPal Developer website at <a href="https://developer.paypal.com/">https://developer.paypal.com/</a>.</li><li>Log in to your PayPal account.</li><li>Click on the “Dashboard” button in the top right corner of the page.</li><li>Select the “My Apps &amp; Credentials” option from the dropdown menu.</li><li>Click the “Create App” button.</li><li>Fill out the necessary information for your app, such as the App Name, App Description, and App Logo.</li><li>Once you have filled out all the necessary information, click the “Create App” button.</li><li>Your app credentials will now be displayed on the “My Apps &amp; Credentials” page. You will need to copy the “Client ID” and “Secret” values to use them in your application.</li></ol><p>☝️: Note that the specific steps may vary depending on the version of the PayPal Developer website and your account settings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6BclFnUERj8FfSaZXuA6Xg.png" /><figcaption>Getting application <strong>credentials</strong></figcaption></figure><h4>Paypal live VS Sandbox Account</h4><p>PayPal offers two types of accounts for developers: <strong>live </strong>and <strong>sandbox</strong>. The main difference between these two types of accounts is that the sandbox account is a testing environment, while the live account is the actual production environment.</p><p>A <strong>sandbox </strong>account allows developers to test their PayPal integrations without actually processing real transactions or charging real money. This is done using test credentials provided by PayPal that simulate a transaction and allow developers to see how their code interacts with the PayPal API.</p><p>On the other hand, a <strong>live </strong>account is used when real money is involved, and actual transactions are processed. This means that you will need to have a verified PayPal Business account and be approved by PayPal to use the live account.</p><p>In summary, the main difference between the two accounts is that the sandbox account is a testing environment used for development and testing purposes, while the live account is used for actual transactions and real money processing. It is important to use the appropriate account type for the appropriate purpose to avoid any unintended financial transactions or complications.</p><h3>Creating firebase functions</h3><p>🔗: You can follow the steps provided here or look at the <a href="https://firebase.google.com/docs/functions/get-started">official firebase functions guide</a></p><h4>🔧Set up a Firebase project</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/851/1*S5v_Ll6Ls4hhwlziDhVEAQ.png" /><figcaption>Creating a new project</figcaption></figure><p>If you haven’t already, you will need to create a Firebase project in the Firebase console and enable Firebase functions.</p><p>🔗: <a href="https://console.firebase.google.com/">Official firebase site</a></p><h4>💻Install the Firebase CLI</h4><p>The Firebase CLI is a command-line interface tool that you can use to create and manage Firebase functions. You can install the Firebase CLI by following the instructions in the Firebase documentation.</p><p>🔗: Documentation of installing firebase CLI on different platforms</p><h4>🚀Create a Firebase function</h4><p>Use the Firebase CLI to create a new Firebase function. You can do this by running the command firebase init functions in your project directory. This will create a new directory for your functions and set up the necessary files and dependencies.</p><h4>Add basic backend Paypal script</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59518bf28a84c97e3a812a8f40b9823b/href">https://medium.com/media/59518bf28a84c97e3a812a8f40b9823b/href</a></iframe><p>☝️: PAYPAL_CLIENT_SECRET will be stored in the Google Secret manager. More about Google Secret Manage read below. To use this secret function should provide runWith({ secrets: [PAYPAL_CLIENT_SECRET]}</p><p>☝️: Only PAYPAL_CLIENT_SECRET is secret information. CLIENT_ID and BASE_URL are environment variables and there is no threat if their data will be public.</p><p>🔗: More about <a href="https://firebase.google.com/docs/functions/config-env#params">configuring environment</a> read in the documentation.</p><p>☝️: In this code, we’ve added AppCheck verification. You can remove it, but for the production system, it’s better to configure it.</p><p>☝️: BASE_URL depends on whether you use a <em>sandbox </em>or <em>live</em> account:</p><pre>{<br>    &quot;sandbox&quot;: &quot;https://api-m.sandbox.paypal.com&quot;,<br>    &quot;production&quot;: &quot;https://api-m.paypal.com&quot;<br>}<br></pre><h4>🤫Extra: What is Google Secret manager?</h4><p>Google Secret Manager is a fully-managed secret management system provided by the Google Cloud Platform. It allows users to store and manage secrets such as API keys, passwords, certificates, and other sensitive data securely. With Secret Manager, users can store secrets separately from the application code and securely share them across different applications and services.</p><p>The main features of Google Secret Manager are:</p><ol><li>🔒Secure storage: Secret Manager stores secrets encrypted at rest using AES256-GCM encryption.</li><li>🔑Access controls: Access to secrets can be controlled by IAM (Identity and Access Management) policies and roles, allowing only authorized users and services to access the secrets.</li><li>📝Versioning and auditing: Secret Manager stores multiple versions of secrets and provides audit logs to track changes and access to the secrets.</li><li>☁️Integration with other Google Cloud services: Secret Manager can be integrated with other Google Cloud services such as Cloud Functions, App Engine, and Compute Engine, making it easy to use the secrets in your applications and services.</li></ol><p>Overall, Google Secret Manager provides a secure and convenient way to manage secrets in the cloud, allowing users to store and manage sensitive data separately from the application code and share it securely across different applications and services.</p><h4>🚢Deploy your function</h4><p>Once you have added your secret to your function, you can deploy it to Firebase by running the command firebase deploy --only functions in your project directory. This will upload your function to Firebase and make it available for use.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VY82NeT_juMhAJRjTURsIA.png" /><figcaption>The functions page after deploy</figcaption></figure><h3>Creating frontend part</h3><h4>Integrate a PayPal library</h4><p>PayPal gives you the ability to integrate script via:</p><ul><li><a href="https://developer.paypal.com/sdk/js/configuration/#link-javascriptsdkscriptconfiguration">use script tag</a></li><li><a href="https://developer.paypal.com/sdk/js/performance/#link-loadthesdkasamodule">importing module</a></li><li><a href="https://www.npmjs.com/package/@paypal/react-paypal-js">using ready React component</a></li></ul><p>Choose the best for your use case scenario. We’ll use react component strategy.</p><h4>Creating our payment page</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e92aba43d229b756810af1ffeb68d309/href">https://medium.com/media/e92aba43d229b756810af1ffeb68d309/href</a></iframe><p>☝️: Notice, we calculate the price for checkout on the backend. It’s a good practice not to trust a front end.</p><h4>Paypal Customization</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oocPEfqTBHZUrhzr-oLHPg.png" /><figcaption>Selecting a colour for <strong>PayPal buttons</strong></figcaption></figure><p>PayPal offers a variety of options for customizing the look and feel of their payment buttons to better match the design and branding of your website or application. Here are some of the ways you can customize PayPal buttons:</p><ol><li>💻Button Style: PayPal offers a variety of pre-built button styles that you can choose from, or you can create your own custom button style by specifying your own button image, colour, and font.</li><li>📏Button Size: You can choose from a range of button sizes to fit your website or application design.</li><li>🔄Button Behavior: You can customize the behaviour of your PayPal buttons, such as the payment flow and redirect URL, to better fit your workflow and user experience.</li><li>🌍Localization: PayPal supports over 25 languages and currencies, allowing you to customize your payment buttons to match your user’s language and currency preferences.</li></ol><p>To customize your PayPal buttons, you can use the PayPal Button Factory, a web-based tool that allows you to easily generate and customize PayPal payment buttons. You can also use the PayPal JavaScript SDK or the REST API to programmatically create and customize PayPal buttons in your application.</p><p>Customizing your PayPal buttons can help create a seamless user experience and build trust with your customers by providing a consistent and branded payment process.</p><p>🔗: About customizing buttons with style options <a href="https://developer.paypal.com/sdk/js/reference/#link-style">read here</a>.</p><h4>Looking at the built page</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XoLNj6Wjwhc7qglv72J52Q.png" /><figcaption>Built <strong>payment page</strong> with PayPal integration</figcaption></figure><p>In conclusion, integrating PayPal with Firebase Functions can enable developers to create seamless payment pages for their web or mobile applications. By following the steps outlined in this article, developers can leverage the power of Firebase Functions to process PayPal transactions and create a smooth user experience. Additionally, integrating PayPal can increase the flexibility and convenience of payment options for customers. As online transactions become increasingly prevalent, integrating PayPal with Firebase Functions can be a valuable addition to any application. So, don’t hesitate to take action and explore the possibilities of integrating PayPal with Firebase Functions to enhance your application’s payment capabilities.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=385e35f5f7ee" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>