Building micro-services in Java

In a quest for the ultimate micro-service technology I have ported the core of PHP-CRUD-API to Java. It is a REST API that reflects the tables in your MySQL database. You can find the code on my Github account. I have found Java to be extremely fast. At 14000 requests per second it outperforms implementations in all other languages (that I tried): Java, 14000 req/sec (source code) Go, 12000 req/sec (source code) PHP 7, 6500 req/sec (source code) C# (.net Core), 5000 req/sec (source code) Node.js, 4200 req/sec (source code) Python, 2600 req/sec (source code) If you feel any code can be improved, please open an issue on Github! ...

January 21, 2017 · Maurits van der Schee

Simple REST API in Node.js

I have written a simple REST API in Node.js. It includes routing a JSON REST request, converting it into SQL, executing it and giving a meaningful response. I tried to write the application as short as possible and came up with these 110 lines of code: var http = require("http"); var mysql = require("mysql"); // connect to the mysql database var pool = mysql.createPool({ connectionLimit: 100, //important host: 'localhost', user: 'my_username', password: 'my_password', database: 'my_database', charset: 'utf8', debug: false }); // ensure request has database connection var withDb = function (handler) { return function (req, resp) { pool.getConnection(function (err, connection) { if (err) { resp.writeHead(404) resp.end(err); return; } req.db = connection; handler(req, resp); }); } }; // ensure request has (post) body var withBody = function (handler) { return function (req, resp) { var input = ""; req.on("data", function (chunk) { input += chunk; }); req.on("end", function () { req.body = input; handler(req, resp); }); } }; // main web handler var server = http.createServer(withDb(withBody(function (req, resp) { // get the HTTP method, path and body of the request var method = req.method; var request = req.url.replace(/^[\/]+|[\/]+$/g, '').split('/'); try { var input = JSON.parse(req.body); } catch (e) { var input = {}; } // retrieve the table and key from the path var table = req.db.escapeId(request.shift()); var key = req.db.escape(request.shift()); // create SQL based on HTTP method var sql = ''; switch (req.method) { case 'GET': sql = "select * from " + table + (key ? " where id=" + key : ''); break; case 'PUT': sql = "update " + table + " set ? where id=" + key; break; case 'POST': sql = "insert into " + table + " set ?"; break; case 'DELETE': sql = "delete " + table + " where id=" + key; break; } // execute SQL statement req.db.query(sql, input, function (err, result) { // stop using mysql connection req.db.release(); // return if SQL statement failed if (err) { resp.writeHead(404) resp.end(err); return; } // print results, insert id or affected row count resp.writeHead(200, { "Content-Type": "application/json" }) if (req.method == 'GET') { resp.end(JSON.stringify(result)); } else if (method == 'POST') { resp.end(JSON.stringify(result.insertId)); } else { resp.end(JSON.stringify(result.affectedRows)); } }); }))); server.listen(8000); The code is available on Github and is written to show you how simple it is to make a fully operational REST API in JavaScript. ...

January 17, 2017 · Maurits van der Schee

Porting PHP-CRUD-API to Go

I have ported the core of PHP-CRUD-API to Go and achieved a nice performance improvement from 6500 requests per second to 12000 requests per second. I found that PHP 7 outperforms C# with Kestrel on the .net Core platform for similar functionlity, whereas PHP 5 was still slower than C#. In PHP 7 the full program executes at roughly 2500 requests per second, which means the added logic makes you lose about two thirds of the performance. In compiled languages (like C# and Go) I expect that adding logic has a lower performance impact. ...

December 17, 2016 · Maurits van der Schee

Advent of Code is fun!

Every day in December I am doing a programming puzzle. The series is called Advent of Code and it follows the advent calendar approach. Every day from the 1st until the 25th of December one puzzle is unlocked. The puzzles get gradually harder and each puzzle has two parts, where the first part is easier than the second. Scores on the Leaderboard There is a leaderboard allowing you to compare yourself with other programmers. A nice 100 points are awarded to the person that solves the puzzle first and 90 to the tenth 80 to the 20th. If you don’t hit the top 100, then you can still see your rank, but you will receive 0 points. Another complicating factor is that the puzzles become available at midnight in New York (EST time). I have to get up at 6:00 in the morning to compete, not fair! Nevertheless I was proud to score 58th on the 5th of December when I actually got up early to try. ...

December 8, 2016 · Maurits van der Schee

JavaScript cannot handle 64 bit integers

JavaScript represents all numbers using IEEE-754 double-precision (64 bit) floating points. This is problematic for 64 bit integers, since this gives only gives you 53 bits of precision (the size of the mantissa). Bit operations are also only available for integer number up to 32 bits. Finally, the JSON exchange format does not support the IEEE-754 “NaN”, “Infinity” and “-Infinity” values. So the only numbers you can freely use in JavaScript without the risk of data-loss are 32 bit integers. ...

November 30, 2016 · Maurits van der Schee

3 programming videos worth watching

