-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add and update Redis and NoSQL pages
- Loading branch information
1 parent
beaec93
commit 06f325f
Showing
10 changed files
with
666 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,42 @@ | ||
# NoSQL | ||
|
||
|
||
### Types of DBs | ||
|
||
The NoSQL database types, from most to least common: | ||
|
||
| Type | Description | Examples | | ||
|------|--------------|----------| | ||
| Key-Value Store | Stores data as key-value pairs, with keys acting as unique identifiers and values containing the associated data. | Redis, DynamoDB, Riak | | ||
| Document Store | Stores data as semi-structured documents, typically in JSON or XML format. Documents can have nested structures and are schema-less. | MongoDB, Couchbase, DocumentDB | | ||
| Column-Family Store | Stores data in column families, which are containers for rows of data. Columns can be added dynamically, and data is stored and retrieved by column family. | Apache Cassandra, HBase, Bigtable | | ||
| Graph Database | Stores data as nodes and relationships (edges) between nodes, optimized for traversing and querying highly connected data. | Neo4j, JanusGraph, Amazon Neptune | | ||
| Time-Series Database | Optimized for storing and analyzing time-series data, such as sensor data, application metrics, and financial data. | InfluxDB, Prometheus, Timescale | | ||
| Search Engine Database | Designed for full-text search and indexing large amounts of data. | Solr, Sphinx, Elasticsearch | | ||
| Multi-Model Database | Supports multiple data models (e.g., key-value, document, graph) within a single database. | ArangoDB, OrientDB, MarkLogic | | ||
| Object Store | Stores data as objects, with each object having a unique identifier and associated metadata. | Amazon S3, Azure Blob Storage, OpenStack Swift | | ||
| Event Store | Designed to store and process event data, often used in event-driven architectures and event sourcing. | Event Store, Kafka Streams | | ||
|
||
|
||
### Comparison of DBs | ||
|
||
Redis, MongoDB, and other popular NoSQL databases, focusing on their differences relevant for cloud engineers: | ||
|
||
| Database | Type | Performance | Storage | Persistence | Support | Cost | | ||
|-----------|------|--------------|---------|--------------|----------|------| | ||
| Redis | Key-Value | Extremely fast, in-memory | RAM, disk (optional) | Configurable, snapshots | Excellent, widely used | Open-source (free), paid enterprise support | | ||
| AWS DynamoDB | Key-Value | High, fully managed | Cloud storage | Automatic, durable | Excellent, fully managed | Pay-per-use, can be expensive at scale | | ||
| MongoDB | Document | High, with indexing | Disk | Automatic, journaling | Excellent, widely used | Open-source (free), paid enterprise support | | ||
| Elasticsearch | Document | High, distributed | Disk | Automatic, replication | Excellent, widely used | Open-source (free), paid enterprise support | | ||
| Couchbase | Document | High, with indexing | Disk, RAM (optional) | Automatic, replication | Good, widely used | Open-source (free), paid enterprise support | | ||
| Cassandra | Wide Column | High, distributed | Disk | Automatic, replication | Good, widely used | Open-source (free), paid enterprise support | | ||
| Neo4j | Graph | High for graph queries | Disk | Automatic, snapshots | Good, widely used | Open-source (free), paid enterprise support | | ||
|
||
Key differences: | ||
|
||
- **Type**: Redis is a key-value store, MongoDB and Couchbase are document stores, Cassandra is a wide column store, and Neo4j is a graph database. | ||
- **Performance**: Redis is extremely fast due to its in-memory nature, while others trade some speed for durability by storing data on disk. | ||
- **Storage**: Redis can use RAM or disk, while others primarily use disk storage. | ||
- **Persistence**: Redis requires configuration for persistence, while others automatically handle it. | ||
- **Support**: Most have excellent community support, with paid enterprise options available. | ||
- **Cost**: Redis, MongoDB, Cassandra, Couchbase, and Elasticsearch have open-source versions, while DynamoDB is a fully managed service with pay-per-use pricing. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
cheatsheets/python/libraries/database/redis/data-types/hashes.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Hash operations | ||
|
||
## Get | ||
|
||
```python | ||
# Get all fields and values of a hash | ||
user = r.hgetall('user:1000') | ||
print(user) # {b'name': b'John Doe', b'email': b'[email protected]', b'age': b'30'} | ||
``` | ||
|
||
```python | ||
# Get the number of fields in a hash | ||
count = r.hlen('user:1000') | ||
print(count) # 3 | ||
|
||
# Get all field names | ||
fields = r.hkeys('user:1000') | ||
print(fields) # [b'name', b'email', b'visits'] | ||
``` | ||
|
||
```python | ||
# Get the value of a specific field | ||
name = r.hget('user:1000', 'name') | ||
print(name) # b'John Doe' | ||
# Get values of multiple fields | ||
values = r.hmget('user:1000', ['name', 'age']) | ||
print(values) # [b'John Doe', b'30'] | ||
``` | ||
|
||
```python | ||
# Check if a field exists | ||
exists = r.hexists('user:1000', 'email') | ||
print(exists) # True | ||
``` | ||
|
||
## Create/update | ||
|
||
```python | ||
# Set hash fields | ||
r.hset('user:1000', mapping={'name': 'John Doe', 'email': '[email protected]', 'age': '30'}) | ||
|
||
# Set a field only if it doesn't exist | ||
r.hsetnx('user:1000', 'visits', '10') | ||
``` | ||
|
||
```python | ||
# Increment the integer value of a field | ||
r.hincrby('user:1000', 'visits', 1) | ||
``` | ||
|
||
|
||
## Remove | ||
|
||
```python | ||
# Delete one or more fields | ||
r.hdel('user:1000', 'age') | ||
``` |
1 change: 1 addition & 0 deletions
1
cheatsheets/python/libraries/database/redis/data-types/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Data types |
63 changes: 63 additions & 0 deletions
63
cheatsheets/python/libraries/database/redis/data-types/lists.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# List operations | ||
|
||
## Get | ||
|
||
```python | ||
# Get list length | ||
length = r.llen('fruits') | ||
print(length) # 4 | ||
``` | ||
|
||
```python | ||
# Get range of items (0-based indexing) | ||
items = r.lrange('fruits', 0, -1) | ||
print(items) # [b'date', b'apple', b'banana', b'cherry'] | ||
``` | ||
|
||
## Create/update | ||
|
||
```python | ||
# Push items to the right of a list | ||
r.rpush('fruits', 'apple', 'banana', 'cherry') | ||
``` | ||
|
||
```python | ||
# Push items to the left of a list | ||
r.lpush('fruits', 'date') | ||
``` | ||
|
||
```python | ||
# Insert item before or after specific value | ||
r.linsert('fruits', 'BEFORE', 'banana', 'kiwi') | ||
``` | ||
|
||
```python | ||
# Set item at specific index | ||
r.lset('fruits', 0, 'grape') | ||
``` | ||
|
||
## Remove | ||
|
||
```python | ||
# Remove and return an item from the right | ||
item = r.rpop('fruits') | ||
print(item) # b'cherry' | ||
``` | ||
|
||
```python | ||
# Remove and return an item from the left | ||
item = r.lpop('fruits') | ||
print(item) # b'date' | ||
``` | ||
|
||
```python | ||
# Trim a list to specified range | ||
r.ltrim('fruits', 0, 1) # Keep only first two items | ||
``` | ||
|
||
## Block | ||
|
||
```python | ||
# Block and wait for item to be pushed to a list | ||
item = r.brpop('fruits', timeout=5) # Wait up to 5 seconds | ||
``` |
62 changes: 62 additions & 0 deletions
62
cheatsheets/python/libraries/database/redis/data-types/sets.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Set operations | ||
|
||
## Get | ||
|
||
```python | ||
# Get the number of members in a set | ||
count = r.scard('fruits') | ||
print(count) # 2 | ||
``` | ||
|
||
```python | ||
# Get all members of a set | ||
members = r.smembers('fruits') | ||
print(members) # {b'apple', b'banana', b'cherry'} | ||
``` | ||
|
||
```python | ||
# Check if a member exists in a set | ||
exists = r.sismember('fruits', 'apple') | ||
print(exists) # True | ||
``` | ||
|
||
## Create/update | ||
|
||
```python | ||
# Add members to a set | ||
r.sadd('fruits', 'apple', 'banana', 'cherry') | ||
``` | ||
|
||
```python | ||
# Move a member from one set to another | ||
r.smove('tropical', 'fruits', 'banana') | ||
``` | ||
|
||
## Remove | ||
|
||
```python | ||
# Remove a member from a set | ||
r.srem('fruits', 'banana') | ||
``` | ||
|
||
|
||
## Comparisons | ||
|
||
```python | ||
# Get the difference between sets | ||
r.sadd('tropical', 'banana', 'mango') | ||
diff = r.sdiff('fruits', 'tropical') | ||
print(diff) # {b'apple', b'cherry'} | ||
``` | ||
|
||
```python | ||
# Get the intersection of sets | ||
inter = r.sinter('fruits', 'tropical') | ||
print(inter) # set() | ||
``` | ||
|
||
```python | ||
# Get the union of sets | ||
union = r.sunion('fruits', 'tropical') | ||
print(union) # {b'apple', b'cherry', b'banana', b'mango'} | ||
``` |
60 changes: 60 additions & 0 deletions
60
cheatsheets/python/libraries/database/redis/data-types/sorted-sets.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Sorted set operations | ||
|
||
## Get | ||
|
||
```python | ||
# Get the score of a member | ||
score = r.zscore('leaderboard', 'Bob') | ||
print(score) # 80.0 | ||
``` | ||
|
||
```python | ||
# Get the rank of a member (0-based) | ||
rank = r.zrank('leaderboard', 'Charlie') | ||
print(rank) # 1 | ||
``` | ||
|
||
```python | ||
# Get range of members by rank (0-based) | ||
members = r.zrange('leaderboard', 0, -1, withscores=True) | ||
print(members) # [(b'Bob', 80.0), (b'Charlie', 95.0), (b'Alice', 100.0)] | ||
``` | ||
|
||
```python | ||
# Get range of members by score | ||
members = r.zrangebyscore('leaderboard', 90, 100, withscores=True) | ||
print(members) # [(b'Charlie', 95.0), (b'Alice', 100.0)] | ||
``` | ||
|
||
```python | ||
# Get the number of members in a sorted set | ||
count = r.zcard('leaderboard') | ||
print(count) # 2 | ||
``` | ||
|
||
```python | ||
# Count members with scores within a range | ||
count = r.zcount('leaderboard', 90, 100) | ||
print(count) # 2 | ||
``` | ||
|
||
|
||
## Create/update | ||
|
||
```python | ||
# Add members with scores to a sorted set | ||
r.zadd('leaderboard', {'Alice': 100, 'Bob': 80, 'Charlie': 95}) | ||
``` | ||
|
||
```python | ||
# Increment the score of a member | ||
r.zincrby('leaderboard', 5, 'Bob') | ||
``` | ||
|
||
## Remove | ||
|
||
```python | ||
# Remove members from a sorted set | ||
r.zrem('leaderboard', 'Charlie') | ||
``` | ||
|
Oops, something went wrong.