Skip to main content

Posts

Showing posts from July, 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 (https://www.slmanju.com/2021/07/basebase62-encoding-with-java.html). 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