From PHP to Java #2 – Development speed

In my previous article I was hyped about getting insane throughput in my first app. Today I started another app and development speed, using quite difficult architecture, surprised me again.

Spring Boot

As you could expect after previous entry I’m focusing on Spring Boot as it delivers everything more or less out of the box so I don’t need to worry that much about additional tools. It’s also similar-ish to Symfony2/3 so I feel close to home.

Building new app today

Last Thursday I visited London’s Java Meetup which was about CQRS applications, event sourcing and how to use it in microservices. Great talk which motivated me to try to build Event Sourced app.

There is nothing better for the weekend than building application in language and environment you don’t really know what well using difficult technology you’ve never used before. Should give me weekend or two of fun playing with it.

Sadly my project, Java and tools said

no

what’s actually good thing.

Spring & Axon Framework

It’s now 14:23 and I’m basically done with fully event sourced API part with enough materializers to create all views needed for the MVP. It’s kinda crazy for me.

Configuration of Axon Framework (CQRS framework for Java) is very easy. Few services registered in container … I mean Beans registered in Application Context and it’s done. Domain objects working as Command Handlers, listening to events and JPA (like Doctrine) read layer based on MySQL. Everything worked on first run and as it suppose to.

Event sourcing has great part in development speed and I don’t need to care about database at all and I don’t even think about it before I finish all domain processes. After that there is time to reflect what I want to show to the user. What views I’ll have in the application. And how I want to put those data so they will be easy to read.

Amazing. Again.

Conclusion

I’d really like to find some downsides, things I won’t like in Java. I know about Websphere and old “Application Servers” ways and that they are terrible but it’s past. Currently I feel like Java is taking back ground after years of being “slow” and “too corporate”.

I remember doing research years ago, when I was looking for language to learn after seeing no future in Delphi. I’ve chosen PHP as it was growing and for being nice and fast to develop websites. Java felt “too big for task”. Today it seems to be other way around.

From PHP to Java #1 – First production-ish application

So I’ve got a task to check difference in performance between Silex app, Symfony 3 with all goodies and JAVA/Spring Boot. Results surprised even me.

TLDR; because there will be separate post about that – PHP is obviously slower than Java and with proper use of opcache there is no difference between Symfony 3 with JMS Serializer and Doctrine and 60 lines unmaintainable app in Silex.

But it should be about Java. Stack Driven Development with Spring Boot took me maybe 1 or 2 hours in comparison about 1h with Symfony. As I’m still newbie I’m not optimising what so eves as I have just slight idea what I’m doing. App is working exactly the same way. Let’s do testing then.

Environment

I’m having all the apps on my poor Vagrant VM with not so much processing power and I’m using DB which is somewhere in the infrastructure. No cache, no in-memory storage. Everything is as dumb as possible ;)

For testing I’m suing Gatling.io. Great tool with Scala based DSL for test scripts.

We’re set up so let’s do some testing. Goal is to kill all the apps and check what’s the most they can handle.

The test

So PHP died before 50 req/s. It was so disappointing I haven’t checked exact number. Note I’ve done all optimisation on opcache and fpm normal user would do. Nothing crazy tho.

Let’s do Java then. same 50 req/s. No sweat. graph flat at response time about 10ms. Boring. As naive I was pushing by 25 on iteration of test. It took some time but finally got some struggle after reaching 250 req/s. At this point I’m shocked and confused. Mainly because even if I got longer response time I’ve got all the response data when PHP started to throw 500’s and 504’s in my face.

Next step my friend from the office came over and we’ve done some system optimisations on my VM. Then I’ve just

(╯°□°)╯︵ ┻━┻

Last successful test we’ve done got us average 360 req/s. On my VM, on small and slow-ish Mac Mini. And no errors. after that point, when I bumped test to over 400 req/s I simply killed TCP driver on my mac, not application. T bloody CP connections couldn’t be made on that rate on my host. App was fine.

Conclusion

I don’t want to say PHP is bad. It’s still good for frontend and not that high load. But if you really want performance you shouldn’t waste time on PHP IMO. Spring Boot feels like Symfony, all the good tooling is basically done for you.

You’re gaining like 7-8 times better performance out of the box with rock solid stability (I haven’t got even one 5xx from Java app).

