Blog

Blog je mesto gde možeš da čitaš o navikama IT-evaca, najavama IT dešavanja, aktuelnostima na tržištu, savetima i cakama kako da uspeš na ovom dinamičnom polju.
Mi pratimo trendove, na tebi je da se zavališ u fotelju i čitaš :)

Tag: REST (34 rezultata)
17.04.2024. ·
5 min

Node.js Lambda Package Optimization: Decrease Size and Increase Performance Using ES Modules

This article explains how to optimize Node.js AWS Lambda functions packaged in ES module format. It also shows an example with bundling and AWS CDK, as well as results for gained performance improvement. Node.js has two formats for organizing and packaging the code: CommonJS (CJS) — legacy, slower, larger, and ES modules (ESM) — modern, faster, smaller. CJS is still a default module system, and sometimes the only supported option for some tools. Let’s say you have a Node.js project, but you haven’t bothered with this before. You may now ask yourself — in which format is my code packaged? Let’s look at some JavaScript code examples: In JavaScript, it is clear by just looking into the code. But in TypeScript, you may find yourself writing code in ESM syntax, but using CJS in runtime! This happens if TypeScript compiler is configured to produce CommonJS output format. Compiler settings can be adjusted in tsconfig.json file and we will show how to avoid CJS output with an example later. There are two ways for Node.js to determine package format. The first way is to look up for the nearest package.json file and its type property. We can set it to module if we want to treat all .js files in the project as ES modules. We can also omit type property or put it to commonjs, if we want to have code packaged in CommonJS format. The second way to configure this is using file extensions. Files ending with .mjs (for ES modules) or .cjs (for CommonJS) will override package.json type and force the specified format. Files ending with just .js will inherit chosen package format. ES modules So how exactly can ESM help us improve Lambda performance? ES modules support features like static code analysis and tree shaking, which means it’s possible to optimize code before the runtime. This can help to eliminate dead code and remove not needed dependencies, which reduces the package size. You can benefit from this in terms of cold start latency. Function size impacts the time needed to load the Lambda, so we want to reduce it as much as possible. Lambda functions support ES modules from Node.js 14.x runtime. Example Let’s take one simple TypeScript project as an example, to show what we need to configure to declare a project as an ES module. We will add just couple of dependencies including aws-sdk for DynamoDB, Logger from Lambda Powertools and Lambda type definitions. The type field in package.json defines the package format for Node.js. We are using module value to target ES modules. The module property in tsconfig.json sets the output format for TypeScript compiler. In this case, ES2022 value says that we are compiling our code to one of the versions of ES modules for JavaScript. You can find additional info for compiler settings on https://www.typescriptlang.org/tsconfig. Bundling To simplify deploy and runtime process, you can use a tool called bundler to combine your application code and dependencies into a single JavaScript file. This procedure is used in frontend applications and browsers, but it’s handy for Lambda as well. Bundlers are also able to use previously mentioned ES modules features, which is the reason why they are important part of this optimization. Some of the popular ones are: esbuild, webpack, rollup, etc. AWS CDK If you’re using CDK to create your cloud infrastructure, good news is that built-in NodejsFunction construct uses esbuild under the hood. It also allows you to configure bundler properties, so you can parametrize the process for your needs. With these settings, bundler will prioritize ES module version of dependencies over CommonJS. But not all 3rd party libraries have a support for ES modules, so in those cases we must use their CommonJS version. ➤ What’s important to mention is that if you have an existing CommonJS project, you can keep it as is and still make use of this improvement. The only thing you need to add is mainFields property in CDK bundling section, which will set the format order when resolving a package. This might help you if you have some troubles switching the project completely over to ES modules. Let’s use a simple function that connects to DynamoDB as an example. Its job is just to read a record from a database. We will create two Lambda functions with this same code. One using the CDK example above, and the other one using the same CDK but without ESM bundling properties. It is just to have separate functions in CommonJS and ES modules so it’s easier to compare them. Here is a bundling output during CDK deploy with esbuild: You can see that ESM version of the function has package size reduced by almost 50%! Source maps file (.map) is also smaller now. esbuild provides a page for visualizing the contents of your bundle through several charts, which helps you understand what your package consists of. It is available here: https://esbuild.github.io/analyze. Here is how it looks like for our test functions: In this case, CommonJS package is improved by bundler only by minifying the code, which got it down to 500kb. Packages under @aws-sdk take up more than half of the package. But with using ES module — first approach when bundling, the package size goes down even further. As you can see, there is still some code in CJS format as some dependencies are only available as CommonJS. Performance results Let’s see now how much improvement is made by comparing cold start latency between ES module and CommonJS version of the function. Small load test with up to 10 concurrent users was executed to obtain the metrics. Below are visualized results using CloudWatch Logs Insights. CommonJS ES modules Numbers above are in milliseconds, so in average we reduced cold start duration by 50+ms, or 17%. Bigger difference is for minimum latency, which was shorter for almost 70ms, or 26%. These are not drastic differences, but from my experience with real-world projects — package size can go down like 10x, and cold start latency by even 300–400ms. Conclusion The improvement from using ES modules can be seen even in the simple example above. How much you can lower cold start latency depends on how big your function is and if it needs a lot of dependencies to do its job. But that’s the way it should be, right? For example, for simple functions that just send a message to SQS/SNS and similar, we don’t need dependencies from the rest of the app — like database or Redis client, which might be heavy. And sometimes shared code ends up all over the place. Even if the improvement in your case is not that big, it still might be worth considering using ESM. Just be aware, some tools and frameworks still have bad or no support for ESM. In the end, why would you want to pack and deploy the code you won’t use, anyway? 😄 Author: Marko Jevtović Software Developer @Levi9Serbia

