Tech C**P
15 subscribers
161 photos
9 videos
59 files
304 links
مدرس و برنامه نویس پایتون و لینوکس @alirezastack
Download Telegram
If you are building a docker image using docker build and you want to force building the package use --no-cache:

docker build --no-cache


#docker #build #no_cache #cache #force
Today we had hundreds of connections to MongoDB that compared to previous days was so high and one of our modules initiating the connections every time a new request was initiated. To address the issue, I used a singleton design pattern to prevent creating many connections. Although pymongo has its own connection pool, we reduced the connections by 40 to 60!

Singleton design pattern makes sure to only create a new object from your class only ONCE:

class Singleton(object):
_instance = None

def __new__(class_, *args, **kwargs):
if not isinstance(class_._instance, class_):
class_._instance = object.__new__(class_, *args, **kwargs)

return class_._instance

__new__ will be called automatically when a new instance of the class is initiated. Here we check whether _instance is set or not, in case it is set we will return the old instance and will not create a new object.

Now you just need to send Singleton as parent class to your classes:

class MongoConnnection(Singleton):
def __init__(self):
pass

def get_connection(self):
pass

Create as many object as you want from MongoConnnection. And now when you print the initiated class you will see one memory address not different ones.

#mongodb #mongo #singleton #design_pattern #__new__
How to determine fragmentation in MongoDB?

use mydb
var s = db.my_collection.stats()
var frag = s.storageSize / (s.size + s.totalIndexSize)

NOTE: A frag value larger than 1 indicates some level of fragmentation.


#mongodb #mongo #stats #fragmentation
In order to check the filesystem of the partition and where that partition maps to that address use:

mount -l
It will show for example that you have a partition that mounted by NFS or that a partition is ext4 and so on:

/dev/mapper/vg-var on /var type ext4 (rw,relatime,stripe=384,data=ordered)

#linux #mount #ext4 #nfs #filesystem #partition
For optimal performance in terms of the storage layer, use disks backed by RAID-10. RAID-5 and RAID-6 do not typically provide sufficient performance to support a MongoDB deployment.

Avoid RAID-0 with MongoDB deployments. While RAID-0 provides good write performance, it also provides limited availability and can lead to reduced performance on read operations, particularly when using Amazon’s EBS volumes.


#mongodb #mongo #disk #raid #SSD
How to detect unused indexes on MongoDB collections?

db.your_collection.aggregate([{$indexStats:{}}]).pretty()

ops displays operations per second on a specific index. If ops is very low compared to other indexes you can drop the index.

#mongodb #mongo #index #unused_indexes
Tech C**P
#mongodb #backlog #road_map
The good news is MongoDB will support transactions from version 4.0 like any other DBMS systems like MySQL, Oracle,...
Scaling MongoDB_ Avoiding Common Pitfalls [720p].mp4
62.5 MB
Scaling MongoDB - Avoiding Common Pitfalls

#mongodb #video #tutorial
Looks like they need to defrag the port.
OnePHP a super simple and super lightweight PHP framework that works in just a few lines:

// index.php
require_once('src/OnePHP/one_framework.php');
$app = new \OnePHP\App();

$app->get('/:name',function( $name ) use ( $app ){//Action
echo "Hello $name";
});
$app->listen();


To install using composer:

composer create-project julces/oneframework

Why OnePHP:

1- Restful Routes

2- Easy and clean (GET, POST, PUT, DELETE...) Requests management

3- Restful Response with HTTP Status Code and custom Headers

4- PHP native Views

5- No dependencies, add extra libraries only when you need it.

#php #framework #onePHP
If you have a dedicated server only for MongoDB database I would I highly recommend to set block size as mongo suggested for their engines.


For the WiredTiger storage engine:

Set the readahead setting to 0 regardless of storage media type (spinning, SSD, etc.).

Setting a higher readahead benefits sequential I/O operations. However, since MongoDB disk access patterns are generally random, setting a higher readahead provides limited benefit or performance degradation. As such, for most workloads, a readahead of 0 provides optimal MongoDB performance.

In general, set the readahead setting to 0 unless testing shows a measurable, repeatable, and reliable benefit in a higher readahead value. MongoDB Professional Support can provide advice and guidance on non-zero readahead configurations.


For the MMAPv1 storage engine:

Ensure that readahead settings for the block devices that store the database files are appropriate. For random access use patterns, set low readahead values. A readahead of 32 (16 kB) often works well.

For a standard block device, you can run sudo blockdev --report to get the readahead settings and sudo blockdev --setra <value> <device> to change the readahead settings. Refer to your specific operating system manual for more information.


#mongodb #mongo #ra #readahead #mmapv1 #wiredtiger
In python in order to swap variables you can use unpack feature:

>>> a, b = 1, 2
>>> a, b = b, a
>>> a, b
(2, 1)

This is a nice way to swap variables without using a third variable.

#python #swap #swap_variables
Extended unpacking (`Python 3 Only`):

>>> a, *b, c = [1, 2, 3, 4, 5]
>>> a
1
>>> b
[2, 3, 4]
>>> c
5

This way first and last list element will be unpacked into a and c and other values will be inside of b variable.

#python #python3 #unpack #extended_unpack
Learn the Zen of Python:

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

By importing this in your python console you will get the zen of python as above. :)

#python #this #zen_of_python #zen
Click is a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary. It’s the “Command Line Interface Creation Kit”. It’s highly configurable but comes with sensible defaults out of the box.

- http://click.pocoo.org/6/

#python #argparse #click #command_line_interface