Skip to content

User Registry Scalability

O edited this page Oct 7, 2025 · 1 revision

User Registry Scalability Analysis for 8 Billion Users

🚨 Current Architecture Issues

Problem: In-Memory Storage

// Current implementation (NOT SCALABLE):
class UserRegistryConsensus {
private:
    std::map<CPubKey, OfficialUser> user_cache;  // ❌ In-memory only
    std::map<uint256, EndorsementRecord> endorsement_cache;  // ❌ In-memory only
    std::map<std::string, CPubKey> government_id_to_user;  // ❌ In-memory only
};

Scalability Calculations:

For 8 Billion Users:

User Record Size:
  - CPubKey: 33 bytes
  - government_id_hash: 64 bytes (SHA256 hex)
  - birth_currency: 8 bytes
  - identity_proof_hash: 64 bytes
  - endorsers (5 avg): 165 bytes (33 Γ— 5)
  - Other fields: ~100 bytes
  Total per user: ~434 bytes

Memory Required:
  8,000,000,000 users Γ— 434 bytes = 3,472 GB = 3.4 TB RAM

Endorsement Records (5 per user):
  40,000,000,000 endorsements Γ— 200 bytes = 8 TB RAM

TOTAL MEMORY: ~11.4 TB RAM ❌ IMPOSSIBLE

Current Design Cannot Scale Beyond:

  • ~10 million users (4.3 GB RAM)
  • ~50 million users (21.7 GB RAM)
  • ~100 million users (43.4 GB RAM)

βœ… Solution: Persistent Database Backend

Recommended Architecture:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ APPLICATION LAYER                                           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ UserRegistryConsensus (API Layer)                          β”‚
β”‚   - RegisterUser()                                          β”‚
β”‚   - SubmitEndorsement()                                     β”‚
β”‚   - GetVerifiedUsers()                                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ CACHE LAYER (In-Memory for Performance)                    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ LRU Cache (100K most recent users)                         β”‚
β”‚   - Hot data in RAM                                         β”‚
β”‚   - Fast lookups for active users                           β”‚
β”‚   - Automatic eviction of cold data                         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ DATABASE LAYER (Persistent Storage)                         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ LevelDB / RocksDB                                           β”‚
β”‚   - Key-Value store (like Bitcoin Core uses)               β”‚
β”‚   - Efficient disk storage                                  β”‚
β”‚   - Supports billions of records                            β”‚
β”‚   - Built-in compression                                    β”‚
β”‚   - Atomic operations                                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Why LevelDB/RocksDB?

  1. Already in Bitcoin Core: LevelDB is used for blockchain data
  2. Proven Scalability: Handles billions of records efficiently
  3. Disk-Based: Only active data in RAM
  4. Fast Lookups: O(log n) complexity with optimizations
  5. Compression: Reduces disk space by 50-70%
  6. Atomic Operations: ACID compliance for data integrity

πŸ”§ Proposed Implementation

Database Schema:

// Key-Value Pairs in LevelDB:

// User Records
Key: "user:" + CPubKey (33 bytes)
Value: Serialized OfficialUser (~434 bytes)
Total: 8B Γ— 467 bytes = 3.7 TB disk (compressed: ~1.5 TB)

// Government ID Index
Key: "govid:" + government_id_hash (64 bytes)
Value: CPubKey (33 bytes)
Total: 8B Γ— 97 bytes = 776 GB disk (compressed: ~300 GB)

// Endorsement Records
Key: "endorse:" + uint256 (32 bytes)
Value: Serialized EndorsementRecord (~200 bytes)
Total: 40B Γ— 232 bytes = 9.3 TB disk (compressed: ~3.7 TB)

// User by Currency Index
Key: "currency:" + currency_code + ":" + CPubKey
Value: 1 byte (existence marker)
Total: 8B Γ— 50 bytes = 400 GB disk (compressed: ~150 GB)

TOTAL DISK SPACE: ~5.6 TB (compressed)

Memory Usage with LRU Cache:

// Only keep hot data in RAM
LRU Cache Size: 100,000 most recent users
Memory: 100,000 Γ— 434 bytes = 43.4 MB

Endorsement Cache: 500,000 recent endorsements
Memory: 500,000 Γ— 200 bytes = 100 MB

Index Caches: ~50 MB

TOTAL RAM: ~200 MB (vs 11.4 TB without database!)

πŸ“Š Performance Analysis

Database Operations:

Lookup Performance:

LevelDB Read: ~1-10 microseconds
LRU Cache Hit: ~100 nanoseconds

With 90% cache hit rate:
  Average lookup: 0.9 Γ— 0.0001ms + 0.1 Γ— 0.01ms = 0.001ms
  
Can handle: 1,000,000 lookups per second per node

Write Performance:

LevelDB Write: ~10-100 microseconds
Batch writes: ~1 microsecond per record

User registration: ~100 microseconds
Can handle: 10,000 registrations per second per node

Scalability Projections:

Users Disk Space RAM (with cache) Lookup Time
1M 700 MB 200 MB 0.001 ms
10M 7 GB 200 MB 0.001 ms
100M 70 GB 200 MB 0.001 ms
1B 700 GB 200 MB 0.002 ms
8B 5.6 TB 200 MB 0.003 ms

Conclusion: Scales linearly with minimal RAM increase βœ…