23.08.2023. ·
3 min

Kotlin - Češki proizvod sa preporukom Gugla

Programski jezik „Kotlin“ osmišljen je tako da bude pristupačan i lagan za učenje, sa akcentom na izbegavanju nekih uobičajenih problema, koji su „nervirali“ Java programere, ali, sa druge strane, 100% je kompatibilan sa „Javom“, te se bez problema može kombinovati sa postojećim „Java“ projektima i, takođe, postojeći „Java“ kod se može pozvati iz „Kotlina“. Razvila ga je poznata softverska kompanija iz Češke, „JetBrains“, a posebno zanimljivo je to da je sam naziv „Kotlin“ nastao po istoimenom ostrvu u Finskom zalivu, koje se nalazi 20 km zapadno od Sankt Peterburga. Sa razvojem su počeli još 2010. godine, ali prva zvanična, stabilna verzija Kotlin 1.0 objavljena je tek 15. februara 2016. godine. Cilj je bio da se stvori savremen programski jezik, koji bi, kao što smo već napomenuli, bio kompatibilan sa postojećim Java kodom, ali sa unapređenom produktivnošću programera. Razvojni tim, na čelu sa Andrejom Breslavom, želeo je to da ostvari poboljšanim funkcijama i sintaksom, koji bi olakšali programiranje. Alternativa Javi Tako se desilo da je 2012. godine „Kotlin“  ponuđen kao open-source, što je svakako doprinelo njegovoj popularnosti, ali najznačajniji trenutak za „Kotlin“ dogodio se 2017., kada ga je „Gugl“ zvanično podržao i preporučio kao alternativni programski jezik za razvoj Android aplikacija. Naravno, alternativa Javi. Jedna od glavnih karakteristika „Kotlina“ je njegova konciznost. Ovo omogućava programerima da napišu manje linija koda, a da opet ostvare isti rezultat kao da koriste Javu. Kraći i čistiji kod postaje posebno koristan u razvoju naprednih aplikacija, jer omogućava lakše korišćenje funkcijskih i reaktivnih paradigmi. Kroz upotrebu Kotlin sintakse, poput „nullable“ tipova, „extension“ funkcija i „Smart Casts“, programeri mogu smanjiti broj grešaka i poboljšati produktivnost. Odlično se kombinuje sa velikim brojem alatki Važno je napomenuti da „Kotlin“ ima potpunu integraciju sa „Android Studiom“, glavnim razvojnim okruženjem za Android aplikacije. To znači da kad započnete sa razvojem aplikacije, „Android Studio“ će pružiti punu podršku za „inteligentno ispravljanje grešaka“ i „automatsko generisanje koda“. Za kreiranje naprednih aplikacija, „Kotlin“ se odlično kombinuje sa velikim brojem biblioteka i alatki. Među njima su „Dagger“ (dependency injection), „Retrofit“ (za rad sa REST API-ma), „Room“ (lokalna baza podataka), „RxJava“ (reaktivno programiranje) i drugi. „Kotlin“ je, takođe, savršen jezik za primenu savremenih arhitekturnih obrazaca, poput „Model-View-Viewmodel“(MVVM) ili „Clean“ arhitekture. MVVM je popularan zbog razdvajanja poslovne logike od korisničkog interfejsa, dok „Clean“ arhitektura olakšava testiranje i održavanje koda. Savršen izbor za Android developere Neke od naprednih funkcija koje imate na raspolaganju su: - Ekstenzije: Možete da definišete ekstenzije i tako proširite funkcije postojećih Android klasa, pa ćete, pored bolje organizacije, dobiti i znatno pojednostavljen kod. -„Inline“ funkcije: One služe da poboljšaju performanse i smanje opterećenje memorije, posebno u slučaju ponovljenih poziva funkcija. - „Lambda“ izrazi: Ovo je jedna od najboljih funkcija „Kotlina“, pošto lambde možete koristiti u skoro svakom aspektu jezika i lambda sintaksa vam daje mogućnost da kod pišete kao da koristite oblasno-specifičan jezik (DLS), a ne jezik opšte namene (GPL). - „Null Safety“: Bezbednost igra važnu ulogu u razvoju naprednih aplikacija, pa pored „neizmenjivih varijabli“ imate na raspolaganju i „Null Safety“. Ovaj mehanizam služi za rešavanje „NullPointerException“ grešaka, pa samim tim povećava pouzdanost aplikacije. Kao zaključak, mogli bi da kažemo da „Kotlin“ sa sobom donosi puno dobrih stvari: u pitanju je svestran i moćan programski jezik koji je idealan za razvoj naprednih Android aplikacija. Njegove karakteristike, velika fleksibilnost i podrška od strane „Gugla“ i razvojne zajednice, čine ga savršenim izborom za sve Android developere. U osnovi, to je „čistiji“ i kraći kod, ali sa dodatkom fantastičnih funkcija, koje će onima koji tačno znaju šta žele doneti bezbroj benefita.

