Why do we use Databases when we could directly use them as variables in server program

I am new to databases, and I find it unnecessarily complex to store data in database when we could have just stored them as variables in server side. I understand that databases are more efficient when it comes to large data storage, and also more secure than just using the main memory from the server program. But for small programs, where we just have to store the usernames of the people logging into our website, could this not be done without using databases and just storing them in some array/object in server program.


There’s absolutely nothing stopping you :stuck_out_tongue:. There are in-memory databases, like Redis or memcached you could use.

You can, of course, but should read about the benefits of using a permanent storage, like, for instance, being able to shutdown your server (or reboot it) without losing the data (except when the hardware fails, but there are measures you can take to mitigate it).

For a test app or even a small app, you can use whatever you like (even when doing tests, we tend to mock the database or even replace it with an in-memory one), but when it grows larger, maintaining 100 GB of data will be really costly (a 100GB SSD disk is far cheaper than what 100GB of server memory are), and not just for the hardware.

But who knows, in the near future we may have everything in memory which would be even faster (in-memory databases are usually used to improve performance on server side apps).

I hope it helps :slight_smile:.


To add onto what is said above, even if you have a giant machine with tons of ram available to save all the data you want in memory.

What happens when that machine restarts? Or if you need another machine to scale? Or want to split up the work so you don’t always have 100gb ram machine running 24/7 costing you thousands of dollars a day?

You might look into saving data into a hard-disk, which is much slower, but cheaper, and can be “detached” and even shared between multiple instances. However due to the speed, you might want to write more performant code on accessing this data, rather than just whatever the OS is using to access data on the hard-disk. Further, you might want to write this code in such a way as to compress/condense data so you don’t have to pay for as much storage.

At this point you actually created your own database, which is software that runs on a server (either on your current machine, or elsewhere) that your code accesses separately. Its goal is to handle all these intricacies for you, allow your application code to be more of a “middleman” between the hard state that is saved in your database, and the end-user.


Programs are ephemeral by their nature. User account usernames should persist, if your program were to crash. You would have some very angry users if they needed to constantly re-create new accounts. So no, it can’t really be done.

Databases also provide a form of “maintainable software”. You don’t want to have too many different things in your program that you have to maintain and think about.

I’d recommend using SQLite for small programs. It’ll provide that separation from your program for maintenance, and is very simple & quick to get up and running: SQLite Home Page

Also, most DBMSes today provide multiple layers of security, and other things that will provide peace of mind.


This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.