Well, choice is yours if you gonna like it.

From PHP to Java #0 – Beginnings are hard

As I’m slowly working my way up from PHP to Java. I think I can write few words about my experience on that topic.

For now I know basics of basics of the language. It’s not that different and more or less the same with different markup in every language. You can’t reinvent loops of variables :P

Difficulty starts now when I want to write simple app in good way. Currently i’m using Symfony 2, Doctrine, Tactician Command Bus and Twig. Switching to do the same thing in Java means I need to choose similar libraries from java world. Another reason I have to use them is market. To get a job you have to know your tools. You have to know the best tools. And you have to know them well.

Now I’m in the stage when “I have no idea what I’m doing” but after research I decided what will be my starting point.

Web framework – Spring Boot – simplified version of Spring. No XML in favour of annotations and code configuration. From what I see it had good feedback from community and it’s similar enough to Symfony 2 to let me get some results in reasonable time.

Database – Hibernate – The heaviest ORM in Java, probably. But it was a source of inspiration for Doctrine 2 and few years back I was working with it’s .NET clone so I hope I will have only problems with configuration and markup.

Command Bus – Axon Framework – Here is the tricky part. Axon is a huge step from DDD light where state is saved on every request to Event Sourcing. Maybe it’s possible to simplify but I’m not sure at this point. Also Google and Stack told me it’s production proven by banks etc so it can be good direction even when learning curve will be very steep.

Templating – Thymeleaf – Default for Spring Boot. Not bad XML-like markup. For what i need it’s Googleable to extend templates and create reasonable DRY structure of template files.

It’s a lot for a first step into new technology. I spent last month-ish trying to figure out if I can make it in smaller chunks but I can’t see a way where I’m not doing dummy “hello Hibernate” examples.

Plan is to write clone of my ftrack-ddd app on the stack I mentioned in this post. As much as it will be possible I’ll be writing posts about my progress.

Programming assingment #1 – Booking tickets

This entry is part 1 of 1 in the series Programming assignments

Programming is fun. Often I have problem to find a issue I want to solve. With that in mind I start to publish my programming assignments I’m making for myself.

Today we will have simple microservice to manage concurrent ticket bookings for “events”. Let’s say Iron Maiden announces small concert for 100 people on all their social media channels with specific time of booking. Our problem is to handle big spike of traffic at the time booking become available.

Acceptance criteria:

  • Application creates event with poll of tickets of specific size
  • Application allows to reserve a ticket(s) for 1 hour
  • Application allows to transform reservation into real ticket
  • After Reservation times out and spot is available for other user
  • It’s impossible to reserve more tickets than available in the pool
  • After last reservation is transformed into ticket event is closed

I’m leaving proper communication of error out of scope but it’s always good to have framework which allows to communicate about state of the system to external clients.

I’m leaving rest up to you how you want to implement solution for this problem.

As always in software there is no one good answer as it’s “possible” to handle this kind of problem basically in any language with out without external libraries or services.

If you have any questions don’t hesitate to ask. I’ll do my best to answer as soon as possible.

Enjoy and good luck coding!

Calculating the interest on savings account

It was long time since I wrote here last time. I’m coming back today with interesting and not so simple problem of your savings account and how interest is calculated for the money you put in.

Assumption is that every month you should get monthly interest worked out on the money from savings account. With this task we want to be penny perfect. So we can’t loose any money during the calculation. Also for simplicity account can be open only for fixed amount of years. As for example I use one year as it’s simple and later we’ll see how to handle situation when money is stored for many years.

At first you can think you need list of numbers. But is it really what you have to calculate? List used every month for crediting your main account is just representation. IMO all what you need here are three numbers. Monthly interest, interest for first month and interest for last month. Forth “hidden” number is reminder after dividing annual interest into monthly one. Let’s take an example scenario.

We want to save 10000 with the annual interest rate of 10% for 1 year. We are opening our savings account as I’m writing this post at 27th Oct 2015.

First things first – total amount of money we get – easy: 10000 * 10% = 1000.