HelloWorld
0
24.02.2023. ·
11 min

How to develop a winning strategy in the "war" for talent?

No one would argue when you say that having the right people is the most important thing for a company. Finding and keeping the right people tend to be one of the hardest things to do. This is especially the case in our (IT) industry.

HelloWorld
0
27.01.2023. ·
5 min

Optimizacija B2B e-fakturisanja inovativnim API rešenjima

E-fakturisanje je inovativna promena za preduzeća kada je u pitanju upravljanje njihovim fakturisanjem i finansijskim operacijama. Preduzeća mogu uštedeti vreme, smanjiti greške i povećati ukupnu efikasnost eliminisanjem fakturisanja na papiru i automatizacijom procesa. Kompanije koriste rešenja za e-fakturisanje kako bi lako upravljale fakturama i stekle uvid u svoje finansijsko poslovanje u realnom vremenu. Od 1. januara 2023. godine e-fakturisanje je zakonski obavezno za srpske kompanije za B2B i B2G transakcije.

HelloWorld
2
16.12.2022. ·
5 min

One of our stories: Continuous Learning

In our first story, we talked about career development at Insightful. However, we all know that our skills don't magically enhance overnight. This is why here we would like to walk you through the way we actually support our team members to expand their level of competencies, preparing them for their current and new responsibilities and roles.

HelloWorld
0
25.10.2022. ·
8 min

Najkul novosti iz .NET 5 i 6 - Lagani pregled za zauzete developere

Microsoft je odredio datum početka .NET Conf 2022, što znači da nam se release .NET 7-ice veoma približio. Kako tvrde, sedmica će biti još brža i lakša⚡no prethodne verzije, a i generisaće značajno veći FOMO nama koji se možda i nećemo tako skoro na nju upgrade-ovati 😅.

HelloWorld
2
19.09.2022. ·
5 min

BMGH Trophy Cabinet: Accolades and Certificates, Ed. 2022

The global pandemic has shaken the foundations of the business world in the last two and a half years. Many businesses have gone through dire straits to keep their heads afloat. All these challenges cried for effective management and fast reactions.

HelloWorld
0
01.09.2022. ·
6 min

Intervju sa Tim Liderima

Milica Mlađan, QA Team Lead Kada me pitaju koliko dugo radim u Computer Rock-u, kažem da su godine samo broj. Bitno je kako se osećamo, stoga se i ja nakon četiri godine provedene u kompaniji i dalje osećam vrlo mladoliko.

HelloWorld
0
01.08.2022. ·
2 min

U prvih 6 meseci kriza nije preterano uticala na IT tržište rada

Tržište informacionih tehnologija u Srbiji se našlo pred velikim izazovom nakon sukoba Rusije i Ukrajine. Veliki broj IT stručnjaka iz ovih država pristigao u našu zemlju da bi ovde nastavio da rade. Pretpostavljalo se da će zbog krize biti znatno usporeno zapošljavanje. Međutim, prema podacima najvećeg sajta za zapošljavanje IT-jevaca HelloWorld.rs broj oglasa za posao u ovoj industriji skočio je za 26% u prvih 6 meseci 2022. godine u odnosu na isti period prošle godine.

Da ti ništa ne promakne

Ako želiš da ti stvarno ništa ne promakne, prijavi se jer šaljemo newsletter svake dve nedelje.