The document discusses using MongoDB and Go together for painless data storage. Some key points:
- Go is a good language for working with MongoDB as it is fast, friendly to use, and supports concurrency well out of the box.
- MongoDB is also fast and scales well. It uses a flexible document model that maps well to Go's data types.
- The mgo driver makes it easy to connect to MongoDB from Go, perform CRUD operations, and work with data concurrently. Data can be serialized and deserialized to BSON format efficiently.
- Features like GridFS allow storing files within MongoDB, providing replicated file storage.
4. Why Another Language?
• Software is slow
• Sofware is hard to write
• Software doesn’t scale well
5. Go is Fast
• Go execution speed is close to C
• Go compile time rivals dynamic
interpretation
6. Go is Friendly
• Feels like a dynamic language in many ways
• Very small core language, easy to
remember all of it
• Single binary installation, no dependencies
• Extensive Tooling & StdLib
7. Go is Concurrent
• Concurrency is part of the language
• Any function can become a goroutine
• Goroutines run concurrently, communicate
through channels
• Select waits for communication on any of a
set of channels
9. Why Another Database?
• Databases are slow
• Relational structures don’t fit well
with modern programming
(ORMs)
• Databases don’t scale well
10. MongoDB is Fast
• Written in C++
• Extensive use of memory-mapped files
i.e. read-through write-through memory caching.
• Runs nearly everywhere
• Data serialized as BSON (fast parsing)
• Full support for primary & secondary indexes
• Document model = less work
11. MongoDB is Friendly
• Ad Hoc queries
• Real time aggregation
• Rich query capabilities
• Traditionally consistent
• Geospatial features
• Support for most programming languages
• Flexible schema
12. MongoDB is “Web Scale”
• Built in sharding support distributes data
across many nodes
• MongoS intelligently routes to the correct
nodes
• Aggregation done in parallel across nodes
13. Document Database
• Not for .PDF & .DOC files
• A document is essentially an associative array
• Document == JSON object
• Document == PHP Array
• Document == Python Dict
• Document == Ruby Hash
• etc
14. Data Serialization
• Applications need persistant data
• The process of translating data structures
into a format that can be stored
• Ideal format accessible from many
languages
15. BSON
• Inspired by JSON
• Cross language binary serialization format
• Optimized for scanning
• Support for richer types
25. mgo (mango)
• Pure Go
• Created in late 2010
("Where do I put my Go data?")
• Adopted by Canonical and MongoDB Inc.
itself
• Sponsored by MongoDB Inc. from late 2011
49. • Find 1 issued
• Doc 1 returned
• Find 2 issued
• Doc 2 returned
A Common Approach
Find 1 Find 2 DB
}
}
50. • Find 1 issued
• Find 2 issued
• Doc 1 returned
• Doc 2 returned
Concurrent Queries
Find 1 Find 2 DB
}
}
51. • Loads 200 results at a time
• Loads next batch with (0.25 * 200) results left to process
Concurrent Loading
session.SetBatch(200)
session.SetPrefetch(0.25)
!
for iter.Next(&result) {
...
}
52. • Each Copy uses a different connection
• Closing session returns socket to the pool
• defer runs at end of function
Handler With Session Copy
func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
session := s.session.Copy()
defer session.Close()
!
// ... handle request ...
}
53. • Shares a single connection
• Still quite efficient thanks to concurrent capabilities of go + mgo
Handler With Single Session
func (s *Server) handle(w http.ResponseWriter, r *http.Request) {
session := s.session
!
// ... handle request ...
}
55. GridFS
• Not quite a file system
• Really useful for local file storage
• A convention, not a feature
• Supported by all drivers
• Fully replicated, sharded file storage