Repository: dgraph-io/badger
Pull request #1559 (2020-10-08 00:29:41)

chore(cmd/info): Fix printed spacing of summary.
Column-aligns the output for the Summary section:

Before:

    [Summary]
    Level 0 size:          0 B
    Level 1 size:       2.3 kB
    Total SST size:   2.3 kB
    Value log size:       20 B

After:

    [Summary]
    Level 0 size:          0 B
    Level 1 size:       2.3 kB
    Total SST size:     2.3 kB
    Value log size:       20 B

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/dgraph-io/badger/1559)
<!-- Reviewable:end -->



Patch:
diff --git a/.gitignore b/.gitignore
index e3efdf5..f34b69d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 p/
 badger-test*/
+.idea/
diff --git a/README.md b/README.md
index 76bc17f..0a0b88e 100644
--- a/README.md
+++ b/README.md
@@ -162,6 +162,7 @@ Below is a list of known projects that use Badger:
 * [emitter](https://github.com/emitter-io/emitter) - Scalable, low latency, distributed pub/sub broker with message storage, uses MQTT, gossip and badger.
 * [OctoSQL](https://github.com/cube2222/octosql) - Query tool that allows you to join, analyse and transform data from multiple databases using SQL.
 * [Dkron](https://dkron.io/) - Distributed, fault tolerant job scheduling system.
+* [smallstep/certificates](https://github.com/smallstep/certificates) - Step-ca is an online certificate authority for secure, automated certificate management.
 * [Sandglass](https://github.com/celrenheit/sandglass) - distributed, horizontally scalable, persistent, time sorted message queue.
 * [TalariaDB](https://github.com/grab/talaria) - Grab's Distributed, low latency time-series database.
 * [Sloop](https://github.com/salesforce/sloop) - Salesforce's Kubernetes History Visualization Project.
diff --git a/backup.go b/backup.go
index 3c62421..ee3d6d7 100644
--- a/backup.go
+++ b/backup.go
@@ -25,6 +25,7 @@ import (
 
 	"github.com/dgraph-io/badger/v2/pb"
 	"github.com/dgraph-io/badger/v2/y"
+	"github.com/dgraph-io/ristretto/z"
 	"github.com/golang/protobuf/proto"
 )
 
@@ -86,7 +87,8 @@ func (stream *Stream) Backup(w io.Writer, since uint64) (uint64, error) {
 
 			// clear txn bits
 			meta := item.meta &^ (bitTxn | bitFinTxn)
-			kv := &pb.KV{
+			kv := itr.NewKV()
+			*kv = pb.KV{
 				Key:       item.KeyCopy(nil),
 				Value:     valCopy,
 				UserMeta:  []byte{item.UserMeta()},
@@ -115,12 +117,22 @@ func (stream *Stream) Backup(w io.Writer, since uint64) (uint64, error) {
 	}
 
 	var maxVersion uint64
-	stream.Send = func(list *pb.KVList) error {
+	stream.Send = func(buf *z.Buffer) error {
+		list, err := BufferToKVList(buf)
+		if err != nil {
+			return err
+		}
+		out := list.Kv[:0]
 		for _, kv := range list.Kv {
 			if maxVersion < kv.Version {
 				maxVersion = kv.Version
 			}
+			if !kv.StreamDone {
+				// Don't pick stream done changes.
+				out = append(out, kv)
+			}
 		}
+		list.Kv = out
 		return writeTo(list, w)
 	}
 
diff --git a/badger/cmd/bank.go b/badger/cmd/bank.go
index d7853a5..6643256 100644
--- a/badger/cmd/bank.go
+++ b/badger/cmd/bank.go
@@ -31,9 +31,9 @@ import (
 	"time"
 
 	"github.com/dgraph-io/badger/v2"
-	"github.com/dgraph-io/badger/v2/options"
 	"github.com/dgraph-io/badger/v2/pb"
 	"github.com/dgraph-io/badger/v2/y"
+	"github.com/dgraph-io/ristretto/z"
 	"github.com/spf13/cobra"
 )
 
@@ -69,7 +69,6 @@ var (
 	numPrevious     int
 	duration        string
 	stopAll         int32
-	mmap            bool
 	checkStream     bool
 	checkSubscriber bool
 	verbose         bool
@@ -91,7 +90,6 @@ func init() {
 	bankTest.Flags().IntVarP(
 		&numGoroutines, "conc", "c", 16, "Number of concurrent transactions to run.")
 	bankTest.Flags().StringVarP(&duration, "duration", "d", "3m", "How long to run the test.")
-	bankTest.Flags().BoolVarP(&mmap, "mmap", "m", false, "If true, mmap LSM tree. Default is RAM.")
 	bankTest.Flags().BoolVarP(&checkStream, "check_stream", "s", false,
 		"If true, the test will send transactions to another badger instance via the stream "+
 			"interface in order to verify that all data is streamed correctly.")
@@ -312,7 +310,8 @@ func runDisect(cmd *cobra.Command, args []string) error {
 	db, err := badger.OpenManaged(badger.DefaultOptions(sstDir).
 		WithValueDir(vlogDir).
 		WithReadOnly(true).
-		WithEncryptionKey([]byte(encryptionKey)))
+		WithEncryptionKey([]byte(encryptionKey)).
+		WithIndexCacheSize(1 << 30))
 	if err != nil {
 		return err
 	}
@@ -357,14 +356,10 @@ func runTest(cmd *cobra.Command, args []string) error {
 	// Open DB
 	opts := badger.DefaultOptions(sstDir).
 		WithValueDir(vlogDir).
-		WithMaxTableSize(4 << 20). // Force more compactions.
-		WithNumLevelZeroTables(2).
-		WithNumMemtables(2).
-		// Do not GC any versions, because we need them for the disect..
+		// Do not GC any versions, because we need them for the disect.
 		WithNumVersionsToKeep(int(math.MaxInt32)).
-		WithValueThreshold(1). // Make all values go to value log
-		WithCompression(options.ZSTD).
-		WithBlockCacheSize(10 << 20)
+		WithBlockCacheSize(1 << 30).
+		WithIndexCacheSize(1 << 30)
 
 	if verbose {
 		opts = opts.WithLoggingLevel(badger.DEBUG)
@@ -504,13 +499,15 @@ func runTest(cmd *cobra.Command, args []string) error {
 				batch := tmpDb.NewWriteBatch()
 
 				stream := db.NewStream()
-				stream.Send = func(list *pb.KVList) error {
-					for _, kv := range list.Kv {
-						if err := batch.Set(kv.Key, kv.Value); err != nil {
+				stream.Send = func(buf *z.Buffer) error {
+					err := buf.SliceIterate(func(s []byte) error {
+						var kv pb.KV
+						if err := kv.Unmarshal(s); err != nil {
 							return err
 						}
-					}
-					return nil
+						return batch.Set(kv.Key, kv.Value)
+					})
+					return err
 				}
 				y.Check(stream.Orchestrate(context.Background()))
 				y.Check(batch.Flush())
diff --git a/badger/cmd/flatten.go b/badger/cmd/flatten.go
index 96e2750..e9b0c58 100644
--- a/badger/cmd/flatten.go
+++ b/badger/cmd/flatten.go
@@ -33,6 +33,7 @@ This command would compact all the LSM tables into one level.
 	RunE: flatten,
 }
 
+var keyPath string
 var numWorkers int
 
 func init() {
@@ -43,6 +44,8 @@ func init() {
 	flattenCmd.Flags().IntVarP(&numVersions, "num_versions", "", 1,
 		"Option to configure the maximum number of versions per key. "+
 			"Values <= 0 will be considered to have the max number of versions.")
+	flattenCmd.Flags().StringVar(&keyPath, "encryption-key-file", "",
+		"Path of the encryption key file.")
 }
 
 func flatten(cmd *cobra.Command, args []string) error {
@@ -50,10 +53,17 @@ func flatten(cmd *cobra.Command, args []string) error {
 		// Keep all versions.
 		numVersions = math.MaxInt32
 	}
+	encKey, err := getKey(keyPath)
+	if err != nil {
+		return err
+	}
 	opt := badger.DefaultOptions(sstDir).
 		WithValueDir(vlogDir).
 		WithNumVersionsToKeep(numVersions).
-		WithNumCompactors(0)
+		WithNumCompactors(0).
+		WithBlockCacheSize(100 << 20).
+		WithIndexCacheSize(200 << 20).
+		WithEncryptionKey(encKey)
 	fmt.Printf("Opening badger with options = %+v\n", opt)
 	db, err := badger.Open(opt)
 	if err != nil {
diff --git a/badger/cmd/info.go b/badger/cmd/info.go
index ec4086e..f7c4002 100644
--- a/badger/cmd/info.go
+++ b/badger/cmd/info.go
@@ -99,6 +99,8 @@ func handleInfo(cmd *cobra.Command, args []string) error {
 	db, err := badger.Open(badger.DefaultOptions(sstDir).
 		WithValueDir(vlogDir).
 		WithReadOnly(opt.readOnly).
+		WithBlockCacheSize(100 << 20).
+		WithIndexCacheSize(200 << 20).
 		WithEncryptionKey([]byte(opt.encryptionKey)))
 	if err != nil {
 		return y.Wrap(err, "failed to open database")
@@ -434,7 +436,7 @@ func printInfo(dir, valueDir string) error {
 		totalSSTSize += sz
 	}
 
-	fmt.Printf("Total SST size: %8s\n", hbytes(totalSSTSize))
+	fmt.Printf("Total SST size: %10s\n", hbytes(totalSSTSize))
 	fmt.Printf("Value log size: %10s\n", hbytes(valueLogSize))
 	fmt.Println()
 	totalExtra := numExtra + numValueDirExtra
diff --git a/badger/cmd/read_bench.go b/badger/cmd/read_bench.go
index 0a25c17..64d6a45 100644
--- a/badger/cmd/read_bench.go
+++ b/badger/cmd/read_bench.go
@@ -25,6 +25,7 @@ import (
 	"time"
 
 	humanize "github.com/dustin/go-humanize"
+	"github.com/pkg/errors"
 	"github.com/spf13/cobra"
 
 	"github.com/dgraph-io/badger/v2"
@@ -79,7 +80,7 @@ func init() {
 
 // Scan the whole database using the iterators
 func fullScanDB(db *badger.DB) {
-	txn := db.NewTransaction(false)
+	txn := db.NewTransactionAt(math.MaxUint64, false)
 	defer txn.Discard()
 
 	startTime = time.Now()
@@ -111,7 +112,7 @@ func readBench(cmd *cobra.Command, args []string) error {
 		WithBlockCacheSize(blockCacheSize << 20).
 		WithIndexCacheSize(indexCacheSize << 20)
 	fmt.Printf("Opening badger with options = %+v\n", opt)
-	db, err := badger.Open(opt)
+	db, err := badger.OpenManaged(opt)
 	if err != nil {
 		return y.Wrapf(err, "unable to open DB")
 	}
@@ -205,21 +206,30 @@ func getSampleKeys(db *badger.DB) ([][]byte, error) {
 		return l, nil
 	}
 
+	errStop := errors.Errorf("Stop iterating")
 	ctx, cancel := context.WithCancel(context.Background())
 	defer cancel()
-	stream.Send = func(l *pb.KVList) error {
+	stream.Send = func(buf *z.Buffer) error {
 		if count >= sampleSize {
 			return nil
 		}
-		for _, kv := range l.Kv {
+		err := buf.SliceIterate(func(s []byte) error {
+			var kv pb.KV
+			if err := kv.Unmarshal(s); err != nil {
+				return err
+			}
 			keys = append(keys, kv.Key)
 			count++
 			if count >= sampleSize {
 				cancel()
-				return nil
+				return errStop
 			}
+			return nil
+		})
+		if err == errStop || err == nil {
+			return nil
 		}
-		return nil
+		return err
 	}
 
 	if err := stream.Orchestrate(ctx); err != nil && err != context.Canceled {
diff --git a/badger/cmd/stream.go b/badger/cmd/stream.go
index 05c6cf1..a7d3ba5 100644
--- a/badger/cmd/stream.go
+++ b/badger/cmd/stream.go
@@ -17,6 +17,7 @@
 package cmd
 
 import (
+	"fmt"
 	"io"
 	"math"
 	"os"
@@ -50,9 +51,11 @@ func init() {
 	streamCmd.Flags().IntVarP(&numVersions, "num_versions", "", 0,
 		"Option to configure the maximum number of versions per key. "+
 			"Values <= 0 will be considered to have the max number of versions.")
-	streamCmd.Flags().Uint32VarP(&compressionType, "compression", "", 0,
+	streamCmd.Flags().Uint32VarP(&compressionType, "compression", "", 1,
 		"Option to configure the compression type in output DB. "+
 			"0 to disable, 1 for Snappy, and 2 for ZSTD.")
+	streamCmd.Flags().StringVarP(&keyPath, "encryption-key-file", "e", "",
+		"Path of the encryption key file.")
 }
 
 func stream(cmd *cobra.Command, args []string) error {
@@ -74,23 +77,37 @@ func stream(cmd *cobra.Command, args []string) error {
 	if numVersions <= 0 {
 		numVersions = math.MaxInt32
 	}
+	encKey, err := getKey(keyPath)
+	if err != nil {
+		return err
+	}
 	inOpt := badger.DefaultOptions(sstDir).
 		WithReadOnly(readOnly).
 		WithValueThreshold(1 << 10 /* 1KB */).
-		WithNumVersionsToKeep(numVersions)
+		WithNumVersionsToKeep(numVersions).
+		WithBlockCacheSize(100 << 20).
+		WithIndexCacheSize(200 << 20).
+		WithEncryptionKey(encKey)
 
 	// Options for output DB.
 	if compressionType < 0 || compressionType > 2 {
 		return errors.Errorf(
 			"compression value must be one of 0 (disabled), 1 (Snappy), or 2 (ZSTD)")
 	}
-	outOpt := inOpt.WithDir(outDir).WithValueDir(outDir).
-		WithCompression(options.CompressionType(compressionType)).WithReadOnly(false)
+	outOpt := inOpt.
+		WithDir(outDir).
+		WithValueDir(outDir).
+		WithNumVersionsToKeep(numVersions).
+		WithCompression(options.CompressionType(compressionType)).
+		WithReadOnly(false)
 
 	inDB, err := badger.OpenManaged(inOpt)
 	if err != nil {
 		return y.Wrapf(err, "cannot open DB at %s", sstDir)
 	}
 	defer inDB.Close()
-	return inDB.StreamDB(outOpt)
+
+	err = inDB.StreamDB(outOpt)
+	fmt.Println("Done.")
+	return err
 }
diff --git a/badger/cmd/write_bench.go b/badger/cmd/write_bench.go
index 91df956..418b3b3 100644
--- a/badger/cmd/write_bench.go
+++ b/badger/cmd/write_bench.go
@@ -66,7 +66,7 @@ var (
 	vlogMaxEntries   uint32
 	loadBloomsOnOpen bool
 	detectConflicts  bool
-	compression      bool
+	zstdComp         bool
 	showDir          bool
 	ttlDuration      string
 	showKeysCount    bool
@@ -113,8 +113,8 @@ func init() {
 		"Load Bloom filter on DB open.")
 	writeBenchCmd.Flags().BoolVar(&detectConflicts, "conficts", false,
 		"If true, it badger will detect the conflicts")
-	writeBenchCmd.Flags().BoolVar(&compression, "compression", true,
-		"If true, badger will use ZSTD mode")
+	writeBenchCmd.Flags().BoolVar(&zstdComp, "zstd", false,
+		"If true, badger will use ZSTD mode. Otherwise, use default.")
 	writeBenchCmd.Flags().BoolVar(&showDir, "show-dir", false,
 		"If true, the report will include the directory contents")
 	writeBenchCmd.Flags().StringVar(&dropAllPeriod, "dropall", "0s",
@@ -204,37 +204,38 @@ func writeSorted(db *badger.DB, num uint64) error {
 	}
 
 	wg := &sync.WaitGroup{}
-	writeCh := make(chan *pb.KVList, 3)
+	writeCh := make(chan *z.Buffer, 3)
 	writeRange := func(start, end uint64, streamId uint32) {
 		// end is not included.
 		defer wg.Done()
-		kvs := &pb.KVList{}
+		kvBuf := z.NewBuffer(5 << 20)
 		var sz int
 		for i := start; i < end; i++ {
 			key := make([]byte, 8)
 			binary.BigEndian.PutUint64(key, i)
-			kvs.Kv = append(kvs.Kv, &pb.KV{
+			kv := &pb.KV{
 				Key:      key,
 				Value:    value,
 				Version:  1,
 				StreamId: streamId,
-			})
+			}
+			badger.KVToBuffer(kv, kvBuf)
 
 			sz += es
 			atomic.AddUint64(&entriesWritten, 1)
 			atomic.AddUint64(&sizeWritten, uint64(es))
 
 			if sz >= 4<<20 { // 4 MB
-				writeCh <- kvs
-				kvs = &pb.KVList{}
+				writeCh <- kvBuf
+				kvBuf = z.NewBuffer(1 << 20)
 				sz = 0
 			}
 		}
-		writeCh <- kvs
+		writeCh <- kvBuf
 	}
 
 	// Let's create some streams.
-	width := num / 16
+	width := num / 4
 	streamID := uint32(0)
 	for start := uint64(0); start < num; start += width {
 		end := start + width
@@ -254,18 +255,13 @@ func writeSorted(db *badger.DB, num uint64) error {
 		if err := writer.Write(kvs); err != nil {
 			panic(err)
 		}
+		y.Check(kvs.Release())
 	}
 	log.Println("DONE streaming. Flushing...")
 	return writer.Flush()
 }
 
 func writeBench(cmd *cobra.Command, args []string) error {
-	var cmode options.CompressionType
-	if compression {
-		cmode = options.ZSTD
-	} else {
-		cmode = options.None
-	}
 	opt := badger.DefaultOptions(sstDir).
 		WithValueDir(vlogDir).
 		WithSyncWrites(syncWrites).
@@ -277,8 +273,10 @@ func writeBench(cmd *cobra.Command, args []string) error {
 		WithValueLogMaxEntries(vlogMaxEntries).
 		WithEncryptionKey([]byte(encryptionKey)).
 		WithDetectConflicts(detectConflicts).
-		WithCompression(cmode).
 		WithLoggingLevel(badger.INFO)
+	if zstdComp {
+		opt = opt.WithCompression(options.ZSTD)
+	}
 
 	if !showLogs {
 		opt = opt.WithLogger(nil)
@@ -314,6 +312,7 @@ func writeBench(cmd *cobra.Command, args []string) error {
 	}
 
 	c.SignalAndWait()
+	fmt.Printf(db.LevelsToString())
 	return err
 }
 
@@ -354,11 +353,13 @@ func reportStats(c *z.Closer, db *badger.DB) {
 	t := time.NewTicker(time.Second)
 	defer t.Stop()
 
+	var count int
 	for {
 		select {
 		case <-c.HasBeenClosed():
 			return
 		case <-t.C:
+			count++
 			if showKeysCount {
 				showKeysStats(db)
 			}
@@ -392,8 +393,14 @@ func reportStats(c *z.Closer, db *badger.DB) {
 			bytesRate := sz / uint64(dur.Seconds())
 			entriesRate := entries / uint64(dur.Seconds())
 			fmt.Printf("[WRITE] Time elapsed: %s, bytes written: %s, speed: %s/sec, "+
-				"entries written: %d, speed: %d/sec, gcSuccess: %d\n", y.FixedDuration(time.Since(startTime)),
-				humanize.Bytes(sz), humanize.Bytes(bytesRate), entries, entriesRate, gcSuccess)
+				"entries written: %d, speed: %d/sec, jemalloc: %s\n",
+				y.FixedDuration(time.Since(startTime)),
+				humanize.Bytes(sz), humanize.Bytes(bytesRate), entries, entriesRate,
+				humanize.IBytes(uint64(z.NumAllocBytes())))
+
+			if count%10 == 0 {
+				fmt.Printf(db.LevelsToString())
+			}
 		}
 	}
 }
diff --git a/badger/main.go b/badger/main.go
index 1d6bcd6..c284f5a 100644
--- a/badger/main.go
+++ b/badger/main.go
@@ -23,6 +23,9 @@ import (
 	"runtime"
 
 	"github.com/dgraph-io/badger/v2/badger/cmd"
+	"github.com/dgraph-io/ristretto/z"
+	"github.com/dustin/go-humanize"
+	"go.opencensus.io/zpages"
 )
 
 func main() {
@@ -36,7 +39,19 @@ func main() {
 			}
 		}
 	}()
+	zpages.Handle(nil, "/z")
 	runtime.SetBlockProfileRate(100)
 	runtime.GOMAXPROCS(128)
+
+	out := z.CallocNoRef(1)
+	fmt.Printf("jemalloc enabled: %v\n", len(out) > 0)
+	z.StatsPrint()
+	z.Free(out)
+
 	cmd.Execute()
+	fmt.Printf("Num Allocated Bytes at program end: %s\n",
+		humanize.IBytes(uint64(z.NumAllocBytes())))
+	if z.NumAllocBytes() > 0 {
+		z.PrintLeaks()
+	}
 }
diff --git a/batch.go b/batch.go
index 60fa2c0..79f28b6 100644
--- a/batch.go
+++ b/batch.go
@@ -18,9 +18,11 @@ package badger
 
 import (
 	"sync"
+	"sync/atomic"
 
 	"github.com/dgraph-io/badger/v2/pb"
 	"github.com/dgraph-io/badger/v2/y"
+	"github.com/dgraph-io/ristretto/z"
 	"github.com/pkg/errors"
 )
 
@@ -30,7 +32,7 @@ type WriteBatch struct {
 	txn      *Txn
 	db       *DB
 	throttle *y.Throttle
-	err      error
+	err      atomic.Value
 
 	isManaged bool
 	commitTs  uint64
@@ -82,31 +84,47 @@ func (wb *WriteBatch) Cancel() {
 	wb.txn.Discard()
 }
 
-// The caller of this callback must hold the lock.
 func (wb *WriteBatch) callback(err error) {
 	// sync.WaitGroup is thread-safe, so it doesn't need to be run inside wb.Lock.
 	defer wb.throttle.Done(err)
 	if err == nil {
 		return
 	}
-
-	if wb.err != nil {
+	if err := wb.Error(); err != nil {
 		return
 	}
-	wb.err = err
+	wb.err.Store(err)
+}
+
+func (wb *WriteBatch) writeKV(kv *pb.KV) error {
+	e := Entry{Key: kv.Key, Value: kv.Value}
+	if len(kv.UserMeta) > 0 {
+		e.UserMeta = kv.UserMeta[0]
+	}
+	y.AssertTrue(kv.Version != 0)
+	e.version = kv.Version
+	return wb.handleEntry(&e)
 }
 
-func (wb *WriteBatch) Write(kvList *pb.KVList) error {
+func (wb *WriteBatch) Write(buf *z.Buffer) error {
 	wb.Lock()
 	defer wb.Unlock()
-	for _, kv := range kvList.Kv {
-		e := Entry{Key: kv.Key, Value: kv.Value}
-		if len(kv.UserMeta) > 0 {
-			e.UserMeta = kv.UserMeta[0]
+
+	err := buf.SliceIterate(func(s []byte) error {
+		kv := &pb.KV{}
+		if err := kv.Unmarshal(s); err != nil {
+			return err
 		}
-		y.AssertTrue(kv.Version != 0)
-		e.version = kv.Version
-		if err := wb.handleEntry(&e); err != nil {
+		return wb.writeKV(kv)
+	})
+	return err
+}
+
+func (wb *WriteBatch) WriteList(kvList *pb.KVList) error {
+	wb.Lock()
+	defer wb.Unlock()
+	for _, kv := range kvList.Kv {
+		if err := wb.writeKV(kv); err != nil {
 			return err
 		}
 	}
@@ -135,7 +153,7 @@ func (wb *WriteBatch) handleEntry(e *Entry) error {
 	// This time the error must not be ErrTxnTooBig, otherwise, we make the
 	// error permanent.
 	if err := wb.txn.SetEntry(e); err != nil {
-		wb.err = err
+		wb.err.Store(err)
 		return err
 	}
 	return nil
@@ -172,7 +190,7 @@ func (wb *WriteBatch) Delete(k []byte) error {
 		return err
 	}
 	if err := wb.txn.Delete(k); err != nil {
-		wb.err = err
+		wb.err.Store(err)
 		return err
 	}
 	return nil
@@ -180,20 +198,20 @@ func (wb *WriteBatch) Delete(k []byte) error {
 
 // Caller to commit must hold a write lock.
 func (wb *WriteBatch) commit() error {
-	if wb.err != nil {
-		return wb.err
+	if err := wb.Error(); err != nil {
+		return err
 	}
 	if wb.finished {
 		return y.ErrCommitAfterFinish
 	}
 	if err := wb.throttle.Do(); err != nil {
-		wb.err = err
-		return wb.err
+		wb.err.Store(err)
+		return err
 	}
 	wb.txn.CommitWith(wb.callback)
 	wb.txn = wb.db.newTransaction(true, wb.isManaged)
 	wb.txn.commitTs = wb.commitTs
-	return wb.err
+	return wb.Error()
 }
 
 // Flush must be called at the end to ensure that any pending writes get committed to Badger. Flush
@@ -210,18 +228,18 @@ func (wb *WriteBatch) Flush() error {
 	wb.Unlock()
 
 	if err := wb.throttle.Finish(); err != nil {
-		if wb.err != nil {
-			return errors.Errorf("wb.err: %s err: %s", wb.err, err)
+		if wb.Error() != nil {
+			return errors.Errorf("wb.err: %s err: %s", wb.Error(), err)
 		}
 		return err
 	}
 
-	return wb.err
+	return wb.Error()
 }
 
 // Error returns any errors encountered so far. No commits would be run once an error is detected.
 func (wb *WriteBatch) Error() error {
-	wb.Lock()
-	defer wb.Unlock()
-	return wb.err
+	// If the interface conversion fails, the err will be nil.
+	err, _ := wb.err.Load().(error)
+	return err
 }
diff --git a/compaction.go b/compaction.go
index 6d0cbf6..56ce645 100644
--- a/compaction.go
+++ b/compaction.go
@@ -33,6 +33,10 @@ type keyRange struct {
 	inf   bool
 }
 
+func (r keyRange) isEmpty() bool {
+	return len(r.left) == 0 && len(r.right) == 0 && !r.inf
+}
+
 var infRange = keyRange{inf: true}
 
 func (r keyRange) String() string {
@@ -45,7 +49,26 @@ func (r keyRange) equals(dst keyRange) bool {
 		r.inf == dst.inf
 }
 
+func (r *keyRange) extend(kr keyRange) {
+	if r.isEmpty() {
+		*r = kr
+	}
+	if len(r.left) == 0 || y.CompareKeys(kr.left, r.left) < 0 {
+		r.left = kr.left
+	}
+	if len(r.right) == 0 || y.CompareKeys(kr.right, r.right) > 0 {
+		r.right = kr.right
+	}
+	if kr.inf {
+		r.inf = true
+	}
+}
+
 func (r keyRange) overlapsWith(dst keyRange) bool {
+	// Empty keyRange always overlaps.
+	if r.isEmpty() {
+		return true
+	}
 	if r.inf || dst.inf {
 		return true
 	}
@@ -127,6 +150,7 @@ func (lcs *levelCompactStatus) remove(dst keyRange) bool {
 type compactStatus struct {
 	sync.RWMutex
 	levels []*levelCompactStatus
+	tables map[uint64]struct{}
 }
 
 func (cs *compactStatus) overlapsWith(level int, this keyRange) bool {
@@ -151,11 +175,10 @@ func (cs *compactStatus) compareAndAdd(_ thisAndNextLevelRLocked, cd compactDef)
 	cs.Lock()
 	defer cs.Unlock()
 
-	level := cd.thisLevel.level
-
-	y.AssertTruef(level < len(cs.levels)-1, "Got level %d. Max levels: %d", level, len(cs.levels))
-	thisLevel := cs.levels[level]
-	nextLevel := cs.levels[level+1]
+	tl := cd.thisLevel.level
+	y.AssertTruef(tl < len(cs.levels)-1, "Got level %d. Max levels: %d", tl, len(cs.levels))
+	thisLevel := cs.levels[cd.thisLevel.level]
+	nextLevel := cs.levels[cd.nextLevel.level]
 
 	if thisLevel.overlapsWith(cd.thisRange) {
 		return false
@@ -171,6 +194,9 @@ func (cs *compactStatus) compareAndAdd(_ thisAndNextLevelRLocked, cd compactDef)
 	thisLevel.ranges = append(thisLevel.ranges, cd.thisRange)
 	nextLevel.ranges = append(nextLevel.ranges, cd.nextRange)
 	thisLevel.delSize += cd.thisSize
+	for _, t := range append(cd.top, cd.bot...) {
+		cs.tables[t.ID()] = struct{}{}
+	}
 	return true
 }
 
@@ -178,24 +204,31 @@ func (cs *compactStatus) delete(cd compactDef) {
 	cs.Lock()
 	defer cs.Unlock()
 
-	level := cd.thisLevel.level
-	y.AssertTruef(level < len(cs.levels)-1, "Got level %d. Max levels: %d", level, len(cs.levels))
+	tl := cd.thisLevel.level
+	y.AssertTruef(tl < len(cs.levels)-1, "Got level %d. Max levels: %d", tl, len(cs.levels))
 
-	thisLevel := cs.levels[level]
-	nextLevel := cs.levels[level+1]
+	thisLevel := cs.levels[cd.thisLevel.level]
+	nextLevel := cs.levels[cd.nextLevel.level]
 
 	thisLevel.delSize -= cd.thisSize
 	found := thisLevel.remove(cd.thisRange)
-	found = nextLevel.remove(cd.nextRange) && found
+	if !cd.nextRange.isEmpty() {
+		found = nextLevel.remove(cd.nextRange) && found
+	}
 
 	if !found {
 		this := cd.thisRange
 		next := cd.nextRange
-		fmt.Printf("Looking for: [%q, %q, %v] in this level.\n", this.left, this.right, this.inf)
+		fmt.Printf("Looking for: %s in this level %d.\n", this, tl)
 		fmt.Printf("This Level:\n%s\n", thisLevel.debug())
 		fmt.Println()
-		fmt.Printf("Looking for: [%q, %q, %v] in next level.\n", next.left, next.right, next.inf)
+		fmt.Printf("Looking for: %s in next level %d.\n", next, cd.nextLevel.level)
 		fmt.Printf("Next Level:\n%s\n", nextLevel.debug())
 		log.Fatal("keyRange not found")
 	}
+	for _, t := range append(cd.top, cd.bot...) {
+		_, ok := cs.tables[t.ID()]
+		y.AssertTrue(ok)
+		delete(cs.tables, t.ID())
+	}
 }
diff --git a/db.go b/db.go
index 7db5216..f0bbcb5 100644
--- a/db.go
+++ b/db.go
@@ -26,6 +26,7 @@ import (
 	"os"
 	"path/filepath"
 	"sort"
+	"strings"
 	"sync"
 	"sync/atomic"
 	"time"
@@ -85,11 +86,6 @@ type DB struct {
 	flushChan chan flushTask // For flushing memtables.
 	closeOnce sync.Once      // For closing DB only once.
 
-	// Number of log rotates since the last memtable flush. We will access this field via atomic
-	// functions. Since we are not going to use any 64bit atomic functions, there is no need for
-	// 64 bit alignment of this struct(see #311).
-	logRotates int32
-
 	blockWrites int32
 	isClosed    uint32
 
@@ -99,6 +95,7 @@ type DB struct {
 	registry   *KeyRegistry
 	blockCache *ristretto.Cache
 	indexCache *ristretto.Cache
+	allocPool  *z.AllocatorPool
 }
 
 const (
@@ -112,10 +109,11 @@ func checkAndSetOptions(opt *Options) error {
 	if opt.NumCompactors == 1 {
 		return errors.New("Cannot have 1 compactor. Need at least 2")
 	}
+
 	if opt.InMemory && (opt.Dir != "" || opt.ValueDir != "") {
 		return errors.New("Cannot use badger in Disk-less mode with Dir or ValueDir set")
 	}
-	opt.maxBatchSize = (15 * opt.MaxTableSize) / 100
+	opt.maxBatchSize = (15 * opt.MemTableSize) / 100
 	opt.maxBatchCount = opt.maxBatchSize / int64(skl.MaxNodeSize)
 
 	// We are limiting opt.ValueThreshold to maxValueThreshold for now.
@@ -127,8 +125,9 @@ func checkAndSetOptions(opt *Options) error {
 	// If ValueThreshold is greater than opt.maxBatchSize, we won't be able to push any data using
 	// the transaction APIs. Transaction batches entries into batches of size opt.maxBatchSize.
 	if int64(opt.ValueThreshold) > opt.maxBatchSize {
-		return errors.Errorf("Valuethreshold greater than max batch size of %d. Either "+
-			"reduce opt.ValueThreshold or increase opt.MaxTableSize.", opt.maxBatchSize)
+		return errors.Errorf("Valuethreshold %d greater than max batch size of %d. Either "+
+			"reduce opt.ValueThreshold or increase opt.MaxTableSize.",
+			opt.ValueThreshold, opt.maxBatchSize)
 	}
 	// ValueLogFileSize should be stricly LESS than 2<<30 otherwise we will
 	// overflow the uint32 when we mmap it in OpenMemtable.
@@ -141,10 +140,6 @@ func checkAndSetOptions(opt *Options) error {
 		return y.ErrZstdCgo
 	}
 
-	// Compact L0 on close if either it is set or if KeepL0InMemory is set. When
-	// keepL0InMemory is set we need to compact L0 on close otherwise we might lose data.
-	opt.CompactL0OnClose = opt.CompactL0OnClose
-
 	if opt.ReadOnly {
 		// Do not perform compaction in read only mode.
 		opt.CompactL0OnClose = false
@@ -224,6 +219,7 @@ func Open(opt Options) (*DB, error) {
 		valueDirGuard: valueDirLockGuard,
 		orc:           newOracle(opt),
 		pub:           newPublisher(),
+		allocPool:     z.NewAllocatorPool(8),
 	}
 	// Cleanup all the goroutines started by badger in case of an error.
 	defer func() {
@@ -257,7 +253,7 @@ func Open(opt Options) (*DB, error) {
 
 	if opt.IndexCacheSize > 0 {
 		// Index size is around 5% of the table size.
-		indexSz := int64(float64(opt.MaxTableSize) * 0.05)
+		indexSz := int64(float64(opt.MemTableSize) * 0.05)
 		numInCache := opt.IndexCacheSize / indexSz
 		if numInCache == 0 {
 			// Make the value of this variable at least one since the cache requires
@@ -304,9 +300,10 @@ func Open(opt Options) (*DB, error) {
 		return nil, y.Wrapf(err, "while opening memtables")
 	}
 
-	db.mt, err = db.newMemTable()
-	if err != nil {
-		return nil, y.Wrapf(err, "cannot create memtable")
+	if !db.opt.ReadOnly {
+		if db.mt, err = db.newMemTable(); err != nil {
+			return nil, y.Wrapf(err, "cannot create memtable")
+		}
 	}
 
 	// newLevelsController potentially loads files in directory.
@@ -334,14 +331,9 @@ func Open(opt Options) (*DB, error) {
 	db.orc.nextTxnTs = db.MaxVersion()
 	db.opt.Infof("Set nextTxnTs to %d", db.orc.nextTxnTs)
 
-	replayCloser := z.NewCloser(1)
-	go db.doWrites(replayCloser)
-
 	if err = db.vlog.open(db); err != nil {
-		replayCloser.SignalAndWait()
 		return db, y.Wrapf(err, "During db.vlog.open")
 	}
-	replayCloser.SignalAndWait() // Wait for replay to be applied first.
 
 	// Let's advance nextTxnTs to one more than whatever we observed via
 	// replaying the logs.
@@ -376,7 +368,10 @@ func (db *DB) MaxVersion() uint64 {
 		}
 	}
 	db.Lock()
-	update(db.mt.maxVersion)
+	// In read only mode, we do not create new mem table.
+	if !db.opt.ReadOnly {
+		update(db.mt.maxVersion)
+	}
 	for _, mt := range db.imm {
 		update(mt.maxVersion)
 	}
@@ -483,7 +478,10 @@ func (db *DB) IsClosed() bool {
 }
 
 func (db *DB) close() (err error) {
+	defer db.allocPool.Release()
+
 	db.opt.Debugf("Closing database")
+	db.opt.Infof("Lifetime L0 stalled for: %s\n", time.Duration(atomic.LoadInt64(&db.lc.l0stallsMs)))
 
 	atomic.StoreInt32(&db.blockWrites, 1)
 
@@ -511,30 +509,36 @@ func (db *DB) close() (err error) {
 	// and remove them completely, while the block / memtable writer is still
 	// trying to push stuff into the memtable. This will also resolve the value
 	// offset problem: as we push into memtable, we update value offsets there.
-	if !db.mt.sl.Empty() {
-		db.opt.Debugf("Flushing memtable")
-		for {
-			pushedFlushTask := func() bool {
-				db.Lock()
-				defer db.Unlock()
-				y.AssertTrue(db.mt != nil)
-				select {
-				case db.flushChan <- flushTask{mt: db.mt}:
-					db.imm = append(db.imm, db.mt) // Flusher will attempt to remove this from s.imm.
-					db.mt = nil                    // Will segfault if we try writing!
-					db.opt.Debugf("pushed to flush chan\n")
-					return true
-				default:
-					// If we fail to push, we need to unlock and wait for a short while.
-					// The flushing operation needs to update s.imm. Otherwise, we have a deadlock.
-					// TODO: Think about how to do this more cleanly, maybe without any locks.
+	if db.mt != nil {
+		if db.mt.sl.Empty() {
+			// Remove the memtable if empty.
+			db.mt.DecrRef()
+		} else {
+			db.opt.Debugf("Flushing memtable")
+			for {
+				pushedFlushTask := func() bool {
+					db.Lock()
+					defer db.Unlock()
+					y.AssertTrue(db.mt != nil)
+					select {
+					case db.flushChan <- flushTask{mt: db.mt}:
+						db.imm = append(db.imm, db.mt) // Flusher will attempt to remove this from s.imm.
+						db.mt = nil                    // Will segfault if we try writing!
+						db.opt.Debugf("pushed to flush chan\n")
+						return true
+					default:
+						// If we fail to push, we need to unlock and wait for a short while.
+						// The flushing operation needs to update s.imm. Otherwise, we have a
+						// deadlock.
+						// TODO: Think about how to do this more cleanly, maybe without any locks.
+					}
+					return false
+				}()
+				if pushedFlushTask {
+					break
 				}
-				return false
-			}()
-			if pushedFlushTask {
-				break
+				time.Sleep(10 * time.Millisecond)
 			}
-			time.Sleep(10 * time.Millisecond)
 		}
 	}
 	db.stopMemoryFlush()
@@ -555,6 +559,7 @@ func (db *DB) close() (err error) {
 		}
 	}
 
+	db.opt.Infof(db.LevelsToString())
 	if lcErr := db.lc.close(); err == nil {
 		err = y.Wrap(lcErr, "DB.Close")
 	}
@@ -621,17 +626,20 @@ func (db *DB) getMemTables() ([]*memTable, func()) {
 	db.RLock()
 	defer db.RUnlock()
 
-	tables := make([]*memTable, len(db.imm)+1)
+	var tables []*memTable
 
-	// Get mutable memtable.
-	tables[0] = db.mt
-	tables[0].IncrRef()
+	// Mutable memtable does not exist in read-only mode.
+	if !db.opt.ReadOnly {
+		// Get mutable memtable.
+		tables = append(tables, db.mt)
+		db.mt.IncrRef()
+	}
 
 	// Get immutable memtables.
 	last := len(db.imm) - 1
 	for i := range db.imm {
-		tables[i+1] = db.imm[last-i]
-		tables[i+1].IncrRef()
+		tables = append(tables, db.imm[last-i])
+		db.imm[last-i].IncrRef()
 	}
 	return tables, func() {
 		for _, tbl := range tables {
@@ -917,35 +925,13 @@ func (db *DB) ensureRoomForWrite() error {
 	db.Lock()
 	defer db.Unlock()
 
-	// Here we determine if we need to force flush memtable. Given we rotated log file, it would
-	// make sense to force flush a memtable, so the updated value head would have a chance to be
-	// pushed to L0. Otherwise, it would not go to L0, until the memtable has been fully filled,
-	// which can take a lot longer if the write load has fewer keys and larger values. This force
-	// flush, thus avoids the need to read through a lot of log files on a crash and restart.
-	// Above approach is quite simple with small drawback. We are calling ensureRoomForWrite before
-	// inserting every entry in Memtable. We will get latest db.head after all entries for a request
-	// are inserted in Memtable. If we have done >= db.logRotates rotations, then while inserting
-	// first entry in Memtable, below condition will be true and we will endup flushing old value of
-	// db.head. Hence we are limiting no of value log files to be read to db.logRotates only.
-	forceFlush := atomic.LoadInt32(&db.logRotates) >= db.opt.LogRotatesToFlush
-
-	// We don't need to force flush the memtable in in-memory mode because the size of the WAL will
-	// always be zero.
-	if !forceFlush && !db.opt.InMemory {
-		// Force flush if memTable WAL is getting filled up.
-		forceFlush = int64(db.mt.wal.writeAt) > db.opt.ValueLogFileSize
-	}
-
-	if !forceFlush && db.mt.sl.MemSize() < db.opt.MaxTableSize {
+	y.AssertTrue(db.mt != nil) // A nil mt indicates that DB is being closed.
+	if !db.mt.isFull() {
 		return nil
 	}
 
-	y.AssertTrue(db.mt != nil) // A nil mt indicates that DB is being closed.
 	select {
 	case db.flushChan <- flushTask{mt: db.mt}:
-		// After every memtable flush, let's reset the counter.
-		atomic.StoreInt32(&db.logRotates, 0)
-
 		db.opt.Debugf("Flushing memtable, mt.size=%d size of flushChan: %d\n",
 			db.mt.sl.MemSize(), len(db.flushChan))
 		// We manage to push this task. Let's modify imm.
@@ -963,16 +949,14 @@ func (db *DB) ensureRoomForWrite() error {
 }
 
 func arenaSize(opt Options) int64 {
-	return opt.MaxTableSize + opt.maxBatchSize + opt.maxBatchCount*int64(skl.MaxNodeSize)
+	return opt.MemTableSize + opt.maxBatchSize + opt.maxBatchCount*int64(skl.MaxNodeSize)
 }
 
 // buildL0Table builds a new table from the memtable.
-func buildL0Table(ft flushTask, bopts table.Options) []byte {
+func buildL0Table(ft flushTask, bopts table.Options) *table.Builder {
 	iter := ft.mt.sl.NewIterator()
 	defer iter.Close()
 	b := table.NewTableBuilder(bopts)
-	defer b.Close()
-
 	var vp valuePointer
 	for iter.SeekToFirst(); iter.Valid(); iter.Next() {
 		if len(ft.dropPrefixes) > 0 && hasAnyPrefixes(iter.Key(), ft.dropPrefixes) {
@@ -984,7 +968,7 @@ func buildL0Table(ft flushTask, bopts table.Options) []byte {
 		}
 		b.Add(iter.Key(), iter.Value(), vp.Len)
 	}
-	return b.Finish(true)
+	return b
 }
 
 type flushTask struct {
@@ -994,32 +978,32 @@ type flushTask struct {
 
 // handleFlushTask must be run serially.
 func (db *DB) handleFlushTask(ft flushTask) error {
-	// There can be a scenario, when empty memtable is flushed. For example, memtable is empty and
-	// after writing request to value log, rotation count exceeds db.LogRotatesToFlush.
+	// There can be a scenario, when empty memtable is flushed.
 	if ft.mt.sl.Empty() {
 		return nil
 	}
 
-	dk, err := db.registry.latestDataKey()
-	if err != nil {
-		return y.Wrapf(err, "failed to get datakey in db.handleFlushTask")
-	}
-	bopts := buildTableOptions(db.opt)
-	bopts.DataKey = dk
-	// Builder does not need cache but the same options are used for opening table.
-	bopts.BlockCache = db.blockCache
-	bopts.IndexCache = db.indexCache
-	tableData := buildL0Table(ft, bopts)
+	bopts := buildTableOptions(db)
+	builder := buildL0Table(ft, bopts)
+	defer builder.Close()
 
 	// buildL0Table can return nil if the none of the items in the skiplist are
 	// added to the builder. This can happen when drop prefix is set and all
 	// the items are skipped.
-	if len(tableData) == 0 {
+	if builder.Empty() {
+		builder.Finish()
 		return nil
 	}
 
 	fileID := db.lc.reserveFileID()
-	tbl, err := table.CreateTable(table.NewFilename(fileID, db.opt.Dir), tableData, bopts)
+	var tbl *table.Table
+	var err error
+	if db.opt.InMemory {
+		data := builder.Finish()
+		tbl, err = table.OpenInMemoryTable(data, fileID, &bopts)
+	} else {
+		tbl, err = table.CreateTable(table.NewFilename(fileID, db.opt.Dir), builder)
+	}
 	if err != nil {
 		return y.Wrap(err, "error while creating table")
 	}
@@ -1311,6 +1295,11 @@ func (db *DB) Tables() []TableInfo {
 	return db.lc.getTableInfo()
 }
 
+// Levels gets the LevelInfo.
+func (db *DB) Levels() []LevelInfo {
+	return db.lc.getLevelInfo()
+}
+
 // KeySplits can be used to get rough key ranges to divide up iteration over
 // the DB.
 func (db *DB) KeySplits(prefix []byte) []string {
@@ -1344,6 +1333,9 @@ func (db *DB) KeySplits(prefix []byte) []string {
 	if len(splits) < 32 {
 		maxPerSplit := 10000
 		mtSplits := func(mt *memTable) {
+			if mt == nil {
+				return
+			}
 			count := 0
 			iter := mt.sl.NewIterator()
 			for iter.SeekToFirst(); iter.Valid(); iter.Next() {
@@ -1443,6 +1435,7 @@ func (db *DB) startMemoryFlush() {
 // stopped. Ideally, no writes are going on during Flatten. Otherwise, it would create competition
 // between flattening the tree and new tables being created at level zero.
 func (db *DB) Flatten(workers int) error {
+
 	db.stopCompactions()
 	defer db.startCompactions()
 
@@ -1478,13 +1471,14 @@ func (db *DB) Flatten(workers int) error {
 		return humanize.Bytes(uint64(sz))
 	}
 
+	t := db.lc.levelTargets()
 	for {
 		db.opt.Infof("\n")
 		var levels []int
 		for i, l := range db.lc.levels {
 			sz := l.getTotalSize()
 			db.opt.Infof("Level: %d. %8s Size. %8s Max.\n",
-				i, hbytes(l.getTotalSize()), hbytes(l.maxTotalSize))
+				i, hbytes(l.getTotalSize()), hbytes(t.targetSz[i]))
 			if sz > 0 {
 				levels = append(levels, i)
 			}
@@ -1635,7 +1629,10 @@ func (db *DB) dropAll() (func(), error) {
 // - Compact rest of the levels, Li->Li, picking tables which have Kp.
 // - Resume memtable flushes, compactions and writes.
 func (db *DB) DropPrefix(prefixes ...[]byte) error {
-	db.opt.Infof("DropPrefix Called")
+	if len(prefixes) == 0 {
+		return nil
+	}
+	db.opt.Infof("DropPrefix called for %s", prefixes)
 	f, err := db.prepareToDrop()
 	if err != nil {
 		return err
@@ -1785,8 +1782,9 @@ func (db *DB) StreamDB(outOptions Options) error {
 	// Stream contents of DB to the output DB.
 	stream := db.NewStreamAt(math.MaxUint64)
 	stream.LogPrefix = fmt.Sprintf("Streaming DB to new DB at %s", outDir)
-	stream.Send = func(kvs *pb.KVList) error {
-		return writer.Write(kvs)
+
+	stream.Send = func(buf *z.Buffer) error {
+		return writer.Write(buf)
 	}
 	if err := stream.Orchestrate(context.Background()); err != nil {
 		return y.Wrapf(err, "cannot stream DB to out DB at %s", outDir)
@@ -1840,3 +1838,28 @@ func (db *DB) CacheMaxCost(cache CacheType, maxCost int64) (int64, error) {
 		return 0, errors.Errorf("invalid cache type")
 	}
 }
+
+func (db *DB) LevelsToString() string {
+	levels := db.Levels()
+	h := func(sz int64) string {
+		return humanize.IBytes(uint64(sz))
+	}
+	base := func(b bool) string {
+		if b {
+			return "B"
+		}
+		return " "
+	}
+
+	var b strings.Builder
+	b.WriteRune('\n')
+	for _, li := range levels {
+		b.WriteString(fmt.Sprintf(
+			"Level %d [%s]: NumTables: %02d. Size: %s of %s. Score: %.2f->%.2f"+
+				" Target FileSize: %s\n",
+			li.Level, base(li.IsBaseLevel), li.NumTables,
+			h(li.Size), h(li.TargetSize), li.Score, li.Adjusted, h(li.TargetFileSize)))
+	}
+	b.WriteString("Level Done\n")
+	return b.String()
+}
diff --git a/discard.go b/discard.go
index 7b2d3d5..9d7d3da 100644
--- a/discard.go
+++ b/discard.go
@@ -38,15 +38,12 @@ type discardStats struct {
 }
 
 const discardFname string = "DISCARD"
-const discardFsize int = 1 << 30
-const maxSlot int = 64 << 20
 
-// TODO(naman): Add a test for this.
 func initDiscardStats(opt Options) (*discardStats, error) {
 	fname := path.Join(opt.ValueDir, discardFname)
 
 	// 1GB file can store 67M discard entries. Each entry is 16 bytes.
-	mf, err := z.OpenMmapFile(fname, os.O_CREATE|os.O_RDWR, discardFsize)
+	mf, err := z.OpenMmapFile(fname, os.O_CREATE|os.O_RDWR, 1<<20)
 	lf := &discardStats{
 		MmapFile: mf,
 		opt:      opt,
@@ -59,7 +56,7 @@ func initDiscardStats(opt Options) (*discardStats, error) {
 		return nil, y.Wrapf(err, "while opening file: %s\n", discardFname)
 	}
 
-	for slot := 0; slot < maxSlot; slot++ {
+	for slot := 0; slot < lf.maxSlot(); slot++ {
 		if lf.get(16*slot) == 0 {
 			lf.nextEmptySlot = slot
 			break
@@ -99,6 +96,10 @@ func (lf *discardStats) zeroOut() {
 	lf.set(lf.nextEmptySlot*16+8, 0)
 }
 
+func (lf *discardStats) maxSlot() int {
+	return len(lf.Data) / 16
+}
+
 // Update would update the discard stats for the given file id. If discard is
 // 0, it would return the current value of discard for the file. If discard is
 // < 0, it would set the current value of discard to zero for the file.
@@ -135,7 +136,9 @@ func (lf *discardStats) Update(fidu uint32, discard int64) int64 {
 
 	// Move to next slot.
 	lf.nextEmptySlot++
-	y.AssertTrue(lf.nextEmptySlot < maxSlot)
+	for lf.nextEmptySlot >= lf.maxSlot() {
+		y.Check(lf.Truncate(2 * int64(len(lf.Data))))
+	}
 	lf.zeroOut()
 
 	sort.Sort(lf)
diff --git a/docs/content/_index.md b/docs/content/_index.md
index 64980ce..3139bce 100644
--- a/docs/content/_index.md
+++ b/docs/content/_index.md
@@ -4,95 +4,79 @@ date: 2020-07-06T17:43:29+05:30
 draft: false
 ---
 
-![Badger mascot](/images/diggy-shadow.png)
-
-**Welcome to the official Badger documentation.**
-
-BadgerDB is an embeddable, persistent and fast key-value (KV) database written
-in pure Go. It is the underlying database for [Dgraph](https://dgraph.io), a
+<div class="landing">
+  <div class="hero">
+    <p>
+BadgerDB is an embeddable, persistent, and fast key-value (KV) database written
+in pure Go. It is the underlying database for <a href="https://dgraph.io">Dgraph</a>, a
 fast, distributed graph database. It's meant to be a performant alternative to
 non-Go-based key-value stores like RocksDB.
+    </p>
+    <img class="hero-deco" src="/images/diggy-shadow.png" />
+  </div>
+  <div class="item">
+    <div class="icon"><i class="lni lni-play" aria-hidden="true"></i></div>
+    <a  href="{{< relref "get-started/_index.md">}}">
+      <h3>Quickstart Guide</h3>
+      <p>
+        A single page quickstart guide to get started with BadgerDB
+      </p>
+    </a>
+  </div>
+  <div class="item">
+    <div class="icon"><i class="lni lni-book" aria-hidden="true"></i></div>
+    <a href="{{< relref "resources/_index.md">}}">
+      <h3>Resources</h3>
+      <p>
+        Additional resources and information
+      </p>
+    </a>
+  </div>
+  <div class="item">
+    <div class="icon"><i class="lni lni-layers" aria-hidden="true"></i></div>
+    <a href="{{< relref "design/_index.md">}}">
+      <h3>Design</h3>
+      <p>
+        Design goals behind BadgerDB
+      </p>
+    </a>
+  </div>
 
-## Table of Contents
-
-<section class="toc">
-  <div class="container">
-    <div class="row row-no-padding">
-      <div class="col-12 col-sm-6">
-        <div class="section-item">
-          <div class="section-name">
-            <a href="{{< relref "get-started/_index.md">}}">
-              Quickstart Guide
-            </a>
-          </div>
-          <p class="section-desc">
-            A single page quickstart guide to get started with BadgerDB
-          </p>
-        </div>
-      </div>
-      <div class="col-12 col-sm-6">
-        <div class="section-item">
-          <div class="section-name">
-            <a href="{{< relref "resources/_index.md">}}">
-              Resources
-            </a>
-          </div>
-          <p class="section-desc">
-            Additional resources and information
-          </p>
-        </div>
-      </div>
-      <div class="col-12 col-sm-6">
-        <div class="section-item">
-          <div class="section-name">
-            <a href="{{< relref "design/_index.md">}}">
-              Design
-            </a>
-          </div>
-          <p class="section-desc">
-            Design goals behind BadgerDB
-          </p>
-        </div>
-      </div>
-      <div class="col-12 col-sm-6">
-        <div class="section-item">
-          <div class="section-name">
-            <a href="{{< relref "projects-using-badger/_index.md">}}">
-              Projects using Badger
-            </a>
-          </div>
-          <p class="section-desc">
-            A list of known projects that use BadgerDB
-          </p>
-        </div>
-      </div>
-      <div class="col-12 col-sm-6">
-        <div class="section-item">
-          <div class="section-name">
-            <a href="/faq">
-              FAQ
-            </a>
-          </div>
-          <p class="section-desc">
-            Frequently asked questions
-          </p>
-        </div>
-      </div>
-      <div class="col-12 col-sm-6">
-        <div class="section-item">
-          <div class="section-name">
-            <a href="https://dgraph.io/badger">
-              Badger
-            </a>
-          </div>
-          <p class="section-desc">
-            Embeddable, persistent and fast key-value database that powers Dgraph
-          </p>
-        </div>
-      </div>
-    </div>
+  <div class="item">
+    <div class="icon"><i class="lni lni-direction-alt" aria-hidden="true"></i></div>
+    <a href="{{< relref "projects-using-badger/_index.md">}}">
+      <h3>Projects using Badger</h3>
+      <p>
+        A list of known projects that use BadgerDB
+      </p>
+    </a>
   </div>
-</section>
+  <div class="item">
+    <div class="icon"><i class="lni lni-question-circle" aria-hidden="true"></i></div>
+    <a href="{{< relref "faq/_index.md">}}">
+      <h3>FAQ</h3>
+      <p>
+        Frequently asked questions
+      </p>
+    </a>
+  </div>
+  <div class="item">
+    <div class="icon"><i class="lni lni-database" aria-hidden="true"></i></div>
+    <a href="https://dgraph.io/badger">
+      <h3>Badger</h3>
+      <p>
+        Embeddable, persistent, and fast key-value database that powers Dgraph
+      </p>
+    </a>
+  </div>
+
+</div>
+
+<style>
+  ul.contents {
+    display: none;
+  }
+</style>
 
 ## Changelog
 
diff --git a/docs/content/resources/index.md b/docs/content/resources/index.md
index 45e2dba..e90d210 100644
--- a/docs/content/resources/index.md
+++ b/docs/content/resources/index.md
@@ -12,10 +12,9 @@ Go](https://open.dgraph.io/post/badger/)
 4. [Concurrent ACID Transactions in Badger](https://blog.dgraph.io/post/badger-txn/)
 
 ## Contact
-- Please use [discuss.dgraph.io](https://discuss.dgraph.io) for questions, feature requests and discussions.
-- Please use [Github issue tracker](https://github.com/dgraph-io/badger/issues) for filing bugs or feature requests.
+- Please use [discuss.dgraph.io](https://discuss.dgraph.io) for questions, bugs, feature requests, and discussions.
 - Follow us on Twitter [@dgraphlabs](https://twitter.com/dgraphlabs).
 
 ## Contributing
 
-If you're interested in contributing to Badger see [CONTRIBUTING.md](https://github.com/dgraph-io/badger/blob/master/CONTRIBUTING.md).
\ No newline at end of file
+If you're interested in contributing to Badger see [CONTRIBUTING.md](https://github.com/dgraph-io/badger/blob/master/CONTRIBUTING.md).
diff --git a/docs/themes/hugo-docs/layouts/partials/header.html b/docs/themes/hugo-docs/layouts/partials/header.html
index 1a29f44..ef7e835 100644
--- a/docs/themes/hugo-docs/layouts/partials/header.html
+++ b/docs/themes/hugo-docs/layouts/partials/header.html
@@ -44,6 +44,7 @@
     <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
     <link href="//fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet" />
     <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous" />
+    <link href="https://cdn.lineicons.com/2.0/LineIcons.css" rel="stylesheet" crossorigin="anonymous" />
 
     <!-- DocSearch -->
     <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css" />
diff --git a/docs/themes/hugo-docs/static/css/theme.css b/docs/themes/hugo-docs/static/css/theme.css
index aa180f3..9d77ab4 100644
--- a/docs/themes/hugo-docs/static/css/theme.css
+++ b/docs/themes/hugo-docs/static/css/theme.css
@@ -621,3 +621,128 @@ section.toc .section-item {
     border-top: 1px solid #ccc !important;
   }
 }
+
+:root {
+  --cols: 1;
+  --width: 90vw;
+  --gap: 5vw;
+}
+@media screen and (min-width: 680px) {
+  :root {
+    --cols: 2;
+    --width: 300px;
+    --gap: 10px;
+  }
+}
+@media screen and (min-width: 1000px) {
+  :root {
+    --cols: 3;
+    --width: 300px;
+    --gap: 10px;
+  }
+}
+@media screen and (min-width: 1200px) {
+  :root {
+    --cols: 3;
+    --width: 320px;
+    --gap: 30px;
+  }
+}
+.landing {
+  display: flex;
+  flex-direction: row;
+  align-items: stretch;
+  flex-wrap: wrap;
+
+  margin: 0 calc(0px - var(--gap));
+  width: calc(var(--cols) * (var(--width) + 2 * var(--gap)));
+}
+.landing .hero {
+  width: calc(var(--cols) * (var(--width) + 2 * var(--gap)));
+  padding: 0 var(--gap);
+}
+.landing .hero p {
+  width: 50%;
+}
+@media screen and (max-width: 1000px) {
+  .landing .hero img {
+    display: none;
+  }
+  .landing .hero p {
+    width: 100%;
+  }
+}
+
+.landing .hero img {
+  right: var(--gap);
+}
+.landing .item {
+  padding: 72px 32px 32px;
+  border-radius: 4px;
+  box-shadow: 0 0 6px 1px #ccc;
+  display: inline-block;
+  margin-bottom: 24px;
+  position: relative;
+
+  box-sizing: border-box;
+  width: var(--width);
+  margin: var(--gap);
+}
+
+.landing .item h3 {
+  margin: 0 0 8px;
+  padding: 0;
+  font-size: 1rem;
+  font-weight: bold;
+}
+.landing .item h3:hover, .landing .item .cta:hover  {
+  text-decoration: underline;
+}
+
+.landing .item .icon {
+  position: absolute;
+  left: 32px;
+  top: 32px;
+  height: 24px;
+  width: 24px;
+  font-size: 24px;
+  line-height: 24px;
+}
+
+.landing .item .icon * {
+  fill: #fb5812;
+  color: #fb5812;
+}
+
+.landing .item p {
+  margin: 0 0 16px;
+  padding: 0;
+  color: #000;
+}
+.landing .item a:hover {
+  text-decoration: none;
+}
+
+.landing .item p.cta {
+  margin: 0 0 16px;
+  padding: 0;
+  color: #fb5812;
+}
+
+.landing .hero {
+  position: relative;
+  height: 160px;
+  margin-bottom: 32px;
+}
+.landing .hero h1 {
+  padding: 24px 0 8px;
+  font-size: 24px;
+}
+.landing .hero img {
+  position: absolute;
+  border-radius: 4px;
+  overflow: hidden;
+  height: 160px;
+  top: 0;
+  max-width: 45%;
+}
diff --git a/fb/TableIndex.go b/fb/TableIndex.go
index 72bea12..6c5d794 100644
--- a/fb/TableIndex.go
+++ b/fb/TableIndex.go
@@ -80,28 +80,28 @@ func (rcv *TableIndex) MutateBloomFilter(j int, n byte) bool {
 	return false
 }
 
-func (rcv *TableIndex) EstimatedSize() uint32 {
+func (rcv *TableIndex) MaxVersion() uint64 {
 	o := flatbuffers.UOffsetT(rcv._tab.Offset(8))
 	if o != 0 {
-		return rcv._tab.GetUint32(o + rcv._tab.Pos)
+		return rcv._tab.GetUint64(o + rcv._tab.Pos)
 	}
 	return 0
 }
 
-func (rcv *TableIndex) MutateEstimatedSize(n uint32) bool {
-	return rcv._tab.MutateUint32Slot(8, n)
+func (rcv *TableIndex) MutateMaxVersion(n uint64) bool {
+	return rcv._tab.MutateUint64Slot(8, n)
 }
 
-func (rcv *TableIndex) MaxVersion() uint64 {
+func (rcv *TableIndex) KeyCount() uint32 {
 	o := flatbuffers.UOffsetT(rcv._tab.Offset(10))
 	if o != 0 {
-		return rcv._tab.GetUint64(o + rcv._tab.Pos)
+		return rcv._tab.GetUint32(o + rcv._tab.Pos)
 	}
 	return 0
 }
 
-func (rcv *TableIndex) MutateMaxVersion(n uint64) bool {
-	return rcv._tab.MutateUint64Slot(10, n)
+func (rcv *TableIndex) MutateKeyCount(n uint32) bool {
+	return rcv._tab.MutateUint32Slot(10, n)
 }
 
 func (rcv *TableIndex) UncompressedSize() uint32 {
@@ -116,7 +116,7 @@ func (rcv *TableIndex) MutateUncompressedSize(n uint32) bool {
 	return rcv._tab.MutateUint32Slot(12, n)
 }
 
-func (rcv *TableIndex) KeyCount() uint32 {
+func (rcv *TableIndex) OnDiskSize() uint32 {
 	o := flatbuffers.UOffsetT(rcv._tab.Offset(14))
 	if o != 0 {
 		return rcv._tab.GetUint32(o + rcv._tab.Pos)
@@ -124,7 +124,7 @@ func (rcv *TableIndex) KeyCount() uint32 {
 	return 0
 }
 
-func (rcv *TableIndex) MutateKeyCount(n uint32) bool {
+func (rcv *TableIndex) MutateOnDiskSize(n uint32) bool {
 	return rcv._tab.MutateUint32Slot(14, n)
 }
 
@@ -143,17 +143,17 @@ func TableIndexAddBloomFilter(builder *flatbuffers.Builder, bloomFilter flatbuff
 func TableIndexStartBloomFilterVector(builder *flatbuffers.Builder, numElems int) flatbuffers.UOffsetT {
 	return builder.StartVector(1, numElems, 1)
 }
-func TableIndexAddEstimatedSize(builder *flatbuffers.Builder, estimatedSize uint32) {
-	builder.PrependUint32Slot(2, estimatedSize, 0)
-}
 func TableIndexAddMaxVersion(builder *flatbuffers.Builder, maxVersion uint64) {
-	builder.PrependUint64Slot(3, maxVersion, 0)
+	builder.PrependUint64Slot(2, maxVersion, 0)
+}
+func TableIndexAddKeyCount(builder *flatbuffers.Builder, keyCount uint32) {
+	builder.PrependUint32Slot(3, keyCount, 0)
 }
 func TableIndexAddUncompressedSize(builder *flatbuffers.Builder, uncompressedSize uint32) {
 	builder.PrependUint32Slot(4, uncompressedSize, 0)
 }
-func TableIndexAddKeyCount(builder *flatbuffers.Builder, keyCount uint32) {
-	builder.PrependUint32Slot(5, keyCount, 0)
+func TableIndexAddOnDiskSize(builder *flatbuffers.Builder, onDiskSize uint32) {
+	builder.PrependUint32Slot(5, onDiskSize, 0)
 }
 func TableIndexEnd(builder *flatbuffers.Builder) flatbuffers.UOffsetT {
 	return builder.EndObject()
diff --git a/fb/flatbuffer.fbs b/fb/flatbuffer.fbs
index 580f183..c76fa7d 100644
--- a/fb/flatbuffer.fbs
+++ b/fb/flatbuffer.fbs
@@ -19,10 +19,10 @@ namespace fb;
 table TableIndex {
   offsets:[BlockOffset];
   bloom_filter:[ubyte];
-  estimated_size:uint32;
   max_version:uint64;
-  uncompressed_size:uint32;
   key_count:uint32;
+  uncompressed_size:uint32;
+  on_disk_size:uint32;
 }
 
 table BlockOffset {
diff --git a/go.mod b/go.mod
index a0a0fb9..c28fd56 100644
--- a/go.mod
+++ b/go.mod
@@ -7,7 +7,7 @@ go 1.12
 require (
 	github.com/DataDog/zstd v1.4.1
 	github.com/cespare/xxhash v1.1.0
-	github.com/dgraph-io/ristretto v0.0.4-0.20201007164332-9739cfa2564b
+	github.com/dgraph-io/ristretto v0.0.4-0.20201125174811-766bca5e9938
 	github.com/dustin/go-humanize v1.0.0
 	github.com/golang/protobuf v1.3.1
 	github.com/golang/snappy v0.0.1
@@ -17,7 +17,8 @@ require (
 	github.com/spaolacci/murmur3 v1.1.0 // indirect
 	github.com/spf13/cobra v0.0.5
 	github.com/stretchr/testify v1.4.0
-	golang.org/x/net v0.0.0-20190620200207-3b0461eec859
-	golang.org/x/sys v0.0.0-20200918174421-af09f7315aff
+	go.opencensus.io v0.22.5
+	golang.org/x/net v0.0.0-20201021035429-f5854403a974
+	golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f
 	gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
 )
diff --git a/go.sum b/go.sum
index ccd9e6f..7f46653 100644
--- a/go.sum
+++ b/go.sum
@@ -1,3 +1,4 @@
+cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 github.com/DataDog/zstd v1.4.1 h1:3oxKN3wbHibqx897utPC2LTQU4J+IHWWJO+glkAkpFM=
 github.com/DataDog/zstd v1.4.1/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
@@ -6,51 +7,73 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE
 github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
 github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
 github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
+github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
 github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
 github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
 github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
+github.com/cosiner/argv v0.1.0/go.mod h1:EusR6TucWKX+zFgtdUsKT2Cvg45K5rtpCcWz4hK06d8=
 github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
+github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-github.com/dgraph-io/ristretto v0.0.4-0.20201007164332-9739cfa2564b h1:CfEwAh85rqfhhE/DrDCbFdcwVpZ8ESnFRhLatiKdDYM=
-github.com/dgraph-io/ristretto v0.0.4-0.20201007164332-9739cfa2564b/go.mod h1:bDI4cDaalvYSji3vBVDKrn9ouDZrwN974u8ZO/AhYXs=
+github.com/dgraph-io/ristretto v0.0.4-0.20201125174811-766bca5e9938 h1:FdSJif9oUVeH+MpsScsrL6OAbdW0pUYvXmkdhDSWWcQ=
+github.com/dgraph-io/ristretto v0.0.4-0.20201125174811-766bca5e9938/go.mod h1:tv2ec8nA7vRpSYX7/MbP52ihrUMXIHit54CQMq8npXQ=
 github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
 github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
 github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
 github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
 github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
+github.com/go-delve/delve v1.5.0/go.mod h1:c6b3a1Gry6x8a4LGCe/CWzrocrfaHvkUxCj3k4bvSUQ=
+github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
+github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
+github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I=
+github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
+github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
+github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
 github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
 github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
 github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
 github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
 github.com/google/flatbuffers v1.12.0 h1:/PtAHvnBY4Kqnx/xCQ3OIV9uYcSFGScBsWI3Oogeh6w=
 github.com/google/flatbuffers v1.12.0/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
+github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
+github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
+github.com/google/go-dap v0.2.0/go.mod h1:5q8aYQFnHOAZEMP+6vmq25HKYAEwE+LF5yh7JKrrhSQ=
+github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
 github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
 github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
 github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
+github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
 github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
 github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
 github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
 github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
+github.com/mattn/go-colorable v0.0.0-20170327083344-ded68f7a9561/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
+github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
 github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
 github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
+github.com/mmcloughlin/avo v0.0.0-20201105074841-5d2f697d268f/go.mod h1:6aKT4zZIrpGqB3RpFU14ByCSSyKY6LfJz4J/JJChHfI=
 github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
+github.com/peterh/liner v0.0.0-20170317030525-88609521dc4b/go.mod h1:xIteQHvHuaLYG9IFj6mSxM0fCKrs34IrEQUhOYuGPHc=
 github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
 github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
+github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
 github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
 github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
 github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
 github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
 github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
+github.com/spf13/cobra v0.0.0-20170417170307-b6cb39589372/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
 github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
 github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
 github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
+github.com/spf13/pflag v0.0.0-20170417173400-9e4c21054fa1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
 github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
 github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
 github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
@@ -58,19 +81,73 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
 github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
 github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
+github.com/twitchyliquid64/golang-asm v0.15.0/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
 github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
 github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
+github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
+go.opencensus.io v0.22.5 h1:dntmOdLpSpHlVqbW5Eay97DelsZHe+55D+xC6i0dDS0=
+go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
+go.starlark.net v0.0.0-20190702223751-32f345186213/go.mod h1:c1/X6cHgvdXj6pUlmWKMkuqRnW4K8x2vwt6JAaaircg=
+golang.org/x/arch v0.0.0-20190927153633-4e8777c89be4/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
+golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
 golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
-golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
+golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
+golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
+golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
+golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
+golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
+golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
+golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
+golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
+golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
+golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
 golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/net v0.0.0-20201021035429-f5854403a974 h1:IX6qOQeG5uLjB/hjjwjedwfjND0hgjPMMyO1RoIXQNI=
+golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
+golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
+golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
-golang.org/x/sys v0.0.0-20200918174421-af09f7315aff h1:1CPUrky56AcgSpxz/KfgzQWzfG09u5YOL8MvPYBlrL8=
-golang.org/x/sys v0.0.0-20200918174421-af09f7315aff/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA=
+golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
+golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
+golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.0.0-20191127201027-ecd32218bd7f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.0.0-20201105001634-bc3cf281b174/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
+google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
+google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
+google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb h1:i1Ppqkc3WQXikh8bXiwHqAN5Rv3/qDCcRk0/Otx73BY=
+google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
+google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
+google.golang.org/grpc v1.20.1 h1:Hz2g2wirWK7H0qIIhGIqRGTuMwTE8HEKFnDZZ7lm9NU=
+google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
 gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
+rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
diff --git a/iterator.go b/iterator.go
index 9eefbac..d6af3d8 100644
--- a/iterator.go
+++ b/iterator.go
@@ -20,11 +20,13 @@ import (
 	"bytes"
 	"fmt"
 	"hash/crc32"
+	"math"
 	"sort"
 	"sync"
 	"sync/atomic"
 	"time"
 
+	"github.com/dgraph-io/badger/v2/pb"
 	"github.com/dgraph-io/badger/v2/table"
 
 	"github.com/dgraph-io/badger/v2/y"
@@ -170,8 +172,33 @@ func (item *Item) yieldItemValue() ([]byte, func(), error) {
 	if err != nil {
 		db.opt.Logger.Errorf("Unable to read: Key: %v, Version : %v, meta: %v, userMeta: %v"+
 			" Error: %v", key, item.version, item.meta, item.userMeta, err)
+		var txn *Txn
+		if db.opt.managedTxns {
+			txn = db.NewTransactionAt(math.MaxUint64, false)
+		} else {
+			txn = db.NewTransaction(false)
+		}
+		defer txn.Discard()
+
+		iopt := DefaultIteratorOptions
+		iopt.AllVersions = true
+		iopt.InternalAccess = true
+		iopt.PrefetchValues = false
+
+		it := txn.NewKeyIterator(item.Key(), iopt)
+		defer it.Close()
+		for it.Rewind(); it.Valid(); it.Next() {
+			item := it.Item()
+			var vp valuePointer
+			if item.meta&bitValuePointer > 0 {
+				vp.Decode(item.vptr)
+			}
+			db.opt.Logger.Errorf("Key: %v, Version : %v, meta: %v, userMeta: %v valuePointer: %+v",
+				item.Key(), item.version, item.meta, item.userMeta, vp)
+		}
 	}
-	return result, cb, err
+	// Don't return error if we cannot read the value. Just log the error.
+	return result, cb, nil
 }
 
 func runCallback(cb func()) {
@@ -327,7 +354,6 @@ func (opt *IteratorOptions) pickTable(t table.TableInterface) bool {
 	// Bloom filter lookup would only work if opt.Prefix does NOT have the read
 	// timestamp as part of the key.
 	if opt.prefixIsKey && t.DoesNotHave(y.Hash(opt.Prefix)) {
-		y.NumLSMBloomHits.Add("pickTable", 1)
 		return false
 	}
 	return true
@@ -342,6 +368,8 @@ func (opt *IteratorOptions) pickTables(all []*table.Table) []*table.Table {
 		return out
 	}
 	sIdx := sort.Search(len(all), func(i int) bool {
+		// table.Biggest >= opt.prefix
+		// if opt.Prefix < table.Biggest, then surely it is not in any of the preceding tables.
 		return opt.compareToPrefix(all[i].Biggest()) >= 0
 	})
 	if sIdx == len(all) {
@@ -359,11 +387,23 @@ func (opt *IteratorOptions) pickTables(all []*table.Table) []*table.Table {
 		return out
 	}
 
+	// opt.prefixIsKey == true. This code is optimizing for opt.prefixIsKey part.
 	var out []*table.Table
+	hash := y.Hash(opt.Prefix)
 	for _, t := range filtered {
-		if opt.pickTable(t) {
-			out = append(out, t)
+		// When we encounter the first table whose smallest key is higher than opt.Prefix, we can
+		// stop. This is an IMPORTANT optimization, just considering how often we call
+		// NewKeyIterator.
+		if opt.compareToPrefix(t.Smallest()) > 0 {
+			// if table.Smallest > opt.Prefix, then this and all tables after this can be ignored.
+			break
+		}
+		// opt.Prefix is actually the key. So, we can run bloom filter checks
+		// as well.
+		if t.DoesNotHave(hash) {
+			continue
 		}
+		out = append(out, t)
 	}
 	return out
 }
@@ -395,6 +435,8 @@ type Iterator struct {
 	// the iterator. It can be used, for example, to uniquely identify each of the
 	// iterators created by the stream interface
 	ThreadId int
+
+	reuse []*pb.KV
 }
 
 // NewIterator returns a new iterator. Depending upon the options, either only keys, or both
@@ -454,6 +496,20 @@ func (txn *Txn) NewKeyIterator(key []byte, opt IteratorOptions) *Iterator {
 	return txn.NewIterator(opt)
 }
 
+// NewKV must be called serially. It is NOT thread-safe.
+func (it *Iterator) NewKV() *pb.KV {
+	if len(it.reuse) == 0 {
+		return &pb.KV{}
+	}
+	kv := it.reuse[len(it.reuse)-1]
+	it.reuse = it.reuse[:len(it.reuse)-1]
+	if kv == nil {
+		kv = &pb.KV{}
+	}
+	kv.Reset()
+	return kv
+}
+
 func (it *Iterator) newItem() *Item {
 	item := it.waste.pop()
 	if item == nil {
diff --git a/key_registry.go b/key_registry.go
index 2547ec2..5c835ef 100644
--- a/key_registry.go
+++ b/key_registry.go
@@ -294,8 +294,8 @@ func WriteKeyRegistry(reg *KeyRegistry, opt KeyRegistryOptions) error {
 	return syncDir(opt.Dir)
 }
 
-// dataKey returns datakey of the given key id.
-func (kr *KeyRegistry) dataKey(id uint64) (*pb.DataKey, error) {
+// DataKey returns datakey of the given key id.
+func (kr *KeyRegistry) DataKey(id uint64) (*pb.DataKey, error) {
 	kr.RLock()
 	defer kr.RUnlock()
 	if id == 0 {
@@ -309,10 +309,10 @@ func (kr *KeyRegistry) dataKey(id uint64) (*pb.DataKey, error) {
 	return dk, nil
 }
 
-// latestDataKey will give you the latest generated datakey based on the rotation
+// LatestDataKey will give you the latest generated datakey based on the rotation
 // period. If the last generated datakey lifetime exceeds the rotation period.
 // It'll create new datakey.
-func (kr *KeyRegistry) latestDataKey() (*pb.DataKey, error) {
+func (kr *KeyRegistry) LatestDataKey() (*pb.DataKey, error) {
 	if len(kr.opt.EncryptionKey) == 0 {
 		// nil is for no encryption.
 		return nil, nil
diff --git a/level_handler.go b/level_handler.go
index 4fa6cae..3836be2 100644
--- a/level_handler.go
+++ b/level_handler.go
@@ -36,10 +36,9 @@ type levelHandler struct {
 	totalSize int64
 
 	// The following are initialized once and const.
-	level        int
-	strLevel     string
-	maxTotalSize int64
-	db           *DB
+	level    int
+	strLevel string
+	db       *DB
 }
 
 func (s *levelHandler) getTotalSize() int64 {
diff --git a/levels.go b/levels.go
index e2ee48f..49f7ccb 100644
--- a/levels.go
+++ b/levels.go
@@ -18,8 +18,10 @@ package badger
 
 import (
 	"bytes"
+	"context"
 	"encoding/hex"
 	"fmt"
+	"math"
 	"math/rand"
 	"os"
 	"sort"
@@ -28,7 +30,7 @@ import (
 	"sync/atomic"
 	"time"
 
-	"golang.org/x/net/trace"
+	otrace "go.opencensus.io/trace"
 
 	"github.com/dgraph-io/badger/v2/pb"
 	"github.com/dgraph-io/badger/v2/table"
@@ -39,6 +41,7 @@ import (
 
 type levelsController struct {
 	nextFileID uint64 // Atomic
+	l0stallsMs int64  // Atomic
 
 	// The following are initialized once and const.
 	levels []*levelHandler
@@ -78,19 +81,11 @@ func newLevelsController(db *DB, mf *Manifest) (*levelsController, error) {
 		kv:     db,
 		levels: make([]*levelHandler, db.opt.MaxLevels),
 	}
+	s.cstatus.tables = make(map[uint64]struct{})
 	s.cstatus.levels = make([]*levelCompactStatus, db.opt.MaxLevels)
 
 	for i := 0; i < db.opt.MaxLevels; i++ {
 		s.levels[i] = newLevelHandler(db, i)
-		switch i {
-		case 0:
-			// Do nothing.
-		case 1:
-			// Level 1 probably shouldn't be too much bigger than level 0.
-			s.levels[i].maxTotalSize = db.opt.LevelOneSize
-		default:
-			s.levels[i].maxTotalSize = s.levels[i-1].maxTotalSize * int64(db.opt.LevelSizeMultiplier)
-		}
 		s.cstatus.levels[i] = new(levelCompactStatus)
 	}
 
@@ -137,17 +132,15 @@ func newLevelsController(db *DB, mf *Manifest) (*levelsController, error) {
 				throttle.Done(rerr)
 				atomic.AddInt32(&numOpened, 1)
 			}()
-			dk, err := db.registry.dataKey(tf.KeyID)
+			dk, err := db.registry.DataKey(tf.KeyID)
 			if err != nil {
 				rerr = y.Wrapf(err, "Error while reading datakey")
 				return
 			}
-			topt := buildTableOptions(db.opt)
-			// Set compression from table manifest.
+			topt := buildTableOptions(db)
+			// Explicitly set Compression and DataKey based on how the table was generated.
 			topt.Compression = tf.Compression
 			topt.DataKey = dk
-			topt.BlockCache = db.blockCache
-			topt.IndexCache = db.indexCache
 
 			mf, err := z.OpenMmapFile(fname, db.opt.getFileFlags(), 0)
 			if err != nil {
@@ -329,18 +322,23 @@ func (s *levelsController) dropPrefixes(prefixes [][]byte) error {
 		if len(tableGroups) == 0 {
 			continue
 		}
-
+		_, span := otrace.StartSpan(context.Background(), "Badger.Compaction")
+		span.Annotatef(nil, "Compaction level: %v", l.level)
+		span.Annotatef(nil, "Drop Prefixes: %v", prefixes)
+		defer span.End()
 		opt.Infof("Dropping prefix at level %d (%d tableGroups)", l.level, len(tableGroups))
 		for _, operation := range tableGroups {
 			cd := compactDef{
-				elog:         trace.New(fmt.Sprintf("Badger.L%d", l.level), "Compact"),
+				span:         span,
 				thisLevel:    l,
 				nextLevel:    l,
 				top:          nil,
 				bot:          operation,
 				dropPrefixes: prefixes,
+				t:            s.levelTargets(),
 			}
-			if err := s.runCompactDef(l.level, cd); err != nil {
+			cd.t.baseLevel = l.level
+			if err := s.runCompactDef(-1, l.level, cd); err != nil {
 				opt.Warningf("While running compact def: %+v. Error: %v", cd, err)
 				return err
 			}
@@ -353,12 +351,70 @@ func (s *levelsController) startCompact(lc *z.Closer) {
 	n := s.kv.opt.NumCompactors
 	lc.AddRunning(n - 1)
 	for i := 0; i < n; i++ {
-		// The worker with id=0 is dedicated to L0 and L1. This is not counted
-		// towards the user specified NumCompactors.
 		go s.runCompactor(i, lc)
 	}
 }
 
+type targets struct {
+	baseLevel int
+	targetSz  []int64
+	fileSz    []int64
+}
+
+// levelTargets calculates the targets for levels in the LSM tree. The idea comes from Dynamic Level
+// Sizes ( https://rocksdb.org/blog/2015/07/23/dynamic-level.html ) in RocksDB. The sizes of levels
+// are calculated based on the size of the lowest level, typically L6. So, if L6 size is 1GB, then
+// L5 target size is 100MB, L4 target size is 10MB and so on.
+//
+// L0 files don't automatically go to L1. Instead, they get compacted to Lbase, where Lbase is
+// chosen based on the first level which is non-empty from top (check L1 through L6). For an empty
+// DB, that would be L6.  So, L0 compactions go to L6, then L5, L4 and so on.
+//
+// Lbase is advanced to the upper levels when its target size exceeds BaseLevelSize. For
+// example, when L6 reaches 1.1GB, then L4 target sizes becomes 11MB, thus exceeding the
+// BaseLevelSize of 10MB. L3 would then become the new Lbase, with a target size of 1MB <
+// BaseLevelSize.
+func (s *levelsController) levelTargets() targets {
+	adjust := func(sz int64) int64 {
+		if sz < s.kv.opt.BaseLevelSize {
+			return s.kv.opt.BaseLevelSize
+		}
+		return sz
+	}
+
+	t := targets{
+		targetSz: make([]int64, len(s.levels)),
+		fileSz:   make([]int64, len(s.levels)),
+	}
+	// DB size is the size of the last level.
+	dbSize := s.lastLevel().getTotalSize()
+	for i := len(s.levels) - 1; i > 0; i-- {
+		ltarget := adjust(dbSize)
+		t.targetSz[i] = ltarget
+		if t.baseLevel == 0 && ltarget <= s.kv.opt.BaseLevelSize {
+			t.baseLevel = i
+		}
+		dbSize /= int64(s.kv.opt.LevelSizeMultiplier)
+	}
+
+	tsz := s.kv.opt.BaseTableSize
+	for i := 0; i < len(s.levels); i++ {
+		if i == 0 {
+			// Use MemTableSize for Level 0. Because at Level 0, we stop compactions based on the
+			// number of tables, not the size of the level. So, having a 1:1 size ratio between
+			// memtable size and the size of L0 files is better than churning out 32 files per
+			// memtable (assuming 64MB MemTableSize and 2MB BaseTableSize).
+			t.fileSz[i] = s.kv.opt.MemTableSize
+		} else if i <= t.baseLevel {
+			t.fileSz[i] = tsz
+		} else {
+			tsz *= int64(s.kv.opt.TableSizeMultiplier)
+			t.fileSz[i] = tsz
+		}
+	}
+	return t
+}
+
 func (s *levelsController) runCompactor(id int, lc *z.Closer) {
 	defer lc.Done()
 
@@ -370,90 +426,143 @@ func (s *levelsController) runCompactor(id int, lc *z.Closer) {
 		return
 	}
 
-	ticker := time.NewTicker(100 * time.Millisecond)
+	ticker := time.NewTicker(50 * time.Millisecond)
 	defer ticker.Stop()
 
+	moveL0toFront := func(prios []compactionPriority) []compactionPriority {
+		idx := -1
+		for i, p := range prios {
+			if p.level == 0 {
+				idx = i
+				break
+			}
+		}
+		// If idx == -1, we didn't find L0.
+		// If idx == 0, then we don't need to do anything. L0 is already at the front.
+		if idx > 0 {
+			out := append([]compactionPriority{}, prios[idx])
+			out = append(out, prios[:idx]...)
+			out = append(out, prios[idx+1:]...)
+			return out
+		}
+		return prios
+	}
+
+	runOnce := func() bool {
+		prios := s.pickCompactLevels()
+		if id == 0 {
+			// Worker ID zero prefers to compact L0 always.
+			prios = moveL0toFront(prios)
+		}
+		for _, p := range prios {
+			if id == 0 && p.level == 0 {
+				// Allow worker zero to run level 0, irrespective of its adjusted score.
+			} else if p.adjusted < 1.0 {
+				break
+			}
+
+			err := s.doCompact(id, p)
+			switch err {
+			case nil:
+				return true
+			case errFillTables:
+				// pass
+			default:
+				s.kv.opt.Warningf("While running doCompact: %v\n", err)
+			}
+		}
+		return false
+	}
+
 	for {
 		select {
 		// Can add a done channel or other stuff.
 		case <-ticker.C:
-			prios := s.pickCompactLevels()
-		loop:
-			for _, p := range prios {
-				if id == 0 && p.level > 1 {
-					// If I'm ID zero, I only compact L0 and L1.
-					continue
-				}
-				if id != 0 && p.level <= 1 {
-					// If I'm ID non-zero, I do NOT compact L0 and L1.
-					continue
-				}
-				err := s.doCompact(id, p)
-				switch err {
-				case nil:
-					break loop
-				case errFillTables:
-					// pass
-				default:
-					s.kv.opt.Warningf("While running doCompact: %v\n", err)
-				}
-			}
+			runOnce()
 		case <-lc.HasBeenClosed():
 			return
 		}
 	}
 }
 
-// Returns true if level zero may be compacted, without accounting for compactions that already
-// might be happening.
-func (s *levelsController) isLevel0Compactable() bool {
-	return s.levels[0].numTables() >= s.kv.opt.NumLevelZeroTables
-}
-
-// Returns true if the non-zero level may be compacted.  delSize provides the size of the tables
-// which are currently being compacted so that we treat them as already having started being
-// compacted (because they have been, yet their size is already counted in getTotalSize).
-func (l *levelHandler) isCompactable(delSize int64) bool {
-	return l.getTotalSize()-delSize >= l.maxTotalSize
-}
-
 type compactionPriority struct {
 	level        int
 	score        float64
+	adjusted     float64
 	dropPrefixes [][]byte
+	t            targets
+}
+
+func (s *levelsController) lastLevel() *levelHandler {
+	return s.levels[len(s.levels)-1]
 }
 
 // pickCompactLevel determines which level to compact.
 // Based on: https://github.com/facebook/rocksdb/wiki/Leveled-Compaction
 func (s *levelsController) pickCompactLevels() (prios []compactionPriority) {
-	// This function must use identical criteria for guaranteeing compaction's progress that
-	// addLevel0Table uses.
-
-	// cstatus is checked to see if level 0's tables are already being compacted
-	if !s.cstatus.overlapsWith(0, infRange) && s.isLevel0Compactable() {
+	t := s.levelTargets()
+	addPriority := func(level int, score float64) {
 		pri := compactionPriority{
-			level: 0,
-			score: float64(s.levels[0].numTables()) / float64(s.kv.opt.NumLevelZeroTables),
+			level:    level,
+			score:    score,
+			adjusted: score,
+			t:        t,
 		}
 		prios = append(prios, pri)
 	}
 
-	for i, l := range s.levels[1:] {
+	// Add L0 priority based on the number of tables.
+	addPriority(0, float64(s.levels[0].numTables())/float64(s.kv.opt.NumLevelZeroTables))
+
+	// All other levels use size to calculate priority.
+	for i := 1; i < len(s.levels); i++ {
 		// Don't consider those tables that are already being compacted right now.
-		delSize := s.cstatus.delSize(i + 1)
+		delSize := s.cstatus.delSize(i)
 
-		if l.isCompactable(delSize) {
-			pri := compactionPriority{
-				level: i + 1,
-				score: float64(l.getTotalSize()-delSize) / float64(l.maxTotalSize),
+		l := s.levels[i]
+		sz := l.getTotalSize() - delSize
+		addPriority(i, float64(sz)/float64(t.targetSz[i]))
+	}
+	y.AssertTrue(len(prios) == len(s.levels))
+
+	// The following code is borrowed from PebbleDB and results in healthier LSM tree structure.
+	// If Li-1 has score > 1.0, then we'll divide Li-1 score by Li. If Li score is >= 1.0, then Li-1
+	// score is reduced, which means we'll prioritize the compaction of lower levels (L5, L4 and so
+	// on) over the higher levels (L0, L1 and so on). On the other hand, if Li score is < 1.0, then
+	// we'll increase the priority of Li-1.
+	// Overall what this means is, if the bottom level is already overflowing, then de-prioritize
+	// compaction of the above level. If the bottom level is not full, then increase the priority of
+	// above level.
+	var prevLevel int
+	for level := t.baseLevel; level < len(s.levels); level++ {
+		if prios[prevLevel].adjusted >= 1 {
+			// Avoid absurdly large scores by placing a floor on the score that we'll
+			// adjust a level by. The value of 0.01 was chosen somewhat arbitrarily
+			const minScore = 0.01
+			if prios[level].score >= minScore {
+				prios[prevLevel].adjusted /= prios[level].adjusted
+			} else {
+				prios[prevLevel].adjusted /= minScore
 			}
-			prios = append(prios, pri)
 		}
+		prevLevel = level
 	}
-	// We should continue to sort the compaction priorities by score. Now that we have a dedicated
-	// compactor for L0 and L1, we don't need to sort by level here.
+
+	// Pick all the levels whose original score is >= 1.0, irrespective of their adjusted score.
+	// We'll still sort them by their adjusted score below. Having both these scores allows us to
+	// make better decisions about compacting L0. If we see a score >= 1.0, we can do L0->L0
+	// compactions. If the adjusted score >= 1.0, then we can do L0->Lbase compactions.
+	out := prios[:0]
+	for _, p := range prios[:len(prios)-1] {
+		if p.score >= 1.0 {
+			out = append(out, p)
+		}
+	}
+	prios = out
+
+	// Sort by the adjusted score.
 	sort.Slice(prios, func(i, j int) bool {
-		return prios[i].score > prios[j].score
+		return prios[i].adjusted > prios[j].adjusted
 	})
 	return prios
 }
@@ -475,20 +584,25 @@ func (s *levelsController) checkOverlap(tables []*table.Table, lev int) bool {
 	return false
 }
 
-// compactBuildTables merges topTables and botTables to form a list of new tables.
-func (s *levelsController) compactBuildTables(
-	lev int, cd compactDef) ([]*table.Table, func() error, error) {
-	topTables := cd.top
-	botTables := cd.bot
-
-	numTables := int64(len(topTables) + len(botTables))
-	y.NumCompactionTables.Add(numTables)
-	defer y.NumCompactionTables.Add(-numTables)
+// subcompact runs a single sub-compaction, iterating over the specified key-range only.
+//
+// We use splits to do a single compaction concurrently. If we have >= 3 tables
+// involved in the bottom level during compaction, we choose key ranges to
+// split the main compaction up into sub-compactions. Each sub-compaction runs
+// concurrently, only iterating over the provided key range, generating tables.
+// This speeds up the compaction significantly.
+func (s *levelsController) subcompact(it y.Iterator, kr keyRange, cd compactDef,
+	inflightBuilders *y.Throttle, res chan<- *table.Table) {
 
 	// Check overlap of the top level with the levels which are not being
 	// compacted in this compaction.
 	hasOverlap := s.checkOverlap(cd.allTables(), cd.nextLevel.level+1)
 
+	// Pick a discard ts, so we can discard versions below this ts. We should
+	// never discard any versions starting from above this timestamp, because
+	// that would affect the snapshot view guarantee provided by transactions.
+	discardTs := s.kv.orc.discardAtOrBelow()
+
 	// Try to collect stats so that we can inform value log about GC. That would help us find which
 	// value log file should be GCed.
 	discardStats := make(map[uint32]int64)
@@ -504,66 +618,31 @@ func (s *levelsController) compactBuildTables(
 		}
 	}
 
-	// Create iterators across all the tables involved first.
-	var iters []y.Iterator
-	switch {
-	case lev == 0:
-		iters = appendIteratorsReversed(iters, topTables, table.NOCACHE)
-	case len(topTables) > 0:
-		y.AssertTrue(len(topTables) == 1)
-		iters = []y.Iterator{topTables[0].NewIterator(table.NOCACHE)}
-	}
-
-	// Next level has level>=1 and we can use ConcatIterator as key ranges do not overlap.
-	var valid []*table.Table
-
-nextTable:
-	for _, table := range botTables {
-		if len(cd.dropPrefixes) > 0 {
-			for _, prefix := range cd.dropPrefixes {
-				if bytes.HasPrefix(table.Smallest(), prefix) &&
-					bytes.HasPrefix(table.Biggest(), prefix) {
-					// All the keys in this table have the dropPrefix. So, this
-					// table does not need to be in the iterator and can be
-					// dropped immediately.
-					continue nextTable
-				}
-			}
+	// exceedsAllowedOverlap returns true if the given key range would overlap with more than 10
+	// tables from level below nextLevel (nextLevel+1). This helps avoid generating tables at Li
+	// with huge overlaps with Li+1.
+	exceedsAllowedOverlap := func(kr keyRange) bool {
+		n2n := cd.nextLevel.level + 1
+		if n2n <= 1 || n2n >= len(s.levels) {
+			return false
 		}
-		valid = append(valid, table)
-	}
-	iters = append(iters, table.NewConcatIterator(valid, table.NOCACHE))
-	it := table.NewMergeIterator(iters, false)
-	defer it.Close() // Important to close the iterator to do ref counting.
+		n2nl := s.levels[n2n]
+		n2nl.RLock()
+		defer n2nl.RUnlock()
 
-	it.Rewind()
-
-	// Pick a discard ts, so we can discard versions below this ts. We should
-	// never discard any versions starting from above this timestamp, because
-	// that would affect the snapshot view guarantee provided by transactions.
-	discardTs := s.kv.orc.discardAtOrBelow()
+		l, r := n2nl.overlappingTables(levelHandlerRLocked{}, kr)
+		return r-l >= 10
+	}
 
-	var numBuilds, numVersions int
 	var lastKey, skipKey []byte
+	var numBuilds, numVersions int
 	var vp valuePointer
-	var newTables []*table.Table
-	mu := new(sync.Mutex) // Guards newTables
 
-	inflightBuilders := y.NewThrottle(5)
-	for it.Valid() {
+	addKeys := func(builder *table.Builder) {
 		timeStart := time.Now()
-		dk, err := s.kv.registry.latestDataKey()
-		if err != nil {
-			return nil, nil,
-				y.Wrapf(err, "Error while retrieving datakey in levelsController.compactBuildTables")
-		}
-		bopts := buildTableOptions(s.kv.opt)
-		bopts.DataKey = dk
-		// Builder does not need cache but the same options are used for opening table.
-		bopts.BlockCache = s.kv.blockCache
-		bopts.IndexCache = s.kv.indexCache
-		builder := table.NewTableBuilder(bopts)
 		var numKeys, numSkips uint64
+		var rangeCheck int
+		var tableKr keyRange
 		for ; it.Valid(); it.Next() {
 			// See if we need to skip the prefix.
 			if len(cd.dropPrefixes) > 0 && hasAnyPrefixes(it.Key(), cd.dropPrefixes) {
@@ -584,7 +663,10 @@ nextTable:
 			}
 
 			if !y.SameKey(it.Key(), lastKey) {
-				if builder.ReachedCapacity(uint64(float64(s.kv.opt.MaxTableSize) * 0.9)) {
+				if len(kr.right) > 0 && y.CompareKeys(it.Key(), kr.right) >= 0 {
+					break
+				}
+				if builder.ReachedCapacity() {
 					// Only break if we are on a different key, and have reached capacity. We want
 					// to ensure that all versions of the key are stored in the same sstable, and
 					// not divided across multiple tables at the same level.
@@ -592,6 +674,24 @@ nextTable:
 				}
 				lastKey = y.SafeCopy(lastKey, it.Key())
 				numVersions = 0
+
+				if len(tableKr.left) == 0 {
+					tableKr.left = y.SafeCopy(tableKr.left, it.Key())
+				}
+				tableKr.right = lastKey
+
+				rangeCheck++
+				if rangeCheck%5000 == 0 {
+					// This table's range exceeds the allowed range overlap with the level after
+					// next. So, we stop writing to this table. If we don't do this, then we end up
+					// doing very expensive compactions involving too many tables. To amortize the
+					// cost of this check, we do it only every N keys.
+					if exceedsAllowedOverlap(tableKr) {
+						// s.kv.opt.Debugf("L%d -> L%d Breaking due to exceedsAllowedOverlap with
+						// kr: %s\n", cd.thisLevel.level, cd.nextLevel.level, tableKr)
+						break
+					}
+				}
 			}
 
 			vs := it.Value()
@@ -642,13 +742,33 @@ nextTable:
 			}
 			builder.Add(it.Key(), vs, vp.Len)
 		}
+		s.kv.opt.Debugf("LOG Compact. Added %d keys. Skipped %d keys. Iteration took: %v",
+			numKeys, numSkips, time.Since(timeStart).Round(time.Millisecond))
+	} // End of function: addKeys
+
+	if len(kr.left) > 0 {
+		it.Seek(kr.left)
+	} else {
+		it.Rewind()
+	}
+	for it.Valid() {
+		if len(kr.right) > 0 && y.CompareKeys(it.Key(), kr.right) >= 0 {
+			break
+		}
+
+		bopts := buildTableOptions(s.kv)
+		// Set TableSize to the target file size for that level.
+		bopts.TableSize = uint64(cd.t.fileSz[cd.nextLevel.level])
+		builder := table.NewTableBuilder(bopts)
+
+		// This would do the iteration and add keys to builder.
+		addKeys(builder)
+
 		// It was true that it.Valid() at least once in the loop above, which means we
 		// called Add() at least once, and builder is not Empty().
-		s.kv.opt.Debugf("LOG Compact. Added %d keys. Skipped %d keys. Iteration took: %v",
-			numKeys, numSkips, time.Since(timeStart))
 		if builder.Empty() {
 			// Cleanup builder resources:
-			builder.Finish(false)
+			builder.Finish()
 			builder.Close()
 			continue
 		}
@@ -659,18 +779,18 @@ nextTable:
 			break
 		}
 		go func(builder *table.Builder) {
-			defer builder.Close()
+			var err error
 			defer inflightBuilders.Done(err)
+			defer builder.Close()
 
 			build := func(fileID uint64) (*table.Table, error) {
 				fname := table.NewFilename(fileID, s.kv.opt.Dir)
-				return table.CreateTable(fname, builder.Finish(false), bopts)
+				return table.CreateTable(fname, builder)
 			}
 
 			var tbl *table.Table
-			var err error
 			if s.kv.opt.InMemory {
-				tbl, err = table.OpenInMemoryTable(builder.Finish(true), fileID, &bopts)
+				tbl, err = table.OpenInMemoryTable(builder.Finish(), fileID, &bopts)
 			} else {
 				tbl, err = build(fileID)
 			}
@@ -679,19 +799,91 @@ nextTable:
 			if err != nil {
 				return
 			}
+			res <- tbl
+		}(builder)
+	}
+	s.kv.vlog.updateDiscardStats(discardStats)
+	s.kv.opt.Debugf("Discard stats: %v", discardStats)
+}
 
-			mu.Lock()
-			newTables = append(newTables, tbl)
-			// num := atomic.LoadInt32(&table.NumBlocks)
-			mu.Unlock()
+// compactBuildTables merges topTables and botTables to form a list of new tables.
+func (s *levelsController) compactBuildTables(
+	lev int, cd compactDef) ([]*table.Table, func() error, error) {
 
-			// TODO(ibrahim): When ristretto PR #186 merges, bring this back.
-			// s.kv.opt.Debugf("Num Blocks: %d. Num Allocs (MB): %.2f\n", num, (z.NumAllocBytes() / 1 << 20))
-		}(builder)
+	topTables := cd.top
+	botTables := cd.bot
+
+	numTables := int64(len(topTables) + len(botTables))
+	y.NumCompactionTables.Add(numTables)
+	defer y.NumCompactionTables.Add(-numTables)
+
+	cd.span.Annotatef(nil, "Top tables count: %v Bottom tables count: %v",
+		len(topTables), len(botTables))
+
+	keepTable := func(t *table.Table) bool {
+		for _, prefix := range cd.dropPrefixes {
+			if bytes.HasPrefix(t.Smallest(), prefix) &&
+				bytes.HasPrefix(t.Biggest(), prefix) {
+				// All the keys in this table have the dropPrefix. So, this
+				// table does not need to be in the iterator and can be
+				// dropped immediately.
+				return false
+			}
+		}
+		return true
+	}
+	var valid []*table.Table
+	for _, table := range botTables {
+		if keepTable(table) {
+			valid = append(valid, table)
+		}
+	}
+
+	newIterator := func() []y.Iterator {
+		// Create iterators across all the tables involved first.
+		var iters []y.Iterator
+		switch {
+		case lev == 0:
+			iters = appendIteratorsReversed(iters, topTables, table.NOCACHE)
+		case len(topTables) > 0:
+			y.AssertTrue(len(topTables) == 1)
+			iters = []y.Iterator{topTables[0].NewIterator(table.NOCACHE)}
+		}
+		// Next level has level>=1 and we can use ConcatIterator as key ranges do not overlap.
+		return append(iters, table.NewConcatIterator(valid, table.NOCACHE))
+	}
+
+	res := make(chan *table.Table, 3)
+	inflightBuilders := y.NewThrottle(8 + len(cd.splits))
+	for _, kr := range cd.splits {
+		// Initiate Do here so we can register the goroutines for buildTables too.
+		if err := inflightBuilders.Do(); err != nil {
+			s.kv.opt.Errorf("cannot start subcompaction: %+v", err)
+			return nil, nil, err
+		}
+		go func(kr keyRange) {
+			defer inflightBuilders.Done(nil)
+			it := table.NewMergeIterator(newIterator(), false)
+			defer it.Close()
+			s.subcompact(it, kr, cd, inflightBuilders, res)
+		}(kr)
 	}
 
+	var newTables []*table.Table
+	var wg sync.WaitGroup
+	wg.Add(1)
+	go func() {
+		defer wg.Done()
+		for t := range res {
+			newTables = append(newTables, t)
+		}
+	}()
+
 	// Wait for all table builders to finish and also for newTables accumulator to finish.
 	err := inflightBuilders.Finish()
+	close(res)
+	wg.Wait() // Wait for all tables to be picked up.
+
 	if err == nil {
 		// Ensure created files' directory entries are visible.  We don't mind the extra latency
 		// from not doing this ASAP after all file creation has finished because this is a
@@ -709,8 +901,6 @@ nextTable:
 	sort.Slice(newTables, func(i, j int) bool {
 		return y.CompareKeys(newTables[i].Biggest(), newTables[j].Biggest()) < 0
 	})
-	s.kv.vlog.updateDiscardStats(discardStats)
-	s.kv.opt.Debugf("Discard stats: %v", discardStats)
 	return newTables, func() error { return decrRefs(newTables) }, nil
 }
 
@@ -768,22 +958,62 @@ func containsAnyPrefixes(smallValue, largeValue []byte, listOfPrefixes [][]byte)
 }
 
 type compactDef struct {
-	elog trace.Trace
+	span *otrace.Span
 
-	thisLevel *levelHandler
-	nextLevel *levelHandler
+	compactorId int
+	t           targets
+	p           compactionPriority
+	thisLevel   *levelHandler
+	nextLevel   *levelHandler
 
 	top []*table.Table
 	bot []*table.Table
 
 	thisRange keyRange
 	nextRange keyRange
+	splits    []keyRange
 
 	thisSize int64
 
 	dropPrefixes [][]byte
 }
 
+// addSplits can allow us to run multiple sub-compactions in parallel across the split key ranges.
+func (s *levelsController) addSplits(cd *compactDef) {
+	cd.splits = cd.splits[:0]
+
+	// Pick one every 3 tables.
+	const N = 3
+	skr := cd.thisRange
+	skr.extend(cd.nextRange)
+
+	addRange := func(right []byte) {
+		skr.right = y.Copy(right)
+		cd.splits = append(cd.splits, skr)
+
+		skr.left = skr.right
+	}
+
+	for i, t := range cd.bot {
+		// last entry in bottom table.
+		if i == len(cd.bot)-1 {
+			addRange([]byte{})
+			return
+		}
+		if i%N == N-1 {
+			// Right should always have ts=maxUint64 otherwise we'll lose keys
+			// in subcompaction. Consider the following.
+			// Top table is [A1...C3(deleted)]
+			// bot table is [B1....C2]
+			// This will generate splits like [A1 ... C2] . Notice that we
+			// dropped the C3 which is the last key of the top table.
+			// See TestCompaction/with_split test.
+			right := y.KeyWithTs(y.ParseKey(t.Biggest()), math.MaxUint64)
+			addRange(right)
+		}
+	}
+}
+
 func (cd *compactDef) lockLevels() {
 	cd.thisLevel.RLock()
 	cd.nextLevel.RLock()
@@ -801,33 +1031,127 @@ func (cd *compactDef) allTables() []*table.Table {
 	return ret
 }
 
-func (s *levelsController) fillTablesL0(cd *compactDef) bool {
+func (s *levelsController) fillTablesL0ToL0(cd *compactDef) bool {
+	if cd.compactorId != 0 {
+		// Only compactor zero can work on this.
+		return false
+	}
+
+	cd.nextLevel = s.levels[0]
+	cd.nextRange = keyRange{}
+	cd.bot = nil
+
 	cd.lockLevels()
 	defer cd.unlockLevels()
 
-	cd.top = make([]*table.Table, len(cd.thisLevel.tables))
-	copy(cd.top, cd.thisLevel.tables)
-	if len(cd.top) == 0 {
+	s.cstatus.Lock()
+	defer s.cstatus.Unlock()
+
+	top := cd.thisLevel.tables
+	var out []*table.Table
+	now := time.Now()
+	for _, t := range top {
+		if t.Size() >= 2*cd.t.fileSz[0] {
+			// This file is already big, don't include it.
+			continue
+		}
+		if now.Sub(t.CreatedAt) < 10*time.Second {
+			// Just created it 10s ago. Don't pick for compaction.
+			continue
+		}
+		if _, beingCompacted := s.cstatus.tables[t.ID()]; beingCompacted {
+			continue
+		}
+		out = append(out, t)
+	}
+
+	if len(out) < 4 {
+		// If we don't have enough tables to merge in L0, don't do it.
 		return false
 	}
 	cd.thisRange = infRange
+	cd.top = out
 
-	kr := getKeyRange(cd.top...)
-	left, right := cd.nextLevel.overlappingTables(levelHandlerRLocked{}, kr)
+	// Avoid any other L0 -> Lbase from happening, while this is going on.
+	thisLevel := s.cstatus.levels[cd.thisLevel.level]
+	thisLevel.ranges = append(thisLevel.ranges, infRange)
+	for _, t := range out {
+		s.cstatus.tables[t.ID()] = struct{}{}
+	}
+
+	// For L0->L0 compaction, we set the target file size to max, so the output is always one file.
+	// This significantly decreases the L0 table stalls and improves the performance.
+	cd.t.fileSz[0] = math.MaxUint32
+	return true
+}
+
+func (s *levelsController) fillTablesL0ToLbase(cd *compactDef) bool {
+	if cd.nextLevel.level == 0 {
+		panic("Base level can't be zero.")
+	}
+	// We keep cd.p.adjusted > 0.0 here to allow functions in db.go to artificially trigger
+	// L0->Lbase compactions. Those functions wouldn't be setting the adjusted score.
+	if cd.p.adjusted > 0.0 && cd.p.adjusted < 1.0 {
+		// Do not compact to Lbase if adjusted score is less than 1.0.
+		return false
+	}
+	cd.lockLevels()
+	defer cd.unlockLevels()
+
+	top := cd.thisLevel.tables
+	if len(top) == 0 {
+		return false
+	}
+
+	var out []*table.Table
+	if len(cd.dropPrefixes) > 0 {
+		// Use all tables if drop prefix is set. We don't want to compact only a
+		// sub-range. We want to compact all the tables.
+		out = top
+
+	} else {
+		var kr keyRange
+		// cd.top[0] is the oldest file. So we start from the oldest file first.
+		for _, t := range top {
+			dkr := getKeyRange(t)
+			if kr.overlapsWith(dkr) {
+				out = append(out, t)
+				kr.extend(dkr)
+			} else {
+				break
+			}
+		}
+	}
+	cd.thisRange = getKeyRange(out...)
+	cd.top = out
+
+	left, right := cd.nextLevel.overlappingTables(levelHandlerRLocked{}, cd.thisRange)
 	cd.bot = make([]*table.Table, right-left)
 	copy(cd.bot, cd.nextLevel.tables[left:right])
 
 	if len(cd.bot) == 0 {
-		cd.nextRange = kr
+		cd.nextRange = cd.thisRange
 	} else {
 		cd.nextRange = getKeyRange(cd.bot...)
 	}
+	return s.cstatus.compareAndAdd(thisAndNextLevelRLocked{}, *cd)
+}
 
-	if !s.cstatus.compareAndAdd(thisAndNextLevelRLocked{}, *cd) {
-		return false
+// fillTablesL0 would try to fill tables from L0 to be compacted with Lbase. If
+// it can not do that, it would try to compact tables from L0 -> L0.
+//
+// Say L0 has 10 tables.
+// fillTablesL0ToLbase picks up 5 tables to compact from L0 -> L5.
+// Next call to fillTablesL0 would run L0ToLbase again, which fails this time.
+// So, instead, we run fillTablesL0ToL0, which picks up rest of the 5 tables to
+// be compacted within L0. Additionally, it would set the compaction range in
+// cstatus to inf, so no other L0 -> Lbase compactions can happen.
+// Thus, L0 -> L0 must finish for the next L0 -> Lbase to begin.
+func (s *levelsController) fillTablesL0(cd *compactDef) bool {
+	if ok := s.fillTablesL0ToLbase(cd); ok {
+		return true
 	}
-
-	return true
+	return s.fillTablesL0ToL0(cd)
 }
 
 // sortByHeuristic sorts tables in increasing order of MaxVersion, so we
@@ -902,12 +1226,25 @@ func (s *levelsController) fillTables(cd *compactDef) bool {
 	return false
 }
 
-func (s *levelsController) runCompactDef(l int, cd compactDef) (err error) {
+func (s *levelsController) runCompactDef(id, l int, cd compactDef) (err error) {
+	if len(cd.t.fileSz) == 0 {
+		return errors.New("Filesizes cannot be zero. Targets are not set")
+	}
 	timeStart := time.Now()
 
 	thisLevel := cd.thisLevel
 	nextLevel := cd.nextLevel
 
+	y.AssertTrue(len(cd.splits) == 0)
+	if thisLevel.level == 0 && nextLevel.level == 0 {
+		// don't do anything for L0 -> L0.
+	} else {
+		s.addSplits(&cd)
+	}
+	if len(cd.splits) == 0 {
+		cd.splits = append(cd.splits, keyRange{})
+	}
+
 	// Table should never be moved directly between levels, always be rewritten to allow discarding
 	// invalid versions.
 
@@ -940,56 +1277,78 @@ func (s *levelsController) runCompactDef(l int, cd compactDef) (err error) {
 	// Note: For level 0, while doCompact is running, it is possible that new tables are added.
 	// However, the tables are added only to the end, so it is ok to just delete the first table.
 
-	if dur := time.Since(timeStart); dur > 3*time.Second {
-		s.kv.opt.Infof("LOG Compact %d->%d, del %d tables, add %d tables, took %v\n",
-			thisLevel.level, nextLevel.level, len(cd.top)+len(cd.bot),
-			len(newTables), dur)
+	from := append(tablesToString(cd.top), tablesToString(cd.bot)...)
+	to := tablesToString(newTables)
+
+	if dur := time.Since(timeStart); dur > 2*time.Second {
+		var expensive string
+		if dur > time.Second {
+			expensive = " [E]"
+		}
+		s.kv.opt.Infof("[%d]%s LOG Compact %d->%d (%d, %d -> %d tables with %d splits)."+
+			" [%s] -> [%s], took %v\n",
+			id, expensive, thisLevel.level, nextLevel.level, len(cd.top), len(cd.bot),
+			len(newTables), len(cd.splits), strings.Join(from, " "), strings.Join(to, " "),
+			dur.Round(time.Millisecond))
 	}
 
 	if cd.thisLevel.level != 0 && len(newTables) > 2*s.kv.opt.LevelSizeMultiplier {
-		s.kv.opt.Infof("This Range (numTables: %d)\nLeft:\n%s\nRight:\n%s\n",
+		s.kv.opt.Debugf("This Range (numTables: %d)\nLeft:\n%s\nRight:\n%s\n",
 			len(cd.top), hex.Dump(cd.thisRange.left), hex.Dump(cd.thisRange.right))
-		s.kv.opt.Infof("Next Range (numTables: %d)\nLeft:\n%s\nRight:\n%s\n",
+		s.kv.opt.Debugf("Next Range (numTables: %d)\nLeft:\n%s\nRight:\n%s\n",
 			len(cd.bot), hex.Dump(cd.nextRange.left), hex.Dump(cd.nextRange.right))
 	}
 	return nil
 }
 
+func tablesToString(tables []*table.Table) []string {
+	var res []string
+	for _, t := range tables {
+		res = append(res, fmt.Sprintf("%05d", t.ID()))
+	}
+	res = append(res, ".")
+	return res
+}
+
 var errFillTables = errors.New("Unable to fill tables")
 
 // doCompact picks some table on level l and compacts it away to the next level.
 func (s *levelsController) doCompact(id int, p compactionPriority) error {
 	l := p.level
 	y.AssertTrue(l+1 < s.kv.opt.MaxLevels) // Sanity check.
+	if p.t.baseLevel == 0 {
+		p.t = s.levelTargets()
+	}
+
+	_, span := otrace.StartSpan(context.Background(), "Badger.Compaction")
+	defer span.End()
 
 	cd := compactDef{
-		elog:         trace.New(fmt.Sprintf("Badger.L%d", l), "Compact"),
+		compactorId:  id,
+		span:         span,
+		p:            p,
+		t:            p.t,
 		thisLevel:    s.levels[l],
-		nextLevel:    s.levels[l+1],
 		dropPrefixes: p.dropPrefixes,
 	}
-	cd.elog.SetMaxEvents(100)
-	defer cd.elog.Finish()
-
-	s.kv.opt.Debugf("[Compactor: %d] Attempting to run compaction: %+v", id, p)
 
 	// While picking tables to be compacted, both levels' tables are expected to
 	// remain unchanged.
 	if l == 0 {
+		cd.nextLevel = s.levels[p.t.baseLevel]
 		if !s.fillTablesL0(&cd) {
 			return errFillTables
 		}
-
 	} else {
+		cd.nextLevel = s.levels[l+1]
 		if !s.fillTables(&cd) {
 			return errFillTables
 		}
 	}
 	defer s.cstatus.delete(cd) // Remove the ranges from compaction status.
 
-	s.kv.opt.Debugf("[Compactor: %d] Running compaction: %+v for level: %d\n",
-		id, p, cd.thisLevel.level)
-	if err := s.runCompactDef(l, cd); err != nil {
+	span.Annotatef(nil, "Compaction: %+v", cd)
+	if err := s.runCompactDef(id, l, cd); err != nil {
 		// This compaction couldn't be done successfully.
 		s.kv.opt.Warningf("[Compactor: %d] LOG Compact FAILED with error: %+v: %+v", id, err, cd)
 		return err
@@ -1016,34 +1375,16 @@ func (s *levelsController) addLevel0Table(t *table.Table) error {
 	}
 
 	for !s.levels[0].tryAddLevel0Table(t) {
-		// Stall. Make sure all levels are healthy before we unstall.
-		s.cstatus.RLock()
-		for i := 0; i < s.kv.opt.MaxLevels; i++ {
-			s.kv.opt.Debugf("level=%d. Status=%s Size=%d\n",
-				i, s.cstatus.levels[i].debug(), s.levels[i].getTotalSize())
-		}
-		s.cstatus.RUnlock()
+		// Before we unstall, we need to make sure that level 0 is healthy.
 		timeStart := time.Now()
-
-		// Before we unstall, we need to make sure that level 0 is healthy. Otherwise, we
-		// will very quickly fill up level 0 again.
-		for i := 0; ; i++ {
-			// It's crucial that this behavior replicates pickCompactLevels' behavior in
-			// computing compactability in order to guarantee progress.
-			// Break the loop once L0 has enough space to accommodate new tables.
-			if !s.isLevel0Compactable() {
-				break
-			}
+		for s.levels[0].numTables() >= s.kv.opt.NumLevelZeroTablesStall {
 			time.Sleep(10 * time.Millisecond)
-			if i%100 == 0 {
-				prios := s.pickCompactLevels()
-				s.kv.opt.Debugf("Waiting to add level 0 table. Compaction priorities: %+v\n", prios)
-				i = 0
-			}
 		}
-		if dur := time.Since(timeStart); dur > time.Second {
+		dur := time.Since(timeStart)
+		if dur > time.Second {
 			s.kv.opt.Infof("L0 was stalled for %s\n", dur.Round(time.Millisecond))
 		}
+		atomic.AddInt64(&s.l0stallsMs, int64(dur.Round(time.Millisecond)))
 	}
 
 	return nil
@@ -1117,7 +1458,7 @@ type TableInfo struct {
 	Left             []byte
 	Right            []byte
 	KeyCount         uint32 // Number of keys in the table
-	EstimatedSz      uint32
+	OnDiskSize       uint32
 	UncompressedSize uint32
 	MaxVersion       uint64
 	IndexSz          int
@@ -1134,7 +1475,7 @@ func (s *levelsController) getTableInfo() (result []TableInfo) {
 				Left:             t.Smallest(),
 				Right:            t.Biggest(),
 				KeyCount:         t.KeyCount(),
-				EstimatedSz:      t.EstimatedSize(),
+				OnDiskSize:       t.OnDiskSize(),
 				IndexSz:          t.IndexSize(),
 				BloomFilterSize:  t.BloomFilterSize(),
 				UncompressedSize: t.UncompressedSize(),
@@ -1153,6 +1494,39 @@ func (s *levelsController) getTableInfo() (result []TableInfo) {
 	return
 }
 
+type LevelInfo struct {
+	Level          int
+	NumTables      int
+	Size           int64
+	TargetSize     int64
+	TargetFileSize int64
+	IsBaseLevel    bool
+	Score          float64
+	Adjusted       float64
+}
+
+func (s *levelsController) getLevelInfo() []LevelInfo {
+	t := s.levelTargets()
+	prios := s.pickCompactLevels()
+	result := make([]LevelInfo, len(s.levels))
+	for i, l := range s.levels {
+		l.RLock()
+		result[i].Level = i
+		result[i].Size = l.totalSize
+		result[i].NumTables = len(l.tables)
+		l.RUnlock()
+
+		result[i].TargetSize = t.targetSz[i]
+		result[i].TargetFileSize = t.fileSz[i]
+		result[i].IsBaseLevel = t.baseLevel == i
+	}
+	for _, p := range prios {
+		result[p.level].Score = p.score
+		result[p.level].Adjusted = p.adjusted
+	}
+	return result
+}
+
 // verifyChecksum verifies checksum for all tables on all levels.
 func (s *levelsController) verifyChecksum() error {
 	var tables []*table.Table
diff --git a/memtable.go b/memtable.go
index 712d2fc..a6f33ba 100644
--- a/memtable.go
+++ b/memtable.go
@@ -81,10 +81,20 @@ func (db *DB) openMemTables(opt Options) error {
 		return fids[i] < fids[j]
 	})
 	for _, fid := range fids {
-		mt, err := db.openMemTable(fid)
+		flags := os.O_RDWR
+		if db.opt.ReadOnly {
+			flags = os.O_RDONLY
+		}
+		mt, err := db.openMemTable(fid, flags)
 		if err != nil {
 			return y.Wrapf(err, "while opening fid: %d", fid)
 		}
+		// If this memtable is empty we don't need to add it. This is a
+		// memtable that was completely truncated.
+		if mt.sl.Empty() {
+			mt.DecrRef()
+			continue
+		}
 		// These should no longer be written to. So, make them part of the imm.
 		db.imm = append(db.imm, mt)
 	}
@@ -97,7 +107,7 @@ func (db *DB) openMemTables(opt Options) error {
 
 const memFileExt string = ".mem"
 
-func (db *DB) openMemTable(fid int) (*memTable, error) {
+func (db *DB) openMemTable(fid, flags int) (*memTable, error) {
 	filepath := db.mtFilePath(fid)
 	s := skl.NewSkiplist(arenaSize(db.opt))
 	mt := &memTable{
@@ -115,8 +125,9 @@ func (db *DB) openMemTable(fid int) (*memTable, error) {
 		path:     filepath,
 		registry: db.registry,
 		writeAt:  vlogHeaderSize,
+		opt:      db.opt,
 	}
-	lerr := mt.wal.open(filepath, os.O_RDWR|os.O_CREATE, db.opt)
+	lerr := mt.wal.open(filepath, flags, 2*db.opt.MemTableSize)
 	if lerr != z.NewFile && lerr != nil {
 		return nil, y.Wrapf(lerr, "While opening memtable: %s", filepath)
 	}
@@ -139,7 +150,7 @@ func (db *DB) openMemTable(fid int) (*memTable, error) {
 var errExpectingNewFile = errors.New("Expecting to create a new file, but found an existing file")
 
 func (db *DB) newMemTable() (*memTable, error) {
-	mt, err := db.openMemTable(db.nextMemFid)
+	mt, err := db.openMemTable(db.nextMemFid, os.O_CREATE|os.O_RDWR)
 	if err == z.NewFile {
 		db.nextMemFid++
 		return mt, nil
@@ -160,6 +171,17 @@ func (mt *memTable) SyncWAL() error {
 	return mt.wal.Sync()
 }
 
+func (mt *memTable) isFull() bool {
+	if mt.sl.MemSize() >= mt.opt.MemTableSize {
+		return true
+	}
+	if mt.opt.InMemory {
+		// InMemory mode doesn't have any WAL.
+		return false
+	}
+	return int64(mt.wal.writeAt) >= mt.opt.MemTableSize
+}
+
 func (mt *memTable) Put(key []byte, value y.ValueStruct) error {
 	entry := &Entry{
 		Key:       key,
@@ -257,13 +279,12 @@ type logFile struct {
 }
 
 func (lf *logFile) Truncate(end int64) error {
-	if lf.size == uint32(end) {
+	if fi, err := lf.Fd.Stat(); err != nil {
+		return fmt.Errorf("while file.stat on file: %s, error: %v\n", lf.Fd.Name(), err)
+	} else if fi.Size() == end {
 		return nil
 	}
-	if lf.opt.ReadOnly {
-		return y.Wrapf(ErrTruncateNeeded,
-			"truncate to %d from %d for file: %s", end, lf.size, lf.path)
-	}
+	y.AssertTrue(!lf.opt.ReadOnly)
 	lf.size = uint32(end)
 	return lf.MmapFile.Truncate(end)
 }
@@ -531,10 +552,8 @@ func (lf *logFile) zeroNextEntry() {
 	z.ZeroOut(lf.Data, int(lf.writeAt), int(lf.writeAt+maxHeaderSize))
 }
 
-func (lf *logFile) open(path string, flags int, opt Options) error {
-	lf.opt = opt
-
-	mf, ferr := z.OpenMmapFile(path, flags, 2*int(opt.ValueLogFileSize))
+func (lf *logFile) open(path string, flags int, fsize int64) error {
+	mf, ferr := z.OpenMmapFile(path, flags, int(fsize))
 	lf.MmapFile = mf
 
 	if ferr == z.NewFile {
@@ -563,7 +582,7 @@ func (lf *logFile) open(path string, flags int, opt Options) error {
 		"Unable to copy from %s, size %d", path, lf.size)
 	keyID := binary.BigEndian.Uint64(buf[:8])
 	// retrieve datakey.
-	if dk, err := lf.registry.dataKey(keyID); err != nil {
+	if dk, err := lf.registry.DataKey(keyID); err != nil {
 		return y.Wrapf(err, "While opening vlog file %d", lf.fid)
 	} else {
 		lf.dataKey = dk
@@ -585,7 +604,7 @@ func (lf *logFile) bootstrap() error {
 
 	// generate data key for the log file.
 	var dk *pb.DataKey
-	if dk, err = lf.registry.latestDataKey(); err != nil {
+	if dk, err = lf.registry.LatestDataKey(); err != nil {
 		return y.Wrapf(err, "Error while retrieving datakey in logFile.bootstarp")
 	}
 	lf.dataKey = dk
diff --git a/options.go b/options.go
index 8184aaa..ede01d2 100644
--- a/options.go
+++ b/options.go
@@ -22,6 +22,7 @@ import (
 
 	"github.com/dgraph-io/badger/v2/options"
 	"github.com/dgraph-io/badger/v2/table"
+	"github.com/dgraph-io/badger/v2/y"
 )
 
 // Note: If you add a new option X make sure you also add a WithX method on Options.
@@ -50,11 +51,15 @@ type Options struct {
 
 	// Fine tuning options.
 
-	MaxTableSize        int64
+	MemTableSize        int64
+	BaseTableSize       int64
+	BaseLevelSize       int64
 	LevelSizeMultiplier int
+	TableSizeMultiplier int
 	MaxLevels           int
-	ValueThreshold      int
-	NumMemtables        int
+
+	ValueThreshold int
+	NumMemtables   int
 	// Changing BlockSize across DB runs will not break badger. The block size is
 	// read from the block index stored at the end of the table.
 	BlockSize          int
@@ -65,13 +70,11 @@ type Options struct {
 	NumLevelZeroTables      int
 	NumLevelZeroTablesStall int
 
-	LevelOneSize       int64
 	ValueLogFileSize   int64
 	ValueLogMaxEntries uint32
 
 	NumCompactors        int
 	CompactL0OnClose     bool
-	LogRotatesToFlush    int32
 	ZSTDCompressionLevel int
 
 	// When set, checksum will be validated for each entry read from the value log file.
@@ -109,13 +112,17 @@ type Options struct {
 // Feel free to modify these to suit your needs with the WithX methods.
 func DefaultOptions(path string) Options {
 	return Options{
-		Dir:                     path,
-		ValueDir:                path,
-		LevelOneSize:            256 << 20,
-		LevelSizeMultiplier:     15,
-		MaxLevels:               7,
-		MaxTableSize:            64 << 20,
-		NumCompactors:           2, // Run at least 2 compactors. One is dedicated for L0.
+		Dir:      path,
+		ValueDir: path,
+
+		MemTableSize:        64 << 20,
+		BaseTableSize:       2 << 20,
+		BaseLevelSize:       10 << 20,
+		TableSizeMultiplier: 2,
+		LevelSizeMultiplier: 10,
+		MaxLevels:           7,
+
+		NumCompactors:           4, // Run at least 2 compactors. Zero-th compactor prioritizes L0.
 		NumLevelZeroTables:      5,
 		NumLevelZeroTablesStall: 15,
 		NumMemtables:            5,
@@ -123,10 +130,10 @@ func DefaultOptions(path string) Options {
 		BlockSize:               4 * 1024,
 		SyncWrites:              false,
 		NumVersionsToKeep:       1,
-		CompactL0OnClose:        true,
+		CompactL0OnClose:        false,
 		VerifyValueChecksum:     false,
-		Compression:             options.None,
-		BlockCacheSize:          0,
+		Compression:             options.Snappy,
+		BlockCacheSize:          256 << 20,
 		IndexCacheSize:          0,
 
 		// The following benchmarks were done on a 4 KB block size (default block size). The
@@ -149,23 +156,29 @@ func DefaultOptions(path string) Options {
 		ValueLogMaxEntries:            1000000,
 		ValueThreshold:                1 << 10, // 1 KB.
 		Logger:                        defaultLogger(INFO),
-		LogRotatesToFlush:             2,
 		EncryptionKey:                 []byte{},
 		EncryptionKeyRotationDuration: 10 * 24 * time.Hour, // Default 10 days.
 		DetectConflicts:               true,
 	}
 }
 
-func buildTableOptions(opt Options) table.Options {
+func buildTableOptions(db *DB) table.Options {
+	opt := db.opt
+	dk, err := db.registry.LatestDataKey()
+	y.Check(err)
 	return table.Options{
 		SyncWrites:           opt.SyncWrites,
 		ReadOnly:             opt.ReadOnly,
-		TableSize:            uint64(opt.MaxTableSize),
+		TableSize:            uint64(opt.BaseTableSize),
 		BlockSize:            opt.BlockSize,
 		BloomFalsePositive:   opt.BloomFalsePositive,
 		ChkMode:              opt.ChecksumVerificationMode,
 		Compression:          opt.Compression,
 		ZSTDCompressionLevel: opt.ZSTDCompressionLevel,
+		BlockCache:           db.blockCache,
+		IndexCache:           db.indexCache,
+		AllocPool:            db.allocPool,
+		DataKey:              dk,
 	}
 }
 
@@ -269,13 +282,13 @@ func (opt Options) WithLoggingLevel(val loggingLevel) Options {
 	return opt
 }
 
-// WithMaxTableSize returns a new Options value with MaxTableSize set to the given value.
+// WithBaseTableSize returns a new Options value with MaxTableSize set to the given value.
 //
-// MaxTableSize sets the maximum size in bytes for each LSM table or file.
+// BaseTableSize sets the maximum size in bytes for LSM table or file in the base level.
 //
-// The default value of MaxTableSize is 64MB.
-func (opt Options) WithMaxTableSize(val int64) Options {
-	opt.MaxTableSize = val
+// The default value of BaseTableSize is 2MB.
+func (opt Options) WithBaseTableSize(val int64) Options {
+	opt.BaseTableSize = val
 	return opt
 }
 
@@ -286,7 +299,7 @@ func (opt Options) WithMaxTableSize(val int64) Options {
 // Once a level grows to be larger than this ratio allowed, the compaction process will be
 //  triggered.
 //
-// The default value of LevelSizeMultiplier is 15.
+// The default value of LevelSizeMultiplier is 10.
 func (opt Options) WithLevelSizeMultiplier(val int) Options {
 	opt.LevelSizeMultiplier = val
 	return opt
@@ -323,6 +336,16 @@ func (opt Options) WithNumMemtables(val int) Options {
 	return opt
 }
 
+// WithMemTableSize returns a new Options value with MemTableSize set to the given value.
+//
+// MemTableSize sets the maximum size in bytes for memtable table.
+//
+// The default value of MemTableSize is 64MB.
+func (opt Options) WithMemTableSize(val int64) Options {
+	opt.MemTableSize = val
+	return opt
+}
+
 // WithBloomFalsePositive returns a new Options value with BloomFalsePositive set
 // to the given value.
 //
@@ -350,10 +373,7 @@ func (opt Options) WithBlockSize(val int) Options {
 	return opt
 }
 
-// WithNumLevelZeroTables returns a new Options value with NumLevelZeroTables set to the given
-// value.
-//
-// NumLevelZeroTables sets the maximum number of Level 0 tables before compaction starts.
+// WithNumLevelZeroTables sets the maximum number of Level 0 tables before compaction starts.
 //
 // The default value of NumLevelZeroTables is 5.
 func (opt Options) WithNumLevelZeroTables(val int) Options {
@@ -361,10 +381,7 @@ func (opt Options) WithNumLevelZeroTables(val int) Options {
 	return opt
 }
 
-// WithNumLevelZeroTablesStall returns a new Options value with NumLevelZeroTablesStall set to the
-// given value.
-//
-// NumLevelZeroTablesStall sets the number of Level 0 tables that once reached causes the DB to
+// WithNumLevelZeroTablesStall sets the number of Level 0 tables that once reached causes the DB to
 // stall until compaction succeeds.
 //
 // The default value of NumLevelZeroTablesStall is 10.
@@ -373,19 +390,15 @@ func (opt Options) WithNumLevelZeroTablesStall(val int) Options {
 	return opt
 }
 
-// WithLevelOneSize returns a new Options value with LevelOneSize set to the given value.
-//
-// LevelOneSize sets the maximum total size for Level 1.
+// WithBaseLevelSize sets the maximum size target for the base level.
 //
-// The default value of LevelOneSize is 20MB.
-func (opt Options) WithLevelOneSize(val int64) Options {
-	opt.LevelOneSize = val
+// The default value is 10MB.
+func (opt Options) WithBaseLevelSize(val int64) Options {
+	opt.BaseLevelSize = val
 	return opt
 }
 
-// WithValueLogFileSize returns a new Options value with ValueLogFileSize set to the given value.
-//
-// ValueLogFileSize sets the maximum size of a single value log file.
+// WithValueLogFileSize sets the maximum size of a single value log file.
 //
 // The default value of ValueLogFileSize is 1GB.
 func (opt Options) WithValueLogFileSize(val int64) Options {
@@ -393,12 +406,9 @@ func (opt Options) WithValueLogFileSize(val int64) Options {
 	return opt
 }
 
-// WithValueLogMaxEntries returns a new Options value with ValueLogMaxEntries set to the given
-// value.
-//
-// ValueLogMaxEntries sets the maximum number of entries a value log file can hold approximately.
-// A actual size limit of a value log file is the minimum of ValueLogFileSize and
-// ValueLogMaxEntries.
+// WithValueLogMaxEntries sets the maximum number of entries a value log file
+// can hold approximately.  A actual size limit of a value log file is the
+// minimum of ValueLogFileSize and ValueLogMaxEntries.
 //
 // The default value of ValueLogMaxEntries is one million (1000000).
 func (opt Options) WithValueLogMaxEntries(val uint32) Options {
@@ -406,10 +416,8 @@ func (opt Options) WithValueLogMaxEntries(val uint32) Options {
 	return opt
 }
 
-// WithNumCompactors returns a new Options value with NumCompactors set to the given value.
-//
-// NumCompactors sets the number of compaction workers to run concurrently.
-// Setting this to zero stops compactions, which could eventually cause writes to block forever.
+// WithNumCompactors sets the number of compaction workers to run concurrently.  Setting this to
+// zero stops compactions, which could eventually cause writes to block forever.
 //
 // The default value of NumCompactors is 2. One is dedicated just for L0 and L1.
 func (opt Options) WithNumCompactors(val int) Options {
@@ -417,35 +425,16 @@ func (opt Options) WithNumCompactors(val int) Options {
 	return opt
 }
 
-// WithCompactL0OnClose returns a new Options value with CompactL0OnClose set to the given value.
-//
-// CompactL0OnClose determines whether Level 0 should be compacted before closing the DB.
-// This ensures that both reads and writes are efficient when the DB is opened later.
-// CompactL0OnClose is set to true if KeepL0InMemory is set to true.
+// WithCompactL0OnClose determines whether Level 0 should be compacted before closing the DB.  This
+// ensures that both reads and writes are efficient when the DB is opened later.
 //
-// The default value of CompactL0OnClose is true.
+// The default value of CompactL0OnClose is false.
 func (opt Options) WithCompactL0OnClose(val bool) Options {
 	opt.CompactL0OnClose = val
 	return opt
 }
 
-// WithLogRotatesToFlush returns a new Options value with LogRotatesToFlush set to the given value.
-//
-// LogRotatesToFlush sets the number of value log file rotates after which the Memtables are
-// flushed to disk. This is useful in write loads with fewer keys and larger values. This work load
-// would fill up the value logs quickly, while not filling up the Memtables. Thus, on a crash
-// and restart, the value log head could cause the replay of a good number of value log files
-// which can slow things on start.
-//
-// The default value of LogRotatesToFlush is 2.
-func (opt Options) WithLogRotatesToFlush(val int32) Options {
-	opt.LogRotatesToFlush = val
-	return opt
-}
-
-// WithEncryptionKey return a new Options value with EncryptionKey set to the given value.
-//
-// EncryptionKey is used to encrypt the data with AES. Type of AES is used based on the key
+// WithEncryptionKey is used to encrypt the data with AES. Type of AES is used based on the key
 // size. For example 16 bytes will use AES-128. 24 bytes will use AES-192. 32 bytes will
 // use AES-256.
 func (opt Options) WithEncryptionKey(key []byte) Options {
@@ -463,10 +452,9 @@ func (opt Options) WithEncryptionKeyRotationDuration(d time.Duration) Options {
 	return opt
 }
 
-// WithCompression returns a new Options value with Compression set to the given value.
-//
-// When compression is enabled, every block will be compressed using the specified algorithm.
-// This option doesn't affect existing tables. Only the newly created tables will be compressed.
+// WithCompression is used to enable or disable compression. When compression is enabled, every
+// block will be compressed using the specified algorithm.  This option doesn't affect existing
+// tables. Only the newly created tables will be compressed.
 //
 // The default compression algorithm used is zstd when built with Cgo. Without Cgo, the default is
 // snappy. Compression is enabled by default.
@@ -475,12 +463,9 @@ func (opt Options) WithCompression(cType options.CompressionType) Options {
 	return opt
 }
 
-// WithVerifyValueChecksum returns a new Options value with VerifyValueChecksum set to
-// the given value.
-//
-// When VerifyValueChecksum is set to true, checksum will be verified for every entry read
-// from the value log. If the value is stored in SST (value size less than value threshold) then the
-// checksum validation will not be done.
+// WithVerifyValueChecksum is used to set VerifyValueChecksum. When VerifyValueChecksum is set to
+// true, checksum will be verified for every entry read from the value log. If the value is stored
+// in SST (value size less than value threshold) then the checksum validation will not be done.
 //
 // The default value of VerifyValueChecksum is False.
 func (opt Options) WithVerifyValueChecksum(val bool) Options {
diff --git a/pb/badgerpb2.pb.go b/pb/badgerpb2.pb.go
index ba6f93b..6da2cf8 100644
--- a/pb/badgerpb2.pb.go
+++ b/pb/badgerpb2.pb.go
@@ -200,7 +200,9 @@ func (m *KV) GetStreamDone() bool {
 }
 
 type KVList struct {
-	Kv                   []*KV    `protobuf:"bytes,1,rep,name=kv,proto3" json:"kv,omitempty"`
+	Kv []*KV `protobuf:"bytes,1,rep,name=kv,proto3" json:"kv,omitempty"`
+	// alloc_ref used internally for memory management.
+	AllocRef             uint64   `protobuf:"varint,10,opt,name=alloc_ref,json=allocRef,proto3" json:"alloc_ref,omitempty"`
 	XXX_NoUnkeyedLiteral struct{} `json:"-"`
 	XXX_unrecognized     []byte   `json:"-"`
 	XXX_sizecache        int32    `json:"-"`
@@ -246,6 +248,13 @@ func (m *KVList) GetKv() []*KV {
 	return nil
 }
 
+func (m *KVList) GetAllocRef() uint64 {
+	if m != nil {
+		return m.AllocRef
+	}
+	return 0
+}
+
 type ManifestChangeSet struct {
 	// A set of changes that are applied atomically.
 	Changes              []*ManifestChange `protobuf:"bytes,1,rep,name=changes,proto3" json:"changes,omitempty"`
@@ -522,44 +531,45 @@ func init() {
 func init() { proto.RegisterFile("badgerpb2.proto", fileDescriptor_e63e84f9f0d3998c) }
 
 var fileDescriptor_e63e84f9f0d3998c = []byte{
-	// 589 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x93, 0xcf, 0x6e, 0xda, 0x4e,
-	0x10, 0xc7, 0x59, 0xe3, 0xf0, 0x67, 0x48, 0x08, 0xbf, 0xd5, 0xaf, 0x92, 0xa3, 0x2a, 0x94, 0x3a,
-	0xaa, 0x8a, 0x2a, 0x15, 0x54, 0xa8, 0x7a, 0x27, 0x04, 0x29, 0x88, 0x44, 0x91, 0xb6, 0x51, 0x14,
-	0xf5, 0x82, 0x16, 0x7b, 0x0a, 0x16, 0xf8, 0x8f, 0x76, 0x17, 0xab, 0x3c, 0x44, 0xef, 0x7d, 0xa4,
-	0x1e, 0x7b, 0xe8, 0x03, 0x54, 0xe9, 0x8b, 0x54, 0xbb, 0x36, 0x14, 0x0e, 0xbd, 0xcd, 0x7c, 0xe7,
-	0xbb, 0x3b, 0xe3, 0xcf, 0x8e, 0xe1, 0x74, 0xc6, 0xfd, 0x39, 0x8a, 0x64, 0xd6, 0xeb, 0x24, 0x22,
-	0x56, 0x31, 0xad, 0xee, 0x04, 0xf7, 0x27, 0x01, 0x6b, 0xf2, 0x40, 0x1b, 0x50, 0x5c, 0xe2, 0xc6,
-	0x21, 0x2d, 0xd2, 0x3e, 0x66, 0x3a, 0xa4, 0xff, 0xc3, 0x51, 0xca, 0x57, 0x6b, 0x74, 0x2c, 0xa3,
-	0x65, 0x09, 0x7d, 0x0e, 0xd5, 0xb5, 0x44, 0x31, 0x0d, 0x51, 0x71, 0xa7, 0x68, 0x2a, 0x15, 0x2d,
-	0xdc, 0xa2, 0xe2, 0xd4, 0x81, 0x72, 0x8a, 0x42, 0x06, 0x71, 0xe4, 0xd8, 0x2d, 0xd2, 0xb6, 0xd9,
-	0x36, 0xa5, 0xe7, 0x00, 0xf8, 0x25, 0x09, 0x04, 0xca, 0x29, 0x57, 0xce, 0x91, 0x29, 0x56, 0x73,
-	0x65, 0xa0, 0x28, 0x05, 0xdb, 0x5c, 0x58, 0x32, 0x17, 0x9a, 0x58, 0x77, 0x92, 0x4a, 0x20, 0x0f,
-	0xa7, 0x81, 0xef, 0x40, 0x8b, 0xb4, 0x4f, 0x58, 0x25, 0x13, 0xc6, 0x3e, 0x7d, 0x01, 0xb5, 0xbc,
-	0xe8, 0xc7, 0x11, 0x3a, 0xb5, 0x16, 0x69, 0x57, 0x18, 0x64, 0xd2, 0x55, 0x1c, 0xa1, 0xfb, 0x1a,
-	0x4a, 0x93, 0x87, 0x9b, 0x40, 0x2a, 0x7a, 0x0e, 0xd6, 0x32, 0x75, 0x48, 0xab, 0xd8, 0xae, 0xf5,
-	0x4e, 0x3a, 0x7f, 0x49, 0x4c, 0x1e, 0x98, 0xb5, 0x4c, 0xdd, 0x6b, 0xf8, 0xef, 0x96, 0x47, 0xc1,
-	0x67, 0x94, 0x6a, 0xb8, 0xe0, 0xd1, 0x1c, 0x3f, 0xa2, 0xa2, 0x7d, 0x28, 0x7b, 0x26, 0x91, 0xf9,
-	0xc1, 0xb3, 0xbd, 0x83, 0x87, 0x76, 0xb6, 0x75, 0xba, 0x5f, 0x2d, 0xa8, 0x1f, 0xd6, 0x68, 0x1d,
-	0xac, 0xb1, 0x6f, 0xa0, 0xda, 0xcc, 0x1a, 0xfb, 0xb4, 0x0f, 0xd6, 0x5d, 0x62, 0x80, 0xd6, 0x7b,
-	0x17, 0xff, 0xbc, 0xb2, 0x73, 0x97, 0xa0, 0xe0, 0x2a, 0x88, 0x23, 0x66, 0xdd, 0x25, 0xfa, 0x21,
-	0x6e, 0x30, 0xc5, 0x95, 0xc1, 0x7d, 0xc2, 0xb2, 0x84, 0x3e, 0x83, 0xd2, 0x12, 0x37, 0x9a, 0x4d,
-	0x86, 0xfa, 0x68, 0x89, 0x9b, 0xb1, 0x4f, 0x2f, 0xe1, 0x14, 0x23, 0x4f, 0x6c, 0x12, 0x7d, 0x7c,
-	0xca, 0x57, 0xf3, 0xd8, 0xd0, 0xae, 0x1f, 0x7c, 0xc1, 0x68, 0xe7, 0x18, 0xac, 0xe6, 0x31, 0xab,
-	0xe3, 0x41, 0x4e, 0x5b, 0x50, 0xf3, 0xe2, 0x30, 0x11, 0x28, 0xcd, 0x53, 0x96, 0x4c, 0xdb, 0x7d,
-	0xc9, 0xbd, 0x80, 0xea, 0x6e, 0x46, 0x0a, 0x50, 0x1a, 0xb2, 0xd1, 0xe0, 0x7e, 0xd4, 0x28, 0xe8,
-	0xf8, 0x6a, 0x74, 0x33, 0xba, 0x1f, 0x35, 0x88, 0x9b, 0x42, 0x65, 0xb8, 0x40, 0x6f, 0x29, 0xd7,
-	0x21, 0x7d, 0x07, 0xb6, 0x99, 0x85, 0x98, 0x59, 0xce, 0xf7, 0x66, 0xd9, 0x5a, 0x3a, 0xba, 0xb5,
-	0x08, 0xd4, 0x22, 0x64, 0xc6, 0xaa, 0x37, 0x52, 0xae, 0x43, 0x03, 0xcb, 0x66, 0x3a, 0x74, 0x5f,
-	0x41, 0x75, 0x67, 0xca, 0xba, 0x0e, 0xfb, 0xbd, 0x61, 0xa3, 0x40, 0x8f, 0xa1, 0xf2, 0xf8, 0x78,
-	0xcd, 0xe5, 0xe2, 0xc3, 0xfb, 0x06, 0x71, 0x3d, 0x28, 0x5f, 0x71, 0xc5, 0x27, 0xb8, 0xd9, 0x83,
-	0x44, 0xf6, 0x21, 0x51, 0xb0, 0x7d, 0xae, 0x78, 0xbe, 0xd9, 0x26, 0xd6, 0x4f, 0x15, 0xa4, 0xf9,
-	0x46, 0x5b, 0x41, 0xaa, 0x37, 0xd6, 0x13, 0xc8, 0x15, 0xfa, 0x7a, 0x63, 0x35, 0xe3, 0x22, 0xab,
-	0xe6, 0xca, 0x40, 0xbd, 0x39, 0x83, 0xfa, 0x21, 0x45, 0x5a, 0x86, 0x22, 0x47, 0xd9, 0x28, 0x5c,
-	0xf6, 0xbf, 0x3f, 0x35, 0xc9, 0x8f, 0xa7, 0x26, 0xf9, 0xf5, 0xd4, 0x24, 0xdf, 0x7e, 0x37, 0x0b,
-	0x9f, 0x5e, 0xce, 0x03, 0xb5, 0x58, 0xcf, 0x3a, 0x5e, 0x1c, 0x76, 0xfd, 0xb9, 0xe0, 0xc9, 0xe2,
-	0x6d, 0x10, 0x77, 0x33, 0x06, 0xdd, 0xb4, 0xd7, 0x4d, 0x66, 0xb3, 0x92, 0xf9, 0x31, 0xfb, 0x7f,
-	0x02, 0x00, 0x00, 0xff, 0xff, 0x1d, 0xc3, 0x11, 0xa3, 0xab, 0x03, 0x00, 0x00,
+	// 604 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x74, 0x53, 0xdd, 0x6e, 0xda, 0x4c,
+	0x10, 0x65, 0x8d, 0xc3, 0xcf, 0x90, 0x10, 0xbe, 0xd5, 0x57, 0xc9, 0x51, 0x15, 0x4a, 0x1d, 0x55,
+	0x42, 0x95, 0x0a, 0x2a, 0x54, 0xbd, 0x27, 0x80, 0x14, 0x44, 0xa2, 0x48, 0xdb, 0x28, 0x8a, 0x7a,
+	0x83, 0x16, 0x7b, 0x02, 0x16, 0xf8, 0x47, 0xbb, 0x8b, 0x55, 0x1e, 0xa2, 0xf7, 0x7d, 0xa4, 0x5e,
+	0xf6, 0xa2, 0x0f, 0x50, 0xa5, 0x2f, 0x52, 0xed, 0xda, 0xa1, 0x70, 0xd1, 0xbb, 0x99, 0x33, 0xe3,
+	0x39, 0xe3, 0x73, 0x66, 0xe1, 0x74, 0xce, 0xfd, 0x05, 0x8a, 0x64, 0xde, 0xeb, 0x24, 0x22, 0x56,
+	0x31, 0xad, 0xee, 0x00, 0xf7, 0x27, 0x01, 0x6b, 0x7a, 0x4f, 0x1b, 0x50, 0x5c, 0xe1, 0xd6, 0x21,
+	0x2d, 0xd2, 0x3e, 0x66, 0x3a, 0xa4, 0xff, 0xc3, 0x51, 0xca, 0xd7, 0x1b, 0x74, 0x2c, 0x83, 0x65,
+	0x09, 0x7d, 0x09, 0xd5, 0x8d, 0x44, 0x31, 0x0b, 0x51, 0x71, 0xa7, 0x68, 0x2a, 0x15, 0x0d, 0xdc,
+	0xa0, 0xe2, 0xd4, 0x81, 0x72, 0x8a, 0x42, 0x06, 0x71, 0xe4, 0xd8, 0x2d, 0xd2, 0xb6, 0xd9, 0x73,
+	0x4a, 0xcf, 0x01, 0xf0, 0x4b, 0x12, 0x08, 0x94, 0x33, 0xae, 0x9c, 0x23, 0x53, 0xac, 0xe6, 0xc8,
+	0x40, 0x51, 0x0a, 0xb6, 0x19, 0x58, 0x32, 0x03, 0x4d, 0xac, 0x99, 0xa4, 0x12, 0xc8, 0xc3, 0x59,
+	0xe0, 0x3b, 0xd0, 0x22, 0xed, 0x13, 0x56, 0xc9, 0x80, 0x89, 0x4f, 0x5f, 0x41, 0x2d, 0x2f, 0xfa,
+	0x71, 0x84, 0x4e, 0xad, 0x45, 0xda, 0x15, 0x06, 0x19, 0x34, 0x8a, 0x23, 0x74, 0x47, 0x50, 0x9a,
+	0xde, 0x5f, 0x07, 0x52, 0xd1, 0x73, 0xb0, 0x56, 0xa9, 0x43, 0x5a, 0xc5, 0x76, 0xad, 0x77, 0xd2,
+	0xf9, 0xab, 0xc4, 0xf4, 0x9e, 0x59, 0xab, 0x54, 0xd3, 0xf0, 0xf5, 0x3a, 0xf6, 0x66, 0x02, 0x1f,
+	0x0d, 0x8d, 0xcd, 0x2a, 0x06, 0x60, 0xf8, 0xe8, 0x5e, 0xc1, 0x7f, 0x37, 0x3c, 0x0a, 0x1e, 0x51,
+	0xaa, 0xe1, 0x92, 0x47, 0x0b, 0xfc, 0x84, 0x8a, 0xf6, 0xa1, 0xec, 0x99, 0x44, 0xe6, 0x53, 0xcf,
+	0xf6, 0xa6, 0x1e, 0xb6, 0xb3, 0xe7, 0x4e, 0xf7, 0xab, 0x05, 0xf5, 0xc3, 0x1a, 0xad, 0x83, 0x35,
+	0xf1, 0x8d, 0xe2, 0x36, 0xb3, 0x26, 0x3e, 0xed, 0x83, 0x75, 0x9b, 0x18, 0xb5, 0xeb, 0xbd, 0x8b,
+	0x7f, 0x8e, 0xec, 0xdc, 0x26, 0x28, 0xb8, 0x0a, 0xe2, 0x88, 0x59, 0xb7, 0x89, 0x76, 0xe9, 0x1a,
+	0x53, 0x5c, 0x1b, 0x2f, 0x4e, 0x58, 0x96, 0xd0, 0x17, 0x50, 0x5a, 0xe1, 0x56, 0x0b, 0x97, 0xf9,
+	0x70, 0xb4, 0xc2, 0xed, 0xc4, 0xa7, 0x97, 0x70, 0x8a, 0x91, 0x27, 0xb6, 0x89, 0xfe, 0x7c, 0xc6,
+	0xd7, 0x8b, 0xd8, 0x58, 0x51, 0x3f, 0xf8, 0x83, 0xf1, 0xae, 0x63, 0xb0, 0x5e, 0xc4, 0xac, 0x8e,
+	0x07, 0x39, 0x6d, 0x41, 0xcd, 0x8b, 0xc3, 0x44, 0xa0, 0x34, 0x3e, 0x97, 0x0c, 0xed, 0x3e, 0xe4,
+	0x5e, 0x40, 0x75, 0xb7, 0x23, 0x05, 0x28, 0x0d, 0xd9, 0x78, 0x70, 0x37, 0x6e, 0x14, 0x74, 0x3c,
+	0x1a, 0x5f, 0x8f, 0xef, 0xc6, 0x0d, 0xe2, 0xa6, 0x50, 0x19, 0x2e, 0xd1, 0x5b, 0xc9, 0x4d, 0x48,
+	0xdf, 0x83, 0x6d, 0x76, 0x21, 0x66, 0x97, 0xf3, 0xbd, 0x5d, 0x9e, 0x5b, 0x3a, 0x9a, 0x5a, 0x04,
+	0x6a, 0x19, 0x32, 0xd3, 0xaa, 0xcf, 0x55, 0x6e, 0x42, 0x23, 0x96, 0xcd, 0x74, 0xe8, 0xbe, 0x81,
+	0xea, 0xae, 0x29, 0x63, 0x1d, 0xf6, 0x7b, 0xc3, 0x46, 0x81, 0x1e, 0x43, 0xe5, 0xe1, 0xe1, 0x8a,
+	0xcb, 0xe5, 0xc7, 0x0f, 0x0d, 0xe2, 0x7a, 0x50, 0x1e, 0x71, 0xc5, 0xa7, 0xb8, 0xdd, 0x13, 0x89,
+	0xec, 0x8b, 0x44, 0xc1, 0xf6, 0xb9, 0xe2, 0xf9, 0xd9, 0x9b, 0x58, 0x5b, 0x15, 0xa4, 0xf9, 0xb9,
+	0x5b, 0x41, 0xaa, 0xcf, 0xd9, 0x13, 0xc8, 0x15, 0xfa, 0xfa, 0x9c, 0xb5, 0xc6, 0x45, 0x56, 0xcd,
+	0x91, 0x81, 0x7a, 0x7b, 0x06, 0xf5, 0x43, 0x15, 0x69, 0x19, 0x8a, 0x1c, 0x65, 0xa3, 0x70, 0xd9,
+	0xff, 0xfe, 0xd4, 0x24, 0x3f, 0x9e, 0x9a, 0xe4, 0xd7, 0x53, 0x93, 0x7c, 0xfb, 0xdd, 0x2c, 0x7c,
+	0x7e, 0xbd, 0x08, 0xd4, 0x72, 0x33, 0xef, 0x78, 0x71, 0xd8, 0xf5, 0x17, 0x82, 0x27, 0xcb, 0x77,
+	0x41, 0xdc, 0xcd, 0x34, 0xe8, 0xa6, 0xbd, 0x6e, 0x32, 0x9f, 0x97, 0xcc, 0xab, 0xed, 0xff, 0x09,
+	0x00, 0x00, 0xff, 0xff, 0xa7, 0xb8, 0x6c, 0x4c, 0xc8, 0x03, 0x00, 0x00,
 }
 
 func (m *KV) Marshal() (dAtA []byte, err error) {
@@ -666,6 +676,11 @@ func (m *KVList) MarshalToSizedBuffer(dAtA []byte) (int, error) {
 		i -= len(m.XXX_unrecognized)
 		copy(dAtA[i:], m.XXX_unrecognized)
 	}
+	if m.AllocRef != 0 {
+		i = encodeVarintBadgerpb2(dAtA, i, uint64(m.AllocRef))
+		i--
+		dAtA[i] = 0x50
+	}
 	if len(m.Kv) > 0 {
 		for iNdEx := len(m.Kv) - 1; iNdEx >= 0; iNdEx-- {
 			{
@@ -932,6 +947,9 @@ func (m *KVList) Size() (n int) {
 			n += 1 + l + sovBadgerpb2(uint64(l))
 		}
 	}
+	if m.AllocRef != 0 {
+		n += 1 + sovBadgerpb2(uint64(m.AllocRef))
+	}
 	if m.XXX_unrecognized != nil {
 		n += len(m.XXX_unrecognized)
 	}
@@ -1366,6 +1384,25 @@ func (m *KVList) Unmarshal(dAtA []byte) error {
 				return err
 			}
 			iNdEx = postIndex
+		case 10:
+			if wireType != 0 {
+				return fmt.Errorf("proto: wrong wireType = %d for field AllocRef", wireType)
+			}
+			m.AllocRef = 0
+			for shift := uint(0); ; shift += 7 {
+				if shift >= 64 {
+					return ErrIntOverflowBadgerpb2
+				}
+				if iNdEx >= l {
+					return io.ErrUnexpectedEOF
+				}
+				b := dAtA[iNdEx]
+				iNdEx++
+				m.AllocRef |= uint64(b&0x7F) << shift
+				if b < 0x80 {
+					break
+				}
+			}
 		default:
 			iNdEx = preIndex
 			skippy, err := skipBadgerpb2(dAtA[iNdEx:])
diff --git a/pb/badgerpb2.proto b/pb/badgerpb2.proto
index 862e147..821185d 100644
--- a/pb/badgerpb2.proto
+++ b/pb/badgerpb2.proto
@@ -37,6 +37,9 @@ message KV {
 
 message KVList {
   repeated KV kv = 1;
+
+  // alloc_ref used internally for memory management.
+  uint64 alloc_ref = 10;
 }
 
 message ManifestChangeSet {
diff --git a/skl/arena.go b/skl/arena.go
index 66a8f48..b4f339e 100644
--- a/skl/arena.go
+++ b/skl/arena.go
@@ -17,6 +17,7 @@
 package skl
 
 import (
+	"log"
 	"sync/atomic"
 	"unsafe"
 
@@ -35,23 +36,28 @@ const (
 
 // Arena should be lock-free.
 type Arena struct {
-	n   uint32
-	buf []byte
+	data   []byte
+	offset uint32
 }
 
-// newArena returns a new arena.
-func newArena(n int64) *Arena {
-	// Don't store data at position 0 in order to reserve offset=0 as a kind
-	// of nil pointer.
-	out := &Arena{
-		n:   1,
-		buf: make([]byte, n),
+func (s *Arena) size() int64 {
+	return int64(atomic.LoadUint32(&s.offset))
+}
+
+func (s *Arena) allocate(sz uint32) uint32 {
+	offset := atomic.AddUint32(&s.offset, sz)
+	if offset >= uint32(len(s.data)) {
+		log.Fatalf("Arena too small, toWrite:%d newTotal:%d limit:%d",
+			sz, offset, len(s.data))
 	}
-	return out
+	return offset
 }
 
-func (s *Arena) size() int64 {
-	return int64(atomic.LoadUint32(&s.n))
+// allocateValue encodes valueStruct and put it in the arena buffer.
+// It returns the encoded uint64 => | size (32 bits) | offset (32 bits) |
+func (s *Arena) allocateValue(v y.ValueStruct) uint64 {
+	valOffset := s.putVal(v)
+	return encodeValue(valOffset, v.EncodedSize())
 }
 
 // putNode allocates a node in the arena. The node is aligned on a pointer-sized
@@ -63,10 +69,7 @@ func (s *Arena) putNode(height int) uint32 {
 
 	// Pad the allocation with enough bytes to ensure pointer alignment.
 	l := uint32(MaxNodeSize - unusedSize + nodeAlign)
-	n := atomic.AddUint32(&s.n, l)
-	y.AssertTruef(int(n) <= len(s.buf),
-		"Arena too small, toWrite:%d newTotal:%d limit:%d",
-		l, n, len(s.buf))
+	n := s.allocate(l)
 
 	// Return the aligned offset.
 	m := (n - l + uint32(nodeAlign)) & ^uint32(nodeAlign)
@@ -79,27 +82,19 @@ func (s *Arena) putNode(height int) uint32 {
 // decoding will incur some overhead.
 func (s *Arena) putVal(v y.ValueStruct) uint32 {
 	l := uint32(v.EncodedSize())
-	n := atomic.AddUint32(&s.n, l)
-	y.AssertTruef(int(n) <= len(s.buf),
-		"Arena too small, toWrite:%d newTotal:%d limit:%d",
-		l, n, len(s.buf))
-	m := n - l
-	v.Encode(s.buf[m:])
-	return m
+	offset := s.allocate(l) - l
+	buf := s.data[offset : offset+l]
+	v.Encode(buf)
+	return offset
 }
 
+// putKey puts the key and returns its offset
 func (s *Arena) putKey(key []byte) uint32 {
-	l := uint32(len(key))
-	n := atomic.AddUint32(&s.n, l)
-	y.AssertTruef(int(n) <= len(s.buf),
-		"Arena too small, toWrite:%d newTotal:%d limit:%d",
-		l, n, len(s.buf))
-	// m is the offset where you should write.
-	// n = new len - key len give you the offset at which you should write.
-	m := n - l
-	// Copy to buffer from m:n
-	y.AssertTrue(len(key) == copy(s.buf[m:n], key))
-	return m
+	keySz := uint32(len(key))
+	offset := s.allocate(keySz) - keySz
+	buf := s.data[offset : offset+keySz]
+	y.AssertTrue(len(key) == copy(buf, key))
+	return offset
 }
 
 // getNode returns a pointer to the node located at offset. If the offset is
@@ -108,19 +103,18 @@ func (s *Arena) getNode(offset uint32) *node {
 	if offset == 0 {
 		return nil
 	}
-
-	return (*node)(unsafe.Pointer(&s.buf[offset]))
+	return (*node)(unsafe.Pointer(&s.data[offset]))
 }
 
 // getKey returns byte slice at offset.
 func (s *Arena) getKey(offset uint32, size uint16) []byte {
-	return s.buf[offset : offset+uint32(size)]
+	return s.data[offset : offset+uint32(size)]
 }
 
 // getVal returns byte slice at offset. The given size should be just the value
 // size and should NOT include the meta bytes.
 func (s *Arena) getVal(offset uint32, size uint32) (ret y.ValueStruct) {
-	ret.Decode(s.buf[offset : offset+size])
+	ret.Decode(s.data[offset : offset+size])
 	return
 }
 
@@ -130,6 +124,7 @@ func (s *Arena) getNodeOffset(nd *node) uint32 {
 	if nd == nil {
 		return 0
 	}
+	val := uint32(uintptr(unsafe.Pointer(nd)) - uintptr(unsafe.Pointer(&s.data[0])))
+	return val
 
-	return uint32(uintptr(unsafe.Pointer(nd)) - uintptr(unsafe.Pointer(&s.buf[0])))
 }
diff --git a/skl/skl.go b/skl/skl.go
index c7c2731..4ad8f38 100644
--- a/skl/skl.go
+++ b/skl/skl.go
@@ -33,6 +33,7 @@ Key differences:
 package skl
 
 import (
+	"bytes"
 	"math"
 	"sync/atomic"
 	"unsafe"
@@ -73,12 +74,17 @@ type node struct {
 	tower [maxHeight]uint32
 }
 
+type comparatorFunc func([]byte, []byte) int
+
 type Skiplist struct {
-	height  int32 // Current height. 1 <= height <= kMaxHeight. CAS.
-	head    *node
-	ref     int32
-	arena   *Arena
-	OnClose func()
+	height      int32 // Current height. 1 <= height <= kMaxHeight. CAS.
+	head        *node
+	ref         int32
+	arena       *Arena
+	hasVersions bool
+	comparator  comparatorFunc
+	OnClose     func()
+	Release     func()
 }
 
 // IncrRef increases the refcount
@@ -95,7 +101,9 @@ func (s *Skiplist) DecrRef() {
 	if s.OnClose != nil {
 		s.OnClose()
 	}
-
+	if s.Release != nil {
+		s.Release()
+	}
 	// Indicate we are closed. Good for testing.  Also, lets GC reclaim memory. Race condition
 	// here would suggest we are accessing skiplist when we are supposed to have no reference!
 	s.arena = nil
@@ -104,14 +112,14 @@ func (s *Skiplist) DecrRef() {
 	s.head = nil
 }
 
-func newNode(arena *Arena, key []byte, v y.ValueStruct, height int) *node {
+func newNode(arena *Arena, key []byte, u uint64, height int) *node {
 	// The base level is already allocated in the node struct.
 	offset := arena.putNode(height)
 	node := arena.getNode(offset)
 	node.keyOffset = arena.putKey(key)
 	node.keySize = uint16(len(key))
 	node.height = uint16(height)
-	node.value = encodeValue(arena.putVal(v), v.EncodedSize())
+	node.value = u
 	return node
 }
 
@@ -125,16 +133,34 @@ func decodeValue(value uint64) (valOffset uint32, valSize uint32) {
 	return
 }
 
-// NewSkiplist makes a new empty skiplist, with a given arena size
+// NewSkiplist makes a new empty skiplist, with a given arena size.
 func NewSkiplist(arenaSize int64) *Skiplist {
-	arena := newArena(arenaSize)
-	head := newNode(arena, nil, y.ValueStruct{}, maxHeight)
-	return &Skiplist{
-		height: 1,
-		head:   head,
-		arena:  arena,
-		ref:    1,
+	buf := z.Calloc(int(arenaSize))
+	s := NewSkiplistWith(buf, true)
+	s.Release = func() {
+		z.Free(s.arena.data)
+	}
+	return s
+}
+
+// NewSkiplistWith makes a new skiplist, with a given byte slice.
+func NewSkiplistWith(buf []byte, hasVersions bool) *Skiplist {
+	arena := new(Arena)
+	arena.data = buf
+	offset := arena.allocateValue(y.ValueStruct{})
+	head := newNode(arena, nil, offset, maxHeight)
+	sl := &Skiplist{
+		height:      1,
+		head:        head,
+		arena:       arena,
+		ref:         1,
+		hasVersions: hasVersions,
+		comparator:  bytes.Compare,
 	}
+	if sl.hasVersions {
+		sl.comparator = y.CompareKeys
+	}
+	return sl
 }
 
 func (s *node) getValueOffset() (uint32, uint32) {
@@ -146,10 +172,8 @@ func (s *node) key(arena *Arena) []byte {
 	return arena.getKey(s.keyOffset, s.keySize)
 }
 
-func (s *node) setValue(arena *Arena, v y.ValueStruct) {
-	valOffset := arena.putVal(v)
-	value := encodeValue(valOffset, v.EncodedSize())
-	atomic.StoreUint64(&s.value, value)
+func (s *node) setUint64(u uint64) {
+	atomic.StoreUint64(&s.value, u)
 }
 
 func (s *node) getNextOffset(h int) uint32 {
@@ -208,9 +232,9 @@ func (s *Skiplist) findNear(key []byte, less bool, allowEqual bool) (*node, bool
 			}
 			return x, false
 		}
-
 		nextKey := next.key(s.arena)
-		cmp := y.CompareKeys(key, nextKey)
+
+		cmp := s.comparator(key, nextKey)
 		if cmp > 0 {
 			// x.key < next.key < key. We can continue to move right.
 			x = next
@@ -265,7 +289,7 @@ func (s *Skiplist) findSpliceForLevel(key []byte, before *node, level int) (*nod
 			return before, next
 		}
 		nextKey := next.key(s.arena)
-		cmp := y.CompareKeys(key, nextKey)
+		cmp := s.comparator(key, nextKey)
 		if cmp == 0 {
 			// Equality case.
 			return next, next
@@ -284,6 +308,12 @@ func (s *Skiplist) getHeight() int32 {
 
 // Put inserts the key-value pair.
 func (s *Skiplist) Put(key []byte, v y.ValueStruct) {
+	val := s.arena.allocateValue(v)
+	s.PutUint64(key, val)
+}
+
+// PutUint64 inserts the key-value pair, with a uint64 value.
+func (s *Skiplist) PutUint64(key []byte, u uint64) {
 	// Since we allow overwrite, we may not need to create a new node. We might not even need to
 	// increase the height. Let's defer these actions.
 
@@ -296,14 +326,14 @@ func (s *Skiplist) Put(key []byte, v y.ValueStruct) {
 		// Use higher level to speed up for current level.
 		prev[i], next[i] = s.findSpliceForLevel(key, prev[i+1], i)
 		if prev[i] == next[i] {
-			prev[i].setValue(s.arena, v)
+			prev[i].setUint64(u)
 			return
 		}
 	}
 
 	// We do need to create a new node.
 	height := s.randomHeight()
-	x := newNode(s.arena, key, v, height)
+	x := newNode(s.arena, key, u, height)
 
 	// Try to increase s.height via CAS.
 	listHeight = s.getHeight()
@@ -340,7 +370,7 @@ func (s *Skiplist) Put(key []byte, v y.ValueStruct) {
 			prev[i], next[i] = s.findSpliceForLevel(key, prev[i], i)
 			if prev[i] == next[i] {
 				y.AssertTruef(i == 0, "Equality can happen only on base level: %d", i)
-				prev[i].setValue(s.arena, v)
+				prev[i].setUint64(u)
 				return
 			}
 		}
@@ -376,20 +406,49 @@ func (s *Skiplist) findLast() *node {
 // Get gets the value associated with the key. It returns a valid value if it finds equal or earlier
 // version of the same key.
 func (s *Skiplist) Get(key []byte) y.ValueStruct {
-	n, _ := s.findNear(key, false, true) // findGreaterOrEqual.
+	n, version := s.getInternal(key)
 	if n == nil {
 		return y.ValueStruct{}
 	}
-
-	nextKey := s.arena.getKey(n.keyOffset, n.keySize)
-	if !y.SameKey(key, nextKey) {
-		return y.ValueStruct{}
-	}
-
 	valOffset, valSize := n.getValueOffset()
 	vs := s.arena.getVal(valOffset, valSize)
-	vs.Version = y.ParseTs(nextKey)
+	vs.Version = version
 	return vs
+
+}
+
+func (s *Skiplist) GetUint64(key []byte) (uint64, bool) {
+	n, _ := s.getInternal(key)
+	if n == nil {
+		return 0, false
+	}
+	return n.value, true
+}
+
+// getInternal finds the node which is greater than or equal to the given key
+// from the skiplist. It returns the fetched node and it's version.
+func (s *Skiplist) getInternal(key []byte) (*node, uint64) {
+	n, _ := s.findNear(key, false, true) // findGreaterOrEqual.
+	if n == nil {
+		return nil, 0
+	}
+	fetchedKey := s.arena.getKey(n.keyOffset, n.keySize)
+
+	// If the node has version, it means it is a badger kv and we should
+	// compare it's key without the version.
+	if s.hasVersions {
+		if !y.SameKey(key, fetchedKey) {
+			return nil, 0
+		}
+		version := y.ParseTs(fetchedKey)
+		return n, version
+	}
+
+	// This is a key without version.
+	if !bytes.Equal(key, fetchedKey) {
+		return nil, 0
+	}
+	return n, 0
 }
 
 // NewIterator returns a skiplist iterator.  You have to Close() the iterator.
@@ -429,6 +488,11 @@ func (s *Iterator) Value() y.ValueStruct {
 	return s.list.arena.getVal(valOffset, valSize)
 }
 
+// ValueUint64 returns the uint64 value of the current node.
+func (s *Iterator) ValueUint64() uint64 {
+	return s.n.value
+}
+
 // Next advances to the next position.
 func (s *Iterator) Next() {
 	y.AssertTrue(s.Valid())
diff --git a/stream.go b/stream.go
index fdbf765..8733fe1 100644
--- a/stream.go
+++ b/stream.go
@@ -26,8 +26,8 @@ import (
 
 	"github.com/dgraph-io/badger/v2/pb"
 	"github.com/dgraph-io/badger/v2/y"
+	"github.com/dgraph-io/ristretto/z"
 	humanize "github.com/dustin/go-humanize"
-	"github.com/golang/protobuf/proto"
 )
 
 const batchSize = 16 << 20 // 16 MB
@@ -68,23 +68,37 @@ type Stream struct {
 	// with a mismatching key. See example usage in ToList function. Can be left nil to use ToList
 	// function by default.
 	//
+	// KeyToList has access to z.Allocator accessible via stream.Allocator(itr.ThreadId). This
+	// allocator can be used to allocate KVs, to decrease the memory pressure on Go GC. Stream
+	// framework takes care of releasing those resources after calling Send. AllocRef does
+	// NOT need to be set in the returned KVList, as Stream framework would ignore that field,
+	// instead using the allocator assigned to that thread id.
+	//
 	// Note: Calls to KeyToList are concurrent.
 	KeyToList func(key []byte, itr *Iterator) (*pb.KVList, error)
 
 	// This is the method where Stream sends the final output. All calls to Send are done by a
 	// single goroutine, i.e. logic within Send method can expect single threaded execution.
-	Send func(*pb.KVList) error
+	Send func(buf *z.Buffer) error
 
 	readTs       uint64
 	db           *DB
 	rangeCh      chan keyRange
-	kvChan       chan *pb.KVList
+	kvChan       chan *z.Buffer
 	nextStreamId uint32
+	doneMarkers  bool
+}
+
+// SendDoneMarkers when true would send out done markers on the stream. False by default.
+func (st *Stream) SendDoneMarkers(done bool) {
+	st.doneMarkers = done
 }
 
 // ToList is a default implementation of KeyToList. It picks up all valid versions of the key,
 // skipping over deleted or expired keys.
 func (st *Stream) ToList(key []byte, itr *Iterator) (*pb.KVList, error) {
+	ka := y.Copy(key)
+
 	list := &pb.KVList{}
 	for ; itr.Valid(); itr.Next() {
 		item := itr.Item()
@@ -96,17 +110,20 @@ func (st *Stream) ToList(key []byte, itr *Iterator) (*pb.KVList, error) {
 			break
 		}
 
-		valCopy, err := item.ValueCopy(nil)
-		if err != nil {
+		kv := itr.NewKV()
+		kv.Key = ka
+
+		if err := item.Value(func(val []byte) error {
+			kv.Value = y.Copy(val)
+			return nil
+
+		}); err != nil {
 			return nil, err
 		}
-		kv := &pb.KV{
-			Key:       item.KeyCopy(nil),
-			Value:     valCopy,
-			UserMeta:  []byte{item.UserMeta()},
-			Version:   item.Version(),
-			ExpiresAt: item.ExpiresAt(),
-		}
+		kv.Version = item.Version()
+		kv.ExpiresAt = item.ExpiresAt()
+		kv.UserMeta = y.Copy([]byte{item.UserMeta()})
+
 		list.Kv = append(list.Kv, kv)
 		if st.db.opt.NumVersionsToKeep == 1 {
 			break
@@ -153,7 +170,6 @@ func (st *Stream) produceRanges(ctx context.Context) {
 
 // produceKVs picks up ranges from rangeCh, generates KV lists and sends them to kvChan.
 func (st *Stream) produceKVs(ctx context.Context, threadId int) error {
-	var size int
 	var txn *Txn
 	if st.readTs > 0 {
 		txn = st.db.NewTransactionAt(st.readTs, false)
@@ -162,6 +178,14 @@ func (st *Stream) produceKVs(ctx context.Context, threadId int) error {
 	}
 	defer txn.Discard()
 
+	// produceKVs is running iterate serially. So, we can define the outList here.
+	outList := z.NewBuffer(2 * batchSize)
+	defer func() {
+		// The outList variable changes. So, we need to evaluate the variable in the defer. DO NOT
+		// call `defer outList.Release()`.
+		outList.Release()
+	}()
+
 	iterate := func(kr keyRange) error {
 		iterOpts := DefaultIteratorOptions
 		iterOpts.AllVersions = true
@@ -174,18 +198,16 @@ func (st *Stream) produceKVs(ctx context.Context, threadId int) error {
 		// This unique stream id is used to identify all the keys from this iteration.
 		streamId := atomic.AddUint32(&st.nextStreamId, 1)
 
-		outList := new(pb.KVList)
-
 		sendIt := func() error {
 			select {
 			case st.kvChan <- outList:
+				outList = z.NewBuffer(2 * batchSize)
 			case <-ctx.Done():
 				return ctx.Err()
 			}
-			outList = new(pb.KVList)
-			size = 0
 			return nil
 		}
+
 		var prevKey []byte
 		for itr.Seek(kr.left); itr.Valid(); {
 			// it.Valid would only return true for keys with the provided Prefix in iterOpts.
@@ -214,25 +236,28 @@ func (st *Stream) produceKVs(ctx context.Context, threadId int) error {
 				continue
 			}
 			for _, kv := range list.Kv {
-				size += proto.Size(kv)
 				kv.StreamId = streamId
-				outList.Kv = append(outList.Kv, kv)
-
-				if size < batchSize {
+				KVToBuffer(kv, outList)
+				if outList.LenNoPadding() < batchSize {
 					continue
 				}
 				if err := sendIt(); err != nil {
 					return err
 				}
 			}
+			if len(itr.reuse) < 100 {
+				itr.reuse = append(itr.reuse, list.Kv...)
+			}
 		}
-		if len(outList.Kv) > 0 {
-			// TODO: Think of a way to indicate that a stream is over.
-			if err := sendIt(); err != nil {
-				return err
+		// Mark the stream as done.
+		if st.doneMarkers {
+			kv := &pb.KV{
+				StreamId:   streamId,
+				StreamDone: true,
 			}
+			KVToBuffer(kv, outList)
 		}
-		return nil
+		return sendIt()
 	}
 
 	for {
@@ -252,33 +277,33 @@ func (st *Stream) produceKVs(ctx context.Context, threadId int) error {
 }
 
 func (st *Stream) streamKVs(ctx context.Context) error {
-	var count int
 	var bytesSent uint64
 	t := time.NewTicker(time.Second)
 	defer t.Stop()
 	now := time.Now()
 
-	sendBatch := func(batch *pb.KVList) error {
-		sz := uint64(proto.Size(batch))
+	sendBatch := func(batch *z.Buffer) error {
+		defer batch.Release()
+		sz := uint64(batch.LenNoPadding())
+		if sz == 0 {
+			return nil
+		}
 		bytesSent += sz
-		count += len(batch.Kv)
-		t := time.Now()
+		st.db.opt.Infof("%s Sending batch of size: %s.\n", st.LogPrefix, humanize.Bytes(sz))
 		if err := st.Send(batch); err != nil {
+			st.db.opt.Warningf("Error while sending: %v\n", err)
 			return err
 		}
-		st.db.opt.Infof("%s Created batch of size: %s in %s.\n",
-			st.LogPrefix, humanize.Bytes(sz), time.Since(t))
 		return nil
 	}
 
-	slurp := func(batch *pb.KVList) error {
+	slurp := func(batch *z.Buffer) error {
 	loop:
 		for {
 			// Send the batch immediately if it already exceeds the maximum allowed size.
 			// If the size of the batch exceeds maxStreamSize, break from the loop to
 			// avoid creating a batch that is so big that certain limits are reached.
-			sz := uint64(proto.Size(batch))
-			if sz > maxStreamSize {
+			if batch.LenNoPadding() > int(maxStreamSize) {
 				break loop
 			}
 			select {
@@ -287,7 +312,9 @@ func (st *Stream) streamKVs(ctx context.Context) error {
 					break loop
 				}
 				y.AssertTrue(kvs != nil)
-				batch.Kv = append(batch.Kv, kvs.Kv...)
+				y.Check2(batch.Write(kvs.Bytes()))
+				y.Check(kvs.Release())
+
 			default:
 				break loop
 			}
@@ -297,7 +324,7 @@ func (st *Stream) streamKVs(ctx context.Context) error {
 
 outer:
 	for {
-		var batch *pb.KVList
+		var batch *z.Buffer
 		select {
 		case <-ctx.Done():
 			return ctx.Err()
@@ -309,8 +336,12 @@ outer:
 				continue
 			}
 			speed := bytesSent / durSec
-			st.db.opt.Infof("%s Time elapsed: %s, bytes sent: %s, speed: %s/sec\n", st.LogPrefix,
-				y.FixedDuration(dur), humanize.Bytes(bytesSent), humanize.Bytes(speed))
+
+			var ms z.MemStats
+			z.ReadMemStats(&ms)
+			st.db.opt.Infof("%s Time elapsed: %s, bytes sent: %s, speed: %s/sec, jemalloc: %s\n",
+				st.LogPrefix, y.FixedDuration(dur), humanize.IBytes(bytesSent),
+				humanize.IBytes(speed), humanize.IBytes(ms.Active))
 
 		case kvs, ok := <-st.kvChan:
 			if !ok {
@@ -326,7 +357,7 @@ outer:
 		}
 	}
 
-	st.db.opt.Infof("%s Sent %d keys\n", st.LogPrefix, count)
+	st.db.opt.Infof("%s Sent data of size %s\n", st.LogPrefix, humanize.IBytes(bytesSent))
 	return nil
 }
 
@@ -337,12 +368,14 @@ outer:
 // are serial. In case any of these steps encounter an error, Orchestrate would stop execution and
 // return that error. Orchestrate can be called multiple times, but in serial order.
 func (st *Stream) Orchestrate(ctx context.Context) error {
+	ctx, cancel := context.WithCancel(ctx)
+	defer cancel()
 	st.rangeCh = make(chan keyRange, 3) // Contains keys for posting lists.
 
 	// kvChan should only have a small capacity to ensure that we don't buffer up too much data if
 	// sending is slow. Page size is set to 4MB, which is used to lazily cap the size of each
 	// KVList. To get 128MB buffer, we can set the channel size to 32.
-	st.kvChan = make(chan *pb.KVList, 32)
+	st.kvChan = make(chan *z.Buffer, 32)
 
 	if st.KeyToList == nil {
 		st.KeyToList = st.ToList
@@ -351,7 +384,7 @@ func (st *Stream) Orchestrate(ctx context.Context) error {
 	// Picks up ranges from Badger, and sends them to rangeCh.
 	go st.produceRanges(ctx)
 
-	errCh := make(chan error, 1) // Stores error by consumeKeys.
+	errCh := make(chan error, st.NumGo) // Stores error by consumeKeys.
 	var wg sync.WaitGroup
 	for i := 0; i < st.NumGo; i++ {
 		wg.Add(1)
@@ -372,10 +405,20 @@ func (st *Stream) Orchestrate(ctx context.Context) error {
 	kvErr := make(chan error, 1)
 	go func() {
 		// Picks up KV lists from kvChan, and sends them to Output.
-		kvErr <- st.streamKVs(ctx)
+		err := st.streamKVs(ctx)
+		if err != nil {
+			cancel() // Stop all the go routines.
+		}
+		kvErr <- err
 	}()
 	wg.Wait()        // Wait for produceKVs to be over.
 	close(st.kvChan) // Now we can close kvChan.
+	defer func() {
+		// If due to some error, we have buffers left in kvChan, we should release them.
+		for buf := range st.kvChan {
+			buf.Release()
+		}
+	}()
 
 	select {
 	case err := <-errCh: // Check error from produceKVs.
@@ -389,7 +432,11 @@ func (st *Stream) Orchestrate(ctx context.Context) error {
 }
 
 func (db *DB) newStream() *Stream {
-	return &Stream{db: db, NumGo: 16, LogPrefix: "Badger.Stream"}
+	return &Stream{
+		db:        db,
+		NumGo:     8,
+		LogPrefix: "Badger.Stream",
+	}
 }
 
 // NewStream creates a new Stream.
@@ -409,3 +456,21 @@ func (db *DB) NewStreamAt(readTs uint64) *Stream {
 	stream.readTs = readTs
 	return stream
 }
+
+func BufferToKVList(buf *z.Buffer) (*pb.KVList, error) {
+	var list pb.KVList
+	err := buf.SliceIterate(func(s []byte) error {
+		kv := new(pb.KV)
+		if err := kv.Unmarshal(s); err != nil {
+			return err
+		}
+		list.Kv = append(list.Kv, kv)
+		return nil
+	})
+	return &list, err
+}
+
+func KVToBuffer(kv *pb.KV, buf *z.Buffer) {
+	out := buf.SliceAllocate(kv.Size())
+	y.Check2(kv.MarshalToSizedBuffer(out))
+}
diff --git a/stream_writer.go b/stream_writer.go
index 53ed36e..6302d89 100644
--- a/stream_writer.go
+++ b/stream_writer.go
@@ -71,16 +71,20 @@ func (sw *StreamWriter) Prepare() error {
 	sw.writeLock.Lock()
 	defer sw.writeLock.Unlock()
 
-	var err error
-	sw.done, err = sw.db.dropAll()
+	done, err := sw.db.dropAll()
+
+	// Ensure that done() is never called more than once.
+	var once sync.Once
+	sw.done = func() { once.Do(done) }
+
 	return err
 }
 
 // Write writes KVList to DB. Each KV within the list contains the stream id which StreamWriter
 // would use to demux the writes. Write is thread safe and can be called concurrently by multiple
 // goroutines.
-func (sw *StreamWriter) Write(kvs *pb.KVList) error {
-	if len(kvs.GetKv()) == 0 {
+func (sw *StreamWriter) Write(buf *z.Buffer) error {
+	if buf.LenNoPadding() == 0 {
 		return nil
 	}
 
@@ -89,10 +93,15 @@ func (sw *StreamWriter) Write(kvs *pb.KVList) error {
 	// the valid kvs.
 	closedStreams := make(map[uint32]struct{})
 	streamReqs := make(map[uint32]*request)
-	for _, kv := range kvs.Kv {
+
+	err := buf.SliceIterate(func(s []byte) error {
+		var kv pb.KV
+		if err := kv.Unmarshal(s); err != nil {
+			return err
+		}
 		if kv.StreamDone {
 			closedStreams[kv.StreamId] = struct{}{}
-			continue
+			return nil
 		}
 
 		// Panic if some kv comes after stream has been marked as closed.
@@ -112,7 +121,7 @@ func (sw *StreamWriter) Write(kvs *pb.KVList) error {
 		}
 		e := &Entry{
 			Key:       y.KeyWithTs(kv.Key, kv.Version),
-			Value:     kv.Value,
+			Value:     y.Copy(kv.Value),
 			UserMeta:  userMeta,
 			ExpiresAt: kv.ExpiresAt,
 			meta:      meta,
@@ -125,7 +134,12 @@ func (sw *StreamWriter) Write(kvs *pb.KVList) error {
 			streamReqs[kv.StreamId] = req
 		}
 		req.Entries = append(req.Entries, e)
+		return nil
+	})
+	if err != nil {
+		return err
 	}
+
 	all := make([]*request, 0, len(streamReqs))
 	for _, req := range streamReqs {
 		all = append(all, req)
@@ -235,9 +249,38 @@ func (sw *StreamWriter) Flush() error {
 	return sw.db.lc.validate()
 }
 
+// Cancel signals all goroutines to exit. Calling defer sw.Cancel() immediately after creating a new StreamWriter
+// ensures that writes are unblocked even upon early return. Note that dropAll() is not called here, so any
+// partially written data will not be erased until a new StreamWriter is initialized.
+func (sw *StreamWriter) Cancel() {
+	sw.writeLock.Lock()
+	defer sw.writeLock.Unlock()
+
+	for _, writer := range sw.writers {
+		if writer != nil {
+			writer.closer.Signal()
+		}
+	}
+	for _, writer := range sw.writers {
+		if writer != nil {
+			writer.closer.Wait()
+		}
+	}
+
+	if err := sw.throttle.Finish(); err != nil {
+		sw.db.opt.Errorf("error in throttle.Finish: %+v", err)
+	}
+
+	// Handle Cancel() being called before Prepare().
+	if sw.done != nil {
+		sw.done()
+	}
+}
+
 type sortedWriter struct {
 	db       *DB
 	throttle *y.Throttle
+	opts     table.Options
 
 	builder  *table.Builder
 	lastKey  []byte
@@ -248,15 +291,13 @@ type sortedWriter struct {
 }
 
 func (sw *StreamWriter) newWriter(streamID uint32) (*sortedWriter, error) {
-	dk, err := sw.db.registry.latestDataKey()
-	if err != nil {
-		return nil, err
+	bopts := buildTableOptions(sw.db)
+	for i := 2; i < sw.db.opt.MaxLevels; i++ {
+		bopts.TableSize *= uint64(sw.db.opt.TableSizeMultiplier)
 	}
-
-	bopts := buildTableOptions(sw.db.opt)
-	bopts.DataKey = dk
 	w := &sortedWriter{
 		db:       sw.db,
+		opts:     bopts,
 		streamID: streamID,
 		throttle: sw.throttle,
 		builder:  table.NewTableBuilder(bopts),
@@ -320,7 +361,7 @@ func (w *sortedWriter) Add(key []byte, vs y.ValueStruct) error {
 
 	sameKey := y.SameKey(key, w.lastKey)
 	// Same keys should go into the same SSTable.
-	if !sameKey && w.builder.ReachedCapacity(uint64(float64(w.db.opt.MaxTableSize)*0.9)) {
+	if !sameKey && w.builder.ReachedCapacity() {
 		if err := w.send(false); err != nil {
 			return err
 		}
@@ -340,7 +381,6 @@ func (w *sortedWriter) send(done bool) error {
 		return err
 	}
 	go func(builder *table.Builder) {
-		defer builder.Close()
 		err := w.createTable(builder)
 		w.throttle.Done(err)
 	}(w.builder)
@@ -351,13 +391,7 @@ func (w *sortedWriter) send(done bool) error {
 		return nil
 	}
 
-	dk, err := w.db.registry.latestDataKey()
-	if err != nil {
-		return y.Wrapf(err, "Error while retriving datakey in sortedWriter.send")
-	}
-	bopts := buildTableOptions(w.db.opt)
-	bopts.DataKey = dk
-	w.builder = table.NewTableBuilder(bopts)
+	w.builder = table.NewTableBuilder(w.opts)
 	return nil
 }
 
@@ -365,6 +399,7 @@ func (w *sortedWriter) send(done bool) error {
 // to sortedWriter. It completes writing current SST to disk.
 func (w *sortedWriter) Done() error {
 	if w.builder.Empty() {
+		w.builder.Close()
 		// Assign builder as nil, so that underlying memory can be garbage collected.
 		w.builder = nil
 		return nil
@@ -374,46 +409,30 @@ func (w *sortedWriter) Done() error {
 }
 
 func (w *sortedWriter) createTable(builder *table.Builder) error {
-	data := builder.Finish(w.db.opt.InMemory)
-
-	if len(data) == 0 {
+	defer builder.Close()
+	if builder.Empty() {
+		builder.Finish()
 		return nil
 	}
+
 	fileID := w.db.lc.reserveFileID()
-	opts := buildTableOptions(w.db.opt)
-	opts.DataKey = builder.DataKey()
-	opts.BlockCache = w.db.blockCache
-	opts.IndexCache = w.db.indexCache
 	var tbl *table.Table
 	if w.db.opt.InMemory {
+		data := builder.Finish()
 		var err error
-		if tbl, err = table.OpenInMemoryTable(data, fileID, &opts); err != nil {
+		if tbl, err = table.OpenInMemoryTable(data, fileID, builder.Opts()); err != nil {
 			return err
 		}
 	} else {
 		var err error
-		if tbl, err = table.CreateTable(
-			table.NewFilename(fileID, w.db.opt.Dir), data, opts); err != nil {
+		fname := table.NewFilename(fileID, w.db.opt.Dir)
+		if tbl, err = table.CreateTable(fname, builder); err != nil {
 			return err
 		}
 	}
 	lc := w.db.lc
 
-	var lhandler *levelHandler
-	// We should start the levels from 1.
-	y.AssertTrue(len(lc.levels) > 1)
-	for _, l := range lc.levels[1:] {
-		ratio := float64(l.getTotalSize()) / float64(l.maxTotalSize)
-		if ratio < 1.0 {
-			lhandler = l
-			break
-		}
-	}
-	if lhandler == nil {
-		// If we're exceeding the size of the lowest level, shove it in the lowest level. Can't do
-		// better than that.
-		lhandler = lc.levels[len(lc.levels)-1]
-	}
+	lhandler := lc.levels[len(lc.levels)-1]
 	// Now that table can be opened successfully, let's add this to the MANIFEST.
 	change := &pb.ManifestChange{
 		Id:          tbl.ID(),
diff --git a/table/builder.go b/table/builder.go
index ef531bc..5f40038 100644
--- a/table/builder.go
+++ b/table/builder.go
@@ -69,28 +69,26 @@ func (h *header) Decode(buf []byte) {
 
 // bblock represents a block that is being compressed/encrypted in the background.
 type bblock struct {
-	data  []byte
-	start uint32 // Points to the starting offset of the block.
-	end   uint32 // Points to the end offset of the block.
+	data         []byte
+	baseKey      []byte   // Base key for the current block.
+	entryOffsets []uint32 // Offsets of entries present in current block.
+	end          int      // Points to the end offset of the block.
 }
 
 // Builder is used in building a table.
 type Builder struct {
 	// Typically tens or hundreds of meg. This is for one single file.
-	buf        []byte
-	sz         uint32
-	bufLock    sync.Mutex // This lock guards the buf. We acquire lock when we resize the buf.
-	actualSize uint32     // Used to store the sum of sizes of blocks after compression/encryption.
+	alloc            *z.Allocator
+	curBlock         *bblock
+	compressedSize   uint32
+	uncompressedSize uint32
 
-	baseKey    []byte // Base key for the current block.
-	baseOffset uint32 // Offset for the current block.
-
-	entryOffsets  []uint32 // Offsets of entries present in current block.
-	offsets       *z.Buffer
+	lenOffsets    uint32
 	estimatedSize uint32
 	keyHashes     []uint32 // Used for building the bloomfilter.
-	opt           *Options
+	opts          *Options
 	maxVersion    uint64
+	onDiskSize    uint32
 
 	// Used to concurrently compress/encrypt blocks.
 	wg        sync.WaitGroup
@@ -98,20 +96,49 @@ type Builder struct {
 	blockList []*bblock
 }
 
+func (b *Builder) allocate(need int) []byte {
+	bb := b.curBlock
+	if len(bb.data[bb.end:]) < need {
+		// We need to reallocate.
+		sz := 2 * len(bb.data)
+		if bb.end+need > sz {
+			sz = bb.end + need
+		}
+		tmp := b.alloc.Allocate(sz)
+		copy(tmp, bb.data)
+		bb.data = tmp
+	}
+	bb.end += need
+	return bb.data[bb.end-need : bb.end]
+}
+
+// append appends to curBlock.data
+func (b *Builder) append(data []byte) {
+	dst := b.allocate(len(data))
+	y.AssertTrue(len(data) == copy(dst, data))
+}
+
+const maxAllocatorInitialSz = 256 << 20
+
 // NewTableBuilder makes a new TableBuilder.
 func NewTableBuilder(opts Options) *Builder {
+	sz := 2 * int(opts.TableSize)
+	if sz > maxAllocatorInitialSz {
+		sz = maxAllocatorInitialSz
+	}
 	b := &Builder{
-		// Additional 16 MB to store index (approximate).
-		// We trim the additional space in table.Finish().
-		// TODO: Switch this buf over to z.Buffer.
-		buf:     make([]byte, int(opts.TableSize+16*MB)),
-		opt:     &opts,
-		offsets: z.NewBuffer(1 << 20),
+		alloc: opts.AllocPool.Get(sz),
+		opts:  &opts,
 	}
+	b.alloc.Tag = "Builder"
+	b.curBlock = &bblock{
+		data: b.alloc.Allocate(opts.BlockSize + padding),
+	}
+	b.opts.tableCapacity = uint64(float64(b.opts.TableSize) * 0.95)
 
 	// If encryption or compression is not enabled, do not start compression/encryption goroutines
 	// and write directly to the buffer.
-	if b.opt.Compression == options.None && b.opt.DataKey == nil {
+	if b.opts.Compression == options.None && b.opts.DataKey == nil {
 		return b
 	}
 
@@ -128,62 +155,48 @@ func NewTableBuilder(opts Options) *Builder {
 func (b *Builder) handleBlock() {
 	defer b.wg.Done()
 
-	doCompress := b.opt.Compression != options.None
+	doCompress := b.opts.Compression != options.None
 	for item := range b.blockChan {
 		// Extract the block.
-		blockBuf := item.data[item.start:item.end]
+		blockBuf := item.data[:item.end]
 		// Compress the block.
 		if doCompress {
-			var err error
-			blockBuf, err = b.compressData(blockBuf)
+			out, err := b.compressData(blockBuf)
 			y.Check(err)
+			blockBuf = out
 		}
 		if b.shouldEncrypt() {
-			eBlock, err := b.encrypt(blockBuf, doCompress)
+			out, err := b.encrypt(blockBuf)
 			y.Check(y.Wrapf(err, "Error while encrypting block in table builder."))
-			blockBuf = eBlock
+			blockBuf = out
 		}
 
 		// BlockBuf should always less than or equal to allocated space. If the blockBuf is greater
 		// than allocated space that means the data from this block cannot be stored in its
-		// existing location and trying to copy it over would mean we would over-write some data
-		// of the next block.
-		allocatedSpace := (item.end - item.start) + padding + 1
-		y.AssertTruef(uint32(len(blockBuf)) <= allocatedSpace, "newend: %d oldend: %d padding: %d",
-			item.start+uint32(len(blockBuf)), item.end, padding)
-
-		// Acquire the buflock here. The builder.grow function might change
-		// the b.buf while this goroutine was running.
-		b.bufLock.Lock()
-		// Copy over compressed/encrypted data back to the main buffer.
-		copy(b.buf[item.start:], blockBuf)
-		b.bufLock.Unlock()
+		// existing location.
+		allocatedSpace := (item.end) + padding + 1
+		y.AssertTrue(len(blockBuf) <= allocatedSpace)
 
-		// Add the actual size of current block.
-		atomic.AddUint32(&b.actualSize, uint32(len(blockBuf)))
-
-		// Fix the boundary of the block.
-		item.end = item.start + uint32(len(blockBuf))
-
-		if doCompress {
-			z.Free(blockBuf)
-		}
+		// blockBuf was allocated on allocator. So, we don't need to copy it over.
+		item.data = blockBuf
+		item.end = len(blockBuf)
+		atomic.AddUint32(&b.compressedSize, uint32(len(blockBuf)))
 	}
 }
 
 // Close closes the TableBuilder.
 func (b *Builder) Close() {
-	b.offsets.Release()
+	b.opts.AllocPool.Return(b.alloc)
 }
 
 // Empty returns whether it's empty.
-func (b *Builder) Empty() bool { return b.sz == 0 }
+func (b *Builder) Empty() bool { return len(b.keyHashes) == 0 }
 
 // keyDiff returns a suffix of newKey that is different from b.baseKey.
 func (b *Builder) keyDiff(newKey []byte) []byte {
 	var i int
-	for i = 0; i < len(newKey) && i < len(b.baseKey); i++ {
-		if newKey[i] != b.baseKey[i] {
+	for i = 0; i < len(newKey) && i < len(b.curBlock.baseKey); i++ {
+		if newKey[i] != b.curBlock.baseKey[i] {
 			break
 		}
 	}
@@ -199,10 +212,10 @@ func (b *Builder) addHelper(key []byte, v y.ValueStruct, vpLen uint32) {
 
 	// diffKey stores the difference of key with baseKey.
 	var diffKey []byte
-	if len(b.baseKey) == 0 {
+	if len(b.curBlock.baseKey) == 0 {
 		// Make a copy. Builder should not keep references. Otherwise, caller has to be very careful
 		// and will have to make copies of keys every time they add to builder, which is even worse.
-		b.baseKey = append(b.baseKey[:0], key...)
+		b.curBlock.baseKey = append(b.curBlock.baseKey[:0], key...)
 		diffKey = key
 	} else {
 		diffKey = b.keyDiff(key)
@@ -217,52 +230,18 @@ func (b *Builder) addHelper(key []byte, v y.ValueStruct, vpLen uint32) {
 	}
 
 	// store current entry's offset
-	y.AssertTrue(b.sz < math.MaxUint32)
-	b.entryOffsets = append(b.entryOffsets, b.sz-b.baseOffset)
+	b.curBlock.entryOffsets = append(b.curBlock.entryOffsets, uint32(b.curBlock.end))
 
 	// Layout: header, diffKey, value.
 	b.append(h.Encode())
 	b.append(diffKey)
 
-	if uint32(len(b.buf)) < b.sz+v.EncodedSize() {
-		b.grow(v.EncodedSize())
-	}
-	b.sz += v.Encode(b.buf[b.sz:])
+	dst := b.allocate(int(v.EncodedSize()))
+	v.Encode(dst)
 
-	// Size of KV on SST.
-	sstSz := uint32(headerSize) + uint32(len(diffKey)) + v.EncodedSize()
-	// Total estimated size = size on SST + size on vlog (length of value pointer).
-	b.estimatedSize += (sstSz + vpLen)
-}
-
-// grow increases the size of b.buf by atleast 50%.
-func (b *Builder) grow(n uint32) {
-	l := uint32(len(b.buf))
-	if n < l/2 {
-		n = l / 2
-	}
-	newBuf := make([]byte, l+n)
-	y.AssertTrue(uint32(len(newBuf)) == l+n)
-
-	b.bufLock.Lock()
-	copy(newBuf, b.buf)
-	b.buf = newBuf
-	b.bufLock.Unlock()
-}
-func (b *Builder) append(data []byte) {
-	// Ensure we have enough space to store new data.
-	if uint32(len(b.buf)) < b.sz+uint32(len(data)) {
-		b.grow(uint32(len(data)))
-	}
-	copy(b.buf[b.sz:], data)
-	b.sz += uint32(len(data))
-}
-
-func (b *Builder) addPadding(sz uint32) {
-	if uint32(len(b.buf)) < b.sz+sz {
-		b.grow(sz)
-	}
-	b.sz += sz
+	// Add the vpLen to the onDisk size. We'll add the size of the block to
+	// onDisk size in Finish() function.
+	b.onDiskSize += vpLen
 }
 
 /*
@@ -278,65 +257,52 @@ Structure of Block.
 */
 // In case the data is encrypted, the "IV" is added to the end of the block.
 func (b *Builder) finishBlock() {
-	if len(b.entryOffsets) == 0 {
-		return
-	}
-	b.append(y.U32SliceToBytes(b.entryOffsets))
-	b.append(y.U32ToBytes(uint32(len(b.entryOffsets))))
-
-	b.writeChecksum(b.buf[b.baseOffset:b.sz])
-
-	// If compression/encryption is disabled, no need to send the block to the blockChan.
-	// There's nothing to be done.
-	if b.blockChan == nil {
-		atomic.StoreUint32(&b.actualSize, b.sz)
-		b.addBlockToIndex()
+	if len(b.curBlock.entryOffsets) == 0 {
 		return
 	}
+	// Append the entryOffsets and its length.
+	b.append(y.U32SliceToBytes(b.curBlock.entryOffsets))
+	b.append(y.U32ToBytes(uint32(len(b.curBlock.entryOffsets))))
 
-	b.addPadding(padding)
+	checksum := b.calculateChecksum(b.curBlock.data[:b.curBlock.end])
 
-	// Block end is the actual end of the block ignoring the padding.
-	block := &bblock{start: b.baseOffset, end: uint32(b.sz - padding), data: b.buf}
-	b.blockList = append(b.blockList, block)
+	// Append the block checksum and its length.
+	b.append(checksum)
+	b.append(y.U32ToBytes(uint32(len(checksum))))
 
-	b.addBlockToIndex()
-	// Push to the block handler.
-	b.blockChan <- block
-}
+	b.blockList = append(b.blockList, b.curBlock)
+	atomic.AddUint32(&b.uncompressedSize, uint32(b.curBlock.end))
 
-func (b *Builder) addBlockToIndex() {
-	blockBuf := b.buf[b.baseOffset:b.sz]
-	// Add key to the block index.
-	builder := fbs.NewBuilder(64)
-	off := builder.CreateByteVector(b.baseKey)
+	// Add length of baseKey (rounded to next multiple of 4 because of alignment).
+	// Add another 40 Bytes, these additional 40 bytes consists of
+	// 12 bytes of metadata of flatbuffer
+	// 8 bytes for Key in flat buffer
+	// 8 bytes for offset
+	// 8 bytes for the len
+	// 4 bytes for the size of slice while SliceAllocate
+	b.lenOffsets += uint32(int(math.Ceil(float64(len(b.curBlock.baseKey))/4))*4) + 40
 
-	fb.BlockOffsetStart(builder)
-	fb.BlockOffsetAddKey(builder, off)
-	fb.BlockOffsetAddOffset(builder, b.baseOffset)
-	fb.BlockOffsetAddLen(builder, uint32(len(blockBuf)))
-	uoff := fb.BlockOffsetEnd(builder)
-	builder.Finish(uoff)
-
-	out := builder.FinishedBytes()
-	dst := b.offsets.SliceAllocate(len(out))
-	copy(dst, out)
+	// If compression/encryption is enabled, we need to send the block to the blockChan.
+	if b.blockChan != nil {
+		b.blockChan <- b.curBlock
+	}
+	return
 }
 
 func (b *Builder) shouldFinishBlock(key []byte, value y.ValueStruct) bool {
 	// If there is no entry till now, we will return false.
-	if len(b.entryOffsets) <= 0 {
+	if len(b.curBlock.entryOffsets) <= 0 {
 		return false
 	}
 
 	// Integer overflow check for statements below.
-	y.AssertTrue((uint32(len(b.entryOffsets))+1)*4+4+8+4 < math.MaxUint32)
+	y.AssertTrue((uint32(len(b.curBlock.entryOffsets))+1)*4+4+8+4 < math.MaxUint32)
 	// We should include current entry also in size, that's why +1 to len(b.entryOffsets).
-	entriesOffsetsSize := uint32((len(b.entryOffsets)+1)*4 +
+	entriesOffsetsSize := uint32((len(b.curBlock.entryOffsets)+1)*4 +
 		4 + // size of list
 		8 + // Sum64 in checksum proto
 		4) // checksum length
-	estimatedSize := uint32(b.sz) - b.baseOffset + uint32(6 /*header size for entry*/) +
+	estimatedSize := uint32(b.curBlock.end) + uint32(6 /*header size for entry*/) +
 		uint32(len(key)) + uint32(value.EncodedSize()) + entriesOffsetsSize
 
 	if b.shouldEncrypt() {
@@ -344,21 +310,21 @@ func (b *Builder) shouldFinishBlock(key []byte, value y.ValueStruct) bool {
 		// So, size of IV is added to estimatedSize.
 		estimatedSize += aes.BlockSize
 	}
+
 	// Integer overflow check for table size.
-	y.AssertTrue(uint64(b.sz)+uint64(estimatedSize) < math.MaxUint32)
+	y.AssertTrue(uint64(b.curBlock.end)+uint64(estimatedSize) < math.MaxUint32)
 
-	return estimatedSize > uint32(b.opt.BlockSize)
+	return estimatedSize > uint32(b.opts.BlockSize)
 }
 
 // Add adds a key-value pair to the block.
 func (b *Builder) Add(key []byte, value y.ValueStruct, valueLen uint32) {
 	if b.shouldFinishBlock(key, value) {
 		b.finishBlock()
-		// Start a new block. Initialize the block.
-		b.baseKey = []byte{}
-		y.AssertTrue(uint32(b.sz) < math.MaxUint32)
-		b.baseOffset = uint32((b.sz))
-		b.entryOffsets = b.entryOffsets[:0]
+		// Create a new block and start writing.
+		b.curBlock = &bblock{
+			data: b.alloc.Allocate(b.opts.BlockSize + padding),
+		}
 	}
 	b.addHelper(key, value, valueLen)
 }
@@ -370,18 +336,23 @@ func (b *Builder) Add(key []byte, value y.ValueStruct, valueLen uint32) {
 // at the end. The diff can vary.
 
 // ReachedCapacity returns true if we... roughly (?) reached capacity?
-func (b *Builder) ReachedCapacity(capacity uint64) bool {
-	blocksSize := atomic.LoadUint32(&b.actualSize) + // actual length of current buffer
-		uint32(len(b.entryOffsets)*4) + // all entry offsets size
+func (b *Builder) ReachedCapacity() bool {
+	// If encryption/compression is enabled then use the compresssed size.
+	sumBlockSizes := atomic.LoadUint32(&b.compressedSize)
+	if b.opts.Compression == options.None && b.opts.DataKey == nil {
+		sumBlockSizes = b.uncompressedSize
+	}
+	blocksSize := sumBlockSizes + // actual length of current buffer
+		uint32(len(b.curBlock.entryOffsets)*4) + // all entry offsets size
 		4 + // count of all entry offsets
 		8 + // checksum bytes
 		4 // checksum length
 
 	estimateSz := blocksSize +
 		4 + // Index length
-		uint32(b.offsets.Len())
+		b.lenOffsets
 
-	return uint64(estimateSz) > capacity
+	return uint64(estimateSz) > b.opts.tableCapacity
 }
 
 // Finish finishes the table by appending the index.
@@ -396,72 +367,72 @@ The table structure looks like
 +---------+------------+-----------+---------------+
 */
 // In case the data is encrypted, the "IV" is added to the end of the index.
-func (b *Builder) Finish(allocate bool) []byte {
+func (b *Builder) Finish() []byte {
+	bd := b.Done()
+	buf := make([]byte, bd.Size)
+	written := bd.Copy(buf)
+	y.AssertTrue(written == len(buf))
+	return buf
+}
+
+type buildData struct {
+	blockList []*bblock
+	index     []byte
+	checksum  []byte
+	Size      int
+	alloc     *z.Allocator
+}
+
+func (bd *buildData) Copy(dst []byte) int {
+	var written int
+	for _, bl := range bd.blockList {
+		written += copy(dst[written:], bl.data[:bl.end])
+	}
+	written += copy(dst[written:], bd.index)
+	written += copy(dst[written:], y.U32ToBytes(uint32(len(bd.index))))
+
+	written += copy(dst[written:], bd.checksum)
+	written += copy(dst[written:], y.U32ToBytes(uint32(len(bd.checksum))))
+	return written
+}
+
+func (b *Builder) Done() buildData {
 	b.finishBlock() // This will never start a new block.
 	if b.blockChan != nil {
 		close(b.blockChan)
 	}
-	if b.sz == 0 {
-		return nil
-	}
 	// Wait for block handler to finish.
 	b.wg.Wait()
 
-	// We have added padding after each block so we should minus the
-	// padding from the actual table size. len(blocklist) would be zero if
-	// there is no compression/encryption.
-	uncompressedSize := b.sz - uint32(padding*len(b.blockList))
-	dst := b.buf
-	// Fix block boundaries. This includes moving the blocks so that we
-	// don't have any interleaving space between them.
-	bo, next := []byte{}, 1
-	if len(b.blockList) > 0 {
-		dstLen := uint32(0)
-		for _, bl := range b.blockList {
-			bo, next = b.offsets.Slice(next)
-			// Length of the block is end minus the start.
-			fbo := fb.GetRootAsBlockOffset(bo, 0)
-			fbo.MutateLen(bl.end - bl.start)
-			// New offset of the block is the point in the main buffer till
-			// which we have written data.
-			fbo.MutateOffset(dstLen)
-
-			copy(dst[dstLen:], b.buf[bl.start:bl.end])
-
-			// New length is the start of the block plus its length.
-			dstLen = fbo.Offset() + fbo.Len()
-		}
-		y.AssertTrue(next == 0)
-		// Start writing to the buffer from the point until which we have valid data.
-		// Fix the length because append and writeChecksum also rely on it.
-		b.sz = dstLen
+	if len(b.blockList) == 0 {
+		return buildData{}
+	}
+	bd := buildData{
+		blockList: b.blockList,
+		alloc:     b.alloc,
 	}
 
 	var f y.Filter
-	if b.opt.BloomFalsePositive > 0 {
-		bits := y.BloomBitsPerKey(len(b.keyHashes), b.opt.BloomFalsePositive)
+	if b.opts.BloomFalsePositive > 0 {
+		bits := y.BloomBitsPerKey(len(b.keyHashes), b.opts.BloomFalsePositive)
 		f = y.NewFilter(b.keyHashes, bits)
 	}
-	index := b.buildIndex(f, uncompressedSize)
+	index, dataSize := b.buildIndex(f)
 
 	var err error
 	if b.shouldEncrypt() {
-		index, err = b.encrypt(index, false)
+		index, err = b.encrypt(index)
 		y.Check(err)
 	}
-	// Write index the buffer.
-	b.append(index)
-	b.append(y.U32ToBytes(uint32(len(index))))
+	checksum := b.calculateChecksum(index)
 
-	b.writeChecksum(index)
-
-	if allocate {
-		return append([]byte{}, b.buf[:b.sz]...)
-	}
-	return b.buf[:b.sz]
+	bd.index = index
+	bd.checksum = checksum
+	bd.Size = int(dataSize) + len(index) + len(checksum) + 4 + 4
+	return bd
 }
 
-func (b *Builder) writeChecksum(data []byte) {
+func (b *Builder) calculateChecksum(data []byte) []byte {
 	// Build checksum for the index.
 	checksum := pb.Checksum{
 		// TODO: The checksum type should be configurable from the
@@ -479,79 +450,69 @@ func (b *Builder) writeChecksum(data []byte) {
 	// Write checksum to the file.
 	chksum, err := proto.Marshal(&checksum)
 	y.Check(err)
-	b.append(chksum)
-
 	// Write checksum size.
-	b.append(y.U32ToBytes(uint32(len(chksum))))
+	return chksum
 }
 
 // DataKey returns datakey of the builder.
 func (b *Builder) DataKey() *pb.DataKey {
-	return b.opt.DataKey
+	return b.opts.DataKey
+}
+
+func (b *Builder) Opts() *Options {
+	return b.opts
 }
 
 // encrypt will encrypt the given data and appends IV to the end of the encrypted data.
 // This should be only called only after checking shouldEncrypt method.
-func (b *Builder) encrypt(data []byte, viaC bool) ([]byte, error) {
+func (b *Builder) encrypt(data []byte) ([]byte, error) {
 	iv, err := y.GenerateIV()
 	if err != nil {
 		return data, y.Wrapf(err, "Error while generating IV in Builder.encrypt")
 	}
 	needSz := len(data) + len(iv)
-	var dst []byte
-	if viaC {
-		dst = z.Calloc(needSz)
-	} else {
-		dst = make([]byte, needSz)
-	}
-	dst = dst[:len(data)]
+	dst := b.alloc.Allocate(needSz)
 
-	if err = y.XORBlock(dst, data, b.DataKey().Data, iv); err != nil {
-		if viaC {
-			z.Free(dst)
-		}
+	if err = y.XORBlock(dst[:len(data)], data, b.DataKey().Data, iv); err != nil {
 		return data, y.Wrapf(err, "Error while encrypting in Builder.encrypt")
 	}
-	if viaC {
-		z.Free(data)
-	}
 
-	y.AssertTrue(cap(dst)-len(dst) >= len(iv))
-	return append(dst, iv...), nil
+	y.AssertTrue(len(iv) == copy(dst[len(data):], iv))
+	return dst, nil
 }
 
 // shouldEncrypt tells us whether to encrypt the data or not.
 // We encrypt only if the data key exist. Otherwise, not.
 func (b *Builder) shouldEncrypt() bool {
-	return b.opt.DataKey != nil
+	return b.opts.DataKey != nil
 }
 
 // compressData compresses the given data.
 func (b *Builder) compressData(data []byte) ([]byte, error) {
-	switch b.opt.Compression {
+	switch b.opts.Compression {
 	case options.None:
 		return data, nil
 	case options.Snappy:
 		sz := snappy.MaxEncodedLen(len(data))
-		dst := z.Calloc(sz)
+		dst := b.alloc.Allocate(sz)
 		return snappy.Encode(dst, data), nil
 	case options.ZSTD:
 		sz := y.ZSTDCompressBound(len(data))
-		dst := z.Calloc(sz)
-		return y.ZSTDCompress(dst, data, b.opt.ZSTDCompressionLevel)
+		dst := b.alloc.Allocate(sz)
+		return y.ZSTDCompress(dst, data, b.opts.ZSTDCompressionLevel)
 	}
 	return nil, errors.New("Unsupported compression type")
 }
 
-func (b *Builder) buildIndex(bloom []byte, tableSz uint32) []byte {
+func (b *Builder) buildIndex(bloom []byte) ([]byte, uint32) {
 	builder := fbs.NewBuilder(3 << 20)
 
-	boList := b.writeBlockOffsets(builder)
+	boList, dataSize := b.writeBlockOffsets(builder)
 	// Write block offset vector the the idxBuilder.
 	fb.TableIndexStartOffsetsVector(builder, len(boList))
 
-	// Write individual block offsets.
-	for i := 0; i < len(boList); i++ {
+	// Write individual block offsets in reverse order to work around how Flatbuffers expects it.
+	for i := len(boList) - 1; i >= 0; i-- {
 		builder.PrependUOffsetT(boList[i])
 	}
 	boEnd := builder.EndVector(len(boList))
@@ -561,45 +522,47 @@ func (b *Builder) buildIndex(bloom []byte, tableSz uint32) []byte {
 	if len(bloom) > 0 {
 		bfoff = builder.CreateByteVector(bloom)
 	}
-
+	b.onDiskSize += dataSize
 	fb.TableIndexStart(builder)
 	fb.TableIndexAddOffsets(builder, boEnd)
 	fb.TableIndexAddBloomFilter(builder, bfoff)
-	fb.TableIndexAddEstimatedSize(builder, b.estimatedSize)
 	fb.TableIndexAddMaxVersion(builder, b.maxVersion)
-	fb.TableIndexAddUncompressedSize(builder, tableSz)
+	fb.TableIndexAddUncompressedSize(builder, b.uncompressedSize)
 	fb.TableIndexAddKeyCount(builder, uint32(len(b.keyHashes)))
+	fb.TableIndexAddOnDiskSize(builder, b.onDiskSize)
 	builder.Finish(fb.TableIndexEnd(builder))
 
-	return builder.FinishedBytes()
+	buf := builder.FinishedBytes()
+	index := fb.GetRootAsTableIndex(buf, 0)
+	// Mutate the ondisk size to include the size of the index as well.
+	y.AssertTrue(index.MutateOnDiskSize(index.OnDiskSize() + uint32(len(buf))))
+	return buf, dataSize
 }
 
 // writeBlockOffsets writes all the blockOffets in b.offsets and returns the
 // offsets for the newly written items.
-func (b *Builder) writeBlockOffsets(builder *fbs.Builder) []fbs.UOffsetT {
-	so := b.offsets.SliceOffsets()
+func (b *Builder) writeBlockOffsets(builder *fbs.Builder) ([]fbs.UOffsetT, uint32) {
+	var startOffset uint32
 	var uoffs []fbs.UOffsetT
-	for i := len(so) - 1; i >= 0; i-- {
-		// We add these in reverse order.
-		data, _ := b.offsets.Slice(so[i])
-		uoff := b.writeBlockOffset(builder, data)
+	for _, bl := range b.blockList {
+		uoff := b.writeBlockOffset(builder, bl, startOffset)
 		uoffs = append(uoffs, uoff)
+		startOffset += uint32(bl.end)
 	}
-	return uoffs
+	return uoffs, startOffset
 }
 
 // writeBlockOffset writes the given key,offset,len triple to the indexBuilder.
 // It returns the offset of the newly written blockoffset.
-func (b *Builder) writeBlockOffset(builder *fbs.Builder, data []byte) fbs.UOffsetT {
+func (b *Builder) writeBlockOffset(
+	builder *fbs.Builder, bl *bblock, startOffset uint32) fbs.UOffsetT {
 	// Write the key to the buffer.
-	bo := fb.GetRootAsBlockOffset(data, 0)
-
-	k := builder.CreateByteVector(bo.KeyBytes())
+	k := builder.CreateByteVector(bl.baseKey)
 
 	// Build the blockOffset.
 	fb.BlockOffsetStart(builder)
 	fb.BlockOffsetAddKey(builder, k)
-	fb.BlockOffsetAddOffset(builder, bo.Offset())
-	fb.BlockOffsetAddLen(builder, bo.Len())
+	fb.BlockOffsetAddOffset(builder, startOffset)
+	fb.BlockOffsetAddLen(builder, uint32(bl.end))
 	return fb.BlockOffsetEnd(builder)
 }
diff --git a/table/table.go b/table/table.go
index 771366d..f596d6d 100644
--- a/table/table.go
+++ b/table/table.go
@@ -29,6 +29,7 @@ import (
 	"strings"
 	"sync"
 	"sync/atomic"
+	"time"
 	"unsafe"
 
 	"github.com/golang/protobuf/proto"
@@ -55,7 +56,8 @@ type Options struct {
 	ReadOnly bool
 
 	// Maximum size of the table.
-	TableSize uint64
+	TableSize     uint64
+	tableCapacity uint64 // 0.9x TableSize.
 
 	// ChkMode is the checksum verification mode for Table.
 	ChkMode options.ChecksumVerificationMode
@@ -78,6 +80,8 @@ type Options struct {
 	BlockCache *ristretto.Cache
 	IndexCache *ristretto.Cache
 
+	AllocPool *z.AllocatorPool
+
 	// ZSTDCompressionLevel is the ZSTD compression level used for compressing blocks.
 	ZSTDCompressionLevel int
 }
@@ -97,15 +101,17 @@ type Table struct {
 	tableSize int // Initialized in OpenTable, using fd.Stat().
 
 	_index *fb.TableIndex // Nil if encryption is enabled. Use fetchIndex to access.
-	ref    int32          // For file garbage collection. Atomic.
+	_cheap *cheapIndex
+	ref    int32 // For file garbage collection. Atomic.
 
 	// The following are initialized once and const.
 	smallest, biggest []byte // Smallest and largest keys (with timestamps).
 	id                uint64 // file id, part of filename
 
-	Checksum []byte
+	Checksum  []byte
+	CreatedAt time.Time
 	// Stores the total size of key-values stored in this table (including the size on vlog).
-	estimatedSize  uint32
+	onDiskSize     uint32
 	indexStart     int
 	indexLen       int
 	hasBloomFilter bool
@@ -114,11 +120,36 @@ type Table struct {
 	opt        *Options
 }
 
-// MaxVersion returns the maximum version across all keys stored in this table.
-func (t *Table) MaxVersion() uint64 {
-	return t.fetchIndex().MaxVersion()
+type cheapIndex struct {
+	MaxVersion        uint64
+	KeyCount          uint32
+	UncompressedSize  uint32
+	OnDiskSize        uint32
+	BloomFilterLength int
+	OffsetsLength     int
 }
 
+func (t *Table) cheapIndex() *cheapIndex {
+	return t._cheap
+}
+func (t *Table) offsetsLength() int { return t.cheapIndex().OffsetsLength }
+
+// MaxVersion returns the maximum version across all keys stored in this table.
+func (t *Table) MaxVersion() uint64 { return t.cheapIndex().MaxVersion }
+
+// BloomFilterSize returns the size of the bloom filter in bytes stored in memory.
+func (t *Table) BloomFilterSize() int { return t.cheapIndex().BloomFilterLength }
+
+// UncompressedSize is the size uncompressed data stored in this file.
+func (t *Table) UncompressedSize() uint32 { return t.cheapIndex().UncompressedSize }
+
+// KeyCount is the total number of keys in this table.
+func (t *Table) KeyCount() uint32 { return t.cheapIndex().KeyCount }
+
+// OnDiskSize returns the total size of key-values stored in this table (including the
+// disk space occupied on the value log).
+func (t *Table) OnDiskSize() uint32 { return t.cheapIndex().OnDiskSize }
+
 // CompressionType returns the compression algorithm used for block compression.
 func (t *Table) CompressionType() options.CompressionType {
 	return t.opt.Compression
@@ -225,8 +256,9 @@ func (b block) verifyCheckSum() error {
 	return y.VerifyChecksum(b.data, cs)
 }
 
-func CreateTable(fname string, data []byte, opts Options) (*Table, error) {
-	mf, err := z.OpenMmapFile(fname, os.O_CREATE|os.O_RDWR|os.O_EXCL, len(data))
+func CreateTable(fname string, builder *Builder) (*Table, error) {
+	bd := builder.Done()
+	mf, err := z.OpenMmapFile(fname, os.O_CREATE|os.O_RDWR|os.O_EXCL, bd.Size)
 	if err == z.NewFile {
 		// Expected.
 	} else if err != nil {
@@ -235,13 +267,14 @@ func CreateTable(fname string, data []byte, opts Options) (*Table, error) {
 		return nil, errors.Errorf("file already exists: %s", fname)
 	}
 
-	copy(mf.Data, data)
-	if opts.SyncWrites {
+	written := bd.Copy(mf.Data)
+	y.AssertTrue(written == len(mf.Data))
+	if builder.opts.SyncWrites {
 		if err := z.Msync(mf.Data); err != nil {
 			return nil, y.Wrapf(err, "while calling msync on %s", fname)
 		}
 	}
-	return OpenTable(mf, opts)
+	return OpenTable(mf, *builder.opts)
 }
 
 // OpenTable assumes file has only one table and opens it. Takes ownership of fd upon function
@@ -273,6 +306,7 @@ func OpenTable(mf *z.MmapFile, opts Options) (*Table, error) {
 		opt:        &opts,
 		IsInmemory: false,
 		tableSize:  int(fileInfo.Size()),
+		CreatedAt:  fileInfo.ModTime(),
 	}
 
 	if err := t.initBiggestAndSmallest(); err != nil {
@@ -318,7 +352,7 @@ func (t *Table) initBiggestAndSmallest() error {
 		return y.Wrapf(err, "failed to read index.")
 	}
 
-	t.smallest = ko.KeyBytes()
+	t.smallest = y.Copy(ko.KeyBytes())
 
 	it2 := t.NewIterator(REVERSED | NOCACHE)
 	defer it2.Close()
@@ -326,7 +360,7 @@ func (t *Table) initBiggestAndSmallest() error {
 	if !it2.Valid() {
 		return y.Wrapf(it2.err, "failed to initialize biggest for table %s", t.Filename())
 	}
-	t.biggest = it2.Key()
+	t.biggest = y.Copy(it2.Key())
 	return nil
 }
 
@@ -383,18 +417,15 @@ func (t *Table) initIndex() (*fb.BlockOffset, error) {
 		// If there's no encryption, this points to the mmap'ed buffer.
 		t._index = index
 	}
-
-	if t.opt.Compression == options.None {
-		t.estimatedSize = index.EstimatedSize()
-	} else {
-		// TODO(ibrahim): This estimatedSize doesn't make any sense. If it is tracking the size of
-		// values including in value log, then index.EstimatedSize() should be used irrespective of
-		// compression or not.
-		//
-		// Due to compression the real size on disk is much
-		// smaller than what we estimate from index.EstimatedSize.
-		t.estimatedSize = uint32(t.tableSize)
+	t._cheap = &cheapIndex{
+		MaxVersion:        index.MaxVersion(),
+		KeyCount:          index.KeyCount(),
+		UncompressedSize:  index.UncompressedSize(),
+		OnDiskSize:        index.OnDiskSize(),
+		OffsetsLength:     index.OffsetsLength(),
+		BloomFilterLength: index.BloomFilterLength(),
 	}
+
 	t.hasBloomFilter = len(index.BloomFilterBytes()) > 0
 
 	var bo fb.BlockOffset
@@ -446,10 +477,6 @@ func (t *Table) fetchIndex() *fb.TableIndex {
 	return index
 }
 
-func (t *Table) offsetsLength() int {
-	return t.fetchIndex().OffsetsLength()
-}
-
 func (t *Table) offsets(ko *fb.BlockOffset, i int) bool {
 	return t.fetchIndex().Offsets(ko, i)
 }
@@ -493,9 +520,11 @@ func (t *Table) block(idx int, useCache bool) (*block, error) {
 
 	if t.shouldDecrypt() {
 		// Decrypt the block if it is encrypted.
-		if blk.data, err = t.decrypt(blk.data); err != nil {
+		if blk.data, err = t.decrypt(blk.data, true); err != nil {
 			return nil, err
 		}
+		// blk.data is allocated via Calloc. So, do free.
+		blk.freeMe = true
 	}
 
 	if err = t.decompress(blk); err != nil {
@@ -574,30 +603,11 @@ func (t *Table) indexKey() uint64 {
 	return t.id
 }
 
-// UncompressedSize is the size of table index in bytes.
-func (t *Table) UncompressedSize() uint32 {
-	return t.fetchIndex().UncompressedSize()
-}
-
-// KeyCount is the total number of keys in this table.
-func (t *Table) KeyCount() uint32 {
-	return t.fetchIndex().KeyCount()
-}
-
 // IndexSize is the size of table index in bytes.
 func (t *Table) IndexSize() int {
 	return t.indexLen
 }
 
-// BloomFilterSize returns the size of the bloom filter in bytes stored in memory.
-func (t *Table) BloomFilterSize() int {
-	return t.fetchIndex().BloomFilterLength()
-}
-
-// EstimatedSize returns the total size of key-values stored in this table (including the
-// disk space occupied on the value log).
-func (t *Table) EstimatedSize() uint32 { return t.estimatedSize }
-
 // Size is its file size in bytes
 func (t *Table) Size() int64 { return int64(t.tableSize) }
 
@@ -620,19 +630,14 @@ func (t *Table) DoesNotHave(hash uint32) bool {
 		return false
 	}
 
+	y.NumLSMBloomHits.Add("DoesNotHave_ALL", 1)
 	index := t.fetchIndex()
 	bf := index.BloomFilterBytes()
-	return !y.Filter(bf).MayContain(hash)
-}
-
-// readBloomFilter reads the bloom filter from the SST and returns its length
-// along with the bloom filter.
-func (t *Table) readBloomFilter() (*z.Bloom, int) {
-	index := t.fetchIndex()
-	// Read bloom filter from the index.
-	bf, err := z.JSONUnmarshal(index.BloomFilterBytes())
-	y.Check(err)
-	return bf, index.BloomFilterLength()
+	mayContain := y.Filter(bf).MayContain(hash)
+	if !mayContain {
+		y.NumLSMBloomHits.Add("DoesNotHave_HIT", 1)
+	}
+	return !mayContain
 }
 
 // readTableIndex reads table index from the sst and returns its pb format.
@@ -641,7 +646,7 @@ func (t *Table) readTableIndex() (*fb.TableIndex, error) {
 	var err error
 	// Decrypt the table index if it is encrypted.
 	if t.shouldDecrypt() {
-		if data, err = t.decrypt(data); err != nil {
+		if data, err = t.decrypt(data, false); err != nil {
 			return nil, y.Wrapf(err,
 				"Error while decrypting table index for the table %d in readTableIndex", t.id)
 		}
@@ -690,14 +695,18 @@ func (t *Table) KeyID() uint64 {
 }
 
 // decrypt decrypts the given data. It should be called only after checking shouldDecrypt.
-func (t *Table) decrypt(data []byte) ([]byte, error) {
+func (t *Table) decrypt(data []byte, viaCalloc bool) ([]byte, error) {
 	// Last BlockSize bytes of the data is the IV.
 	iv := data[len(data)-aes.BlockSize:]
 	// Rest all bytes are data.
 	data = data[:len(data)-aes.BlockSize]
 
-	// TODO: Check if this is done via Calloc. Otherwise, we'll have a memory leak.
-	dst := make([]byte, len(data))
+	var dst []byte
+	if viaCalloc {
+		dst = z.Calloc(len(data))
+	} else {
+		dst = make([]byte, len(data))
+	}
 	if err := y.XORBlock(dst, data, t.opt.DataKey.Data, iv); err != nil {
 		return nil, y.Wrapf(err, "while decrypt")
 	}
@@ -736,6 +745,9 @@ func (t *Table) decompress(b *block) error {
 	var dst []byte
 	var err error
 
+	// Point to the original b.data
+	src := b.data
+
 	switch t.opt.Compression {
 	case options.None:
 		// Nothing to be done here.
@@ -763,6 +775,11 @@ func (t *Table) decompress(b *block) error {
 		return errors.New("Unsupported compression type")
 	}
 
+	if b.freeMe == true {
+		z.Free(src)
+		b.freeMe = false
+	}
+
 	if len(b.data) > 0 && len(dst) > 0 && &dst[0] != &b.data[0] {
 		z.Free(dst)
 	} else {
diff --git a/value.go b/value.go
index 9ea4271..bcce9bb 100644
--- a/value.go
+++ b/value.go
@@ -18,6 +18,7 @@ package badger
 
 import (
 	"bytes"
+	"context"
 	"fmt"
 	"hash"
 	"hash/crc32"
@@ -34,6 +35,7 @@ import (
 	"github.com/dgraph-io/badger/v2/y"
 	"github.com/dgraph-io/ristretto/z"
 	"github.com/pkg/errors"
+	otrace "go.opencensus.io/trace"
 )
 
 // maxVlogFileSize is the maximum size of the vlog file which can be created. Vlog Offset is of
@@ -506,8 +508,9 @@ func (vlog *valueLog) createVlogFile() (*logFile, error) {
 		path:     path,
 		registry: vlog.db.registry,
 		writeAt:  vlogHeaderSize,
+		opt:      vlog.opt,
 	}
-	err := lf.open(path, os.O_RDWR|os.O_CREATE|os.O_EXCL, vlog.opt)
+	err := lf.open(path, os.O_RDWR|os.O_CREATE|os.O_EXCL, 2*vlog.opt.ValueLogFileSize)
 	if err != z.NewFile && err != nil {
 		return nil, err
 	}
@@ -560,6 +563,9 @@ func (vlog *valueLog) open(db *DB) error {
 	}
 	// If no files are found, then create a new file.
 	if len(vlog.filesMap) == 0 {
+		if vlog.opt.ReadOnly {
+			return nil
+		}
 		_, err := vlog.createVlogFile()
 		return y.Wrapf(err, "Error while creating log file in valueLog.open")
 	}
@@ -569,7 +575,9 @@ func (vlog *valueLog) open(db *DB) error {
 		y.AssertTrue(ok)
 
 		// Just open in RDWR mode. This should not create a new log file.
-		if err := lf.open(vlog.fpath(fid), os.O_RDWR, vlog.opt); err != nil {
+		lf.opt = vlog.opt
+		if err := lf.open(vlog.fpath(fid), os.O_RDWR,
+			2*vlog.opt.ValueLogFileSize); err != nil {
 			return y.Wrapf(err, "Open existing file: %q", lf.path)
 		}
 		// We shouldn't delete the maxFid file.
@@ -582,6 +590,9 @@ func (vlog *valueLog) open(db *DB) error {
 		}
 	}
 
+	if vlog.opt.ReadOnly {
+		return nil
+	}
 	// Now we can read the latest value log file, and see if it needs truncation. We could
 	// technically do this over all the value log files, but that would mean slowing down the value
 	// log open.
@@ -796,8 +807,9 @@ func (vlog *valueLog) write(reqs []*request) error {
 
 		n := uint32(buf.Len())
 		endOffset := atomic.AddUint32(&vlog.writableLogOffset, n)
+		// Increase the file size if we cannot accommodate this entry.
 		if int(endOffset) >= len(curlf.Data) {
-			return y.Wrapf(ErrTxnTooBig, "endOffset: %d len: %d\n", endOffset, len(curlf.Data))
+			curlf.Truncate(int64(endOffset))
 		}
 
 		start := int(endOffset - n)
@@ -819,7 +831,6 @@ func (vlog *valueLog) write(reqs []*request) error {
 				return err
 			}
 			curlf = newlf
-			atomic.AddInt32(&vlog.db.logRotates, 1)
 		}
 		return nil
 	}
@@ -853,7 +864,6 @@ func (vlog *valueLog) write(reqs []*request) error {
 			if err != nil {
 				return err
 			}
-			// TODO(Naman): Add a test to check that meta bits are correctly set?
 			// Restore the meta.
 			e.meta = tmpMeta
 
@@ -893,7 +903,9 @@ func (vlog *valueLog) getFileRLocked(vp valuePointer) (*logFile, error) {
 
 	// Check for valid offset if we are reading from writable log.
 	maxFid := vlog.maxFid
-	if vp.Fid == maxFid {
+	// In read-only mode we don't need to check for writable offset as we are not writing anything.
+	// Moreover, this offset is not set in readonly mode.
+	if !vlog.opt.ReadOnly && vp.Fid == maxFid {
 		currentOffset := vlog.woffset()
 		if vp.Offset >= currentOffset {
 			return nil, errors.Errorf(
@@ -973,13 +985,13 @@ func (vlog *valueLog) pickLog(discardRatio float64) *logFile {
 	defer vlog.filesLock.RUnlock()
 
 LOOP:
-	// TODO(naman): Add a test for MaxDiscard which checks for the zero value.
 	// Pick a candidate that contains the largest amount of discardable data
 	fid, discard := vlog.discardStats.MaxDiscard()
 
 	// MaxDiscard will return fid=0 if it doesn't have any discard data. The
 	// vlog files start from 1.
 	if fid == 0 {
+		vlog.opt.Debugf("No file with discard stats")
 		return nil
 	}
 	lf, ok := vlog.filesMap[fid]
@@ -995,9 +1007,9 @@ LOOP:
 		vlog.opt.Errorf("Unable to get stats for value log fid: %d err: %+v", fi, err)
 		return nil
 	}
-	if float64(discard) < discardRatio*float64(fi.Size()) {
-		vlog.opt.Debugf("Discard: %d less than DiscardRatio: %f for file: %s",
-			discard, discardRatio, fi.Name())
+	if thr := discardRatio * float64(fi.Size()); float64(discard) < thr {
+		vlog.opt.Debugf("Discard: %d less than threshold: %.0f for file: %s",
+			discard, thr, fi.Name())
 		return nil
 	}
 	maxFid := atomic.LoadUint32(&vlog.maxFid)
@@ -1038,6 +1050,9 @@ type reason struct {
 }
 
 func (vlog *valueLog) doRunGC(lf *logFile) error {
+	_, span := otrace.StartSpan(context.Background(), "Badger.GC")
+	span.Annotatef(nil, "GC rewrite for: %v", lf.path)
+	defer span.End()
 	if err := vlog.rewrite(lf); err != nil {
 		return err
 	}
diff --git a/y/y.go b/y/y.go
index cd75c3d..e527d42 100644
--- a/y/y.go
+++ b/y/y.go
@@ -29,6 +29,8 @@ import (
 	"time"
 	"unsafe"
 
+	"github.com/dgraph-io/badger/v2/pb"
+	"github.com/dgraph-io/ristretto/z"
 	"github.com/pkg/errors"
 )
 
@@ -465,3 +467,13 @@ func (r *PageBufferReader) Read(p []byte) (int, error) {
 
 	return read, nil
 }
+
+const kvsz = int(unsafe.Sizeof(pb.KV{}))
+
+func NewKV(alloc *z.Allocator) *pb.KV {
+	if alloc == nil {
+		return &pb.KV{}
+	}
+	b := alloc.AllocateAligned(kvsz)
+	return (*pb.KV)(unsafe.Pointer(&b[0]))
+}


Test patch:
diff --git a/batch_test.go b/batch_test.go
index 273a297..300e053 100644
--- a/batch_test.go
+++ b/batch_test.go
@@ -80,13 +80,16 @@ func TestWriteBatch(t *testing.T) {
 		runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
 			test(t, db)
 		})
+		t.Logf("Disk mode done\n")
 	})
 	t.Run("InMemory mode", func(t *testing.T) {
+		t.Skipf("TODO(ibrahim): Please fix this")
 		opt := getTestOptions("")
 		opt.InMemory = true
 		db, err := Open(opt)
 		require.NoError(t, err)
 		test(t, db)
+		t.Logf("Disk mode done\n")
 		require.NoError(t, db.Close())
 	})
 }
diff --git a/db2_test.go b/db2_test.go
index afecd1b..2d40b1e 100644
--- a/db2_test.go
+++ b/db2_test.go
@@ -18,6 +18,7 @@ package badger
 
 import (
 	"bytes"
+	"context"
 	"encoding/binary"
 	"flag"
 	"fmt"
@@ -33,6 +34,8 @@ import (
 	"testing"
 	"time"
 
+	"github.com/dgraph-io/badger/v2/options"
+
 	"github.com/dgraph-io/badger/v2/pb"
 	"github.com/dgraph-io/badger/v2/table"
 	"github.com/dgraph-io/badger/v2/y"
@@ -101,7 +104,7 @@ func TestTruncateVlogWithClose(t *testing.T) {
 	for i := 0; i < 32; i++ {
 		err := db.View(func(txn *Txn) error {
 			item, err := txn.Get(key(i))
-			require.NoError(t, err)
+			require.NoError(t, err, "key: %s", key(i))
 			val := getItemValue(t, item)
 			require.Equal(t, 10, len(val))
 			return nil
@@ -209,7 +212,7 @@ func TestBigKeyValuePairs(t *testing.T) {
 
 	// Passing an empty directory since it will be filled by runBadgerTest.
 	opts := DefaultOptions("").
-		WithMaxTableSize(1 << 20).
+		WithBaseTableSize(1 << 20).
 		WithValueLogMaxEntries(64)
 	runBadgerTest(t, &opts, func(t *testing.T, db *DB) {
 		bigK := make([]byte, 65001)
@@ -502,8 +505,9 @@ func addToManifest(t *testing.T, db *DB, tab *table.Table, level uint32) {
 // createTableWithRange function is used in TestCompactionFilePicking. It creates
 // a table with key starting from start and ending with end.
 func createTableWithRange(t *testing.T, db *DB, start, end int) *table.Table {
-	bopts := buildTableOptions(db.opt)
+	bopts := buildTableOptions(db)
 	b := table.NewTableBuilder(bopts)
+	defer b.Close()
 	nums := []int{start, end}
 	for _, i := range nums {
 		key := make([]byte, 8)
@@ -514,7 +518,7 @@ func createTableWithRange(t *testing.T, db *DB, start, end int) *table.Table {
 	}
 
 	fileID := db.lc.reserveFileID()
-	tab, err := table.CreateTable(table.NewFilename(fileID, db.opt.Dir), b.Finish(false), bopts)
+	tab, err := table.CreateTable(table.NewFilename(fileID, db.opt.Dir), b)
 	require.NoError(t, err)
 	return tab
 }
@@ -904,3 +908,126 @@ func TestTxnReadTs(t *testing.T) {
 	require.NoError(t, err)
 	require.Equal(t, 1, int(db.orc.readTs()))
 }
+
+// This tests failed for stream writer with jemalloc and compression enabled.
+func TestKeyCount(t *testing.T) {
+	if !*manual {
+		t.Skip("Skipping test meant to be run manually.")
+		return
+	}
+
+	writeSorted := func(db *DB, num uint64) {
+		valSz := 128
+		value := make([]byte, valSz)
+		y.Check2(rand.Read(value))
+		es := 8 + valSz // key size is 8 bytes and value size is valSz
+
+		writer := db.NewStreamWriter()
+		require.NoError(t, writer.Prepare())
+
+		wg := &sync.WaitGroup{}
+		writeCh := make(chan *pb.KVList, 3)
+		writeRange := func(start, end uint64, streamId uint32) {
+			// end is not included.
+			defer wg.Done()
+			kvs := &pb.KVList{}
+			var sz int
+			for i := start; i < end; i++ {
+				key := make([]byte, 8)
+				binary.BigEndian.PutUint64(key, i)
+				kvs.Kv = append(kvs.Kv, &pb.KV{
+					Key:      key,
+					Value:    value,
+					Version:  1,
+					StreamId: streamId,
+				})
+
+				sz += es
+
+				if sz >= 4<<20 { // 4 MB
+					writeCh <- kvs
+					kvs = &pb.KVList{}
+					sz = 0
+				}
+			}
+			writeCh <- kvs
+		}
+
+		// Let's create some streams.
+		width := num / 16
+		streamID := uint32(0)
+		for start := uint64(0); start < num; start += width {
+			end := start + width
+			if end > num {
+				end = num
+			}
+			streamID++
+			wg.Add(1)
+			go writeRange(start, end, streamID)
+		}
+		go func() {
+			wg.Wait()
+			close(writeCh)
+		}()
+
+		write := func(kvs *pb.KVList) error {
+			buf := z.NewBuffer(1 << 20)
+			defer buf.Release()
+
+			for _, kv := range kvs.Kv {
+				KVToBuffer(kv, buf)
+			}
+			writer.Write(buf)
+			return nil
+		}
+
+		for kvs := range writeCh {
+			require.NoError(t, write(kvs))
+		}
+		require.NoError(t, writer.Flush())
+	}
+
+	N := uint64(10 * 1e6) // 10 million entries
+	dir, err := ioutil.TempDir("", "badger-test")
+	require.NoError(t, err)
+	defer removeDir(dir)
+	opt := DefaultOptions(dir).
+		WithBlockCacheSize(100 << 20).
+		WithCompression(options.ZSTD)
+
+	db, err := Open(opt)
+	y.Check(err)
+	defer db.Close()
+	writeSorted(db, N)
+	require.NoError(t, db.Close())
+	t.Logf("Writing DONE\n")
+
+	// Read the db
+	db2, err := Open(DefaultOptions(dir))
+	y.Check(err)
+	defer db.Close()
+	lastKey := -1
+	count := 0
+
+	streams := make(map[uint32]int)
+	stream := db2.NewStream()
+	stream.Send = func(buf *z.Buffer) error {
+		list, err := BufferToKVList(buf)
+		if err != nil {
+			return err
+		}
+		for _, kv := range list.Kv {
+			last := streams[kv.StreamId]
+			key := binary.BigEndian.Uint64(kv.Key)
+			// The following should happen as we're writing sorted data.
+			if last > 0 {
+				require.Equalf(t, last+1, int(key), "Expected key: %d, Found Key: %d", lastKey+1, int(key))
+			}
+			streams[kv.StreamId] = int(key)
+		}
+		count += len(list.Kv)
+		return nil
+	}
+	require.NoError(t, stream.Orchestrate(context.Background()))
+	require.Equal(t, N, uint64(count))
+}
diff --git a/db_test.go b/db_test.go
index 8ef6d35..fb45721 100644
--- a/db_test.go
+++ b/db_test.go
@@ -38,6 +38,7 @@ import (
 	"github.com/dgraph-io/badger/v2/options"
 	"github.com/dgraph-io/badger/v2/pb"
 	"github.com/dgraph-io/badger/v2/y"
+	"github.com/dgraph-io/ristretto/z"
 )
 
 // summary is produced when DB is closed. Currently it is used only for testing.
@@ -67,9 +68,11 @@ func (s *DB) validate() error { return s.lc.validate() }
 
 func getTestOptions(dir string) Options {
 	opt := DefaultOptions(dir).
-		WithMaxTableSize(1 << 15). // Force more compaction.
-		WithLevelOneSize(4 << 15). // Force more compaction.
-		WithSyncWrites(false)
+		WithMemTableSize(1 << 15).
+		WithBaseTableSize(1 << 15). // Force more compaction.
+		WithBaseLevelSize(4 << 15). // Force more compaction.
+		WithSyncWrites(false).
+		WithLoggingLevel(WARNING)
 	return opt
 }
 
@@ -340,7 +343,8 @@ func TestForceCompactL0(t *testing.T) {
 	require.NoError(t, err)
 	defer removeDir(dir)
 
-	opts := getTestOptions(dir)
+	// This test relies on CompactL0OnClose
+	opts := getTestOptions(dir).WithCompactL0OnClose(true)
 	opts.ValueLogFileSize = 15 << 20
 	opts.managedTxns = true
 	db, err := Open(opts)
@@ -371,6 +375,19 @@ func TestForceCompactL0(t *testing.T) {
 }
 
 func TestStreamDB(t *testing.T) {
+	check := func(db *DB) {
+		for i := 0; i < 100; i++ {
+			key := []byte(fmt.Sprintf("key%d", i))
+			val := []byte(fmt.Sprintf("val%d", i))
+			txn := db.NewTransactionAt(1, false)
+			item, err := txn.Get(key)
+			require.NoError(t, err)
+			require.EqualValues(t, val, getItemValue(t, item))
+			require.Equal(t, byte(0x00), item.UserMeta())
+			txn.Discard()
+		}
+	}
+
 	dir, err := ioutil.TempDir("", "badger-test")
 	require.NoError(t, err)
 	defer removeDir(dir)
@@ -391,6 +408,7 @@ func TestStreamDB(t *testing.T) {
 		require.NoError(t, writer.SetEntryAt(NewEntry(key, val).WithMeta(0x00), 1))
 	}
 	require.NoError(t, writer.Flush())
+	check(db)
 
 	outDir, err := ioutil.TempDir("", "badger-test")
 	require.NoError(t, err)
@@ -402,17 +420,7 @@ func TestStreamDB(t *testing.T) {
 	defer func() {
 		require.NoError(t, outDB.Close())
 	}()
-
-	for i := 0; i < 100; i++ {
-		key := []byte(fmt.Sprintf("key%d", i))
-		val := []byte(fmt.Sprintf("val%d", i))
-		txn := outDB.NewTransactionAt(1, false)
-		item, err := txn.Get(key)
-		require.NoError(t, err)
-		require.EqualValues(t, val, getItemValue(t, item))
-		require.Equal(t, byte(0x00), item.UserMeta())
-		txn.Discard()
-	}
+	check(outDB)
 }
 
 func dirSize(path string) (int64, error) {
@@ -457,8 +465,8 @@ func BenchmarkDbGrowth(b *testing.B) {
 	maxWrites := 200
 	opts := getTestOptions(dir)
 	opts.ValueLogFileSize = 64 << 15
-	opts.MaxTableSize = 4 << 15
-	opts.LevelOneSize = 16 << 15
+	opts.BaseTableSize = 4 << 15
+	opts.BaseLevelSize = 16 << 15
 	opts.NumVersionsToKeep = 1
 	opts.NumLevelZeroTables = 1
 	opts.NumLevelZeroTablesStall = 2
@@ -518,15 +526,20 @@ func BenchmarkDbGrowth(b *testing.B) {
 // Put a lot of data to move some data to disk.
 // WARNING: This test might take a while but it should pass!
 func TestGetMore(t *testing.T) {
+	if !*manual {
+		t.Skip("Skipping test meant to be run manually.")
+		return
+	}
 	runBadgerTest(t, nil, func(t *testing.T, db *DB) {
-
 		data := func(i int) []byte {
 			return []byte(fmt.Sprintf("%b", i))
 		}
-		//	n := 500000
-		n := 10000
+		n := 200000
 		m := 45 // Increasing would cause ErrTxnTooBig
 		for i := 0; i < n; i += m {
+			if (i % 10000) == 0 {
+				fmt.Printf("Inserting i=%d\n", i)
+			}
 			txn := db.NewTransaction(true)
 			for j := i; j < i+m && j < n; j++ {
 				require.NoError(t, txn.SetEntry(NewEntry(data(j), data(j))))
@@ -909,6 +922,10 @@ func TestIterateDeleted(t *testing.T) {
 }
 
 func TestIterateParallel(t *testing.T) {
+	if !*manual {
+		t.Skip("Skipping test meant to be run manually.")
+		return
+	}
 	key := func(account int) []byte {
 		var b [4]byte
 		binary.BigEndian.PutUint32(b[:], uint32(account))
@@ -1751,10 +1768,11 @@ func TestLSMOnly(t *testing.T) {
 	// Also test for error, when ValueThresholdSize is greater than maxBatchSize.
 	dopts.ValueThreshold = LSMOnlyOptions(dir).ValueThreshold
 	// maxBatchSize is calculated from MaxTableSize.
-	dopts.MaxTableSize = int64(LSMOnlyOptions(dir).ValueThreshold)
+	dopts.MemTableSize = int64(LSMOnlyOptions(dir).ValueThreshold)
 	_, err = Open(dopts)
 	require.Error(t, err, "db creation should have been failed")
-	require.Contains(t, err.Error(), "Valuethreshold greater than max batch size")
+	require.Contains(t, err.Error(),
+		fmt.Sprintf("Valuethreshold %d greater than max batch size", dopts.ValueThreshold))
 
 	opts.ValueLogMaxEntries = 100
 	db, err := Open(opts)
@@ -1814,6 +1832,10 @@ func TestMinReadTs(t *testing.T) {
 }
 
 func TestGoroutineLeak(t *testing.T) {
+	if !*manual {
+		t.Skip("Skipping test meant to be run manually.")
+		return
+	}
 	test := func(t *testing.T, opt *Options) {
 		time.Sleep(1 * time.Second)
 		before := runtime.NumGoroutine()
@@ -2024,7 +2046,6 @@ func TestForceFlushMemtable(t *testing.T) {
 
 	ops := getTestOptions(dir)
 	ops.ValueLogMaxEntries = 1
-	ops.LogRotatesToFlush = 1
 
 	db, err := Open(ops)
 	require.NoError(t, err, "error while openning db")
@@ -2068,17 +2089,19 @@ func TestVerifyChecksum(t *testing.T) {
 		runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
 			value := make([]byte, 32)
 			y.Check2(rand.Read(value))
-			l := &pb.KVList{}
 			st := 0
+
+			buf := z.NewBuffer(10 << 20)
+			defer buf.Release()
 			for i := 0; i < 1000; i++ {
 				key := make([]byte, 8)
 				binary.BigEndian.PutUint64(key, uint64(i))
-				l.Kv = append(l.Kv, &pb.KV{
+				KVToBuffer(&pb.KV{
 					Key:      key,
 					Value:    value,
 					StreamId: uint32(st),
 					Version:  1,
-				})
+				}, buf)
 				if i%100 == 0 {
 					st++
 				}
@@ -2086,7 +2109,7 @@ func TestVerifyChecksum(t *testing.T) {
 
 			sw := db.NewStreamWriter()
 			require.NoError(t, sw.Prepare(), "sw.Prepare() failed")
-			require.NoError(t, sw.Write(l), "sw.Write() failed")
+			require.NoError(t, sw.Write(buf), "sw.Write() failed")
 			require.NoError(t, sw.Flush(), "sw.Flush() failed")
 
 			require.NoError(t, db.VerifyChecksum(), "checksum verification failed for DB")
@@ -2187,3 +2210,80 @@ func TestUpdateMaxCost(t *testing.T) {
 	require.NoError(t, err)
 	require.Equal(t, int64(4<<20), cost)
 }
+
+func TestOpenDBReadOnly(t *testing.T) {
+	dir, err := ioutil.TempDir("", "badger-test")
+	require.NoError(t, err)
+	defer os.RemoveAll(dir)
+
+	mp := make(map[string][]byte)
+
+	ops := getTestOptions(dir)
+	ops.ReadOnly = false
+	db, err := Open(ops)
+	require.NoError(t, err)
+	// Add bunch of entries that don't go into value log.
+	require.NoError(t, db.Update(func(txn *Txn) error {
+		val := make([]byte, 10)
+		rand.Read(val)
+		for i := 0; i < 10; i++ {
+			key := fmt.Sprintf("key-%05d", i)
+			require.NoError(t, txn.Set([]byte(key), val))
+			mp[key] = val
+		}
+		return nil
+	}))
+	require.NoError(t, db.Close())
+
+	ops.ReadOnly = true
+	db, err = Open(ops)
+	require.NoError(t, err)
+	require.NoError(t, db.Close())
+
+	db, err = Open(ops)
+	require.NoError(t, err)
+	var count int
+	read := func() {
+		count = 0
+		db.View(func(txn *Txn) error {
+			it := txn.NewIterator(DefaultIteratorOptions)
+			defer it.Close()
+			for it.Rewind(); it.Valid(); it.Next() {
+				item := it.Item()
+				require.NoError(t, item.Value(func(val []byte) error {
+					require.Equal(t, mp[string(item.Key())], val)
+					return nil
+				}))
+				count++
+			}
+			return nil
+		})
+	}
+	read()
+	require.Equal(t, 10, count)
+	require.NoError(t, db.Close())
+
+	ops.ReadOnly = false
+	db, err = Open(ops)
+	require.NoError(t, err)
+	// Add bunch of entries that go into value log.
+	require.NoError(t, db.Update(func(txn *Txn) error {
+		require.Greater(t, db.opt.ValueThreshold, 10)
+		val := make([]byte, db.opt.ValueThreshold+10)
+		rand.Read(val)
+		for i := 0; i < 10; i++ {
+			key := fmt.Sprintf("KEY-%05d", i)
+			require.NoError(t, txn.Set([]byte(key), val))
+			mp[key] = val
+		}
+		return nil
+	}))
+	require.NoError(t, db.Close())
+
+	ops.ReadOnly = true
+	db, err = Open(ops)
+	require.NoError(t, err)
+	read()
+	require.Equal(t, 20, count)
+	require.NoError(t, db.Close())
+}
diff --git a/discard_test.go b/discard_test.go
index 351273f..28c70a2 100644
--- a/discard_test.go
+++ b/discard_test.go
@@ -31,6 +31,10 @@ func TestDiscardStats(t *testing.T) {
 	opt := DefaultOptions(dir)
 	ds, err := initDiscardStats(opt)
 	require.NoError(t, err)
+	require.Zero(t, ds.nextEmptySlot)
+	fid, _ := ds.MaxDiscard()
+	require.Zero(t, fid)
+
 	for i := uint32(0); i < 20; i++ {
 		require.Equal(t, int64(i*100), ds.Update(i, int64(i*100)))
 	}
@@ -48,3 +52,26 @@ func TestDiscardStats(t *testing.T) {
 		require.Equal(t, int(id*100), int(val))
 	})
 }
+
+func TestReloadDiscardStats(t *testing.T) {
+	dir, err := ioutil.TempDir("", "badger-test")
+	require.NoError(t, err)
+	defer removeDir(dir)
+
+	opt := DefaultOptions(dir)
+	db, err := Open(opt)
+	require.NoError(t, err)
+	ds := db.vlog.discardStats
+
+	ds.Update(uint32(1), 1)
+	ds.Update(uint32(2), 1)
+	ds.Update(uint32(1), -1)
+	require.NoError(t, db.Close())
+
+	// Reopen the DB, discard stats should be same.
+	db2, err := Open(opt)
+	require.NoError(t, err)
+	ds2 := db2.vlog.discardStats
+	require.Zero(t, ds2.Update(uint32(1), 0))
+	require.Equal(t, 1, int(ds2.Update(uint32(2), 0)))
+}
diff --git a/iterator_test.go b/iterator_test.go
index 28ff3fd..d52c56d 100644
--- a/iterator_test.go
+++ b/iterator_test.go
@@ -123,6 +123,10 @@ func TestPickSortTables(t *testing.T) {
 }
 
 func TestIteratePrefix(t *testing.T) {
+	if !*manual {
+		t.Skip("Skipping test meant to be run manually.")
+		return
+	}
 	testIteratorPrefix := func(t *testing.T, db *DB) {
 		bkey := func(i int) []byte {
 			return []byte(fmt.Sprintf("%04d", i))
@@ -199,14 +203,12 @@ func TestIteratePrefix(t *testing.T) {
 	}
 
 	t.Run("With Default options", func(t *testing.T) {
-		t.Parallel()
 		runBadgerTest(t, nil, func(t *testing.T, db *DB) {
 			testIteratorPrefix(t, db)
 		})
 	})
 
 	t.Run("With Block Offsets in Cache", func(t *testing.T) {
-		t.Parallel()
 		opts := getTestOptions("")
 		opts.IndexCacheSize = 100 << 20
 		runBadgerTest(t, &opts, func(t *testing.T, db *DB) {
@@ -215,7 +217,6 @@ func TestIteratePrefix(t *testing.T) {
 	})
 
 	t.Run("With Block Offsets and Blocks in Cache", func(t *testing.T) {
-		t.Parallel()
 		opts := getTestOptions("")
 		opts.BlockCacheSize = 100 << 20
 		opts.IndexCacheSize = 100 << 20
@@ -225,7 +226,6 @@ func TestIteratePrefix(t *testing.T) {
 	})
 
 	t.Run("With Blocks in Cache", func(t *testing.T) {
-		t.Parallel()
 		opts := getTestOptions("")
 		opts.BlockCacheSize = 100 << 20
 		runBadgerTest(t, &opts, func(t *testing.T, db *DB) {
diff --git a/key_registry_test.go b/key_registry_test.go
index ce7fcf7..3c47ffd 100644
--- a/key_registry_test.go
+++ b/key_registry_test.go
@@ -42,12 +42,12 @@ func TestBuildRegistry(t *testing.T) {
 
 	kr, err := OpenKeyRegistry(opt)
 	require.NoError(t, err)
-	dk, err := kr.latestDataKey()
+	dk, err := kr.LatestDataKey()
 	require.NoError(t, err)
 	// We're resetting the last created timestamp. So, it creates
 	// new datakey.
 	kr.lastCreated = 0
-	dk1, err := kr.latestDataKey()
+	dk1, err := kr.LatestDataKey()
 	// We generated two key. So, checking the length.
 	require.Equal(t, 2, len(kr.dataKeys))
 	require.NoError(t, err)
@@ -72,12 +72,12 @@ func TestRewriteRegistry(t *testing.T) {
 	opt := getRegistryTestOptions(dir, encryptionKey)
 	kr, err := OpenKeyRegistry(opt)
 	require.NoError(t, err)
-	_, err = kr.latestDataKey()
+	_, err = kr.LatestDataKey()
 	require.NoError(t, err)
 	// We're resetting the last created timestamp. So, it creates
 	// new datakey.
 	kr.lastCreated = 0
-	_, err = kr.latestDataKey()
+	_, err = kr.LatestDataKey()
 	require.NoError(t, err)
 	require.NoError(t, kr.Close())
 	delete(kr.dataKeys, 1)
@@ -123,14 +123,14 @@ func TestEncryptionAndDecryption(t *testing.T) {
 	opt := getRegistryTestOptions(dir, encryptionKey)
 	kr, err := OpenKeyRegistry(opt)
 	require.NoError(t, err)
-	dk, err := kr.latestDataKey()
+	dk, err := kr.LatestDataKey()
 	require.NoError(t, err)
 	require.NoError(t, kr.Close())
 	// Checking the correctness of the datakey after closing and
 	// opening the key registry.
 	kr, err = OpenKeyRegistry(opt)
 	require.NoError(t, err)
-	dk1, err := kr.dataKey(dk.GetKeyId())
+	dk1, err := kr.DataKey(dk.GetKeyId())
 	require.NoError(t, err)
 	require.Equal(t, dk.Data, dk1.Data)
 	require.NoError(t, kr.Close())
@@ -146,12 +146,12 @@ func TestKeyRegistryInMemory(t *testing.T) {
 
 	kr, err := OpenKeyRegistry(opt)
 	require.NoError(t, err)
-	_, err = kr.latestDataKey()
+	_, err = kr.LatestDataKey()
 	require.NoError(t, err)
 	// We're resetting the last created timestamp. So, it creates
 	// new datakey.
 	kr.lastCreated = 0
-	_, err = kr.latestDataKey()
+	_, err = kr.LatestDataKey()
 	// We generated two key. So, checking the length.
 	require.Equal(t, 2, len(kr.dataKeys))
 	require.NoError(t, err)
diff --git a/levels_test.go b/levels_test.go
index 63f6df2..f928df2 100644
--- a/levels_test.go
+++ b/levels_test.go
@@ -37,6 +37,7 @@ func createAndOpen(db *DB, td []keyValVersion, level int) {
 		ChkMode:            options.NoVerification,
 	}
 	b := table.NewTableBuilder(opts)
+	defer b.Close()
 
 	// Add all keys and versions to the table.
 	for _, item := range td {
@@ -45,7 +46,7 @@ func createAndOpen(db *DB, td []keyValVersion, level int) {
 		b.Add(key, val, 0)
 	}
 	fname := table.NewFilename(db.lc.reserveFileID(), db.opt.Dir)
-	tab, err := table.CreateTable(fname, b.Finish(false), opts)
+	tab, err := table.CreateTable(fname, b)
 	if err != nil {
 		panic(err)
 	}
@@ -54,8 +55,10 @@ func createAndOpen(db *DB, td []keyValVersion, level int) {
 	}); err != nil {
 		panic(err)
 	}
+	db.lc.levels[level].Lock()
 	// Add table to the given level.
 	db.lc.levels[level].tables = append(db.lc.levels[level].tables, tab)
+	db.lc.levels[level].Unlock()
 }
 
 type keyValVersion struct {
@@ -181,8 +184,10 @@ func TestCompaction(t *testing.T) {
 				nextLevel: db.lc.levels[1],
 				top:       db.lc.levels[0].tables,
 				bot:       db.lc.levels[1].tables,
+				t:         db.lc.levelTargets(),
 			}
-			require.NoError(t, db.lc.runCompactDef(0, cdef))
+			cdef.t.baseLevel = 1
+			require.NoError(t, db.lc.runCompactDef(-1, 0, cdef))
 			// foo version 2 should be dropped after compaction.
 			getAllAndCheck(t, db, []keyValVersion{{"foo", "bar", 3, 0}, {"fooz", "baz", 1, 0}})
 		})
@@ -211,8 +216,10 @@ func TestCompaction(t *testing.T) {
 				nextLevel: db.lc.levels[1],
 				top:       db.lc.levels[0].tables,
 				bot:       db.lc.levels[1].tables,
+				t:         db.lc.levelTargets(),
 			}
-			require.NoError(t, db.lc.runCompactDef(0, cdef))
+			cdef.t.baseLevel = 1
+			require.NoError(t, db.lc.runCompactDef(-1, 0, cdef))
 			// foo version 3 (both) should be dropped after compaction.
 			getAllAndCheck(t, db, []keyValVersion{{"foo", "bar", 4, 0}, {"fooz", "baz", 1, 0}})
 		})
@@ -244,8 +251,10 @@ func TestCompaction(t *testing.T) {
 				nextLevel: db.lc.levels[1],
 				top:       db.lc.levels[0].tables,
 				bot:       db.lc.levels[1].tables,
+				t:         db.lc.levelTargets(),
 			}
-			require.NoError(t, db.lc.runCompactDef(0, cdef))
+			cdef.t.baseLevel = 1
+			require.NoError(t, db.lc.runCompactDef(-1, 0, cdef))
 			// foo version 2 and version 1 should be dropped after compaction.
 			getAllAndCheck(t, db, []keyValVersion{
 				{"foo", "bar", 3, 0}, {"foo", "bar", 0, 0}, {"fooz", "baz", 1, 0},
@@ -271,8 +280,10 @@ func TestCompaction(t *testing.T) {
 				nextLevel: db.lc.levels[2],
 				top:       db.lc.levels[1].tables,
 				bot:       db.lc.levels[2].tables,
+				t:         db.lc.levelTargets(),
 			}
-			require.NoError(t, db.lc.runCompactDef(1, cdef))
+			cdef.t.baseLevel = 2
+			require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
 			// foo version 2 should be dropped after compaction.
 			getAllAndCheck(t, db, []keyValVersion{{"foo", "bar", 3, 0}, {"fooz", "baz", 1, 0}})
 		})
@@ -302,8 +313,10 @@ func TestCompaction(t *testing.T) {
 					nextLevel: db.lc.levels[2],
 					top:       db.lc.levels[1].tables,
 					bot:       db.lc.levels[2].tables,
+					t:         db.lc.levelTargets(),
 				}
-				require.NoError(t, db.lc.runCompactDef(1, cdef))
+				cdef.t.baseLevel = 2
+				require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
 				// foo bar version 2 should be dropped after compaction. fooz
 				// baz version 1 will remain because overlap exists, which is
 				// expected because `hasOverlap` is only checked once at the
@@ -320,8 +333,10 @@ func TestCompaction(t *testing.T) {
 					nextLevel: db.lc.levels[3],
 					top:       db.lc.levels[2].tables,
 					bot:       db.lc.levels[3].tables,
+					t:         db.lc.levelTargets(),
 				}
-				require.NoError(t, db.lc.runCompactDef(2, cdef))
+				cdef.t.baseLevel = 3
+				require.NoError(t, db.lc.runCompactDef(-1, 2, cdef))
 				// everything should be removed now
 				getAllAndCheck(t, db, []keyValVersion{})
 			})
@@ -349,8 +364,10 @@ func TestCompaction(t *testing.T) {
 					nextLevel: db.lc.levels[2],
 					top:       db.lc.levels[1].tables,
 					bot:       db.lc.levels[2].tables,
+					t:         db.lc.levelTargets(),
 				}
-				require.NoError(t, db.lc.runCompactDef(1, cdef))
+				cdef.t.baseLevel = 2
+				require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
 				// the top table at L1 doesn't overlap L3, but the bottom table at L2
 				// does, delete keys should not be removed.
 				getAllAndCheck(t, db, []keyValVersion{
@@ -378,12 +395,57 @@ func TestCompaction(t *testing.T) {
 					nextLevel: db.lc.levels[2],
 					top:       db.lc.levels[1].tables,
 					bot:       db.lc.levels[2].tables,
+					t:         db.lc.levelTargets(),
 				}
-				require.NoError(t, db.lc.runCompactDef(1, cdef))
+				cdef.t.baseLevel = 2
+				require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
 				// foo version 2 should be dropped after compaction.
 				getAllAndCheck(t, db, []keyValVersion{{"fooo", "barr", 2, 0}})
 			})
 		})
+		t.Run("with splits", func(t *testing.T) {
+			runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
+				l1 := []keyValVersion{{"C", "bar", 3, bitDelete}}
+				l21 := []keyValVersion{{"A", "bar", 2, 0}}
+				l22 := []keyValVersion{{"B", "bar", 2, 0}}
+				l23 := []keyValVersion{{"C", "bar", 2, 0}}
+				l24 := []keyValVersion{{"D", "bar", 2, 0}}
+				l3 := []keyValVersion{{"fooz", "baz", 1, 0}}
+				createAndOpen(db, l1, 1)
+				createAndOpen(db, l21, 2)
+				createAndOpen(db, l22, 2)
+				createAndOpen(db, l23, 2)
+				createAndOpen(db, l24, 2)
+				createAndOpen(db, l3, 3)
+
+				// Set a high discard timestamp so that all the keys are below the discard timestamp.
+				db.SetDiscardTs(10)
+
+				getAllAndCheck(t, db, []keyValVersion{
+					{"A", "bar", 2, 0},
+					{"B", "bar", 2, 0},
+					{"C", "bar", 3, bitDelete},
+					{"C", "bar", 2, 0},
+					{"D", "bar", 2, 0},
+					{"fooz", "baz", 1, 0},
+				})
+				cdef := compactDef{
+					thisLevel: db.lc.levels[1],
+					nextLevel: db.lc.levels[2],
+					top:       db.lc.levels[1].tables,
+					bot:       db.lc.levels[2].tables,
+					t:         db.lc.levelTargets(),
+				}
+				cdef.t.baseLevel = 2
+				require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
+				getAllAndCheck(t, db, []keyValVersion{
+					{"A", "bar", 2, 0},
+					{"B", "bar", 2, 0},
+					{"D", "bar", 2, 0},
+					{"fooz", "baz", 1, 0},
+				})
+			})
+		})
 	})
 }
 
@@ -414,8 +476,10 @@ func TestCompactionTwoVersions(t *testing.T) {
 				nextLevel: db.lc.levels[2],
 				top:       db.lc.levels[1].tables,
 				bot:       db.lc.levels[2].tables,
+				t:         db.lc.levelTargets(),
 			}
-			require.NoError(t, db.lc.runCompactDef(1, cdef))
+			cdef.t.baseLevel = 2
+			require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
 			// Nothing should be dropped after compaction because number of
 			// versions to keep is 2.
 			getAllAndCheck(t, db, []keyValVersion{
@@ -430,8 +494,10 @@ func TestCompactionTwoVersions(t *testing.T) {
 				nextLevel: db.lc.levels[3],
 				top:       db.lc.levels[2].tables,
 				bot:       db.lc.levels[3].tables,
+				t:         db.lc.levelTargets(),
 			}
-			require.NoError(t, db.lc.runCompactDef(2, cdef))
+			cdef.t.baseLevel = 3
+			require.NoError(t, db.lc.runCompactDef(-1, 2, cdef))
 			getAllAndCheck(t, db, []keyValVersion{
 				{"foo", "bar", 3, 0},
 				{"foo", "bar", 2, 0},
@@ -467,8 +533,10 @@ func TestCompactionAllVersions(t *testing.T) {
 				nextLevel: db.lc.levels[2],
 				top:       db.lc.levels[1].tables,
 				bot:       db.lc.levels[2].tables,
+				t:         db.lc.levelTargets(),
 			}
-			require.NoError(t, db.lc.runCompactDef(1, cdef))
+			cdef.t.baseLevel = 2
+			require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
 			// Nothing should be dropped after compaction because all versions
 			// should be kept.
 			getAllAndCheck(t, db, []keyValVersion{
@@ -483,8 +551,10 @@ func TestCompactionAllVersions(t *testing.T) {
 				nextLevel: db.lc.levels[3],
 				top:       db.lc.levels[2].tables,
 				bot:       db.lc.levels[3].tables,
+				t:         db.lc.levelTargets(),
 			}
-			require.NoError(t, db.lc.runCompactDef(2, cdef))
+			cdef.t.baseLevel = 3
+			require.NoError(t, db.lc.runCompactDef(-1, 2, cdef))
 			getAllAndCheck(t, db, []keyValVersion{
 				{"foo", "bar", 3, 0},
 				{"foo", "bar", 2, 0},
@@ -510,8 +580,10 @@ func TestCompactionAllVersions(t *testing.T) {
 				nextLevel: db.lc.levels[2],
 				top:       db.lc.levels[1].tables,
 				bot:       db.lc.levels[2].tables,
+				t:         db.lc.levelTargets(),
 			}
-			require.NoError(t, db.lc.runCompactDef(1, cdef))
+			cdef.t.baseLevel = 2
+			require.NoError(t, db.lc.runCompactDef(-1, 1, cdef))
 			// foo version 2 should be dropped after compaction.
 			getAllAndCheck(t, db, []keyValVersion{{"fooo", "barr", 2, 0}})
 		})
@@ -546,8 +618,10 @@ func TestDiscardTs(t *testing.T) {
 				nextLevel: db.lc.levels[1],
 				top:       db.lc.levels[0].tables,
 				bot:       db.lc.levels[1].tables,
+				t:         db.lc.levelTargets(),
 			}
-			require.NoError(t, db.lc.runCompactDef(0, cdef))
+			cdef.t.baseLevel = 1
+			require.NoError(t, db.lc.runCompactDef(-1, 0, cdef))
 			// No keys should be dropped.
 			getAllAndCheck(t, db, []keyValVersion{
 				{"foo", "bar", 4, 0}, {"foo", "bar", 3, 0},
@@ -577,8 +651,10 @@ func TestDiscardTs(t *testing.T) {
 				nextLevel: db.lc.levels[1],
 				top:       db.lc.levels[0].tables,
 				bot:       db.lc.levels[1].tables,
+				t:         db.lc.levelTargets(),
 			}
-			require.NoError(t, db.lc.runCompactDef(0, cdef))
+			cdef.t.baseLevel = 1
+			require.NoError(t, db.lc.runCompactDef(-1, 0, cdef))
 			// foo1 and foo2 should be dropped.
 			getAllAndCheck(t, db, []keyValVersion{
 				{"foo", "bar", 4, 0}, {"foo", "bar", 3, 0}, {"fooz", "baz", 2, 0},
@@ -608,8 +684,10 @@ func TestDiscardTs(t *testing.T) {
 				nextLevel: db.lc.levels[1],
 				top:       db.lc.levels[0].tables,
 				bot:       db.lc.levels[1].tables,
+				t:         db.lc.levelTargets(),
 			}
-			require.NoError(t, db.lc.runCompactDef(0, cdef))
+			cdef.t.baseLevel = 1
+			require.NoError(t, db.lc.runCompactDef(-1, 0, cdef))
 			// Only one version of every key should be left.
 			getAllAndCheck(t, db, []keyValVersion{{"foo", "bar", 4, 0}, {"fooz", "baz", 3, 0}})
 		})
@@ -649,8 +727,10 @@ func TestDiscardFirstVersion(t *testing.T) {
 			nextLevel: db.lc.levels[1],
 			top:       db.lc.levels[0].tables,
 			bot:       db.lc.levels[1].tables,
+			t:         db.lc.levelTargets(),
 		}
-		require.NoError(t, db.lc.runCompactDef(0, cdef))
+		cdef.t.baseLevel = 1
+		require.NoError(t, db.lc.runCompactDef(-1, 0, cdef))
 
 		// - Version 10, 9 lie above version 7 so they should be there.
 		// - Version 4, 3, 2 lie below the discardTs but they don't have the
@@ -672,6 +752,8 @@ func TestDiscardFirstVersion(t *testing.T) {
 // This test ensures we don't stall when L1's size is greater than opt.LevelOneSize.
 // We should stall only when L0 tables more than the opt.NumLevelZeroTableStall.
 func TestL1Stall(t *testing.T) {
+	// TODO(ibrahim): Is this test still valid?
+	t.Skip()
 	opt := DefaultOptions("")
 	// Disable all compactions.
 	opt.NumCompactors = 0
@@ -680,7 +762,7 @@ func TestL1Stall(t *testing.T) {
 	// Addition of new tables will stall if there are 4 or more L0 tables.
 	opt.NumLevelZeroTablesStall = 4
 	// Level 1 size is 10 bytes.
-	opt.LevelOneSize = 10
+	opt.BaseLevelSize = 10
 
 	runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
 		// Level 0 has 4 tables.
@@ -724,11 +806,12 @@ func createEmptyTable(db *DB) *table.Table {
 		ChkMode:            options.NoVerification,
 	}
 	b := table.NewTableBuilder(opts)
+	defer b.Close()
 	// Add one key so that we can open this table.
 	b.Add(y.KeyWithTs([]byte("foo"), 1), y.ValueStruct{}, 0)
 
 	// Open table in memory to avoid adding changes to manifest file.
-	tab, err := table.OpenInMemoryTable(b.Finish(true), db.lc.reserveFileID(), &opts)
+	tab, err := table.OpenInMemoryTable(b.Finish(), db.lc.reserveFileID(), &opts)
 	if err != nil {
 		panic(err)
 	}
@@ -737,6 +820,8 @@ func createEmptyTable(db *DB) *table.Table {
 }
 
 func TestL0Stall(t *testing.T) {
+	// TODO(ibrahim): Is this test still valid?
+	t.Skip()
 	opt := DefaultOptions("")
 	// Disable all compactions.
 	opt.NumCompactors = 0
@@ -886,8 +971,7 @@ func TestKeyVersions(t *testing.T) {
 	inMemoryOpt := DefaultOptions("").
 		WithSyncWrites(false).
 		WithInMemory(true).
-		WithLogRotatesToFlush(math.MaxInt32).
-		WithMaxTableSize(4 << 20)
+		WithMemTableSize(4 << 20)
 
 	t.Run("disk", func(t *testing.T) {
 		t.Run("small table", func(t *testing.T) {
diff --git a/managed_db_test.go b/managed_db_test.go
index e7f1b96..dd2d099 100644
--- a/managed_db_test.go
+++ b/managed_db_test.go
@@ -416,13 +416,14 @@ func TestDropPrefix(t *testing.T) {
 	populate(db)
 	require.Equal(t, int(N), numKeys(db))
 	require.NoError(t, db.DropPrefix([]byte("key")))
-	db.Close()
+	require.Equal(t, 0, numKeys(db))
+	require.NoError(t, db.Close())
 
 	// Ensure that value log is correctly replayed.
 	db2, err := Open(opts)
 	require.NoError(t, err)
 	require.Equal(t, 0, numKeys(db2))
-	db2.Close()
+	require.NoError(t, db2.Close())
 }
 
 func TestDropPrefixWithPendingTxn(t *testing.T) {
@@ -605,7 +606,7 @@ func TestWriteBatchManagedMode(t *testing.T) {
 	}
 	opt := DefaultOptions("")
 	opt.managedTxns = true
-	opt.MaxTableSize = 1 << 20 // This would create multiple transactions in write batch.
+	opt.BaseTableSize = 1 << 20 // This would create multiple transactions in write batch.
 	runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
 		wb := db.NewWriteBatchAt(1)
 		defer wb.Cancel()
@@ -651,7 +652,7 @@ func TestWriteBatchManaged(t *testing.T) {
 	}
 	opt := DefaultOptions("")
 	opt.managedTxns = true
-	opt.MaxTableSize = 1 << 15 // This would create multiple transactions in write batch.
+	opt.BaseTableSize = 1 << 15 // This would create multiple transactions in write batch.
 	runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
 		wb := db.NewManagedWriteBatch()
 		defer wb.Cancel()
@@ -720,7 +721,7 @@ func TestWriteBatchDuplicate(t *testing.T) {
 
 	t.Run("writebatch", func(t *testing.T) {
 		opt := DefaultOptions("")
-		opt.MaxTableSize = 1 << 15 // This would create multiple transactions in write batch.
+		opt.BaseTableSize = 1 << 15 // This would create multiple transactions in write batch.
 
 		runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
 			wb := db.NewWriteBatch()
@@ -736,7 +737,7 @@ func TestWriteBatchDuplicate(t *testing.T) {
 	})
 	t.Run("writebatch at", func(t *testing.T) {
 		opt := DefaultOptions("")
-		opt.MaxTableSize = 1 << 15 // This would create multiple transactions in write batch.
+		opt.BaseTableSize = 1 << 15 // This would create multiple transactions in write batch.
 		opt.managedTxns = true
 
 		runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
@@ -755,7 +756,7 @@ func TestWriteBatchDuplicate(t *testing.T) {
 	t.Run("managed writebatch", func(t *testing.T) {
 		opt := DefaultOptions("")
 		opt.managedTxns = true
-		opt.MaxTableSize = 1 << 15 // This would create multiple transactions in write batch.
+		opt.BaseTableSize = 1 << 15 // This would create multiple transactions in write batch.
 		runBadgerTest(t, &opt, func(t *testing.T, db *DB) {
 			wb := db.NewManagedWriteBatch()
 			defer wb.Cancel()
diff --git a/manifest_test.go b/manifest_test.go
index e79bc2e..eca43c5 100644
--- a/manifest_test.go
+++ b/manifest_test.go
@@ -17,6 +17,7 @@
 package badger
 
 import (
+	"context"
 	"fmt"
 	"io/ioutil"
 	"math/rand"
@@ -25,7 +26,7 @@ import (
 	"sort"
 	"testing"
 
-	"golang.org/x/net/trace"
+	otrace "go.opencensus.io/trace"
 
 	"github.com/dgraph-io/badger/v2/options"
 	"github.com/dgraph-io/badger/v2/pb"
@@ -150,7 +151,7 @@ func buildTable(t *testing.T, keyValues [][]string, bopts table.Options) *table.
 		}, 0)
 	}
 
-	tbl, err := table.CreateTable(filename, b.Finish(false), bopts)
+	tbl, err := table.CreateTable(filename, b)
 	require.NoError(t, err)
 	return tbl
 }
@@ -171,20 +172,26 @@ func TestOverlappingKeyRangeError(t *testing.T) {
 
 	done := lh0.tryAddLevel0Table(t1)
 	require.Equal(t, true, done)
-
+	_, span := otrace.StartSpan(context.Background(), "Badger.Compaction")
+	span.Annotatef(nil, "Compaction level: %v", lh0)
 	cd := compactDef{
 		thisLevel: lh0,
 		nextLevel: lh1,
-		elog:      trace.New("Badger", "Compact"),
+		span:      span,
+		t:         kv.lc.levelTargets(),
 	}
+	cd.t.baseLevel = 1
 
 	manifest := createManifest()
 	lc, err := newLevelsController(kv, &manifest)
 	require.NoError(t, err)
 	done = lc.fillTablesL0(&cd)
 	require.Equal(t, true, done)
-	lc.runCompactDef(0, cd)
+	lc.runCompactDef(-1, 0, cd)
+	span.End()
 
+	_, span = otrace.StartSpan(context.Background(), "Badger.Compaction")
+	span.Annotatef(nil, "Compaction level: %v", lh0)
 	t2 := buildTestTable(t, "l", 2, opts)
 	defer t2.DecrRef()
 	done = lh0.tryAddLevel0Table(t2)
@@ -193,10 +200,12 @@ func TestOverlappingKeyRangeError(t *testing.T) {
 	cd = compactDef{
 		thisLevel: lh0,
 		nextLevel: lh1,
-		elog:      trace.New("Badger", "Compact"),
+		span:      span,
+		t:         kv.lc.levelTargets(),
 	}
+	cd.t.baseLevel = 1
 	lc.fillTablesL0(&cd)
-	lc.runCompactDef(0, cd)
+	lc.runCompactDef(-1, 0, cd)
 }
 
 func TestManifestRewrite(t *testing.T) {
diff --git a/merge_test.go b/merge_test.go
index 2b22ea9..38c93c0 100644
--- a/merge_test.go
+++ b/merge_test.go
@@ -115,7 +115,8 @@ func TestGetMergeOperator(t *testing.T) {
 		require.NoError(t, err)
 		defer removeDir(dir)
 
-		opts := getTestOptions(dir)
+		// This test relies on CompactL0OnClose
+		opts := getTestOptions(dir).WithCompactL0OnClose(true)
 		db, err := Open(opts)
 		require.NoError(t, err)
 		mergeKey := []byte("foo")
@@ -164,6 +165,6 @@ func bytesToUint64(b []byte) uint64 {
 }
 
 // Merge function to add two uint64 numbers
-func add(existing, new []byte) []byte {
-	return uint64ToBytes(bytesToUint64(existing) + bytesToUint64(new))
+func add(existing, latest []byte) []byte {
+	return uint64ToBytes(bytesToUint64(existing) + bytesToUint64(latest))
 }
diff --git a/stream_test.go b/stream_test.go
index f3c4b1d..1bc7b97 100644
--- a/stream_test.go
+++ b/stream_test.go
@@ -25,8 +25,11 @@ import (
 	"strings"
 	"testing"
 
+	"github.com/dgraph-io/badger/v2/pb"
 	bpb "github.com/dgraph-io/badger/v2/pb"
 	"github.com/dgraph-io/badger/v2/y"
+	"github.com/dgraph-io/ristretto/z"
+	"github.com/golang/protobuf/proto"
 	"github.com/stretchr/testify/require"
 )
 
@@ -49,9 +52,19 @@ type collector struct {
 	kv []*bpb.KV
 }
 
-func (c *collector) Send(list *bpb.KVList) error {
-	c.kv = append(c.kv, list.Kv...)
-	return nil
+func (c *collector) Send(buf *z.Buffer) error {
+	list, err := BufferToKVList(buf)
+	if err != nil {
+		return err
+	}
+	for _, kv := range list.Kv {
+		if kv.StreamDone == true {
+			return nil
+		}
+		cp := proto.Clone(kv).(*bpb.KV)
+		c.kv = append(c.kv, cp)
+	}
+	return err
 }
 
 var ctxb = context.Background()
@@ -205,6 +218,10 @@ func TestStreamWithThreadId(t *testing.T) {
 }
 
 func TestBigStream(t *testing.T) {
+	if !*manual {
+		t.Skip("Skipping test meant to be run manually.")
+		return
+	}
 	// Set the maxStreamSize to 1MB for the duration of the test so that the it can use a smaller
 	// dataset than it would otherwise need.
 	originalMaxStreamSize := maxStreamSize
@@ -254,3 +271,57 @@ func TestBigStream(t *testing.T) {
 	}
 	require.NoError(t, db.Close())
 }
+
+// There was a bug in the stream writer code which would cause allocators to be
+// freed up twice if the default keyToList was not used. This test verifies that issue.
+func TestStreamCustomKeyToList(t *testing.T) {
+	dir, err := ioutil.TempDir("", "badger-test")
+	require.NoError(t, err)
+	defer removeDir(dir)
+
+	db, err := OpenManaged(DefaultOptions(dir))
+	require.NoError(t, err)
+
+	var count int
+	for _, key := range []string{"p0", "p1", "p2"} {
+		for i := 1; i <= 100; i++ {
+			txn := db.NewTransactionAt(math.MaxUint64, true)
+			require.NoError(t, txn.SetEntry(NewEntry([]byte(key), value(i))))
+			count++
+			require.NoError(t, txn.CommitAt(uint64(i), nil))
+		}
+	}
+
+	stream := db.NewStreamAt(math.MaxUint64)
+	stream.LogPrefix = "Testing"
+	stream.KeyToList = func(key []byte, itr *Iterator) (*pb.KVList, error) {
+		item := itr.Item()
+		val, err := item.ValueCopy(nil)
+		if err != nil {
+			return nil, err
+		}
+		kv := itr.NewKV()
+		*kv = pb.KV{
+			Key:   y.Copy(item.Key()),
+			Value: val,
+		}
+		return &pb.KVList{
+			Kv: []*pb.KV{kv},
+		}, nil
+	}
+	res := map[string]struct{}{"p0": {}, "p1": {}, "p2": {}}
+	stream.Send = func(buf *z.Buffer) error {
+		list, err := BufferToKVList(buf)
+		require.NoError(t, err)
+		for _, kv := range list.Kv {
+			key := string(kv.Key)
+			if _, ok := res[key]; !ok {
+				panic(fmt.Sprintf("%s key not found", key))
+			}
+			delete(res, key)
+		}
+		return nil
+	}
+	require.NoError(t, stream.Orchestrate(ctxb))
+	require.Zero(t, len(res))
+}
diff --git a/stream_writer_test.go b/stream_writer_test.go
index 001aa84..02859ee 100644
--- a/stream_writer_test.go
+++ b/stream_writer_test.go
@@ -30,23 +30,24 @@ import (
 
 	"github.com/dgraph-io/badger/v2/pb"
 	"github.com/dgraph-io/badger/v2/y"
+	"github.com/dgraph-io/ristretto/z"
 )
 
-func getSortedKVList(valueSize, listSize int) *pb.KVList {
+func getSortedKVList(valueSize, listSize int) *z.Buffer {
 	value := make([]byte, valueSize)
 	y.Check2(rand.Read(value))
-	list := &pb.KVList{}
+	buf := z.NewBuffer(10 << 20)
 	for i := 0; i < listSize; i++ {
 		key := make([]byte, 8)
 		binary.BigEndian.PutUint64(key, uint64(i))
-		list.Kv = append(list.Kv, &pb.KV{
+		KVToBuffer(&pb.KV{
 			Key:     key,
 			Value:   value,
 			Version: 20,
-		})
+		}, buf)
 	}
 
-	return list
+	return buf
 }
 
 // check if we can read values after writing using stream writer
@@ -54,7 +55,7 @@ func TestStreamWriter1(t *testing.T) {
 	test := func(t *testing.T, opts *Options) {
 		runBadgerTest(t, opts, func(t *testing.T, db *DB) {
 			// write entries using stream writer
-			noOfKeys := 1000
+			noOfKeys := 10
 			valueSize := 128
 			list := getSortedKVList(valueSize, noOfKeys)
 			sw := db.NewStreamWriter()
@@ -173,22 +174,23 @@ func TestStreamWriter3(t *testing.T) {
 			// insert keys which are even
 			value := make([]byte, valueSize)
 			y.Check2(rand.Read(value))
-			list := &pb.KVList{}
 			counter := 0
+			buf := z.NewBuffer(10 << 20)
+			defer buf.Release()
 			for i := 0; i < noOfKeys; i++ {
 				key := make([]byte, 8)
 				binary.BigEndian.PutUint64(key, uint64(counter))
-				list.Kv = append(list.Kv, &pb.KV{
+				KVToBuffer(&pb.KV{
 					Key:     key,
 					Value:   value,
 					Version: 20,
-				})
+				}, buf)
 				counter = counter + 2
 			}
 
 			sw := db.NewStreamWriter()
 			require.NoError(t, sw.Prepare(), "sw.Prepare() failed")
-			require.NoError(t, sw.Write(list), "sw.Write() failed")
+			require.NoError(t, sw.Write(buf), "sw.Write() failed")
 			// get max version of sw, will be used in transactions for managed mode
 			maxVs := sw.maxVersion
 			require.NoError(t, sw.Flush(), "sw.Flush() failed")
@@ -270,24 +272,23 @@ func TestStreamWriter4(t *testing.T) {
 			require.NoError(t, err, "error while updating db")
 		}
 
-		list := &pb.KVList{}
-		list.Kv = append(list.Kv, &pb.KV{
+		buf := z.NewBuffer(10 << 20)
+		defer buf.Release()
+		KVToBuffer(&pb.KV{
 			Key:     []byte("key-1"),
 			Value:   []byte("value-1"),
 			Version: 1,
-		})
+		}, buf)
 
 		sw := db.NewStreamWriter()
 		require.NoError(t, sw.Prepare(), "sw.Prepare() failed")
-		require.NoError(t, sw.Write(list), "sw.Write() failed")
+		require.NoError(t, sw.Write(buf), "sw.Write() failed")
 		require.NoError(t, sw.Flush(), "sw.Flush() failed")
 	})
 }
 
 func TestStreamWriter5(t *testing.T) {
 	runBadgerTest(t, nil, func(t *testing.T, db *DB) {
-		list := &pb.KVList{}
-
 		left := make([]byte, 6)
 		left[0] = 0x00
 		copy(left[1:], []byte("break"))
@@ -296,20 +297,22 @@ func TestStreamWriter5(t *testing.T) {
 		right[0] = 0xff
 		copy(right[1:], []byte("break"))
 
-		list.Kv = append(list.Kv, &pb.KV{
+		buf := z.NewBuffer(10 << 20)
+		defer buf.Release()
+		KVToBuffer(&pb.KV{
 			Key:     left,
 			Value:   []byte("val"),
 			Version: 1,
-		})
-		list.Kv = append(list.Kv, &pb.KV{
+		}, buf)
+		KVToBuffer(&pb.KV{
 			Key:     right,
 			Value:   []byte("val"),
 			Version: 1,
-		})
+		}, buf)
 
 		sw := db.NewStreamWriter()
 		require.NoError(t, sw.Prepare(), "sw.Prepare() failed")
-		require.NoError(t, sw.Write(list), "sw.Write() failed")
+		require.NoError(t, sw.Write(buf), "sw.Write() failed")
 		require.NoError(t, sw.Flush(), "sw.Flush() failed")
 		require.NoError(t, db.Close())
 
@@ -323,36 +326,41 @@ func TestStreamWriter5(t *testing.T) {
 // This test tries to insert multiple equal keys(without version) and verifies
 // if those are going to same table.
 func TestStreamWriter6(t *testing.T) {
+
 	runBadgerTest(t, nil, func(t *testing.T, db *DB) {
-		list := &pb.KVList{}
-		str := []string{"a", "a", "b", "b", "c", "c"}
-		ver := 1
+		str := []string{"a", "b", "c"}
+		ver := uint64(0)
+		// The baseTable size is 32 KB (1<<15) and the max table size for level
+		// 6 table is 1 mb (look at newWrite function). Since all the tables
+		// will be written to level 6, we need to insert at least 1 mb of data.
+		// Setting keycount below 32 would cause this test to fail.
+		keyCount := 40
+		buf := z.NewBuffer(10 << 20)
+		defer buf.Release()
 		for i := range str {
-			kv := &pb.KV{
-				Key:     bytes.Repeat([]byte(str[i]), int(db.opt.MaxTableSize)),
-				Value:   []byte("val"),
-				Version: uint64(ver),
+			for j := 0; j < keyCount; j++ {
+				ver++
+				kv := &pb.KV{
+					Key:     bytes.Repeat([]byte(str[i]), int(db.opt.BaseTableSize)),
+					Value:   []byte("val"),
+					Version: uint64(keyCount - j),
+				}
+				KVToBuffer(kv, buf)
 			}
-			list.Kv = append(list.Kv, kv)
-			ver = (ver + 1) % 2
 		}
 
 		// list has 3 pairs for equal keys. Since each Key has size equal to MaxTableSize
 		// we would have 6 tables, if keys are not equal. Here we should have 3 tables.
 		sw := db.NewStreamWriter()
 		require.NoError(t, sw.Prepare(), "sw.Prepare() failed")
-		require.NoError(t, sw.Write(list), "sw.Write() failed")
+		require.NoError(t, sw.Write(buf), "sw.Write() failed")
 		require.NoError(t, sw.Flush(), "sw.Flush() failed")
 
 		tables := db.Tables()
 		require.Equal(t, 3, len(tables), "Count of tables not matching")
 		for _, tab := range tables {
-			if tab.Level > 0 {
-				require.Equal(t, 2, int(tab.KeyCount),
-					fmt.Sprintf("failed for level: %d", tab.Level))
-			} else {
-				require.Equal(t, 1, int(tab.KeyCount)) // level 0 table will have head key
-			}
+			require.Equal(t, keyCount, int(tab.KeyCount),
+				fmt.Sprintf("failed for level: %d", tab.Level))
 		}
 		require.NoError(t, db.Close())
 		db, err := Open(db.opt)
@@ -361,6 +369,37 @@ func TestStreamWriter6(t *testing.T) {
 	})
 }
 
+// This test uses a StreamWriter without calling Flush() at the end.
+func TestStreamWriterCancel(t *testing.T) {
+	runBadgerTest(t, nil, func(t *testing.T, db *DB) {
+		str := []string{"a", "a", "b", "b", "c", "c"}
+		ver := 1
+		buf := z.NewBuffer(10 << 20)
+		defer buf.Release()
+		for i := range str {
+			kv := &pb.KV{
+				Key:     bytes.Repeat([]byte(str[i]), int(db.opt.BaseTableSize)),
+				Value:   []byte("val"),
+				Version: uint64(ver),
+			}
+			KVToBuffer(kv, buf)
+			ver = (ver + 1) % 2
+		}
+
+		sw := db.NewStreamWriter()
+		require.NoError(t, sw.Prepare(), "sw.Prepare() failed")
+		require.NoError(t, sw.Write(buf), "sw.Write() failed")
+		sw.Cancel()
+
+		// Use the API incorrectly.
+		sw1 := db.NewStreamWriter()
+		defer sw1.Cancel()
+		require.NoError(t, sw1.Prepare())
+		defer sw1.Cancel()
+		sw1.Flush()
+	})
+}
+
 func TestStreamDone(t *testing.T) {
 	runBadgerTest(t, nil, func(t *testing.T, db *DB) {
 		sw := db.NewStreamWriter()
@@ -369,7 +408,8 @@ func TestStreamDone(t *testing.T) {
 		var val [10]byte
 		rand.Read(val[:])
 		for i := 0; i < 10; i++ {
-			list := &pb.KVList{}
+			buf := z.NewBuffer(10 << 20)
+			defer buf.Release()
 			kv1 := &pb.KV{
 				Key:      []byte(fmt.Sprintf("%d", i)),
 				Value:    val[:],
@@ -380,8 +420,9 @@ func TestStreamDone(t *testing.T) {
 				StreamId:   uint32(i),
 				StreamDone: true,
 			}
-			list.Kv = append(list.Kv, kv1, kv2)
-			require.NoError(t, sw.Write(list), "sw.Write() failed")
+			KVToBuffer(kv1, buf)
+			KVToBuffer(kv2, buf)
+			require.NoError(t, sw.Write(buf), "sw.Write() failed")
 		}
 		require.NoError(t, sw.Flush(), "sw.Flush() failed")
 		require.NoError(t, db.Close())
@@ -408,7 +449,8 @@ func TestSendOnClosedStream(t *testing.T) {
 
 	var val [10]byte
 	rand.Read(val[:])
-	list := &pb.KVList{}
+	buf := z.NewBuffer(10 << 20)
+	defer buf.Release()
 	kv1 := &pb.KV{
 		Key:      []byte(fmt.Sprintf("%d", 1)),
 		Value:    val[:],
@@ -419,8 +461,9 @@ func TestSendOnClosedStream(t *testing.T) {
 		StreamId:   uint32(1),
 		StreamDone: true,
 	}
-	list.Kv = append(list.Kv, kv1, kv2)
-	require.NoError(t, sw.Write(list), "sw.Write() failed")
+	KVToBuffer(kv1, buf)
+	KVToBuffer(kv2, buf)
+	require.NoError(t, sw.Write(buf), "sw.Write() failed")
 
 	// Defer for panic.
 	defer func() {
@@ -429,15 +472,16 @@ func TestSendOnClosedStream(t *testing.T) {
 		require.NoError(t, db.Close())
 	}()
 	// Send once stream is closed.
-	list = &pb.KVList{}
+	buf1 := z.NewBuffer(10 << 20)
+	defer buf1.Release()
 	kv1 = &pb.KV{
 		Key:      []byte(fmt.Sprintf("%d", 2)),
 		Value:    val[:],
 		Version:  1,
 		StreamId: uint32(1),
 	}
-	list.Kv = append(list.Kv, kv1)
-	sw.Write(list)
+	KVToBuffer(kv1, buf1)
+	sw.Write(buf1)
 }
 
 func TestSendOnClosedStream2(t *testing.T) {
@@ -455,7 +499,8 @@ func TestSendOnClosedStream2(t *testing.T) {
 
 	var val [10]byte
 	rand.Read(val[:])
-	list := &pb.KVList{}
+	buf := z.NewBuffer(10 << 20)
+	defer buf.Release()
 	kv1 := &pb.KV{
 		Key:      []byte(fmt.Sprintf("%d", 1)),
 		Value:    val[:],
@@ -472,7 +517,9 @@ func TestSendOnClosedStream2(t *testing.T) {
 		Version:  1,
 		StreamId: uint32(1),
 	}
-	list.Kv = append(list.Kv, kv1, kv2, kv3)
+	KVToBuffer(kv1, buf)
+	KVToBuffer(kv2, buf)
+	KVToBuffer(kv3, buf)
 
 	// Defer for panic.
 	defer func() {
@@ -481,7 +528,7 @@ func TestSendOnClosedStream2(t *testing.T) {
 		require.NoError(t, db.Close())
 	}()
 
-	require.NoError(t, sw.Write(list), "sw.Write() failed")
+	require.NoError(t, sw.Write(buf), "sw.Write() failed")
 }
 
 func TestStreamWriterEncrypted(t *testing.T) {
@@ -499,16 +546,17 @@ func TestStreamWriterEncrypted(t *testing.T) {
 	key := []byte("mykey")
 	value := []byte("myvalue")
 
-	list := &pb.KVList{}
-	list.Kv = append(list.Kv, &pb.KV{
+	buf := z.NewBuffer(10 << 20)
+	defer buf.Release()
+	KVToBuffer(&pb.KV{
 		Key:     key,
 		Value:   value,
 		Version: 20,
-	})
+	}, buf)
 
 	sw := db.NewStreamWriter()
 	require.NoError(t, sw.Prepare(), "Prepare failed")
-	require.NoError(t, sw.Write(list), "Write failed")
+	require.NoError(t, sw.Write(buf), "Write failed")
 	require.NoError(t, sw.Flush(), "Flush failed")
 
 	err = db.View(func(txn *Txn) error {
diff --git a/table/builder_test.go b/table/builder_test.go
index df551c6..00ad479 100644
--- a/table/builder_test.go
+++ b/table/builder_test.go
@@ -97,6 +97,7 @@ func TestTableIndex(t *testing.T) {
 		t.Run(tt.name, func(t *testing.T) {
 			opt := tt.opts
 			builder := NewTableBuilder(opt)
+			defer builder.Close()
 			filename := fmt.Sprintf("%s%c%d.sst", os.TempDir(), os.PathSeparator, rand.Uint32())
 
 			blockFirstKeys := make([][]byte, 0)
@@ -114,7 +115,7 @@ func TestTableIndex(t *testing.T) {
 				}
 				builder.Add(k, vs, 0)
 			}
-			tbl, err := CreateTable(filename, builder.Finish(false), opt)
+			tbl, err := CreateTable(filename, builder)
 			require.NoError(t, err, "unable to open table")
 
 			if opt.DataKey == nil {
@@ -182,7 +183,8 @@ func BenchmarkBuilder(b *testing.B) {
 			for j := 0; j < keysCount; j++ {
 				builder.Add(keyList[j], vs, 0)
 			}
-			_ = builder.Finish(false)
+			_ = builder.Finish()
+			builder.Close()
 		}
 	}
 
@@ -271,6 +273,7 @@ func TestBloomfilter(t *testing.T) {
 func TestEmptyBuilder(t *testing.T) {
 	opts := Options{BloomFalsePositive: 0.1}
 	b := NewTableBuilder(opts)
-	require.Nil(t, b.Finish(false))
+	defer b.Close()
+	require.Equal(t, []byte{}, b.Finish())
 
 }
diff --git a/table/table_test.go b/table/table_test.go
index f138f19..b4d8a07 100644
--- a/table/table_test.go
+++ b/table/table_test.go
@@ -79,7 +79,7 @@ func buildTable(t *testing.T, keyValues [][]string, opts Options) *Table {
 		b.Add(y.KeyWithTs([]byte(kv[0]), 0),
 			y.ValueStruct{Value: []byte(kv[1]), Meta: 'A', UserMeta: 0}, 0)
 	}
-	tbl, err := CreateTable(filename, b.Finish(false), opts)
+	tbl, err := CreateTable(filename, b)
 	require.NoError(t, err, "writing to file failed")
 	return tbl
 }
@@ -645,6 +645,7 @@ func TestTableBigValues(t *testing.T) {
 	opts := Options{Compression: options.ZSTD, BlockSize: 4 * 1024, BloomFalsePositive: 0.01,
 		TableSize: uint64(n) * 1 << 20}
 	builder := NewTableBuilder(opts)
+	defer builder.Close()
 	for i := 0; i < n; i++ {
 		key := y.KeyWithTs([]byte(key("", i)), uint64(i+1))
 		vs := y.ValueStruct{Value: value(i)}
@@ -652,7 +653,7 @@ func TestTableBigValues(t *testing.T) {
 	}
 
 	filename := fmt.Sprintf("%s%s%d.sst", os.TempDir(), string(os.PathSeparator), rand.Int63())
-	tbl, err := CreateTable(filename, builder.Finish(false), opts)
+	tbl, err := CreateTable(filename, builder)
 	require.NoError(t, err, "unable to open table")
 	defer tbl.DecrRef()
 
@@ -737,7 +738,7 @@ func BenchmarkReadAndBuild(b *testing.B) {
 				vs := it.Value()
 				newBuilder.Add(it.Key(), vs, 0)
 			}
-			newBuilder.Finish(false)
+			newBuilder.Finish()
 		}()
 	}
 }
@@ -764,8 +765,9 @@ func BenchmarkReadMerged(b *testing.B) {
 			v := fmt.Sprintf("%d", id)
 			builder.Add([]byte(k), y.ValueStruct{Value: []byte(v), Meta: 123, UserMeta: 0}, 0)
 		}
-		tbl, err := CreateTable(filename, builder.Finish(false), opts)
+		tbl, err := CreateTable(filename, builder)
 		y.Check(err)
+		builder.Close()
 		tables = append(tables, tbl)
 		defer tbl.DecrRef()
 	}
@@ -845,6 +847,7 @@ func getTableForBenchmarks(b *testing.B, count int, cache *ristretto.Cache) *Tab
 	}
 	opts.BlockCache = cache
 	builder := NewTableBuilder(opts)
+	defer builder.Close()
 	filename := fmt.Sprintf("%s%s%d.sst", os.TempDir(), string(os.PathSeparator), rand.Int63())
 	for i := 0; i < count; i++ {
 		k := fmt.Sprintf("%016x", i)
@@ -852,7 +855,7 @@ func getTableForBenchmarks(b *testing.B, count int, cache *ristretto.Cache) *Tab
 		builder.Add([]byte(k), y.ValueStruct{Value: []byte(v)}, 0)
 	}
 
-	tbl, err := CreateTable(filename, builder.Finish(false), opts)
+	tbl, err := CreateTable(filename, builder)
 	require.NoError(b, err, "unable to open table")
 	return tbl
 }
@@ -862,31 +865,6 @@ func TestMain(m *testing.M) {
 	os.Exit(m.Run())
 }
 
-func TestOpenKVSize(t *testing.T) {
-	t.Run("compression", func(t *testing.T) {
-		// When compression is on
-		opts := getTestTableOptions()
-		opts.Compression = options.ZSTD
-		table := buildTestTable(t, "foo", 1000, opts)
-		defer table.DecrRef()
-
-		// The estimated size is same as table size in case compression is enabled.
-		require.Equal(t, uint32(table.tableSize), table.EstimatedSize())
-	})
-
-	t.Run("no compressin", func(t *testing.T) {
-		// When compression is off
-		opts := getTestTableOptions()
-		opts.Compression = options.None
-		table := buildTestTable(t, "foo", 1, opts)
-		defer table.DecrRef()
-
-		stat, err := table.Fd.Stat()
-		require.NoError(t, err)
-		require.Less(t, table.EstimatedSize(), uint32(stat.Size()))
-	})
-}
-
 // Run this test with command "go test -race -run TestDoesNotHaveRace"
 func TestDoesNotHaveRace(t *testing.T) {
 	opts := getTestTableOptions()
@@ -914,7 +892,7 @@ func TestMaxVersion(t *testing.T) {
 	for i := 0; i < N; i++ {
 		b.Add(y.KeyWithTs([]byte(fmt.Sprintf("foo:%d", i)), uint64(i+1)), y.ValueStruct{}, 0)
 	}
-	table, err := CreateTable(filename, b.Finish(false), opt)
+	table, err := CreateTable(filename, b)
 	require.NoError(t, err)
 	require.Equal(t, N, int(table.MaxVersion()))
 }
diff --git a/test.sh b/test.sh
index 23bb660..e4f688a 100755
--- a/test.sh
+++ b/test.sh
@@ -1,39 +1,120 @@
 #!/bin/bash
 
-set -e
+set -eo pipefail
 
 go version
 
-packages=$(go list ./... | grep github.com/dgraph-io/badger/v2/)
+# Run `go list` BEFORE setting GOFLAGS so that the output is in the right
+# format for grep.
+# export packages because the test will run in a sub process.
+export packages=$(go list ./... | grep "github.com/dgraph-io/badger/v2/")
 
 if [[ ! -z "$TEAMCITY_VERSION" ]]; then
   export GOFLAGS="-json"
 fi
 
+function InstallJemalloc() {
+  pushd .
+  if [ ! -f /usr/local/lib/libjemalloc.a ]; then
+		USER_ID=`id -u`
+    	JEMALLOC_URL="https://github.com/jemalloc/jemalloc/releases/download/5.2.1/jemalloc-5.2.1.tar.bz2"
+
+		mkdir -p /tmp/jemalloc-temp && cd /tmp/jemalloc-temp ;
+		echo "Downloading jemalloc" ;
+		curl -s -L ${JEMALLOC_URL} -o jemalloc.tar.bz2 ;
+		tar xjf ./jemalloc.tar.bz2 ;
+		cd jemalloc-5.2.1 ;
+		./configure --with-jemalloc-prefix='je_' ;
+		make ;
+		if [ "$USER_ID" -eq "0" ]; then
+			make install ;
+		else
+			echo "==== Need sudo access to install jemalloc" ;
+			sudo make install ;
+		fi
+	fi
+  popd
+}
+
+tags="-tags=jemalloc"
+
 # Ensure that we can compile the binary.
 pushd badger
-go build -v .
+go build -v $tags .
 popd
 
+# tags=""
+InstallJemalloc
+
 # Run the memory intensive tests first.
-go test -v -run='TestBigKeyValuePairs$' --manual=true
-go test -v -run='TestPushValueLogLimit' --manual=true
-
-# Run the special Truncate test.
-rm -rf p
-go test -v -run='TestTruncateVlogNoClose$' --manual=true
-truncate --size=4096 p/000000.vlog
-go test -v -run='TestTruncateVlogNoClose2$' --manual=true
-go test -v -run='TestTruncateVlogNoClose3$' --manual=true
-rm -rf p
-
-# Run the normal tests.
-echo "==> Starting tests.. "
-# go test -timeout=25m -v -race github.com/dgraph-io/badger/v2/...
-for pkg in $packages; do
-  echo "===> Testing $pkg"
-  go test -timeout=25m -v -race $pkg
-done
-
-echo "===> Testing root level"
-go test -timeout=25m -v . -race
+manual() {
+  timeout="-timeout 2m"
+  echo "==> Running package tests for $packages"
+  set -e
+  for pkg in $packages; do
+    echo "===> Testing $pkg"
+    go test $tags -timeout=25m -race $pkg -parallel 16
+  done
+  echo "==> DONE package tests"
+
+  echo "==> Running manual tests"
+  # Run the special Truncate test.
+  rm -rf p
+  set -e
+  go test $tags $timeout -run='TestTruncateVlogNoClose$' --manual=true
+  truncate --size=4096 p/000000.vlog
+  go test $tags $timeout -run='TestTruncateVlogNoClose2$' --manual=true
+  go test $tags $timeout -run='TestTruncateVlogNoClose3$' --manual=true
+  rm -rf p
+
+  # TODO(ibrahim): Let's make these tests have Manual prefix.
+  # go test $tags -run='TestManual' --manual=true --parallel=2
+  # TestWriteBatch
+  # TestValueGCManaged
+  # TestDropPrefix
+  # TestDropAllManaged
+  go test $tags $timeout -run='TestBigKeyValuePairs$' --manual=true
+  go test $tags $timeout -run='TestPushValueLogLimit' --manual=true
+  go test $tags $timeout -run='TestKeyCount' --manual=true
+  go test $tags $timeout -run='TestIteratePrefix' --manual=true
+  go test $tags $timeout -run='TestIterateParallel' --manual=true
+  go test $tags $timeout -run='TestBigStream' --manual=true
+  go test $tags $timeout -run='TestGoroutineLeak' --manual=true
+  go test $tags $timeout -run='TestGetMore' --manual=true
+
+  echo "==> DONE manual tests"
+}
+
+root() {
+  # Run the normal tests.
+  # go test -timeout=25m -v -race github.com/dgraph-io/badger/v2/...
+
+  echo "==> Running root level tests."
+  set -e
+  go test $tags -timeout=25m . -v -race -parallel 16
+  echo "==> DONE root level tests"
+}
+
+stream() {
+  set -eo pipefail
+  pushd badger
+  baseDir=$(mktemp -d -p .)
+  ./badger benchmark write -s --dir=$baseDir/test | tee $baseDir/log.txt
+  ./badger benchmark read --dir=$baseDir/test --full-scan | tee --append $baseDir/log.txt
+  ./badger benchmark read --dir=$baseDir/test -d=30s | tee --append $baseDir/log.txt
+  ./badger stream --dir=$baseDir/test -o "$baseDir/test2" | tee --append $baseDir/log.txt
+  count=$(cat "$baseDir/log.txt" | grep "at program end: 0 B" | wc -l)
+  rm -rf $baseDir
+  if [ $count -ne 4 ]; then
+    echo "LEAK detected in Badger stream."
+    return 1
+  fi
+  echo "==> DONE stream test"
+  return 0
+}
+
+export -f stream
+export -f manual
+export -f root
+
+parallel --halt now,fail=1 --progress --line-buffer ::: stream manual root
diff --git a/txn_test.go b/txn_test.go
index 46a2915..ffa8c1f 100644
--- a/txn_test.go
+++ b/txn_test.go
@@ -844,8 +844,8 @@ func TestArmV7Issue311Fix(t *testing.T) {
 
 	db, err := Open(DefaultOptions(dir).
 		WithValueLogFileSize(16 << 20).
-		WithLevelOneSize(8 << 20).
-		WithMaxTableSize(2 << 20).
+		WithBaseLevelSize(8 << 20).
+		WithBaseTableSize(2 << 20).
 		WithSyncWrites(false))
 
 	require.NoError(t, err)
diff --git a/value_test.go b/value_test.go
index c351da4..4b85056 100644
--- a/value_test.go
+++ b/value_test.go
@@ -432,8 +432,10 @@ func TestPersistLFDiscardStats(t *testing.T) {
 	require.NoError(t, err)
 	defer removeDir(dir)
 	opt := getTestOptions(dir)
+	// Force more compaction by reducing the number of L0 tables.
+	opt.NumLevelZeroTables = 1
 	opt.ValueLogFileSize = 1 << 20
-	// avoid compaction on close, so that discard map remains same
+	// Avoid compaction on close so that the discard map remains the same.
 	opt.CompactL0OnClose = false
 
 	db, err := Open(opt)
@@ -661,7 +663,7 @@ func TestReadOnlyOpenWithPartialAppendToWAL(t *testing.T) {
 		{Key: k2, Value: v2},
 	})
 	buf = buf[:offset-6]
-	require.NoError(t, ioutil.WriteFile(vlogFilePath(dir, 1), buf, 0777))
+	require.NoError(t, ioutil.WriteFile(kv.mtFilePath(1), buf, 0777))
 
 	opts.ReadOnly = true
 	// Badger should fail a read-only open with values to replay
@@ -868,7 +870,7 @@ func TestBug578(t *testing.T) {
 
 	db, err := Open(DefaultOptions(dir).
 		WithValueLogMaxEntries(64).
-		WithMaxTableSize(1 << 13))
+		WithBaseTableSize(1 << 13))
 	require.NoError(t, err)
 
 	h := testHelper{db: db, t: t}
@@ -957,6 +959,7 @@ func BenchmarkReadWrite(b *testing.B) {
 }
 
 // Regression test for https://github.com/dgraph-io/badger/issues/817
+// This test verifies if fully corrupted memtables are deleted on reopen.
 func TestValueLogTruncate(t *testing.T) {
 	dir, err := ioutil.TempDir("", "badger-test")
 	require.NoError(t, err)
@@ -987,7 +990,7 @@ func TestValueLogTruncate(t *testing.T) {
 	require.Equal(t, 4, int(db.mt.wal.fid))
 	fileStat, err := db.mt.wal.Fd.Stat()
 	require.NoError(t, err)
-	require.Equal(t, 2*db.opt.ValueLogFileSize, fileStat.Size())
+	require.Equal(t, 2*db.opt.MemTableSize, fileStat.Size())
 
 	fileCountAfterCorruption := len(db.Tables()) + len(db.imm) + 1 // +1 for db.mt
 	// We should have one memtable and one sst file.
@@ -1081,10 +1084,14 @@ func TestValueEntryChecksum(t *testing.T) {
 		entry, err := txn.Get(k)
 		require.NoError(t, err)
 
-		x, err := entry.ValueCopy(nil)
-		require.Error(t, err)
-		require.Contains(t, err.Error(), "ErrEOF")
-		require.Nil(t, x)
+		// TODO(ibrahim): This test is broken since we're not returning errors
+		// in case we cannot read the values. This is incorrect behavior but
+		// we're doing this to debug an issue where the values are being read
+		// from old vlog files.
+		_, _ = entry.ValueCopy(nil)
+		// require.Error(t, err)
+		// require.Contains(t, err.Error(), "ErrEOF")
+		// require.Nil(t, x)
 
 		require.NoError(t, db.Close())
 	})
@@ -1150,3 +1157,60 @@ func TestValidateWrite(t *testing.T) {
 	err = log.validateWrites([]*request{req1, req})
 	require.Error(t, err)
 }
+
+func TestValueLogMeta(t *testing.T) {
+	dir, err := ioutil.TempDir("", "badger-test")
+	y.Check(err)
+	defer removeDir(dir)
+
+	opt := getTestOptions(dir).WithValueThreshold(16)
+	db, _ := Open(opt)
+	defer db.Close()
+	txn := db.NewTransaction(true)
+	for i := 0; i < 10; i++ {
+		k := []byte(fmt.Sprintf("key=%d", i))
+		v := []byte(fmt.Sprintf("val=%020d", i))
+		require.NoError(t, txn.SetEntry(NewEntry(k, v)))
+	}
+	require.NoError(t, txn.Commit())
+	fids := db.vlog.sortedFids()
+	require.Equal(t, 1, len(fids))
+
+	// vlog entries must not have txn meta.
+	db.vlog.filesMap[fids[0]].iterate(true, 0, func(e Entry, vp valuePointer) error {
+		require.Zero(t, e.meta&(bitTxn|bitFinTxn))
+		return nil
+	})
+
+	// Entries in LSM tree must have txn bit of meta set
+	txn = db.NewTransaction(false)
+	defer txn.Discard()
+	iopt := DefaultIteratorOptions
+	key := []byte("key")
+	iopt.Prefix = key
+	itr := txn.NewIterator(iopt)
+	defer itr.Close()
+	var count int
+	for itr.Seek(key); itr.ValidForPrefix(key); itr.Next() {
+		item := itr.Item()
+		require.Equal(t, bitTxn, item.meta&(bitTxn|bitFinTxn))
+		count++
+	}
+	require.Equal(t, 10, count)
+}
+
+// This tests asserts the condition that vlog fids start from 1.
+// TODO(naman): should this be changed to assert instead?
+func TestFirstVlogFile(t *testing.T) {
+	dir, err := ioutil.TempDir("", "badger-test")
+	require.NoError(t, err)
+	defer removeDir(dir)
+
+	opt := DefaultOptions(dir)
+	db, err := Open(opt)
+	defer db.Close()
+
+	fids := db.vlog.sortedFids()
+	require.NotZero(t, len(fids))
+	require.Equal(t, uint32(1), fids[0])
+}
diff --git a/y/encrypt_test.go b/y/encrypt_test.go
new file mode 100644
index 0000000..91d50ac
--- /dev/null
+++ b/y/encrypt_test.go
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2020 Dgraph Labs, Inc. and Contributors
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package y
+
+import (
+	"crypto/aes"
+	"crypto/rand"
+	"testing"
+
+	"github.com/stretchr/testify/require"
+)
+
+func TestXORBlock(t *testing.T) {
+	key := make([]byte, 32)
+	rand.Read(key)
+
+	var iv []byte
+	{
+		b, err := aes.NewCipher(key)
+		require.NoError(t, err)
+		iv = make([]byte, b.BlockSize())
+		rand.Read(iv)
+		t.Logf("Using %d size IV\n", len(iv))
+	}
+
+	src := make([]byte, 1024)
+	rand.Read(src)
+
+	dst := make([]byte, 1024)
+	err := XORBlock(dst, src, key, iv)
+	require.NoError(t, err)
+
+	act := make([]byte, 1024)
+	err = XORBlock(act, dst, key, iv)
+	require.NoError(t, err)
+	require.Equal(t, src, act)
+
+	// Now check if we can use the same byte slice as src and dst. While this is useful to know that
+	// we can use src and dst as the same slice, this isn't applicable to Badger because we're
+	// reading data right off mmap. We should not modify that data, so we have to use a different
+	// slice for dst anyway.
+	cp := append([]byte{}, src...)
+	err = XORBlock(cp, cp, key, iv)
+	require.NoError(t, err)
+	require.Equal(t, dst, cp)
+
+	err = XORBlock(cp, cp, key, iv)
+	require.NoError(t, err)
+	require.Equal(t, src, cp)
+}