There are many conferences that publish their talks on YouTube, but not all talks are equally good. I try to watch as many as I can and then take the best ones that I have seen and publish them here on this blog. I was doing this with lists of conference videos before, but today will experiment with individual videos: 3 very interesting talks from 3 different interesting conferences. C Ruby? C Ruby Go! Go Ruby Go! Ever wanted to rewrite performance sensitive code as a native Ruby extension, but got stuck trying to navigate the depths of Ruby’s C API before you could get anything done? Or maybe you’re just not comfortable with C and want an easier path. ...

November 27, 2016 · Maurits van der Schee

Mathematical barbecue riddle

On the 19th of November, the Dutch Financial Times (Financieel Dagblad) has published a barbecue riddle on page 14. It is a combinatorial problem that certainly will have a nice theoretical solution, but that “we programmers” tend to solve in a few minutes using our favorite scripting language. The problem states (freely translated): Problem “Today is ‘International Men’s Day’ and that’s why we have a mathematical riddle about barbecuing. On our roof terrace there is a big barbecue with enough space for twenty pieces of meat next to each other. On this joyful day we have bought plenty of slices of bacon and hamburgers. Everybody knows you should never put two slices of bacon next to each other on the barbecue: the fat that drips from the bacon will make the coal catch fire. That’s why next to a slice of bacon you should always put a hamburger, except for the position at the end of the BBQ, where there is no more room for meat. ...

November 21, 2016 · Maurits van der Schee

Devoxx 2016 videos online

Devoxx 2016 (a developer conference) was held November 7-11 in Antwerp, Belgium. Hubert Sablonnière’s excellent talk titled “100% Stateless with JWT” is available on Youtube! The other 43 “top-rated” talks (a list curated by Devoxx) can be found below. Talks Flying services with the drone [45:40] Declarative Thinking, Declarative Practice [1:00:54] Make CSS Fun Again with Flexbox! [30:51] Reactive Programming [2:36:06] Twelve Ways to Make Code Suck Less [1:00:44] How Google DeepMind conquered the game of Go [52:56] Refactoring to Java 8 [52:10] Continuous Delivery At GitHub [1:01:13] Java Language and Platform Futures: A Sneak Peek [1:00:25] Modular monoliths [52:45] Java EE, TypeScript and Angular2 [2:48:41] Machine Learning for Developers [58:31] Optional - The Mother of All Bikesheds [58:43] Javaslang - Functional Java The Easy Way [50:00] How Angular Makes the Mobile Web Awesome [45:44] Zen & The Art of Angular 2 [52:33] 100% Stateless with JWT (JSON Web Token) [57:50] The end of polling : why and how to transform a REST API into a Data Streaming API? [25:50] A Crash Course in Modern Hardware [59:21] Thinking In Parallel [1:00:04] Deep Dive into JUnit 5 [1:02:24] Security and Microservices [1:00:49] Easily secure your Front and back applications with KeyCloak [31:38] Effective Service API Design [1:00:41] Using Machine Learning to Enhance your Apps [44:08] Java 9 Modularity in Action [2:26:10] Keynote Session by Mark Reinhold and Brian Goetz [37:29] Advanced Spring Data REST [1:02:00] Reactive Web Applications with Spring 5 [58:25] g ∘ f patterns [1:01:40] Project Jigsaw: Under The Hood [59:58] Java Collections: The Force Awakens [50:05] Designing for Performance [56:56] It's a kind of magic: under the covers of Spring Boot [2:35:09] Why you should really care about the blockchain [1:00:35] Do you really want to go fully micro? [31:20] Developing Reactive applications with Reactive Streams and Java 8 [2:40:59] Anticipating Java 9 - Functionality and Tooling [30:49] Hot.orElse(Not) [15:00] Microservices Evolution: How to break your monolithic database [1:00:30] Devoxx Belgium 2016 Opening Keynote [2:00:22] Billions of lines of code in a single repository, SRSLY? [16:07] Authentication and Authorization in a Cloud and Microservice World [53:20] Are you looking for other talks? There are 178 talks from Devoxx Belgium 2016 available on YouTube! ...

November 18, 2016 · Maurits van der Schee

Stop using passwords

Every site nowadays has a username/password login and that is problematic. Google plus and Facebook OAuth2 login exists, but it’s coverage is not 100% and there are privacy concerns. Luckily there is an alternative that has 100% coverage and no privacy concerns. So, let’s stop using passwords. This post will explain how. Single field login Your login form should be: Email: __________ [OK] That’s it! You will receive an email containing a link that you can click to be logged in. The link contains a JSON Web Token (JWT) with the claims “username” and “ip” and is signed by the website with a user specific secret (generated password). ...

November 12, 2016 · Maurits van der Schee

Programming C# on Ubuntu Linux

In this post we will be installing Microsoft .net Core SDK and Visual Studio Code on Ubuntu Linux and create high performance HTTP end-point in C# on which you can build a micro-service (or anything else). Runtime installation Follow instructions on https://www.microsoft.com/net/core#ubuntu: sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list' sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 417A0893 sudo apt-get update sudo apt-get install dotnet-dev-1.0.0-preview2-003131 This instructions are for Ubuntu 16.04, but other operating systems are supported as well. ...

November 10, 2016 · Maurits van der Schee