How it works
An HBase table is a sparse, sorted map. A row key, column family, column qualifier, and timestamp identify a cell and allow multiple value versions. The schema declares column families, but qualifiers can differ between rows. Wide-column does not mean an analytical columnar format like Parquet.
Tables have regions that cover contiguous row-key ranges. RegionServers serve these regions. Clients cache region locations that they find through the hbase:meta catalog. Reads and writes normally go directly to RegionServers. HMaster assigns regions, balances load, and does administrative work rather than forwards every request.
For normal durable writes, the RegionServer records changes in a write-ahead log and updates an in-memory MemStore. Each column family in a region has its own store. MemStores flush into immutable HFiles, normally on HDFS. Background compactions merge files and remove eligible obsolete versions and deleted data.
Reads combine visible data from MemStores and HFiles. Block caches reduce storage reads. Bloom filters help skip files that cannot contain a requested key. Many files, cache misses, and compaction pressure can increase read latency. Fast writes do not remove storage maintenance costs.
HBase provides atomic changes within one row and strongly consistent reads from the primary region replica. A scan across many rows is not a table-wide snapshot. Optional timeline-consistent reads from secondary region replicas can return stale data.
What it gives you in a design
- Ordered key access
- HBase directly supports point reads and scans over contiguous row-key ranges. Put frequently queried dimensions into the key. Filters on unrelated columns do not replace an index.
- Sparse, flexible columns
- Rows can contain different qualifiers without space for every possible column. Keep few column families. Each family adds storage overhead and work to manage flushes.
- Row-level conditional updates
- Conditional changes and atomic row operations support compare-and-set workflows and counters. They do not provide a transaction across multiple rows.
- Cell versions and expiration
- Cell versions with timestamps and column-family TTL support history retention and data expiration. Configure version retention explicitly. Expiration does not mean immediate physical deletion.
- Bulk loading
- Large imports can bulk-load prepared HFiles instead of sending every cell through the normal mutation path. The import still needs a plan for key distribution and concurrent changes.
- Cluster-to-cluster replication
- WAL-based replication can copy edits asynchronously to another cluster. It supports disaster recovery and remote copies. It does not provide globally synchronous commits or cross-cluster serializability.
Scaling, replication and failure
HBase partitions data by ordered row-key ranges, not consistent hashing. Regions split as data grows and can move between RegionServers. Pre-splitting helps distribute the initial load, but only if new keys span the split ranges.
Row-key design determines write distribution. An increasing timestamp at the start can direct new writes to one region. A tenant or device prefix can isolate useful scans but concentrate a large customer's traffic. Salting or bucketing distributes writes but requires parallel scans and a merge of results.
HDFS normally replicates the underlying files. One primary region replica provides strongly consistent access for each region. Optional secondary region replicas serve timeline-consistent reads. After a RegionServer failure, region reassignment and recovery of durable WAL edits reduce availability for a period.
More RegionServers help when many regions share the load. They do not let one hot row scale without limit. Cross-cluster replication is asynchronous. A regional failover can lose access to recent edits that have not reached the destination. State the recovery-point objective. Explain how the system fences writers during failover.
Limits and defaults to know
Many of these values are configurable. Check the current documentation before you use an exact value.
| Maximum row-key length | A row key can contain at most 32,767 bytes. Much shorter keys are usually better because keys add storage and I/O overhead. |
|---|---|
| Default client cell-size check | The hbase.client.keyvalue.maxsize setting defaults to 10 MiB for a serialized KeyValue. You can change it. This default does not recommend large objects in cells. |
| Default version retention | Column families retain one cell version by default. You can configure the maximum version count. |
| Default TTL | Column-family TTL defaults to FOREVER. A configured TTL uses seconds. Compaction generally removes expired data. |
| Atomicity boundary | HBase provides atomic changes within one row, not general multi-row transactions. A batch of changes across rows is not an all-or-nothing transaction. |
Use it when
- The dataset is large and has frequent writes. Access mainly uses exact keys or bounded key-range scans.
- Metrics, trace records, or event histories can use a stable key that combines entity and time.
- Sparse records or retained cell versions are useful. The design does not need general relational queries.
- Single-row conditional updates meet the correctness requirements. The team can operate an HBase storage stack.
Avoid it when
- The main workflow needs joins, foreign keys, or atomic transactions across many independent records.
- Users need ad hoc filters and aggregation across arbitrary fields without extra indexes or analytical systems.
- A simpler relational database or managed key-value service can easily support the dataset and traffic.
- Most traffic targets one row, or the design requires synchronous, strongly consistent writes across multiple regions.
What interviewers ask about it
When HBase is on your whiteboard, expect these follow-up questions. Each answer gives the core of a strong answer.
- How would you choose a row key for time-series data?
- Start with the required scan, such as device readings within a set time interval. Consider a device-plus-time key. Check whether a busy device or sequential prefix creates a hotspot. Buckets spread traffic but require reads across buckets.
- What happens when a RegionServer fails?
- The cluster detects the failure, reassigns its regions, and recovers durable edits from WALs. With normal synchronous WAL durability, acknowledged writes should survive the failure. However, affected regions can be unavailable during recovery.
- Is HBase strongly consistent?
- Normal reads from the primary region replica are strongly consistent. Mutations are atomic within a row. These guarantees do not imply multi-row serializability, a consistent table scan snapshot, or fresh reads from optional timeline-consistent replicas.
- How do you query by a field that is not in the row key?
- Maintain another table with that field as its key, or use a separate index system. Explain how index updates survive retries and partial failures. Updates to the source row and a separate index row do not form one native atomic transaction.
- Why can latency rise even with more RegionServers?
- A hot row or narrow key range can still concentrate requests on one primary region replica. Compaction, excessive HFiles, cache misses, and contention for storage can also dominate latency. Inspect key distribution and storage pressure before you add nodes.
- How would you make write retries safe?
- Use deterministic row keys and, where appropriate, explicit cell timestamps so replay targets the same logical data. A repeated increment is not inherently idempotent. Use request identifiers and row-level conditional mutations when duplicate application would be incorrect.
Common mistakes
- Use a monotonically increasing timestamp first in the row key without a plan for concentrated writes.
- Assume that a sparse wide-column schema provides efficient queries on every column.
- Treat HDFS replication, secondary region replicas, and cross-cluster replication as equivalent methods for consistency and availability.
- Assume that a batch is a multi-row transaction or that TTL immediately erases bytes from storage.
Alternatives
- Apache Cassandra
- Prefer it when decentralized operation, multi-datacenter writes, and tunable consistency better meet the requirements. Compare these with HBase's primary-region access model and ordered row-key ranges.
- Google Cloud Bigtable
- Prefer it when a managed wide-column service fits the deployment environment. Less cluster administration must matter more than control of an Apache HBase installation.
- PostgreSQL
- Prefer it when relational queries, secondary indexes, and multi-row transactions are central. This applies especially when scale does not justify a specialized distributed store.
- ClickHouse
- Prefer it when the main workload aggregates many records for analysis rather than serves mutable data through row keys.
Where it is used
HBase is a good fit for these interview questions. Each line tells you why.
- Design a metrics monitoring system like DatadogHBase can store time series with device or metric keys. Important design decisions include row-key distribution, cardinality, TTL, and scan boundaries.
- Design a distributed tracing systemHBase can store trace records by trace identifier. Search by service, time, or tags requires a separate index strategy.
- Design a distributed key-value storeHBase provides a concrete reference for range partitioning, WAL recovery, row-level consistency, and replication that uses storage.
- Design Google AnalyticsHBase can serve precomputed aggregates and entity histories. The design must address hot tenants, updates that tolerate retries, and keys for specific access patterns.
Common questions
What is Apache HBase used for?
Apache HBase stores large sparse datasets that need frequent writes, key lookups, or ordered range scans. Examples include device histories, metrics storage, and event records with entity keys. Its suitability depends heavily on whether the row-key layout matches the queries.
What is the difference between HBase and HDFS?
HDFS is a distributed file system. HBase is a database that normally uses HDFS for durable files and logs. HBase adds row-level reads and writes, sorted regions, a cache, versioned cells, and database recovery.
Is HBase SQL or NoSQL?
HBase is a NoSQL wide-column database. Its APIs support key-based reads, mutations, and scans, rather than a native general-purpose SQL interface. Apache Phoenix can provide a SQL layer over HBase. However, it does not remove row-key design constraints or make arbitrary queries inexpensive.