What the hell is scalable code anyway?
I want to write one, but don’t know how?
My earlier definition of Scalable
Back in 2015, when I had come into the workforce I had heard this phrase in a lot of job postings “Should be able to write scalable code”. This requirement was as irrelevant as “Should be good with MS-Office” for me. It was a taken for granted thing that whatever code I will write, many people will be able to run it. I will try and keep the time complexity of my code to O(1) or max O(n) and if somehow it became O(n2) it is not scalable as more people will use that block of code, time to run that loop will be more and hence no scalability. Yup, that was my idea of scalability!!! Laugh all you want but there are people who still think the same way. Because scalable for me meant a number of people should be able to run it. Like the code at facebook. More than 1 billion people have an account on Facebook and they must have written some amazing code. Which is true, but the ability to concurrently serve to say 20 million people, doesn’t depend mearly on the PHP/React/C++ code they have written but rather on the Ops part of it. How many servers, how many connections and what is the lag time. Make no mistake they have done an amazing job with backend and frontend, but definition of scalability differs slightly at different places
1 . Server Level Scalability:
I would explain by taking the example of three software which affects Server scalability and explains the rationale behind what is meant by scalability when you are talking about the server as a resource.
Server side scalability in its very essence means the easiness to upgrade when your number of incoming request increases. So if yesterday you were receiving 100 hits/ second on your server and today you are receiving 10000 hits/second. How easy would it be for you to
a. get your server up and running and
b. share the load between servers to best serve those requests.
Before we dive into the details I just want to explain the two terms I will be using
Vertical Scalability: Vertical means take a machine and keep increasing the size(RAM, processing power, hard disk) of it to handle more load/data. This has it’s own disadvantage as there is a limit of how heavy server you can get.
Horizontal Scalability: Means lot of cheaper server and load is distributed across them.
Now, let us take these three into considerations and I’ll try to be as precise as possible as each topic here deserves a blog post of its own.
1. MongoDB vs MySQL
2. Nginx vs Apache
3. NodeJS vs Java
1. MongoDB vs MySQL:
MongoDB is more scalable than MySQL. More specifically MongoDB is more Horizontally scalable than MySQL. How? Because MongoDB has chucked the idea of Joins which made the idea of Horizontal Scalability appear like a nightmare for MySQL Database admins/backend developers. Instead, it uses a concept called Sharding which essentially makes it easy to have small DB instances spread across servers.
2. Nginx vs Apache and Node.Js vs Java
I am clubbing these two, because of Idea of scalability in Node.js and Nginx erupts from a software design pattern called Event Driven Programming. which chucks the idea of threads used by Apache and Java. How Event-driven programming proves to be more solid than thread based programming(in cases where the number of I/O >> computational complexity), I’ll leave you guys to find out about it.
2. Code level scalability
1. Writing scalable code means that if a new requirements come to you, in how much of a change, you can adapt to that requirement. The lesser the lines required, the more scalable the code is.
For Ex. : Say you are writing Node.js for Mac OSX and now you have to move to writing same for Linux/Debian based system. The code will be considered scalable on the basis of how many lines of code needs to be changed to adapt a Mac code to linux code.
2. A code which has least repeating parts is also considered to be scalable.
For Ex. If you have to say, send 4 types of Email. One is activation of the account, confirmation of account, Newsletter and payment details if any. You could either write, Email sending code 4 times or write it once in a function and use it differently based on some arguments sent in a function. The latter approach is termed as writing scalable code.
3. Say you are building a student portal and in your initial design you considered three stakeholders. Students, Teacher, and the Support Staff. The application is built and been running for close to 6 months now. Now suddenly you get a call you also need to bring Parents in the area of stakeholders. How easy would it be for you to change the database/software design and integrate it with your backend application for scalability? Would you change the whole application, because one more table is introduced in the DB, or with minimal change you will get it to work.
There maybe other kinds of code scalability also available but I consider these three
1. Writing OS independent code,
2. Writing a piece of code only once in my software
3. Database design
to be my main focus areas while writing scalable software.
How does writing scalable code make you a better developer?
Writing scalable code is an art in itself and one that should be mastered at the earliest. I love working with people who keep scalability of the software at priority rather than just make it work and later spend hours trying to make it scalable. It really challenges you to think away and beyond in terms of how a software might be used by your users and making it future proof in a way that, if that requirement comes all I have to do it just change this variable and it will work for those cases as well.
This is my brief overview over scalability and some things that I wish I had known when I started my career as a software developer.
If you write scalable code and reside in Bengaluru, India, give a shout out to the author who is a Software Lead at a connected car startup called Vehico and see if we can take our discussion forward in anyways. You can reach out to me at [email protected]