<?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 Alin Velican on Medium]]></title>
        <description><![CDATA[Stories by Alin Velican on Medium]]></description>
        <link>https://medium.com/@alinvelican_49750?source=rss-8363b65d05b6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*HK6a2lNKr6Dqg0EDQAQ0cw@2x.jpeg</url>
            <title>Stories by Alin Velican on Medium</title>
            <link>https://medium.com/@alinvelican_49750?source=rss-8363b65d05b6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 19:22:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alinvelican_49750/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[How dependency injection works?]]></title>
            <link>https://medium.com/@alinvelican_49750/how-dependency-injection-works-72855dcd0fda?source=rss-8363b65d05b6------2</link>
            <guid isPermaLink="false">https://medium.com/p/72855dcd0fda</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Alin Velican]]></dc:creator>
            <pubDate>Mon, 11 May 2020 15:25:41 GMT</pubDate>
            <atom:updated>2020-05-25T22:31:04.223Z</atom:updated>
            <content:encoded><![CDATA[<p>What do we actually understand when we talk about a DI Framework? Examples with simple design patterns and the transition to Dependency Injection with Spring!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2AJkrdHhXzN5oHcr" /><figcaption>Photo by <a href="https://unsplash.com/@emilep?utm_source=medium&amp;utm_medium=referral">Émile Perron</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>Q</strong>: What is dependency injection? <strong>A</strong>: <strong>Dependency injection</strong> is a technique in which a service receives other services that it depends on. Given this simple definition, let’s see an example!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b8526c5c8b28284960b05980ad77cb8/href">https://medium.com/media/7b8526c5c8b28284960b05980ad77cb8/href</a></iframe><p>These are some classes and interface definition, we have 2 checkers, one for english and one for french which implement the simple <strong>Checker </strong>interface.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dadfffb40d3c8228b9dd15e01f69fc50/href">https://medium.com/media/dadfffb40d3c8228b9dd15e01f69fc50/href</a></iframe><p>The EmailService class depends on a <strong>checker </strong>in order<strong> </strong>to send the email.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16fbc5c73c9543769d8ab0033a6c739f/href">https://medium.com/media/16fbc5c73c9543769d8ab0033a6c739f/href</a></iframe><p>If we want to use our new email service, we just need to create a new object of EmailService, and call the send() method. All it is crystal clear, we don’t care about the <strong>Checker </strong>dependency, it is something that the service needs to handle, we just want to send our email.</p><p>What if we want to send an email in french language? Of course we need to use the <strong>FrenchChecker</strong>. But how can we change the <strong>checker </strong>dependency in our already created object. The only one solution is <strong>Reflection</strong>. Well, when you have a problem and want to use reflection to solve that problem, you will have two problems. So this is a clear signal that your design is wrong. Of course you can go and switch in the constructor of EmailService to FrenchChecker, but just think that you receive the EmailService library as a JAR, and can’t change the source code.</p><h4>What can we do?</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ebc05f295acb7547d13eca3eb3d080d2/href">https://medium.com/media/ebc05f295acb7547d13eca3eb3d080d2/href</a></iframe><p>As the developer of our new email service, we can change the constructor to receive its <strong>dependency </strong>at creation time, like here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/daefee8b5ba6cc68f9e940ff19b778c6/href">https://medium.com/media/daefee8b5ba6cc68f9e940ff19b778c6/href</a></iframe><p>So now our EmailService is flexible enough to receive any kind of Checker implementation, even new ones, created by the user of our small library. If you didn’t notice, we already made <strong>Dependency Injection. </strong>As the definition of DI said, we just injected by hand the <strong>checker </strong>which is the dependency of EmailService.</p><p>But what is the problem with this new design? We, as the clients of EmailService, need to know the internal dependencies of the email service. Just think about an extreme scenario of 100 dependencies which a service could depend on, and we, as users of that service, need to decide what dependencies to use. This is an obvious violation of the concept of encapsulation. We as simple users of a service, need to manually create all the dependencies. This is named the dependency graph, and we don’t have to care about them as clients of one service. So this problem should be resolved by someone else. Before Dependency Injection framework come to life, there was some way of managing this service creation and dependency injection. One of the first is the Factory design pattern.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09e8054fcb1acef9ff5fca4a6f613616/href">https://medium.com/media/09e8054fcb1acef9ff5fca4a6f613616/href</a></iframe><p>For this, as the developers of the EmailService, we make available for our clients, an easy way in which they can use our service, without the need for them to think about the internals of our service. If some people need a FrenchEmailer, they just need to call our Factory method <strong>newFrenchEmailer()</strong>, and that’s all, they don’t need to think about what Checker to use, or any other dependency in case our service depends on something else.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40a5dfa9fde84565d9520d79576fa32e/href">https://medium.com/media/40a5dfa9fde84565d9520d79576fa32e/href</a></iframe><p>Of course there are some problems with this approach, otherwise why we need a DI framework :)? The problem with this is the static form of creation, if for example, some clients want to develop a new Checker for Chinese language, they will need to create a new Factory method, but given that our library is distributed as a JAR, they can’t change our code without non-religious methods(like reflection). So the users, if want to extends our service, need to step back to the classical way of creating and managing dependency by hand.</p><p>Another, more complicated design pattern used for managing dependencies is <strong>Service Locator</strong>. This is a common design pattern in Java EE world, and could be considered as a parent of Dependency Injection Frameworks.</p><p>For this we need a Context class with a lookup method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/094ba694d559fd4bcbe8e3e29eb1421a/href">https://medium.com/media/094ba694d559fd4bcbe8e3e29eb1421a/href</a></iframe><p>We also need a ServiceCache with get and put methods.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9e3a3b01fed9d3697e14fa689a1340c3/href">https://medium.com/media/9e3a3b01fed9d3697e14fa689a1340c3/href</a></iframe><p>And finally the ServiceLocator class, which return a dependency from cache if it is present, and if not, it will create using the context lookup.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f4c066ff9ae304c7bcdd91567f48809/href">https://medium.com/media/3f4c066ff9ae304c7bcdd91567f48809/href</a></iframe><p>For using this approach, we need a reference to the ServiceLocator object, and we can use our service.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74cfb398c8066326cb428074d7e04c1e/href">https://medium.com/media/74cfb398c8066326cb428074d7e04c1e/href</a></iframe><p>Before I go to the disadvantages, I want to keep in mind some ideas: <strong>Context, Cache, </strong>and <strong>lookup. </strong>So the disadvantages: first of all, this seems like a Factory with a <strong>switch</strong> in place of explicitly creating a method for every case. This approach is more flexible, but the fact that we use String keys, put us at a risk of RunTime errors, compared with Factory approach where we call a method and we are protected by the compiler. But this flexibility comes with some advantages like: we can manage all dependency in a single place, we have Checkers in same place like EmailServices, and we also have a form of reutilization for our objects. Because we are talking about Service Objects, we don’t need a new object for every usage.</p><h4>Introducing Spring</h4><p>Before talking about Spring Framework, I want to have a discussion about what a framework means. A framework is a form of <strong>Inversion of Control. </strong>Think about a framework like a place where you can control nothing, you don’t have actually a main method, you don’t really know where your code starts and finish, you just have some places where you can put your code, and someone else uses that code when it consider so. Think about a web server, you don’t really manage or know about handling request, and about the fact that it will create a new thread for a new request or use one from the threads pool. You just have a Servlet interface, with a clear contract that you receive a request and return a response, and a place where you link that servlet to an URL endpoint. The discussion about <strong>IoC </strong>needs to be clear in terms of what controls are exactly taken from us.</p><p>Before going deeper, let’s see a live example of what Spring tastes like. Disclaimer: what I will show you in the next section, it is a legacy form of using Spring, but it is clearer to see this, than the new way, which is not really new nor that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99b3f657328dd12c43fe363e00bbbfbd/href">https://medium.com/media/99b3f657328dd12c43fe363e00bbbfbd/href</a></iframe><p>Yes, I know, it is XML. Some people used to live like that. You can ignore the first 7 lines, and let’s focus on &lt;bean&gt; tag. What is a <strong>bean</strong>? A bean is an object which is managed by Spring. Nothing more than that. You can see the declarative way in which we say to Spring that we want an EmailService, which depends on a checker, and the way we reference that dependency. So we declare 2 beans, the <strong>checker </strong>bean which will be created from EnglishChecker class, and the <strong>emailService </strong>which is created from EmailService class, and its dependency is injected through constructor. For using this we have to create the <strong>Context</strong>, remember this word from ServiceLocator discussion.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/197e7740d297a3a66079e966d3fe349e/href">https://medium.com/media/197e7740d297a3a66079e966d3fe349e/href</a></iframe><p>As you can see we create the Context based on spring.xml file, and after that we call getBean() method to get, well, our bean. We get our service in a similar fashion like in the ServiceLocator case, an unsafe way, using a String key which we declared in the .xml file, or using a secure way using the .class key, without a need for cast. Related to the IoC discussion, I want to make it crystal clear that we have no point in which we use the <strong>new</strong> operator as in the case of Factory, ServiceLocator or manual DI. So in this case the control which is taken from us, is the creation and linking of services and their dependencies. We have just declared what we need, and how should looks like the Dependency Graph, and the framework decides how and when should create that objects, meaning <strong>beans</strong>. For example you can see that in the xml file we first declared the EmailService, and after that the Checker. With this I want to make clear that we can’t influence the order of creating beans, by placing higher in the declaration file, it is up to Spring to decide when to create them based on the dependency graph.</p><p>I told you in the beginning of introducing Spring, that this way of using XML, it is a legacy one. After Java 5 introduced the Annotation feature, things took a good turn in the development and usage of Spring, and together with Java Configuration we were able to completely get rid of XML files. So let’s see how it works now.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89e911330b33cf093c8d951c68ec27c2/href">https://medium.com/media/89e911330b33cf093c8d951c68ec27c2/href</a></iframe><p>First of all we need to declare our beans. @Service Annotation which is similar with @Component Annotation tells Spring that this should be managed by itself. So this is equivalent with declaring a &lt;bean&gt; tag in XML.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e1de329407b9e59949713c0cd9d87a0/href">https://medium.com/media/4e1de329407b9e59949713c0cd9d87a0/href</a></iframe><p>The EmailService as you know is dependent by a checker, but earlier we declared 2 checkers with @Service Annotation, so we need to be explicit when we inject in the constructor, which one we need, english or french one and for this we use the @Qualifier Annotation. We can use the <em>@Conditional Annotation </em>for selecting in a more dynamic way which one to use, but I don’t want to go in more details. For this annotation to means something for Spring we need to declare an empty class with @Configuration and @ComponentScan Annotations, and create the Context.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44dc86261a18b2028a4da480efca1a01/href">https://medium.com/media/44dc86261a18b2028a4da480efca1a01/href</a></iframe><h4>Final words</h4><p>What I want to be clear is the way in which Spring manage the creation of beans and components, and how this is taken from us, because of the idea of Inversion of Control. Using this standard way for getting all our service and dependencies, let us concentrate on business development, not on linking services and managing dependencies. I showed you some manual dependency management with some well known design patterns, and their advantages and disadvantages, and some basic usage of Spring. This article is not meant to learn you basic of Spring, is meant to give a strong knowledge about Dependency Injection idea.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=72855dcd0fda" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Go vs Java — JSON serialization and deserialization]]></title>
            <link>https://medium.com/@alinvelican_49750/go-vs-java-json-serialization-and-deserialization-feeb18803525?source=rss-8363b65d05b6------2</link>
            <guid isPermaLink="false">https://medium.com/p/feeb18803525</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Alin Velican]]></dc:creator>
            <pubDate>Sat, 28 Dec 2019 00:52:23 GMT</pubDate>
            <atom:updated>2019-12-28T00:52:23.371Z</atom:updated>
            <content:encoded><![CDATA[<h3>Go vs Java — JSON serialization and deserialization</h3><p>I know there is no link between Go, Java and cats, but who doesn’t like cats? :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uxFZ07AuX226YotG" /><figcaption>Photo by <a href="https://unsplash.com/@calypso999?utm_source=medium&amp;utm_medium=referral">Raul Varzar</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>When somebody wants to develop a new web application, no matter what that application will do, for sure there will be a layer where you will need to deal with JSON serialization and deserialization. No matter what your business requirements are, you for sure will need to receive some data, understand it, do your magic, and after that send back. For this, you need to deserialize, apply your business rules, serialize and send back. So for most of the time, your application will do this work, even if you don’t really see it because every language or framework try to hide this from you.</p><h4>What are we going to do?</h4><p>For this test we are going to test just this small part of a web application. So we will just read some JSON, deserialize it and serialize it. There will be no read from files to not involve any other moving parts. Just JSON part. We will KISS.</p><h4>Unexpected results</h4><p>When I started this test, I admit that I started just to see how good Go will do, and how poor Java is, but when I actually run the code, I was shocked! Not really shocked, but surprised for sure. So let’s see what happend!</p><h4>Java Code</h4><p>I started with Java deserialization. I have a simple Java class with few fields and the Main class where deserialization happens. For this I use Jackson library.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd5a0a53848ec023e75cd9d18d1a5271/href">https://medium.com/media/fd5a0a53848ec023e75cd9d18d1a5271/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51c28760bc8b32068682f7dc8430b8af/href">https://medium.com/media/51c28760bc8b32068682f7dc8430b8af/href</a></iframe><p>And results: <strong>7.096s</strong> for <strong>10000000</strong> iterations</p><h4>Go code</h4><p>I have a similar Go struct and main func</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6c17f91054f72ccb261b76be4a01b84/href">https://medium.com/media/f6c17f91054f72ccb261b76be4a01b84/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb106ddac9c3b48b15e432851c2430a2/href">https://medium.com/media/eb106ddac9c3b48b15e432851c2430a2/href</a></iframe><p>And results for Go: <strong>34.607s</strong> for <strong>10000000</strong>(same as for Java)<strong> </strong>iterations. Again, unexpected results, seems that Go standard library for JSON is about 5 times slower than Jackson library in Java.</p><h3>Serialization test</h3><p>For <strong>Java</strong> I have the following code</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43572a43359f70d78ed1f3e2e962d055/href">https://medium.com/media/43572a43359f70d78ed1f3e2e962d055/href</a></iframe><p>And results: <strong>3.582s</strong> for <strong>10000000</strong> iterations</p><p>For Go:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/33543becae62ce02052ea36152d2e6a8/href">https://medium.com/media/33543becae62ce02052ea36152d2e6a8/href</a></iframe><p>And results <strong>8.428s </strong>for <strong>10000000 </strong>iterations. There is still a big difference between Java and Go, but Go seems to be recovering.</p><h4>EasyJson</h4><p>Intrigued by the fact that my expectations was unmet, I started to find out what can be improved with Go. After all Go it is a compiled language and at least in theory should be faster. In fact it is a known problem with the poor performance of Go standard Json library. The main problem is that <strong>encoding/json </strong>library use reflexion, and because of this we have this problem. I find out that there are actually a lot of 3rd party library which deal with Json in Go, and one of the best known is <strong>easyjson. </strong>Because this one use no reflection, we need to work with generated code. For a detailed tutorial for easyjson you can take a look here: <a href="https://github.com/mailru/easyjson">https://github.com/mailru/easyjson</a>. After I install and generate the code, the only one modification was to change json.Marshal with easyjson.Marshal(or Unmarshal)</p><p>New results: For <strong>serialization</strong> now I get <strong>3.513s </strong>while with standard library I get <strong>8.428s. </strong>This result is similar with Java one where I get <strong>3.582s. </strong>For <strong>deserialization </strong>with <strong>easyjson </strong>I get <strong>12.354s </strong>while with standard library I get <strong>34.607s. </strong>Still weaker than with <strong>Java </strong>where for same thing it takes just <strong>7.096s.</strong></p><p>But Java can do even better if you use <strong>jsoniter</strong> with <strong>dynamic code generation. </strong>For more details take a look here <a href="http://jsoniter.com/java-features.html">http://jsoniter.com/java-features.html</a> The main point is that you need to have all fields public and you have to use <strong>javassist. </strong>This is the new code</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dedd13adea4938e334cc00767fa73c9f/href">https://medium.com/media/dedd13adea4938e334cc00767fa73c9f/href</a></iframe><p>And results <strong>4.750s. </strong>About 3 times faster than <strong>easyjson </strong>Go version.</p><h4>Final words</h4><p>Even though from that test, an extreme one actually, because 10.000.000 iterations for same thing it is not a real world use case. For example if I run the test for just 100.000, <strong>Go </strong>version is twice faster, and for smaller number of iterations Go version is even faster. This is happening probably because of JVM cold start-up. The reason for Java getting so fast after a huge number of repetitions is because of its magic JIT optimisations. Of course we have to look also in the memory consumptions. For Java we have 830MB of RAM while working and 24 threads.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*IY3DYysXtGKOdpMmrpWLCA.png" /></figure><p>And for Go we have only 5.7MB</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*zUB70m8I8h0v3e2tY_Z9_Q.png" /></figure><p>In term of performance this are also very important numbers. And even maybe Go for this specific task is 3 times slower, it takes 150 times lower memory.</p><p>If you want to see more comparisons between Java and Go take a look in my others stories:</p><ul><li><a href="https://medium.com/@alinvelican_49750/go-vs-java-27e60527f72c">Go vs Java.</a></li><li><a href="https://medium.com/@alinvelican_49750/go-vs-swift-vs-spring-boot-vs-spring-webflux-c9b396203bbc">Go vs Swift vs Spring Boot vs Spring WebFlux</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=feeb18803525" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Go vs Java.]]></title>
            <link>https://medium.com/@alinvelican_49750/go-vs-java-27e60527f72c?source=rss-8363b65d05b6------2</link>
            <guid isPermaLink="false">https://medium.com/p/27e60527f72c</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Alin Velican]]></dc:creator>
            <pubDate>Sat, 23 Nov 2019 03:03:06 GMT</pubDate>
            <atom:updated>2019-12-07T12:42:06.640Z</atom:updated>
            <content:encoded><![CDATA[<h4>Reading from file and counting words using a map.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QExDcLPh2OxSxNmV" /><figcaption>Photo by <a href="https://unsplash.com/@ryanquintal?utm_source=medium&amp;utm_medium=referral">Ryan Quintal</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>I assume that everyone know what <strong>Java</strong> is. <strong>Go</strong> is becoming more and more popular, is the de facto cloud language and this is happening because it has a lot of advantages. One of them is performance, due to the fact that it is a compiled language. Let’s find out what that really means in numbers!</p><h3>What I measured</h3><p>For this benchmark I used a simple program which read from a big text file and count the number of occurrences for every word using a map. This is the code for Java. The text file has something like 4.5 millions lines which means <strong>182MB</strong>.This is the <strong>Java</strong> code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce1e3feb99b0bf20b8d5b2b891194685/href">https://medium.com/media/ce1e3feb99b0bf20b8d5b2b891194685/href</a></iframe><p>This is the <strong>Go</strong> code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9ada8d34fa5b43c3260f01975da9044/href">https://medium.com/media/a9ada8d34fa5b43c3260f01975da9044/href</a></iframe><p>And results For <strong>Java </strong>I get this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1020/1*ImL4pjSwIMLWxJxVA4tjUQ.png" /></figure><p>For <strong>Go:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/1*Tyhi3fnYJK96FZNt1R6mVg.png" /></figure><h4>Comments</h4><p>So the Go is 2 time faster than Java echivalent, and also has a more elegant solution.</p><h4>Some improvements</h4><p>For <strong>Java </strong>variant I made some improvements in code in order to get a better performance, and I don’t use anymore split() function. In place of it I use substring(). This is the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d84bccdf63c805681a628dca98ce822/href">https://medium.com/media/1d84bccdf63c805681a628dca98ce822/href</a></iframe><p>For this I get better results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q2Dil_OCCuHFd4l4lFNpkw.png" /></figure><h4>Comments</h4><p>Compared with <strong>Go </strong>version it is still ~50% slower, but not bad.</p><h4>Memory usage:</h4><p>Another important aspect related to performance is memory consumption. For this I used the <strong>/usr/bin/time -l </strong>from MacOS. Let’s see the results:</p><p>For <strong>Java </strong>I get over <strong>1GB </strong>maximum memory usage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/930/1*K7IeBlPd5-eTSn2gI3QgJg.png" /></figure><p>For <strong>Go </strong>I get <strong>7MB </strong>maximum memory usage</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*jm399nk2IiCqzR_lNTrUHA.png" /></figure><p>Looking through this perspective, <strong>Go </strong>seems a far better choice.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27e60527f72c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Go vs Swift vs Spring Boot vs Spring WebFlux]]></title>
            <link>https://medium.com/@alinvelican_49750/go-vs-swift-vs-spring-boot-vs-spring-webflux-c9b396203bbc?source=rss-8363b65d05b6------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9b396203bbc</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Alin Velican]]></dc:creator>
            <pubDate>Sun, 08 Sep 2019 23:23:32 GMT</pubDate>
            <atom:updated>2019-09-08T23:23:32.755Z</atom:updated>
            <content:encoded><![CDATA[<p>Comparing the new with the old.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*g0gG4LZpfu8ZZK92" /><figcaption>Photo by <a href="https://unsplash.com/@pankajpatel?utm_source=medium&amp;utm_medium=referral">Pankaj Patel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><em>Bored of the daily Java, I thought I’d try something new last week. So I started researching the market for new and cool languages so I came to Swift for the beginning. Excited about the fact that I could develop applications for iOS I started my adventure with Swift. Long story short, I did not develop any application and got to read about some Swift backend frameworks(Vapor and Perfect). After that, I got to compare them with Node JS, Spring and Go.</em></p><h3>What I tested</h3><p>For each of them I made the minimum application every framework provided in documentation and I tested with Bombardier. This is the command I run for every application <strong>./bombardier -c 125 -n 2500000 localhost:8080/test. </strong>So 125 concurrent clients for <strong>2500000 </strong>requests.</p><p><a href="https://github.com/codesenberg/bombardier">codesenberg/bombardier</a></p><h3>Swift</h3><p>The most popular backend frameworks in Swift world are Vapor and Perfect. So I went to their <strong>Getting Started </strong>pages and I put them to work.</p><p>For <strong>Perfect</strong> this is the Getting Started page:</p><p><a href="https://perfect.org/docs/gettingStarted.html">Documentation | Perfect.org</a></p><p>And for <strong>Vapor</strong>:</p><p><a href="https://docs.vapor.codes/3.0/getting-started/hello-world/">Hello, world - Vapor Docs</a></p><h4><strong>And the results:</strong></h4><p><strong>Vapor: </strong>Reqs/sec 13701.82</p><p><strong>Perfect:</strong> Reqs/sec <strong>41246.18. </strong>It seems that Perfect has performed better than Vapor by far.</p><h3>Node.JS</h3><p>For Node.JS part I used the well known ExpressJS framework:</p><p><a href="https://expressjs.com/en/starter/hello-world.html">Express</a></p><h4>And the results:</h4><p><strong>Express: </strong>Reqs/sec <strong>20178.73. </strong>Neither good nor bad. Better than <strong>Vapor</strong>, but weaker than <strong>Perfect</strong>. The fact that Swift is a <strong>compiled</strong> language said his word.</p><h3>Go</h3><p>I had big expectations from Go, and it didn’t disappoint me. For Go I used the <strong>net/http</strong> module and this is the simple cod I run:</p><pre>package main</pre><pre>import (<br> “fmt”<br> “net/http”<br>)</pre><pre>func main() {<br> http.HandleFunc(“/test”, HelloServer)<br> http.ListenAndServe(“:8080”, nil)<br>}</pre><pre>func HelloServer(w http.ResponseWriter, r *http.Request) {<br> fmt.Fprintf(w, “Hello, %s!”, r.URL.Path[1:])<br>}</pre><h4>And the results:</h4><p><strong>Go</strong>: Reqs/sec <strong>56270.09. </strong>Compared with <strong>Perfect</strong> results<strong> </strong>Reqs/sec <strong>41246.18 </strong>seems that Go is faster this time.</p><h3>Java — Spring</h3><p>For Java I used the everlasting Spring Framework, Spring Boot in fact and Spring Rest more specific. Honestly, I had no expectations, only to be over Node.JS but under Go and Swift. The fact that Spring runs in Java and Java is not a pure compiled language it will definitely hang hard.</p><h4>And the results:</h4><p>Because it is Java, we need first to ramp up the JVM:</p><p><strong>Spring</strong> — first run: Reqs/sec <strong>22573.60 — </strong>close to Node (<strong>20178.73)</strong></p><p><strong>Spring</strong> — second run: Reqs/sec <strong>31517.60 — </strong>better than Node<strong> </strong>but weaker than Go and Swift as expected.</p><h3>One more thing</h3><p>In Spring world these days it is very popular a new framework: <strong>Spring Web Flux, </strong>or Web on Reactive Stack as they call it. It is a non-blocking framework which use Netty under the hood instead of the old Servlet.</p><p><a href="https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html">Web on Reactive Stack</a></p><p>I gave it a chance without any expectations but I was very surprised. No. I lied. I was <strong>shocked</strong>. Before I will show you the results, I only want to remember the best result:</p><p><strong>Go</strong>: Reqs/sec <strong>56270.09. </strong>And now, prepare yourself:</p><h4>And the results:</h4><p><strong>Spring Web Flux: </strong>Reqs/sec <strong><em>66734.35</em></strong></p><p>This result exceeded all my expectations by far. I never thought Java could beat a compiled language. It seems that the guys from Netty made a great job. By the way, Netty is made by Apple.</p><p><a href="https://netty.io/">Netty: Home</a></p><h3>Final thoughts</h3><p>Even though Java is not so cool these days, I think there are still amazing things to learn about.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9b396203bbc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Java. Where to go after you learned basics.]]></title>
            <link>https://medium.com/@alinvelican_49750/java-where-to-go-after-you-learned-basics-672e81e4c127?source=rss-8363b65d05b6------2</link>
            <guid isPermaLink="false">https://medium.com/p/672e81e4c127</guid>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Alin Velican]]></dc:creator>
            <pubDate>Sun, 11 Aug 2019 22:08:27 GMT</pubDate>
            <atom:updated>2019-08-13T01:42:49.825Z</atom:updated>
            <content:encoded><![CDATA[<h4>Make some sense in the huge Java ecosystem.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*pPhbOWXwn06AiQcjJuskjw@2x.jpeg" /></figure><p>Let’s say you already learned the basic stuff about OOP and Java. You already know what a class or an object is. Now you want to make some important things with all things you just find out about. In real world, Java is mostly used for back end of web applications. For the most part of your tasks you will get some data from a database, a SQL one or a NoSQL one, or you will call some external services. After that you will make some business processing and you will send this data to new consumers or directly to a front-end client. In both cases your application will act as a REST service. That’s where Spring Framework comes to rescue.</p><h4>Spring Framework</h4><p>A lot can be said about Spring Framework, but for now we’ll only discuss about the most used parts from this great framework.</p><h4>Spring Boot</h4><p>Because Spring is a huge framework, something needed to be developed to make it easier to work with this. So because of that, Spring Boot appeared. It provides some starter packages for ones of the most use cases.</p><h4>REST API</h4><p>One of the most common task is to expose a service through a REST API. This can be achieved using Spring Web MVC. For using this we need a Spring Boot starter-web which contains all necessary libraries for exposing an application as a REST API.</p><p>For more details you can follow the official exemple which is as comprehensive as you need.</p><p><a href="https://spring.io/guides/gs/rest-service/">Building a RESTful Web Service</a></p><h4>Spring Data</h4><p>Another important part of your day to day job as a Java developer is to get data from a database. Even if your application works with a SQL or NoSQL database, Spring Data can save you a lot of time. This framework has a lot of abstractions over database drivers so you can work with any database in same consistent way.</p><p>If you want to try out, the best source is the official guide for Spring Data.</p><p><a href="https://spring.io/guides/gs/accessing-data-jpa/">Accessing Data with JPA</a></p><p>If you want to combine the REST part with data part, you can take a look at the following link. There you will see a complete example with a real database which serve data thought a REST API.</p><p><a href="https://spring.io/guides/gs/accessing-data-mysql/">Getting Started | Accessing data with MySQL</a></p><h4>Final thoughts</h4><p>If you manage to make these things work, you will become quite productive in the new language you have learned, succeeding to do some pretty cool things. Of course there is a lot about Java and Spring. Spring is a huge framework which can make things you can’t even imagine. And in Java there is a lot of frameworks like Spring, but this one is the most popular and the most compreshive of all.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=672e81e4c127" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kubernetes, from zero to hero!]]></title>
            <link>https://medium.com/@alinvelican_49750/kubernetes-from-zero-to-hero-e8b1cbff1dc7?source=rss-8363b65d05b6------2</link>
            <guid isPermaLink="false">https://medium.com/p/e8b1cbff1dc7</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Alin Velican]]></dc:creator>
            <pubDate>Sat, 10 Aug 2019 17:49:53 GMT</pubDate>
            <atom:updated>2019-08-14T06:44:57.782Z</atom:updated>
            <content:encoded><![CDATA[<h4>Kubernetes in everybody’s language!</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pNhNBM2BQXMUcKyV9CcSJg@2x.jpeg" /></figure><ul><li><strong>Pod</strong></li><li><strong>Replication Controller</strong></li><li><strong>Services</strong></li><li><strong>Deployment</strong></li></ul><p>Kubernetes begins when you already have a Docker image in a Docker registry like Docker Hub.</p><p>So for this we suppose that we already have an application in Docker. For example you can follow this link:</p><p><a href="https://link.medium.com/CMcxqMZJ2Y">How to put your Java application into Docker container</a></p><h4>Pod</h4><p>The most important part of Kubernetes is a pod. It encapsulates one or more containers in a high-level structure. Although a pod can contains more containers, is recommended to keep only one container in a pod. In day to day work with Kubernetes we don’t release a pod itself. For managing pods we use a higher structure for exemple a Replication Controller or a Deployment. We’ll see later how these works, but for now we only create a plain pod. For this we have to create this file.</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: my-app<br>  labels:<br>    run: my-app<br>spec:<br>  containers:<br>  - image: alinvelican/serverjava<br>    name: my-app<br>    ports:<br>    - containerPort: 8080<br>      protocol: TCP</pre><p>And then <strong>kubectl apply -f simple-pod.yaml</strong></p><p>Now if we run <strong>kubectl get pods,</strong> we can see our pod. In <strong>spec</strong> part we can see <strong>containers</strong>, but like we said we only create pods with only one container. In the image part you have to put your Docker image. If you don’t have one you can use mine, so let it like that. <strong>Labels</strong> part is very important and we’ll see why in the next section.</p><h3>Replication Controller</h3><p>The problem with this pod is that if for some reasons the pod fail, there is nobody to restart it. So in order to manage better this kind of situation and to automate more and more of our infrastructure we need a higher Kubernetes structure. This is where <strong>Replication Controller</strong> come on the stage. Firstly we create this file and then we see what we get.</p><pre>apiVersion: v1<br>kind: ReplicationController<br>metadata:<br>  name: my-app<br>spec:<br>  replicas: 3<br>  selector:<br>    run: my-app<br>template:<br>  metadata:<br>    labels:<br>      run: my-app<br>  spec:<br>    containers:<br>    - name: my-app<br>      image: alinvelican/serverjava<br>      ports:<br>      - containerPort: 8080</pre><p>Now <strong>kubectl create -f apply simple-rc.yaml </strong>and<strong> kubectl get pods. </strong>We can see that now we have 3 pods because we declared that we need 3 replicas.</p><p>There is two important parts. First we have a <strong>spec</strong> part where we declare how many replicas we need and a <strong>selector</strong>. This selector is very important because this is where we link our pods with this <strong>RC. </strong>In selector part we need a label match. Those labels we saw earlier. <strong>Template</strong> part is where we effectively declare how Kubernetes will create our pods in case there is no pods already which match the selector. Now if we delete one of the pods with <strong>kubectl delete pod …</strong><em> (one of the pod you saw when you run kubectl get pods) </em>we’ll see that a new one will be recreated. This is the main feature of a <strong>RC. </strong>To show you that a <strong>RC</strong> is linked with pods just because of <strong>labes </strong>we change the label of one pod and we see that another one we’ll be released.</p><p><strong>kubectl label pod … </strong><em>(one of the pod you saw when you run kubectl get pods) </em><strong>run=my-app2</strong></p><p>Now that the label of this pod changed, the <strong>RC</strong> notice the fact that a pod exit from its incidence and release a new one for maintain the state asked in <strong>spec</strong> part.</p><p>The numbers of replicas can be changed in case of traffic increase for exemple. We only need to apply this command <strong>kubectl scale rc my-rc – –replicas=10 </strong>and we’ll see how Kubernetes will start new pods to achieve the number of replicas asked. This can be made automatically and we’ll see how lately.</p><h4>Services</h4><p>Now that we released our application we want to access it from the internet. By default in Kubernetes a pod it’s accessible only from the cluster. To make it available outside the cluster, we need a <strong>Service</strong> Kubernetes object. If you play with Kubernetes locally in <strong>minikube</strong> you won’t be able to access it from Internet because minikube can’t provide you a public IP, but if you use Kubernetes from a cloud provider you will have access from anywhere. For my exemple I use Kubernetes from Google Kubernetes Engine (GKE) but this is applicable for any cloud providers. So for using a Service we have to create the next file and <strong>kubectl apply -f simple-svc.yaml</strong></p><pre>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: my-app<br>spec:<br>  ports:<br>  - port: 80<br>    targetPort: 8080<br>  selector:<br>    run: my-app<br>  type: LoadBalancer</pre><p>Now that we already have our application deployed on Kubernetes and it’s accessible from Internet, now we can think for new improvements. To take advantage of our automated infrastructure we have to use a new Kubernetes structure which allows us to deploy our new version of application in an automated, safe and fast way. For this we have to use a <strong>Deployment</strong> object firstly for deploy our initial application. So for moment we have to delete our old <strong>RC</strong> with <strong>kubectl delete rc … </strong>and then create the following file.</p><h4>kubectl apply -f simple-depl.yaml</h4><pre>apiVersion: apps/v1beta1<br>kind: Deployment<br>metadata:<br>  name: my-app<br>spec:<br>  replicas: 3<br>  template:<br>    metadata:<br>      name: my-app<br>      labels:<br>        run: my-app<br>    spec:<br>      containers:<br>      - image: alinvelican/serverjava<br>        name: my-app</pre><p>For moment nothing changed, because Deployment it’s a higher Kubernetes object which create a <strong>RC. </strong>So for now we are still at a RC level. But besides the RC which our Deployment just created there is another important part. To see it we have to update our application and make a new Docker image with <strong>v2</strong> tag. If you want to skip this part you can use mine. So let’s see in action what our Deployment can do.</p><p><em>kubectl set image deployment my-app my-app=alinvelican/serverjava:v2</em></p><p>If you run kubectl get pods you will see that a new pod is created and a old one is terminating. This is a rolling update. In this way we achieve a deploy of our new application with zero down-time. There is another ways and a lot of discussion about release patterns. For exemple we can make a blue-green release with help of two Deployment objects or a canary release with Istio.</p><h4>Final thoughts</h4><p>This is a short intro into the huge universe of Kubernetes.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e8b1cbff1dc7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>