Skip to main content


Showing posts from 2021

Design a unique sequence with MongoDB

Greetings! If you have used RDBMS like MySQL or Oracle you know that those databases support auto-increment fields or sequences. However MongoDB's way of ID is using an ObjectId which is a large string. However if you want an unique sequence what will you do? One solution is of cause to use RDBMS solution like Oracle. Unlike NoSQL solutions, RDBMS have problems with scaling. Let's imagine that to overcome scaling problems we are using MongoDB. Why do we need a unique sequence? We will need unique sequences for several reasons. Design a hit counter. Short string generation ( Rate limiting. Easy to read identifier value. You are migrating from relational database. Eventhough MongoDB doesn't offer this feature out of box, we can implement it using it's atomic operations. The trick is to create a collection with desired name as the _id and findAndModify with $inc. db.counters.insert({ _id: "my-se

Base/Base62 encoding with Java

Greetings! Converting base 10 number to another base is a common mathematical operation. First of all, why do we need base62? Shorten a given URL Saving a file using timestamp Short text As the base62 has lot more characters, we can easily create a small version of a given number. Let's do the Math Converting to another base has common steps. ( base-10-to-other-bases ) Divide the number by the base and get the remainder. Divide the quotient and get the remainder. Continue this process untill the quotient is zero. Order the remainders from last to first. We can try this process for hexa-decimal. As hexa-decimal is 16 chars, we need a way to represent that value. For that 0123456789ABCDEF are used. 42 42 / 16 = remainder 10, quotient 2 2 / 16 = remainder 2, quotient 0 Since 10 is A in hexa representation 42 = 2A in hexa-decimal number system. This same logic is applied for Base62. We can use 0-9A-Za-z characters to encoe base62. 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopq

Batch processing with Iterator pattern

 Greetings! While peeping on a pull request I came across this 60+ lines long complex looking code. Intention of the code was to fetch data from the legacy database as batches. Source code -  Iterator Pattern   In brief, idea was like this, while there_are_more_records fetch next batch do any processing handle exceptions calculate next batches calculate is there another iteration Though lines count has been reduced by creating private methods later, this has multpiple problems. Doing multiple things in this method. Looks too complex. Difficult to unit tests. Unit tests will not be that much valid. Hard to maintain. Error prone. Cannot resuse the logics. Unfortunately, people think doing this kind of complex codes are the good work. A better code will treated like an easy work. Anyway, we can do better with an Iterator. As Java's collection framework uses this heavily, most of the time, people don't use this pattern as they don't know they can use Ite

Why you should not use field injection

 Greetings! While doing code reviews, I usually come across DI framework (ex: Spring) managed beans with no constructor. I myself have done this many times (and still for POCs). What you can see classes is, fields are annotated with @Autowired, @Resource, @Inject, etc. How do they write unit tests then? They will for example use @InjectMocks using mock framework. As I am not a fan of this approach I ask the reasons why are you creating classes like this and use constructors. However, many will not agree with me giving these kinds of reasons. Reasons to use field injection We are using a DI framework (Spring), do not need to worry about this. The unit testing framework we use support field mocking (InjectMocks). We can use reflection in unit tests. Different developers have different practices, you need to accept that fact and respect them. This is how the previous developer coded. This is how our internal framework use. I'm senior than you, do as I say :P. While those can be accept

How I achieved CKAD

 Greetings! I finally became a Certified Kubernetes Application Developer aka CKAD offered by the Cloud Native Computing Foundation (CNCF), in collaboration with The Linux Foundation. I would like to share my experience with few tips with you. I do not use, do not have a chance to use Kubernetes in my daily work. Not only Kubernetes, I do not have real world experience in Docker as well. So it is a hard journey. Exam details I don't want to repeat it here. You can read it here . This exam is not something you learn and remember things and answer the questions. You have to perform labs in live environment. You have to fight with the time and that is the most difficult part.  Even if you remember, know everything you can't pass!!! This is why everyone says practice! practice! practice! There are discount coupons. Use them to buy the voucher. Recommended materials Learning depends on your character. Some people like to read documenatatio