nosql refers to fee relational database
Not only sql is not just a database, it complements a relational database.
Stored is a special data structure, characterized by fast speed and low defect security. Relational databases are characterized by slow speed, but high security. The two of them are used in combination in development projects. Why there is nosql
Current social characteristics:
The era of big data: large amounts of data
Many data types (diversity)
Real-time data
Features of Web:
High concurrency: There are many cases of simultaneous access
High availability: 99.99%
high performance:
In order to solve the network problems in the web2.0 era, nosql came into being
NoSql features:
1. Easy to expand: It is easy to add a new server, as long as the environment is configured, it can be used directly
2. Fast speed: basically nosql is based on memory, so fast speed, low security
3. Good flexibility: data can be stored casually without special requirements. Compared with the traditional relational database, its flexibility just meets the data diversity of the current era
4. High availability: one server is down, it does not affect the operation of other servers
Four categories
- key-value database (redis): suitable for solving cache problems
- Columnar storage database (Hbase): suitable for distributed file storage
- Document database (MongoDb): nodejs + mongodb
- Graphic data storage (Neo4j): suitable for social networks, recommendation system
Redis architecture
Redis is a single-threaded architecture
Single thread safety, low efficiency
Multi-thread efficiency is high, there are thread safety issues
This simplifies the implementation of data structures and algorithms:
Redis uses the event model mechanism i / o multiplexing mechanism (Linux file processing mechanism)
Single thread asynchronous callback: node js
Redis is a single thread, why is it still so efficient?
① Redis is based on memory, its reading speed is very fast
②The use of single thread avoids the CPU to switch the thread and improves the efficiency to a certain extent
③ Redis handling problem event model mechanism
Redis application scenarios
- String
Memache (data structure only has string)
Can apply cache (enhanced version of memache): to deal with high concurrency issues, but also to reduce the pressure of the database to read data
Hit rate: access data is obtained through redis, hit
Session sharing
Redis saves all sessions in the distributed web server to achieve session sharing, and users can obtain login information through redis
Counting function
Apply safe speed limit
Set the key expiration time expire key second The return value is an integer
ttl keyCheck the remaining time of this key> 0 -1 -2
Panic buying / second kill exist ()
- Hash
- Set
Out of order and unique, to achieve panic buying and spike
- List
List, realize news chat form
- Soreset
Realize game rankings (Renovation Sports Health, WeChat Mini Games)
Redis transaction
Affairs:
Represents a series of operations that, as atoms, either succeed or fail simultaneously
Four characteristics:
⑴ Atomicity
Atomicity means that all operations included in a transaction are either successful or failed and rolled back. This is the same concept as the transaction function introduced in the previous two blogs. Therefore, if the operation of the transaction is successful, it must be fully applied to the database. Failure cannot have any effect on the database.
⑵ Consistency (Consistency)
Consistency means that the transaction must change the database from one consistency state to another consistency state, which means that a transaction must be in a consistent state before and after execution.
Taking transfers, assuming that the sum of both user A and user B's total is 5000, then no matter how the transfer between A and B, how many transfers, the sum of the two users' money after the transaction should be added back It is 5000, which is the consistency of the transaction.
⑶ Isolation (Isolation)
Isolation means that when multiple users access the database concurrently, such as when operating the same table, the transaction initiated by the database for each user cannot be disturbed by the operation of other transactions. Multiple concurrent transactions must be isolated from each other.
That is to achieve such an effect: For any two concurrent transactions T1 and T2, from the perspective of transaction T1, T2 will either end before T1 starts, or start after T1 ends, so every transaction does not feel Until there are other transactions executed concurrently.
⑷ Durability
Persistence means that once a transaction is committed, the changes to the data in the database are permanent. Even if the database system encounters a failure, the operation of submitting the transaction will not be lost.
For example, when we use JDBC to operate the database, after submitting the transaction method, the user is prompted to complete the transaction operation. When the execution of our program is completed until we see the prompt, we can determine the transaction and submit it correctly. Even if there is a problem with the database at this time, we must We need to complete the execution of our transaction, otherwise it will cause us to see that the transaction is complete, but the database has no major errors in executing the transaction because of the failure.
1, dirty reading
Dirty read refers to reading data in another uncommitted transaction during a transaction process.
2. Non-repeatable reading
Non-repeatable reading means that for a certain data in the database, multiple queries within a transaction range return different data values. This is due to the modification and submission of another transaction during the query interval.
For example, transaction T1 is reading a certain data, and transaction T2 immediately modified the data and submitted the transaction to the database. Transaction T1 reads the data again and gets a different result, sending a non-repeatable read.
The difference between non-repeatable reads and dirty reads is that dirty reads read dirty data that was not committed by another transaction, while non-repeatable reads read data submitted by the previous transaction.
3. Virtual reading (phantom reading)
Phantom read is a phenomenon that occurs when transactions are not executed independently. For example, transaction T1 modifies a data item of all rows in a table from "1" to "2". At this time, transaction T2 inserts a row of data item into this table, and the value of this data item Still "1" and submitted to the database. If the user who operates transaction T1 looks at the data that was just modified, there will be a row that has not been modified. In fact, this row was added from transaction T2, as if an illusion has occurred. This is a phantom reading.
Phantom reads and non-repeatable reads read another transaction that has been submitted (this is different from dirty reads), the difference is that the non-repeatable read queries are all the same data item, and the magic read is for a batch The overall data (such as the number of data).
Redis transaction
Redis transactions can execute multiple commands at once, and come with the following two important guarantees:
A transaction is a separate isolation operation: all commands in a transaction are serialized and executed sequentially. During the execution of a transaction, it will not be interrupted by command requests sent by other clients.
A transaction is an atomic operation: the commands in the transaction are either all executed or none at all.
A transaction will go through the following three stages from start to execution:
Start the transaction.
multi
Order to join the team.
Perform transactions.
exec
Cancel execution
decrby
Redis transaction commands
1 |
DISCARD cancels the transaction and gives up executing all commands in the transaction block. |
2 |
EXEC executes all commands within the transaction block. |
3 |
MULTI marks the beginning of a transaction block. |
4 |
UNWATCH cancels the monitoring of all keys by the WATCH command. |
5 |
WATCH key [key ...] monitors a key (or keys), if the key (or keys) is changed by other commands before the transaction is executed, Then the transaction will be interrupted. |
Watch监听 watch key.....
A key was monitored, and the key must be modified during the transaction. Before the transaction is executed, someone else modified the key, so there is no failure in this matter.
Unwatch