Next we can calculate our average monthly interest: 1000 / 12 = 83.33. And here we can see our hidden fourth number because 83.33 * 12 = 999.96. As we talking about money (and as it’s the great explanation why you shouldn’t use floats for money calculations) we have missing 0.04. But as we are aware of this we can fix this on the first or last interest payment. I’ll add it for the last one.

But first let’s calculate the first interest payment. There are 4 days till end of the October so again easy calculation 4 / 31 * 83.33 = 10.75. Again. Money. I’m loosing some as the real result is 10.75225806451613. But that’s what the last interest payment is for :)

I know that last one could be calculated by checking date and proportion of days from the beginning of the month. Sometimes it’s needed but as we have annual plan all we need to do is to subtract first interest payment from average monthly one: 83.33 – 10.75 = 72.58. And we are nearly done. All we need to add here is the reminder we saved from division of annual interest by months: 72.58 + 0.04 = 72.62.

That’s all. Intentionally I’m not writing code to give you a try on that. It’s very interesting problem and great subject for short kata in the evening.

Let me know if you like this kind of articles without the code as I have an idea about focusing blog posts more on everyday problems than fancy code snippets for copy/paste ;)

Update:

After all I’ve created sample Scala Worksheet with the object solving problem I described. Moved the remainder to first payment to show more money on first month ;) Here’s the Gist.

Stateless authentication in Symfony 2

Everyone these days want’s an API. Mostly for some SPAs or for external applications. Common issue here is a security layer where you can’t use standard form and cookies. Other thing about state saved in cookies is f.ex. clickjacking. James Ward wrote about this some time ago.

So we have some requirements:

  • Authentication based on token
  • Token is send as part of each request to make API stateless
  • I want to be able to use Symfony Security layer as I do in “normal” applications
  • Bonus: I want authenticate each device with different token (when someone is using app on PC and phone in the same time)

So I went to Symfony Cookbook and I’ve found solution with my requirement in the title. And it wasn’t what I needed. OK, it’s about keys/tokens and probably I could make it working but I have a lot of free time during holidays so I can do better. So I went one article further to write my my own authentication provider. And there is of course article about it too.

Lets start with UserProvider. What I want is find User entity with given token and device identifier. Simple schema looks like this:

User and Identities

To find user I query by username when authenticating user by token and device on the beginning of each request and once in a while by username and password to create new Identity.

Interface like this is everything I need and it’s consistent with Liskov’s substitution principle (L in SOLID).

When I can find user all I need is follow Cookbook and everything is working great. SecureApiBundle is available on GitHub so you can check how I’ve done this.

Update your composer.json file

enable it

and use secure-api in your security config:

I know it is possible to add paths for registration and session creation to config, but for now I don’t need this so I keep it simple :P Doctrine UserRepository is used here as User Provider with interface I mentioned earlier. Line 28 is marker that we are using our token solution for this firewall.

 

11 reasons why I’ve chosen Scala

As you probably know in the office I am a PHP developer. Last few month I was looking for second language which can give me fun and profit. It took time, but I think now I know that it is Scala.

A little history. Few moths ago I started to learn Ruby and Rails. It was fun, but I’ve started to feel lonely and afraid in code without interfaces, type hinting over interface/class and DI Container. It was also boring after some time – I was doing same thing typing same structures only words was different.

After that my thinking was shifted to Functional Programming. Quite hot topic on HackerNews and in community. After some research I tried Clojure and I knew Lisp syntax is not my thing ;) But after that I knew that FP is the choice.

So Scala it is.

  1. Scala can be functional as much as Object Oriented – so when learning new things I fees safe with good old objects
  2. It’s statically typed. I like this and I’m a bit sad I can’t do this in PHP ;)
  3. It takes a lot from Java so when doing Scala I learn a bit about another environment.
  4. It’s no yet another scripting language for web development …
  5. … but I can do Web
  6. I can finally get out of my thread and do something parallel ;)
  7. Play Framework
  8. Akka
  9. (Play) Non-blocking IO operations without JavaScript – IMHO real (and much better) alternative for NodeJS
  10. LinkedIn and Twitter are using Scala/Play on production
  11. Good community support and courses on Coursera 1, 2

Now I’m doing simple scripts, I’m watching presentations on YouTube and I’m working with “Programming with Scala” book. It’s fun and FP is really great to shift your thinking about programming to other level.