<p>Historically <code>bdb</code> was used for the UTXO as well, so you could justify using it for the wallet as well for consistency. It makes absolutely no sense today, other than that it will always need to be supported to some extent for compatibility. </p>

<p>Really the wallet could be replaced with any key value store at all and it would function completely fine. You would not want a single key per file because this tends to be incredibly inefficient and difficult to deal with from a filesystem, and a human interaction perspective. It's easier to back up a single file than tens of thousands in a directory. </p>

CC BY-SA 4.0
2020-06-08T16:09:19.247Z
Could you be more specific about a single key per file being inefficient and difficult to dealt with from a FS, and a human interaction perspective? Does that only include backing up the files? Also by easier to back up, do you mean easier from the user perspective? I can imagine that If I'd use a GUI, copying tens of thousands of files to create a backup would be difficult and not user-friendly. But if I used a CLI, it wouldn't be much of a problem. All I would need to do is use the command `cp wallet_keys/* <destination_directory>`.
Aschente
CC BY-SA 4.0
2020-06-08T18:28:07.860Z
@Aschente How would you know whether you had them all? A single file has useful properties such as a size, a last modified date, and so on. Dealing with, potentially, thousands of files is a nightmare. And what if you have multiple wallets and want to switch between them?
David Schwartz
CC BY-SA 4.0
2020-06-08T19:58:02.790Z
@David Schwartz I see, that's a good point.
Aschente
CC BY-SA 4.0
2020-06-08T10:24:39.013Z
2020-06-08T14:33:33.203Z
2020-06-08T14:33:33.203Z
Claris
<p>According to <a href="https://github.com/bitcoin/bitcoin/blob/master/doc/files.md" rel="nofollow noreferrer">this</a> Bitcoin stores the user's keys and some additional data in a file called wallet.dat, which is a Berkley DB. Let's suppose the wallet.dat contained only the user's keys. Let's also suppose we're using linux. </p>

<p>What is then the main reason for Bitcoin to store keys in a database? Wouldn't it have the same effect if keys were stored as flat files: the public key would be the filename and the private key would be the content of the file? </p>

<p>Even if we had millions of keys, and therefore millions of files in a single directory, the i-node limit (maximum number of files in a directory) and file lookup speed shouldn't be a problem. The majority of linux systems use the ext4 as its filesystem and according to <a href="https://stackoverflow.com/questions/466521/how-many-files-can-i-put-in-a-directory">this</a> the number of i-nodes in ext4 is unlimited (limited only by the disk memory). Also according to <a href="https://en.wikipedia.org/wiki/Ext4" rel="nofollow noreferrer">this</a> the EXT4 uses a structure called HTree, which allows for file lookups to have a constant time complexity. </p>

<wallet><wallet.dat><database>
Why does Bitcoin store keys in a database?
Aschente
# Why does Bitcoin store keys in a database?

(Asked by: username_0 on 2020-06-08)

According to [this](https://github.com/bitcoin/bitcoin/blob/master/doc/files.md) Bitcoin stores the user's keys and some additional data in a file called wallet.dat, which is a Berkley DB. Let's suppose the wallet.dat contained only the user's keys. Let's also suppose we're using linux.

What is then the main reason for Bitcoin to store keys in a database? Wouldn't it have the same effect if keys were stored as flat files: the public key would be the filename and the private key would be the content of the file?

Even if we had millions of keys, and therefore millions of files in a single directory, the i-node limit (maximum number of files in a directory) and file lookup speed shouldn't be a problem. The majority of linux systems use the ext4 as its filesystem and according to [this](https://stackoverflow.com/questions/466521/how-many-files-can-i-put-in-a-directory) the number of i-nodes in ext4 is unlimited (limited only by the disk memory). Also according to [this](https://en.wikipedia.org/wiki/Ext4) the EXT4 uses a structure called HTree, which allows for file lookups to have a constant time complexity.

**Tags:** wallet, wallet.dat, database


---

## 1 answers

### Answer by username_1 on 2020-06-08. Score: 3 | Accepted answer

Historically `bdb` was used for the UTXO as well, so you could justify using it for the wallet as well for consistency. It makes absolutely no sense today, other than that it will always need to be supported to some extent for compatibility.

Really the wallet could be replaced with any key value store at all and it would function completely fine. You would not want a single key per file because this tends to be incredibly inefficient and difficult to deal with from a filesystem, and a human interaction perspective. It's easier to back up a single file than tens of thousands in a directory.

> **Comments:**
> **username_0**: Could you be more specific about a single key per file being inefficient and difficult to dealt with from a FS, and a human interaction perspective? Does that only include backing up the files? Also by easier to back up, do you mean easier from the user perspective? I can imagine that If I'd use a GUI, copying tens of thousands of files to create a backup would be difficult and not user-friendly. But if I used a CLI, it wouldn't be much of a problem. All I would need to do is use the command `cp wallet_keys/* <destination_directory>`.  
> **username_2**: @username_0 How would you know whether you had them all? A single file has useful properties such as a size, a last modified date, and so on. Dealing with, potentially, thousands of files is a nightmare. And what if you have multiple wallets and want to switch between them?  
> **username_0**: @username_2 I see, that's a good point.  

---