πŸ› οΈ Implementation Plan

Phase 1: Database Backend (High Priority)

class UserDatabase {
public:
    // Core operations
    bool StoreUser(const CPubKey& key, const OfficialUser& user);
    std::optional<OfficialUser> GetUser(const CPubKey& key);
    bool DeleteUser(const CPubKey& key);
    
    // Index operations
    bool IndexGovernmentID(const std::string& gov_id, const CPubKey& key);
    std::optional<CPubKey> LookupByGovernmentID(const std::string& gov_id);
    
    // Batch operations
    bool StoreBatch(const std::vector<std::pair<CPubKey, OfficialUser>>& users);
    std::vector<CPubKey> GetUsersByCurrency(const std::string& currency);
    
    // Statistics
    uint64_t GetTotalUsers();
    uint64_t GetVerifiedUsers();
    
private:
    std::unique_ptr<CDBWrapper> m_db;  // LevelDB wrapper
    LRUCache<CPubKey, OfficialUser> m_cache;  // In-memory cache
};

Phase 2: Migration Strategy

1. Create new database schema
2. Implement database wrapper
3. Add LRU cache layer
4. Migrate existing in-memory data
5. Update all access methods
6. Test with large datasets
7. Deploy gradually

Phase 3: Optimization

1. Add bloom filters for fast existence checks
2. Implement sharding for distributed storage
3. Add read replicas for high availability
4. Optimize indexes for common queries
5. Implement background compaction

πŸ” Current vs Proposed

Current (In-Memory):

βœ… Pros:
  - Fast lookups (O(log n))
  - Simple implementation
  - No database complexity

❌ Cons:
  - Limited to ~10M users (RAM constraint)
  - Data lost on restart
  - Cannot scale to billions
  - Single point of failure

Proposed (Database + Cache):

βœ… Pros:
  - Scales to billions of users
  - Persistent storage
  - Minimal RAM usage (~200 MB)
  - Fast lookups (with cache)
  - Distributed storage possible
  - Production-ready

❌ Cons:
  - More complex implementation
  - Requires database maintenance
  - Slightly slower than pure in-memory (but still fast)

πŸ“ˆ Real-World Comparison

Similar Scale Systems:

Facebook (3 billion users):

  • Database: MySQL + RocksDB
  • RAM per server: 256 GB
  • Distributed across thousands of servers

WhatsApp (2 billion users):

  • Database: Custom distributed system
  • Minimal RAM per user
  • Horizontal scaling

Bitcoin Core (UTXO set ~100M entries):

  • Database: LevelDB
  • RAM: ~2-4 GB (with cache)
  • Disk: ~500 GB
  • Proven scalable architecture βœ…

O Blockchain (Target: 8 billion users):

  • Should use: LevelDB/RocksDB (like Bitcoin Core)
  • Estimated RAM: ~200 MB (with LRU cache)
  • Estimated Disk: ~5.6 TB (compressed)
  • Scalable: βœ… With proper database backend

🎯 Recommendations

Immediate Actions:

  1. Migrate to LevelDB (High Priority)

    • Use Bitcoin Core's existing CDBWrapper
    • Implement user database layer
    • Add LRU cache for performance
    • Estimated effort: 1-2 weeks
  2. Add Indexing (High Priority)

    • Government ID β†’ User mapping
    • Currency β†’ Users mapping
    • Country β†’ Users mapping
    • Estimated effort: 1 week
  3. Implement Caching (Medium Priority)

    • LRU cache for hot users
    • Bloom filters for existence checks
    • Estimated effort: 1 week
  4. Test at Scale (High Priority)

    • Generate 10M test users
    • Measure performance
    • Optimize bottlenecks
    • Estimated effort: 1 week

Long-Term (For Billions of Users):

  1. Sharding (When > 100M users)

    • Shard by country/region
    • Distribute across nodes
    • Estimated effort: 1 month
  2. Replication (For High Availability)

    • Master-slave replication
    • Read replicas
    • Estimated effort: 2 weeks
  3. Distributed Consensus (For Global Scale)

    • Partition user registry
    • Cross-shard queries
    • Estimated effort: 2 months

πŸ’‘ Conclusion

Current Status:

❌ NOT READY for 8 billion users (in-memory design) βœ… WORKS for up to ~10 million users (prototype/testing)

To Support 8 Billion Users:

MUST implement persistent database backend with:

  1. LevelDB/RocksDB for storage
  2. LRU cache for performance
  3. Proper indexing for fast lookups
  4. Compression for disk efficiency
  5. Sharding for distribution (at massive scale)

Estimated Implementation:

  • Basic Database Backend: 2-3 weeks
  • Production-Ready: 1-2 months
  • Billion-Scale Optimizations: 3-4 months

Good News:

Bitcoin Core already uses LevelDB extensively, so we can leverage existing infrastructure and expertise. The migration path is well-understood and proven.


πŸš€ Next Steps

  1. Acknowledge the scalability limitation
  2. Plan database migration (LevelDB integration)
  3. Implement in phases (start with basic backend)
  4. Test at increasing scales (1M β†’ 10M β†’ 100M β†’ 1B+)
  5. Optimize based on real-world usage

The O Blockchain user registry needs a database backend to scale to billions of users, but this is a well-solved problem with clear implementation path using Bitcoin Core's existing LevelDB infrastructure.

Clone this wiki locally